Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 50 additions & 42 deletions lib/ex_unit/lib/ex_unit/assertions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,25 @@ defmodule ExUnit.Assertions do

"""
defmacro assert(expected) do
translate_assertion(expected, fn ->
quote do
value = unquote(expected)

unless value do
raise ExUnit.ExpectationError,
expr: unquote(Macro.to_string(expected)),
assertion: "be",
expected: "true",
actual: inspect(value)
case translate_assertion(expected) do
nil ->
# Default message in case no transform was performed
quote do
value = unquote(expected)

unless value do
raise ExUnit.ExpectationError,
expr: unquote(Macro.to_string(expected)),
assertion: "be",
expected: "true",
actual: inspect(value)
end

value
end

value
end
end)
value -> value
end
end

@doc """
Expand All @@ -79,28 +83,32 @@ defmodule ExUnit.Assertions do

"""
defmacro refute(expected) do
contents = translate_assertion({ :!, [], [expected] }, fn ->
quote do
value = unquote(expected)

if value do
raise ExUnit.ExpectationError,
expr: unquote(Macro.to_string(expected)),
assertion: "be",
expected: "false",
actual: inspect(value)
contents = case translate_assertion({ :!, [], [expected] }) do
nil ->
# Default message in case no transform was performed
quote do
value = unquote(expected)

if value do
raise ExUnit.ExpectationError,
expr: unquote(Macro.to_string(expected)),
assertion: "be",
expected: "false",
actual: inspect(value)
end

true
end

true
end
end)
value -> value
end

{ :!, [], [contents] }
end

## START HELPERS

defp translate_assertion({ :=, _, [left, right] }, _else) do
defp translate_assertion({ :=, _, [left, right] }) do
quote do
right = unquote(right)
case right do
Expand All @@ -115,43 +123,43 @@ defmodule ExUnit.Assertions do
end
end

defp translate_assertion({ :==, _, [left, right] }, _else) do
defp translate_assertion({ :==, _, [left, right] }) do
assert_operator :==, left, right, "be equal to (==)"
end

defp translate_assertion({ :<, _, [left, right] }, _else) do
defp translate_assertion({ :<, _, [left, right] }) do
assert_operator :<, left, right, "be less than"
end

defp translate_assertion({ :>, _, [left, right] }, _else) do
defp translate_assertion({ :>, _, [left, right] }) do
assert_operator :>, left, right, "be more than"
end

defp translate_assertion({ :<=, _, [left, right] }, _else) do
defp translate_assertion({ :<=, _, [left, right] }) do
assert_operator :<=, left, right, "be less than or equal to"
end

defp translate_assertion({ :>=, _, [left, right] }, _else) do
defp translate_assertion({ :>=, _, [left, right] }) do
assert_operator :>=, left, right, "be more than or equal to"
end

defp translate_assertion({ :===, _, [left, right] }, _else) do
defp translate_assertion({ :===, _, [left, right] }) do
assert_operator :===, left, right, "be equal to (===)"
end

defp translate_assertion({ :!==, _, [left, right] }, _else) do
defp translate_assertion({ :!==, _, [left, right] }) do
assert_operator :!==, left, right, "be not equal to (!==)"
end

defp translate_assertion({ :!=, _, [left, right] }, _else) do
defp translate_assertion({ :!=, _, [left, right] }) do
assert_operator :!=, left, right, "be not equal to (!=)"
end

defp translate_assertion({ :=~, _, [left, right] }, _else) do
defp translate_assertion({ :=~, _, [left, right] }) do
assert_operator :=~, left, right, "match (=~)"
end

defp translate_assertion({ :in, _, [left, right] }, _else) do
defp translate_assertion({ :in, _, [left, right] }) do
quote do
left = unquote(left)
right = unquote(right)
Expand All @@ -161,7 +169,7 @@ defmodule ExUnit.Assertions do

## Negative versions

defp translate_assertion({ :!, _, [{ :=, _, [left, right] }] }, _else) do
defp translate_assertion({ :!, _, [{ :=, _, [left, right] }] }) do
quote do
right = unquote(right)
case right do
Expand All @@ -177,15 +185,15 @@ defmodule ExUnit.Assertions do
end
end

defp translate_assertion({ :!, _, [{ :=~, _, [left, right] }] }, _else) do
defp translate_assertion({ :!, _, [{ :=~, _, [left, right] }] }) do
quote do
left = unquote(left)
right = unquote(right)
assert !(left =~ right), left, right, assertion: "match (=~)", negation: true
end
end

defp translate_assertion({ negation, _, [{ :in, _, [left, right] }] }, _else) when negation in [:!, :not] do
defp translate_assertion({ negation, _, [{ :in, _, [left, right] }] }) when negation in [:!, :not] do
quote do
left = unquote(left)
right = unquote(right)
Expand All @@ -195,8 +203,8 @@ defmodule ExUnit.Assertions do

## Fallback

defp translate_assertion(_expected, fallback) do
fallback.()
defp translate_assertion(_expected) do
nil
end

defp assert_operator(operator, expected, actual, text) do
Expand Down