stratacast

stratacast

Doctests with PIDs in output from iex

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?

First 5 of 5 Posts Switch mode

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?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement