allenwyma
Testing IO from STDIN in Elixir
I’m quite stuck at something I thought would be quite straight forward, but it seems maybe this topic hasn’t come up much in my career in Elixir.
I am trying to run a test to check STOUT and test STDIN for a function:
test "can read and output" do
input =
"""
3
Hello
Hello World
Hello A B World
"""
assert ExUnit.CaptureIO.capture_io(fn ->
IO.write(input)
task = Task.async(Example, :start, [])
Task.await(task)
end) == """
3
Hello
Hello World
Hello B A World
"""
end
Within my code I’ve been trying to read from stdin, but I keep getting :eof when I used IO.read(:line) or ""if I tryIO.read(:all)`.
I’m pretty sure I must be close, but I am missing something somewhere. The reason I’m launching a task is because I’m pretty sure the code will block so I’m unable to write to :stdio.
here’s my code for the function:
def start(pid \\ :stdio) do
number_of_lines =
IO.read(pid, :read)
|> String.trim()
|> String.to_integer()
Enum.each(0..number_of_lines, fn _i ->
sentence =
IO.read(pid, :line)
|> IO.inspect()
|> String.trim()
|> String.split()
sentence_to_write =
if length(sentence) > 2 do
[hd | tail] = sentence
{last, rest} = List.pop_at(tail, length(tail))
new_tail = Enum.reverse(rest) ++ [last]
[hd | new_tail]
else
sentence
end
sentence_to_write
|> Enum.reduce(fn word, acc ->
acc <> " " <> word
end)
|> IO.write(pid)
end)
end
The there’s a pid there is I was also thinking I can create a pid that responds to the message protocol for writing to a device, but I kind of gave up around there, thinking there must be a way to do this without creating another process.
Does anyone have any idea about how I can achieve this?
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








