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

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

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

Other popular topics Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement