stratacast

stratacast

Hey everybody! I’m coming back at Elixir again after maybe a year of not being able to jump into my web development hobby. I wanted to get back into Elixir from the ground floor again so I’ve been relearning this last week. I jumped into this starter project from Jose

And one of the things he mentions is creating docs and doctests. So I have been. I ran into one issue though and can’t find an answer anywhere. And that is when trying to test a function that has a Tuple with an atom and PID as the output. For example with creating an Agent:

iex> {:ok, pid} = Agent.start_link(fn -> [] end)
{:ok, #PID<0.106.0>}

as a part of the document to be tested with doctest. With the function transfer in the example in the introduction I created a doc like so:

  @doc """
  Starts transferring `data` from `left` to `right`.

  ## Parameters

  - `left` - A portal shot with Portal.shoot/1
  - `right` - Another portal shot with Portal.shoot/1
  - `data` - An enumerable type of data

  ## Examples

      iex> Portal.shoot(:red)
      {:ok, pid}
      iex> Portal.shoot(:green)
      {:ok, pid}
      iex> portal = Portal.transfer(:red, :green, [1,2,3])
      #Portal<
             :red <=> :green
        [1, 2, 3] <=> []
      >
      iex> portal
      #Portal<
             :red <=> :green
        [1, 2, 3] <=> []
      >
  """

If I remove the {:ok, pid} for example, it compares the wrong things in the doctest:

  1) doctest Portal.transfer/3 (1) (PortalTest)
     test/portal_test.exs:3
     Doctest failed
     doctest:
       iex> Portal.shoot(:red)
       iex> Portal.shoot(:green)
       iex> portal = Portal.transfer(:red, :green, [1,2,3])
       "#Portal<\n       :red <=> :green\n  [1, 2, 3] <=> []\n>"
     code:  inspect(portal = Portal.transfer(:red, :green, [1, 2, 3])) === "#Portal<\n       :red <=> :green\n  [1, 2, 3] <=> []\n>"
     left:  "#Portal<\n       :red <=> :green\n  [1, 2, 3] <=> []\n>\n"
     right: "#Portal<\n       :red <=> :green\n  [1, 2, 3] <=> []\n>"
     stacktrace:
       lib/portal.ex:19: Portal (module)

Thoughts? Workarounds?

Showing Posts 1 to 5

LostKobrakai

LostKobrakai

There’s documentation about it: ExUnit.DocTest — ExUnit v1.20.2

al2o3cr

al2o3cr

I couldn’t find an official “don’t put a newline at the end of your inspect output” in the docs, but note that the examples for Kernel.inspect/2 don’t, even in the case when there are embedded newlines in the result.

stratacast

stratacast OP

I did look at that, but it doesn’t seem to help the issue. If anything, it might fall under “when not to use doctest”? Which kind of sucks but perhaps it has to be done.

I don’t think either of those 2 solutions they provide don’t work, since I don’t have a # at the beginning of the output, and the second scenario doesn’t seem to work either…am I missing something?

stratacast

stratacast OP

That’s another thing I was trying to solve…and adding a newline or a \n seems to break the test

stratacast

stratacast OP

Okay, I had a friend help me out. And it was pretty relevant to what @al2o3cr was saying about the newline being added. In the project, you are asked to output to a heredoc to get the output shown. The code was originally this:

    """
    #Portal<
      #{String.pad_leading(left_door, max)} <=> #{right_door}
      #{String.pad_leading(left_data, max)} <=> #{right_data}
    >
    """

And he wrapped it with String.trim which removed the newline escape character. So the change to the code itself was:

    String.trim("""
    #Portal<
      #{String.pad_leading(left_door, max)} <=> #{right_door}
      #{String.pad_leading(left_data, max)} <=> #{right_data}
    >
    """)

And the doctest example being

  ## Examples

      iex> Portal.shoot(:red)
      iex> Portal.shoot(:green)     
      iex> portal = Portal.transfer(:red, :green, [1,2,3])
      #Portal<
             :red <=> :green
        [1, 2, 3] <=> []
      >
      iex> portal
      #Portal<
             :red <=> :green
        [1, 2, 3] <=> []
      >
  """

Noting the removal of {:ok, pid} after the iex prompts that run Portal.shoot(color)

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews