Is there a way to use a module alias in the doctests? I don't want to have to type out a long name every single time.
defmodule SomeLongModuleName.SubModule do
alias SomeLongModuleName.SubModule, as: SubModule
@doc """
iex> SubModule.method(%{property_a: 1, property_b: 2) # CompileError
3
"""
def method(%{property_a: a, property_b: b) do
a + b
end
end
The example above shows a situation where I might want to use the alias to avoid long lines. Is it at all possible to use an alias in a doctest?
alias allows you to set up aliases for any given module name. The original List can still be accessed within Stats by the fully-qualified name Elixir.
Doctests are specified by an indentation of four spaces followed by the iex> prompt in a documentation string. If a command spans multiple lines, you can use ...> , as in IEx. The expected result should start at the next line after iex> or ...> line(s) and is terminated either by a newline or a new iex> prefix.
There are two ways I can think of to not have to type the module name again and again.
Use interpolation in your docs and use the aliased name:
defmodule SomeLongModuleName.SubModule do
alias SomeLongModuleName.SubModule, as: SubModule
@doc """
iex> #{SubModule}.method(%{property_a: 1, property_b: 2})
3
"""
def method(%{property_a: a, property_b: b}) do
a + b
end
end
Use just the function name without module and in your call to doctest
from your tests, add import: true
:
defmodule SomeLongModuleName.SubModule do
@doc """
iex> method(%{property_a: 1, property_b: 2})
3
"""
def method(%{property_a: a, property_b: b}) do
a + b
end
end
doctest SomeLongModuleName.SubModule, import: true
Building on the answer from lab419 and dylanthepiguy:
Module with doctest:
defmodule SomeLongModuleName.SubModule do
@doc """
iex> SubModule.add(x, y)
3
"""
def add(x, y) do
x + y
end
end
Test case for module with doctest:
defmodule SomeLongModuleName.SubModuleTest do
use ExUnit.Case, async: true
# Alias the submodule so we don't have to use the fully qualified name
alias SomeLongModuleName.SubModule
doctest SomeLongModuleName.SubModule, import: true
end
As mentioned by dylanthepiguy it is definitely a better solution to put the aliases into the testfile, just before the doctest
line.
Instrumenting your code for tests is IMHO a code smell.
Also note that as: Submodule
is the default and therefore unnecessary.
It turns out you can put a alias SomeLongModuleName.SubModule, as: SubModule
line just before the test.
A better solution is to not put too many tests in the docs, and not use an alias. Then, in your test file you can put alias SomeLongModuleName.SubModule, as: SubModule
to save.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With