Elixir:doctesting sigil

时间:2016-06-12 16:02:42

标签: elixir doctest

我写了一个很棒的印记:

$(document).ready(function() {

  // using jQuery
  function getCookie(name) {
    var cookieValue = null;
    if (document.cookie && document.cookie != '') {
      var cookies = document.cookie.split(';');
      for (var i = 0; i < cookies.length; i++) {
        var cookie = jQuery.trim(cookies[i]);
        // Does this cookie string begin with the name we want?
        if (cookie.substring(0, name.length + 1) == (name + '=')) {
          cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
          break;
        }
      }
    }
    return cookieValue;
  }
  var csrftoken = getCookie('csrftoken');

  function.vote(eintragID) {
    $.ajax({
      type: "POST",
      url: "/vote/",
      data: {
        "eintrag": eintragID
      },
      success: function() {
        $("#eintrag-vote-" + eintragID).hide();
        $("#eintrag-title-" + eintragID).css({
          "margin-left": "15px"
        });
      },
      headers: {
        'X-CSRFToken': csrftoken
      }
    });
    return false;
  }

  $("a.vote").click(function() {
    var eintragID = parseInt(this.id.split("-")[2]);
    return vote(eintragID);
  })

});

以下是测试模块:

defmodule Sigiltest do

  @doc """
  An awesome sigil!

  ### Usage

      iex> ~a{I love elixir}
      "I love elixir, awesome!"
  """
  def sigil_a(content, _flags) do
    "#{content}, awesome!"
  end
end

当我运行defmodule SigiltestTest do use ExUnit.Case doctest Sigiltest end 时,我得到以下输出:

mix test

有没有办法在sigil上运行doctests? 如果是这样,怎么样?

1 个答案:

答案 0 :(得分:4)

这是因为在运行doctests的上下文中不存在sigil_a。您可以明确导入Sigiltest

@doc """
An awesome sigil!

### Usage

    iex> import Sigiltest
    iex> ~a{I love elixir}
    "I love elixir, awesome!"
"""

或将import: true添加到doctest调用:

doctest Sigiltest, import: true