summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2015-09-26 18:23:26 +0200
committerJosé Valim <jose.valim@gmail.com>2015-09-26 18:23:26 +0200
commit7ba752e0381d24361347d2071527a2e6874d97ee (patch)
tree05e25a4dfd25986a55c65b1b0680a92129fb7d47
parent6f5ca9b4f530c31cdb5c6d156ba3fcff19fe76ab (diff)
parentb206ce368f53d5f9e8d1b197dca64fe4173bdfe4 (diff)
downloadelixir-7ba752e0381d24361347d2071527a2e6874d97ee.tar.gz
Merge pull request #3772 from antipax/fix-assertion-errors-with-more-than-1-pinned-var
Fix assertion errors with more than 1 pinned var
-rw-r--r--lib/ex_unit/lib/ex_unit/assertions.ex8
-rw-r--r--lib/ex_unit/test/ex_unit/assertions_test.exs40
2 files changed, 45 insertions, 3 deletions
diff --git a/lib/ex_unit/lib/ex_unit/assertions.ex b/lib/ex_unit/lib/ex_unit/assertions.ex
index b579c6e28..7a06a8261 100644
--- a/lib/ex_unit/lib/ex_unit/assertions.ex
+++ b/lib/ex_unit/lib/ex_unit/assertions.ex
@@ -353,7 +353,9 @@ defmodule ExUnit.Assertions do
defp pinned_vars([]), do: ""
defp pinned_vars(pins) do
- content = Enum.map(pins, fn(var) ->
+ content = Enum.reverse(pins)
+ |> Enum.uniq_by(&elem(&1, 0))
+ |> Enum.map(fn(var) ->
binary = Macro.to_string(var)
quote do
"\n " <> unquote(binary) <> " = " <> inspect(unquote(var))
@@ -361,7 +363,7 @@ defmodule ExUnit.Assertions do
end)
quote do
- "\nThe following variables were pinned:" <> unquote_splicing(content)
+ <<"\nThe following variables were pinned:", unquote_splicing(content)>>
end
end
@@ -384,7 +386,7 @@ defmodule ExUnit.Assertions do
assert_raise ArithmeticError, "bad argument in arithmetic expression", fn ->
1 + "test"
end
-
+
assert_raise RuntimeError, ~r/^Today's lucky number is 0\.\d+!$/, fn ->
raise "Today's lucky number is #{:random.uniform}!"
end
diff --git a/lib/ex_unit/test/ex_unit/assertions_test.exs b/lib/ex_unit/test/ex_unit/assertions_test.exs
index 5f93d152a..201156371 100644
--- a/lib/ex_unit/test/ex_unit/assertions_test.exs
+++ b/lib/ex_unit/test/ex_unit/assertions_test.exs
@@ -127,6 +127,46 @@ defmodule ExUnit.AssertionsTest do
end
end
+ test "assert received with multiple identical pinned variables" do
+ status = :valid
+ send self(), {:status, :invalid, :invalid}
+ try do
+ "This should never be tested" = assert_received {
+ :status,
+ ^status,
+ ^status
+ }
+ rescue
+ error in [ExUnit.AssertionError] ->
+ "No message matching {:status, ^status, ^status} after 0ms.\n" <>
+ "The following variables were pinned:\n" <>
+ " status = :valid\n" <>
+ "Process mailbox:\n" <>
+ " {:status, :invalid, :invalid}" = error.message
+ end
+ end
+
+ test "assert received with multiple unique pinned variables" do
+ status = :valid
+ other_status = :invalid
+ send self(), {:status, :invalid, :invalid}
+ try do
+ "This should never be tested" = assert_received {
+ :status,
+ ^status,
+ ^other_status
+ }
+ rescue
+ error in [ExUnit.AssertionError] ->
+ "No message matching {:status, ^status, ^other_status} after 0ms.\n" <>
+ "The following variables were pinned:\n" <>
+ " status = :valid\n" <>
+ " other_status = :invalid\n" <>
+ "Process mailbox:\n" <>
+ " {:status, :invalid, :invalid}" = error.message
+ end
+ end
+
test "assert received when empty mailbox" do
try do
"This should never be tested" = assert_received :hello