Euphorichuman

Euphorichuman

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

Showing Posts 1 to 4

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?

Euphorichuman

Euphorichuman OP

Removing this space does not solve the problem.

Tutorial btw: https://elixir-lang.org/getting-started/mix-otp/introduction-to-mix.html

lucaong

lucaong

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?

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews