Euphorichuman

Euphorichuman

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

First Post!

gpopides

gpopides

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?

Most Liked

josevalim

josevalim

Creator of Elixir

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.

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New

Other popular topics Top

hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44167 214
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement