Elixir: Newline character ("\n") breaks doctest

Created on 13 Oct 2016  路  2Comments  路  Source: elixir-lang/elixir

Environment

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

Current behavior

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

Expected behavior

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.

ExUnit Bug Unresolved

Most helpful comment

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.

All 2 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ericmj picture ericmj  路  3Comments

GianFF picture GianFF  路  3Comments

Irio picture Irio  路  3Comments

coryodaniel picture coryodaniel  路  3Comments

LucianaMarques picture LucianaMarques  路  3Comments