Elixir: Weird behaviour for doctests which contain a function and string interpolation

Created on 27 Mar 2018  路  1Comment  路  Source: elixir-lang/elixir

  • Elixir & Erlang versions (elixir --version):
 $ elixir --version
Erlang/OTP 20 [erts-9.2] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:10] [hipe] [kernel-poll:false]
Elixir 1.6.3 (compiled with OTP 19)
  • Operating system: Ubuntu 17.10

Current behavior

defmodule Dtest do
  @doc """
  ## Examples
  iex> tap(33, fn x -> "> #{x}" end)
  33
  """
  def tap(input, input_fun) do
    input_fun.(input)
    input
  end
end

ExUnit.start()

defmodule DtestTest do
  use ExUnit.Case
  doctest Dtest, import: true
end
warning: variable "x" does not exist and is being expanded to "x()", please use parentheses to remove the ambiguity or change the variable name
  opt/app/x.exs:4
** (CompileError) opt/app/x.exs:4: undefined function x/0
    (elixir) src/elixir_bitstring.erl:60: :elixir_bitstring.expand_expr/4
    (elixir) src/elixir_bitstring.erl:19: :elixir_bitstring.expand/6
    (elixir) src/elixir_bitstring.erl:12: :elixir_bitstring.expand/4
    (stdlib) lists.erl:1354: :lists.mapfoldl/3
    (stdlib) lists.erl:1355: :lists.mapfoldl/3
    (elixir) expanding macro: Kernel.@/1

Expected behavior

Tests should be executed without any errors.

Most helpful comment

The documentation is also a string. So you are interpolating the outer string and not the example one. Writing @doc ~S""" should solve it as it disables interpolation.

>All comments

The documentation is also a string. So you are interpolating the outer string and not the example one. Writing @doc ~S""" should solve it as it disables interpolation.

Was this page helpful?
0 / 5 - 0 ratings