Search code examples
elixirdoctest

Elixir: doctesting a sigil


I wrote an awesome sigil:

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

Here is the tests module:

defmodule SigiltestTest do
  use ExUnit.Case
  doctest Sigiltest
end

When I run mix test I get the following output:

Compiled lib/sigiltest.ex
** (CompileError) (for doctest at) lib/sigiltest.ex:7: undefined function sigil_a/2
    (stdlib) lists.erl:1337: :lists.foreach/2
    (stdlib) erl_eval.erl:670: :erl_eval.do_apply/6

Is there a way to run doctests on a sigil? If so, how?


Solution

  • That's because sigil_a is not present in the context the doctests are being run. You can either import Sigiltest explicitly:

    @doc """
    An awesome sigil!
    
    ### Usage
    
        iex> import Sigiltest
        iex> ~a{I love elixir}
        "I love elixir, awesome!"
    """
    

    or add import: true to the doctest invocation:

    doctest Sigiltest, import: true