Extract answers from processes.

For some reason I don't quite understand I had to rewrite
assert_receive as I was not getting failures when I was expecting them.
This commit is contained in:
Felipe Sere
2016-03-17 10:04:01 +00:00
parent e4d1cad6df
commit b70e8ea095
3 changed files with 41 additions and 29 deletions

View File

@@ -27,15 +27,14 @@ defmodule BlankAssertions do
end end
end end
defmacro assert_receive(expr) do def assert_receive(expr) do
if contains_blank?(expr) do if expr == :__ do
code = Macro.escape(expr) raise ExUnit.AssertionError, expr: expr
quote do
raise ExUnit.AssertionError, expr: unquote(code)
end
else else
quote do receive do
ExUnit.Assertions.assert_receive(unquote(expr), 100) ^expr -> true
after
100 -> flunk("No message matching #{expr} found in mailbox")
end end
end end
end end
@@ -48,10 +47,8 @@ defmodule BlankAssertions do
ExUnit.Assertions.refute(value, opts) ExUnit.Assertions.refute(value, opts)
end end
defmacro flunk(message \\ "Flunked!") do def flunk(message \\ "Flunked!") do
quote do ExUnit.Assertions.flunk(message)
assert false, message: unquote(message)
end
end end
defp contains_blank?(expr) do defp contains_blank?(expr) do

View File

@@ -2,18 +2,18 @@ defmodule Processes do
use Koans use Koans
koan "tests run in a process!" do koan "tests run in a process!" do
assert Process.alive?(self) assert Process.alive?(:__)
end end
koan "can spew out information about a process" do koan "can spew out information about a process" do
information = Process.info(self) information = Process.info(self)
assert information[:status] == :running assert information[:status] == :__
end end
koan "process can send messages to itself" do koan "process can send messages to itself" do
send self(), "hola!" send self(), "hola!"
assert_receive "hola!" assert_receive :__
end end
koan "a common pattern is to include the sender in the message" do koan "a common pattern is to include the sender in the message" do
@@ -24,7 +24,7 @@ defmodule Processes do
end) end)
send pid, {:hello, self()} send pid, {:hello, self()}
assert_receive :how_are_you? assert_receive :__
end end
koan "you don't have to wait forever for messages" do koan "you don't have to wait forever for messages" do
@@ -36,22 +36,20 @@ defmodule Processes do
end end
end) end)
assert_receive {:waited_too_long, "I am inpatient"} assert_receive :__
end end
koan "killing a process will terminate it" do koan "killing a process will terminate it" do
pid = spawn(fn -> Process.exit(self(), :kill) end) pid = spawn(fn -> Process.exit(self(), :kill) end)
:timer.sleep(500) :timer.sleep(500)
refute Process.alive?(pid) assert Process.alive?(pid) == :__
end end
koan "killing a process kills it for good" do koan "killing a process kills it for good" do
pid = spawn(fn -> receive do pid = spawn(fn -> receive do end end)
end
end)
assert Process.alive?(pid) assert Process.alive?(pid)
Process.exit(pid, :kill) Process.exit(pid, :kill)
refute Process.alive?(pid) assert Process.alive?(pid) == :__
end end
koan "can trap a signal in a child process" do koan "can trap a signal in a child process" do
@@ -66,8 +64,7 @@ defmodule Processes do
wait() wait()
Process.exit(pid, :random_reason) Process.exit(pid, :random_reason)
assert_receive {:exited, :random_reason} assert_receive :__
refute Process.alive?(pid)
end end
koan "quitting normally has no effect" do koan "quitting normally has no effect" do
@@ -75,7 +72,7 @@ defmodule Processes do
end end
end) end)
Process.exit(pid, :normal) Process.exit(pid, :normal)
assert Process.alive?(pid) assert Process.alive?(pid) == :__
end end
koan "quititing your own process normally does terminate it though" do koan "quititing your own process normally does terminate it though" do
@@ -84,10 +81,9 @@ defmodule Processes do
end end
end) end)
assert Process.alive?(pid)
send pid, :bye send pid, :bye
:timer.sleep(100) :timer.sleep(100)
refute Process.alive?(pid) assert Process.alive?(pid) == :__
end end
koan "linked processes are informed about exit signals of children when trapping those signals" do koan "linked processes are informed about exit signals of children when trapping those signals" do
@@ -100,7 +96,7 @@ defmodule Processes do
end end
end) end)
assert_receive {:exited, :normal} assert_receive :__
end end
koan "monitoring processes are informed via messages without having trapping" do koan "monitoring processes are informed via messages without having trapping" do
@@ -112,7 +108,7 @@ defmodule Processes do
end end
end) end)
assert_receive {:exited, :normal} assert_receive :__
end end
def wait do def wait do

View File

@@ -185,6 +185,25 @@ defmodule KoansHarnessTest do
test_all(PatternMatching, answers) test_all(PatternMatching, answers)
end end
test "Processes" do
answers = [
self,
:running,
"hola!",
:how_are_you?,
{:waited_too_long, "I am inpatient"},
false,
false,
{:exited, :random_reason},
true,
false,
{:exited, :normal},
{:exited, :normal}
]
test_all(Processes, answers)
end
def test_all(module, answers) do def test_all(module, answers) do
module.all_koans module.all_koans
|> Enum.zip(answers) |> Enum.zip(answers)