仅了解Elixir中的doctest,我正在尝试编写Elixir doctest以验证结构将包含两个空的MapSet。
defstruct [:coordinates, :hit_coordinates]
@doc """
Creates a new Island structure
## Examples
iex> IslandsEngine.Island.new()
%IslandsEngine.Island { coordinates: #MapSet<[]>, hit_coordinates: #MapSet<[]> }
"""
def new(), do:
%Island{ coordinates: MapSet.new(), hit_coordinates: MapSet.new() }
使用mix test
运行此命令时,出现以下错误:
Compiling 1 file (.ex)
1) doctest IslandsEngine.Island.new/0 (1) (IslandsEngine.IslandTest)
test/islands_engine/island_test.exs:3
Doctest did not compile, got: (TokenMissingError) lib/islands_engine/island.ex:19: missing terminator: } (for "{" starting at line 19)
code: %IslandsEngine.Island { coordinates: #MapSet<[]>, hit_coordinates: #MapSet<[]> }
stacktrace:
lib/islands_engine/island.ex:19: IslandsEngine.Island (module)
.......
Finished in 0.1 seconds
6 doctests, 2 tests, 1 failure
但是在IEx会话中一切运行正常:
iex> IslandsEngine.Island.new()
%IslandsEngine.Island{coordinates: #MapSet<[]>, hit_coordinates: #MapSet<[]>}
iex>
感谢您的帮助!
答案 0 :(得分:1)
您需要使代码构造有效的答案,而不是doctest中的文本表示形式。所以代替:
%IslandsEngine.Island{coordinates: #MapSet<[]>, hit_coordinates: #MapSet<[]>}
要做:
%IslandsEngine.Island{coordinates: %MapSet{}, hit_coordinates: %MapSet{}}
我想在这种情况下,该示例作为测试几乎没有价值,因为它只是复制代码,但作为文档可能仍然有效。