From b70e8ea0954d89f049ba9ac1cc1120bb4b0c9f44 Mon Sep 17 00:00:00 2001 From: Felipe Sere Date: Thu, 17 Mar 2016 10:04:01 +0000 Subject: [PATCH] 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. --- lib/blank_assertions.ex | 21 +++++++++------------ lib/koans/10_processes.ex | 30 +++++++++++++----------------- test/koans_harness_test.exs | 19 +++++++++++++++++++ 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/lib/blank_assertions.ex b/lib/blank_assertions.ex index 12c1de8..7458040 100644 --- a/lib/blank_assertions.ex +++ b/lib/blank_assertions.ex @@ -27,15 +27,14 @@ defmodule BlankAssertions do end end - defmacro assert_receive(expr) do - if contains_blank?(expr) do - code = Macro.escape(expr) - quote do - raise ExUnit.AssertionError, expr: unquote(code) - end + def assert_receive(expr) do + if expr == :__ do + raise ExUnit.AssertionError, expr: expr else - quote do - ExUnit.Assertions.assert_receive(unquote(expr), 100) + receive do + ^expr -> true + after + 100 -> flunk("No message matching #{expr} found in mailbox") end end end @@ -48,10 +47,8 @@ defmodule BlankAssertions do ExUnit.Assertions.refute(value, opts) end - defmacro flunk(message \\ "Flunked!") do - quote do - assert false, message: unquote(message) - end + def flunk(message \\ "Flunked!") do + ExUnit.Assertions.flunk(message) end defp contains_blank?(expr) do diff --git a/lib/koans/10_processes.ex b/lib/koans/10_processes.ex index 62173cf..8fd1630 100644 --- a/lib/koans/10_processes.ex +++ b/lib/koans/10_processes.ex @@ -2,18 +2,18 @@ defmodule Processes do use Koans koan "tests run in a process!" do - assert Process.alive?(self) + assert Process.alive?(:__) end koan "can spew out information about a process" do information = Process.info(self) - assert information[:status] == :running + assert information[:status] == :__ end koan "process can send messages to itself" do send self(), "hola!" - assert_receive "hola!" + assert_receive :__ end koan "a common pattern is to include the sender in the message" do @@ -24,7 +24,7 @@ defmodule Processes do end) send pid, {:hello, self()} - assert_receive :how_are_you? + assert_receive :__ end koan "you don't have to wait forever for messages" do @@ -36,22 +36,20 @@ defmodule Processes do end end) - assert_receive {:waited_too_long, "I am inpatient"} + assert_receive :__ end koan "killing a process will terminate it" do pid = spawn(fn -> Process.exit(self(), :kill) end) :timer.sleep(500) - refute Process.alive?(pid) + assert Process.alive?(pid) == :__ end koan "killing a process kills it for good" do - pid = spawn(fn -> receive do - end - end) + pid = spawn(fn -> receive do end end) assert Process.alive?(pid) Process.exit(pid, :kill) - refute Process.alive?(pid) + assert Process.alive?(pid) == :__ end koan "can trap a signal in a child process" do @@ -66,8 +64,7 @@ defmodule Processes do wait() Process.exit(pid, :random_reason) - assert_receive {:exited, :random_reason} - refute Process.alive?(pid) + assert_receive :__ end koan "quitting normally has no effect" do @@ -75,7 +72,7 @@ defmodule Processes do end end) Process.exit(pid, :normal) - assert Process.alive?(pid) + assert Process.alive?(pid) == :__ end koan "quititing your own process normally does terminate it though" do @@ -84,10 +81,9 @@ defmodule Processes do end end) - assert Process.alive?(pid) send pid, :bye :timer.sleep(100) - refute Process.alive?(pid) + assert Process.alive?(pid) == :__ end koan "linked processes are informed about exit signals of children when trapping those signals" do @@ -100,7 +96,7 @@ defmodule Processes do end end) - assert_receive {:exited, :normal} + assert_receive :__ end koan "monitoring processes are informed via messages without having trapping" do @@ -112,7 +108,7 @@ defmodule Processes do end end) - assert_receive {:exited, :normal} + assert_receive :__ end def wait do diff --git a/test/koans_harness_test.exs b/test/koans_harness_test.exs index 7d9df22..d1d187b 100644 --- a/test/koans_harness_test.exs +++ b/test/koans_harness_test.exs @@ -185,6 +185,25 @@ defmodule KoansHarnessTest do test_all(PatternMatching, answers) 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 module.all_koans |> Enum.zip(answers)