Auto-pin variables when replacing blanks in assert_receive

This commit is contained in:
Jay Hayes
2016-05-02 20:45:03 -05:00
parent 1b99ab0650
commit c7c2fbcdff
3 changed files with 31 additions and 6 deletions

View File

@@ -7,10 +7,25 @@ defmodule Blanks do
|> elem(0)
end
defp pre({:assert_receive, _, args} = node, replacements) do
{args, replacements} = Macro.prewalk(args, replacements, &pre_pin/2)
{put_elem(node, 2, args), replacements}
end
defp pre(:___, [first | remainder]), do: {first, remainder}
defp pre({:___, _, _}, [first | remainder]), do: {first, remainder}
defp pre(node, acc), do: {node, acc}
defp pre_pin(:___, [first | remainder]), do: {pin(first), remainder}
defp pre_pin({:___, _, _}, [first | remainder]), do: {pin(first), remainder}
defp pre_pin(node, acc), do: {node, acc}
defp pin(var) when is_tuple(var) do
quote do
^unquote(var)
end
end
defp pin(var), do: var
def count(ast) do
ast
|> Macro.prewalk(0, &count/2)

View File

@@ -13,7 +13,7 @@ defmodule Processes do
koan "You can send messages to any process you want" do
send self(), "hola!"
assert_receive ^___
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 ^___
assert_receive ___
end
koan "Waiting for a message can get boring" do
@@ -35,7 +35,7 @@ defmodule Processes do
end
end)
assert_receive ^___
assert_receive ___
end
koan "Killing a process will terminate it" do
@@ -64,7 +64,7 @@ defmodule Processes do
wait()
Process.exit(pid, :random_reason)
assert_receive ^___
assert_receive ___
end
koan "Trying to quit normally has no effect" do
@@ -96,7 +96,7 @@ defmodule Processes do
end
end)
assert_receive ^___
assert_receive ___
end
koan "If you monitor your children, you'll be automatically informed for their depature" do
@@ -108,7 +108,7 @@ defmodule Processes do
end
end)
assert_receive ^___
assert_receive ___
end
def wait do

View File

@@ -24,6 +24,16 @@ defmodule BlanksTest do
assert Blanks.replace(ast, [true, false]) == quote(do: assert true == false)
end
test "pins variables in assert_receive replacement" do
ast = quote do: assert_receive ___
assert Blanks.replace(ast, Macro.var(:answer, __MODULE__)) == quote(do: assert_receive ^answer)
end
test "does not pin values in assert_receive replacement" do
ast = quote do: assert_receive ___
assert Blanks.replace(ast, :lolwat) == quote(do: assert_receive :lolwat)
end
test "counts simple blanks" do
ast = quote do: 1 + ___