Erlang/OTP 19 [erts-8.1] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
Elixir 1.3.4
Operating system: macOS Sierra 10.12
When including a string in a doctest that contains a newline character ("\n") the doctest crashes because of a syntax error:
$ mix test
1) test doc at DoctestBug.boom/0 (1) (DoctestBugTest)
test/doctest_bug_test.exs:3
Doctest did not compile, got: (TokenMissingError) lib/doctest_bug.ex:3: missing terminator: " (for string starting at line 3)
code: "test
stacktrace:
lib/doctest_bug.ex:3: DoctestBug (module)
Here is the test in question:
defmodule DoctestBug do
@doc """
iex> DoctestBug.boom
"test\n"
"""
def boom(), do: "test\n"
end
Implementing the test outside of a doctest works as expected:
defmodule DoctestBugTest do
use ExUnit.Case
doctest DoctestBug
test "should work outside of doctest" do
assert DoctestBug.boom == "test\n"
end
end
The doctest should pass as well.
Here is a repo that contains a mix project that shows the bug: https://github.com/uri/elixir-doctest-bug-example. Run mix test to see behavior.
Isn't that supposed to be \\n?
TL;DR: It is supposed to be \\n (tks @Kabie)
Yes, \\n would certainly work or using the ~S""" sigil. I kept the issue open to investigate if doctest could cope with the reported approach but it cannot. doctest uses new lines and indentation to know when the code samples finish and the example above ends-up being interpreted as:
@doc """
iex> DoctestBug.boom
"test
"
"""
Which means that the final quote is not present in the code sample and that's why elixir complains the quote is missing.
Most helpful comment
TL;DR: It is supposed to be
\\n(tks @Kabie)Yes,
\\nwould certainly work or using the~S"""sigil. I kept the issue open to investigate if doctest could cope with the reported approach but it cannot. doctest uses new lines and indentation to know when the code samples finish and the example above ends-up being interpreted as:Which means that the final quote is not present in the code sample and that's why elixir complains the quote is missing.