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
https://howistart.org/posts/elixir/1/
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?