Error with Doctests: unexpected token: "

I’ve been doing the Get Started elixir. I’m currently going part 9 of MIX AND OTP, but I am having an error with the Doctests that I cannot fix:

I have this command parser file just as the tutorial tells me:

defmodule KVServer.Command do
  @doc ~S"""
  Parses the given `line` into a command.

  ## Examples

      iex> KVServer.Command.parse "CREATE shopping\r\n"
      {:ok, {:create, "shopping"}}

      iex> KVServer.Command.parse "CREATE  shopping  \r\n"
      {:ok, {:create, "shopping"}}

      iex> KVServer.Command.parse "PUT shopping milk 1\r\n"
      {:ok, {:put, "shopping", "milk", "1"}}

      iex> KVServer.Command.parse "GET shopping milk\r\n"
      {:ok, {:get, "shopping", "milk"}}

      iex> KVServer.Command.parse "DELETE shopping eggs\r\n"
      {:ok, {:delete, "shopping", "eggs"}}

  Unknown commands or commands with the wrong number of
  arguments return an error:

      iex> KVServer.Command.parse "UNKNOWN shopping eggs\r\n"
      {:error, :unknown_command}

      iex> KVServer.Command.parse "GET shopping\r\n"
      {:error, :unknown_command}

  """

  def parse(line) do
    case String.split(line) do
      ["CREATE", bucket] -> {:ok, {:create, bucket}}
      ["GET", bucket, key] -> {:ok, {:get, bucket, key}}
      ["PUT", bucket, key, value] -> {:ok, {:put, bucket, key, value}}
      ["DELETE", bucket, key] -> {:ok, {:delete, bucket, key}}
      _ -> {:error, :unknown_command}
    end
  end

  @doc """
  Runs the given command.
  """
  def run(command)

  def run({:create, bucket}) do
    MixTest.Registry.create(MixTest.Registry, bucket)
    {:ok, "OK\r\n"}
  end

  def run({:get, bucket, key}) do
    lookup(bucket, fn pid ->
      value = MixTest.Bucket.get(pid, key)
      {:ok, "#{value}\r\nOK\r\n"}
    end)
  end

  def run({:put, bucket, key, value}) do
    lookup(bucket, fn pid ->
      MixTest.Bucket.put(pid, key, value)
      {:ok, "OK\r\n"}
    end)
  end

  def run({:delete, bucket, key}) do
    lookup(bucket, fn pid ->
      MixTest.Bucket.delete(pid, key)
      {:ok, "OK\r\n"}
    end)
  end

  defp lookup(bucket, callback) do
    case MixTest.Registry.lookup(MixTest.Registry, bucket) do
      {:ok, pid} -> callback.(pid)
      :error -> {:error, :not_found}
    end
  end
end

Note: MixTest is my module from another application that is under umbrella (KV module in the tutorial).

When I try to run the tests, all the doc tests fail, apparently because it does not recognize a blank space or something that I do not understand.

These are the messages that the console shows me when I run the mix test command:

==> kv_server


  1) doctest KVServer.Command.parse/1 (2) (KVServer.CommandTest)
     apps/kv_server/test/kv_server/command_test.exs:3
" (column 49, code point U+000D)t: (SyntaxError) lib/kv_server/command.ex:10:49: unexpected token: "
     doctest:
       iex> KVServer.Command.parse "CREATE  shopping  \r\n"
       {:ok, {:create, "shopping"}}
     stacktrace:
       lib/kv_server/command.ex:10: KVServer.Command (module)



  2) doctest KVServer.Command.parse/1 (3) (KVServer.CommandTest)
     apps/kv_server/test/kv_server/command_test.exs:3
" (column 50, code point U+000D)t: (SyntaxError) lib/kv_server/command.ex:13:50: unexpected token: "
     doctest:
       iex> KVServer.Command.parse "PUT shopping milk 1\r\n"
       {:ok, {:put, "shopping", "milk", "1"}}
     stacktrace:
       lib/kv_server/command.ex:13: KVServer.Command (module)



  3) doctest KVServer.Command.parse/1 (4) (KVServer.CommandTest)
     apps/kv_server/test/kv_server/command_test.exs:3
" (column 48, code point U+000D)t: (SyntaxError) lib/kv_server/command.ex:16:48: unexpected token: "
     doctest:
       iex> KVServer.Command.parse "GET shopping milk\r\n"
       {:ok, {:get, "shopping", "milk"}}
     stacktrace:
       lib/kv_server/command.ex:16: KVServer.Command (module)



  4) doctest KVServer.Command.parse/1 (6) (KVServer.CommandTest)
     apps/kv_server/test/kv_server/command_test.exs:3
" (column 52, code point U+000D)t: (SyntaxError) lib/kv_server/command.ex:25:52: unexpected token: "
     doctest:
       iex> KVServer.Command.parse "UNKNOWN shopping eggs\r\n"
       {:error, :unknown_command}
     stacktrace:
       lib/kv_server/command.ex:25: KVServer.Command (module)



  5) doctest KVServer.Command.parse/1 (7) (KVServer.CommandTest)
     apps/kv_server/test/kv_server/command_test.exs:3
" (column 43, code point U+000D)t: (SyntaxError) lib/kv_server/command.ex:28:43: unexpected token: "
     doctest:
       iex> KVServer.Command.parse "GET shopping\r\n"
       {:error, :unknown_command}
     stacktrace:
       lib/kv_server/command.ex:28: KVServer.Command (module)



  6) doctest KVServer.Command.parse/1 (1) (KVServer.CommandTest)
     apps/kv_server/test/kv_server/command_test.exs:3
" (column 46, code point U+000D)t: (SyntaxError) lib/kv_server/command.ex:7:46: unexpected token: "
     doctest:
       iex> KVServer.Command.parse "CREATE shopping\r\n"
       {:ok, {:create, "shopping"}}
     stacktrace:
       lib/kv_server/command.ex:7: KVServer.Command (module)



  7) doctest KVServer.Command.parse/1 (5) (KVServer.CommandTest)
     apps/kv_server/test/kv_server/command_test.exs:3
" (column 51, code point U+000D)t: (SyntaxError) lib/kv_server/command.ex:19:51: unexpected token: "
     doctest:
       iex> KVServer.Command.parse "DELETE shopping eggs\r\n"
       {:ok, {:delete, "shopping", "eggs"}}
     stacktrace:
       lib/kv_server/command.ex:19: KVServer.Command (module)

.

Finished in 0.1 seconds (0.1s async, 0.00s sync)
7 doctests, 1 test, 7 failures

i don’t know the tutorial but i can see that the difference between rest examples are that

KVServer.Command.parse "CREATE  shopping  \r\n"

contains a space between shopping and \r\n.

What happens if you remove that space?

Removing this space does not solve the problem.

Tutorial btw: Introduction to Mix - The Elixir programming language

I am on my phone now, so I cannot verify, but it looks like a problem with escaping of \r\n. Indeed it says that the unicode code point where it breaks is the hexadecimal 000D, which is the carriage return.

What happen if you escape the backslash with another backslash like \\r\\n?

This is an Elixir bug that has been fixed in the upcoming v1.13: doctest raises syntaxerror: unexpected token · Issue #11291 · elixir-lang/elixir · GitHub

A temporary solution for now is to configure your editor to use Unix/DOS style newlines.

7 Likes