Skip to content

Commit

Permalink
Revert removal of assert_equal
Browse files Browse the repository at this point in the history
  • Loading branch information
José Valim committed Apr 25, 2012
1 parent d515e0a commit 929f84f
Show file tree
Hide file tree
Showing 40 changed files with 831 additions and 780 deletions.
196 changes: 110 additions & 86 deletions lib/ex_unit/assertions.ex
Expand Up @@ -53,91 +53,6 @@ defmodule ExUnit.Assertions do
true
end

## START HELPERS

defmacrop negation?(op) do
quote do: (var!(op) == :! or var!(op) == :not)
end

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

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

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

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

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

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

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

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

defp translate_assertion({ :access, _, [container, base] }) do
quote do
container = unquote(container)
base = unquote(base)
assert(container[base], "Expected #{inspect base} to access #{inspect container}")
end
end

defp translate_assertion({ op, _, [{ :access, _, [container, base] }] }) when negation?(op) do
quote do
container = unquote(container)
base = unquote(base)
assert(!container[base], "Expected #{inspect base} to not access #{inspect container}")
end
end

defp translate_assertion(expected) do
quote do
value = unquote(expected)
assert value, "Expected #{inspect value} to be true"
end
end

defp guess_expected_and_actual(left, right) do
case right do
match: { fun, i, _ } when is_integer(i) and (fun != :<<>> or fun != :{})
{ left, right }
else:
{ right, left }
end
end

defp assert_operator(operator, expected, actual, text) do
quote do
left = unquote(expected)
right = unquote(actual)
assert unquote(operator).(left, right),
"Expected #{inspect left} to be #{unquote(text)} #{inspect right}"
end
end

## END HELPERS

@doc """
Asserts the `expected` value matches `received`. This relies
on Elixir's pattern match instead of simply comparing terms.
Expand Down Expand Up @@ -172,6 +87,19 @@ defmodule ExUnit.Assertions do
assert(Enum.find(container, &1 == base), message)
end

@doc """
Asserts the `expected` value is equal to `received`.
## Examples
assert_equal 0, 0
"""
def assert_equal(expected, received, message // nil) do
message = message || "Expected #{inspect received} to be equal to #{inspect expected}"
assert(expected == received, message)
end

@doc """
Asserts the `exception` is raised during `function` execution with the expected message.
Expand All @@ -183,7 +111,7 @@ defmodule ExUnit.Assertions do
"""
def assert_raise(exception, expected_message, function) do
error = assert_raise(exception, function)
assert expected_message == error.message
assert_equal expected_message, error.message
end

@doc """
Expand Down Expand Up @@ -334,6 +262,19 @@ defmodule ExUnit.Assertions do
end
end

@doc """
Asserts the `expected` value is not equal to `received`.
## Examples
refute_equal 0, 1
"""
def refute_equal(expected, received, message // nil) do
message = message || "Expected #{inspect received} to not be equal to #{inspect expected}"
refute(expected == received, message)
end

@doc """
Asserts the `enum` collection is not empty.
Expand Down Expand Up @@ -397,4 +338,87 @@ defmodule ExUnit.Assertions do
def flunk(message // "Epic Fail!") do
assert false, message
end

## Helpers

defmacrop negation?(op) do
quote do: (var!(op) == :! or var!(op) == :not)
end

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

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

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

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

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

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

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

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

defp translate_assertion({ :access, _, [container, base] }) do
quote do
container = unquote(container)
base = unquote(base)
assert(container[base], "Expected #{inspect base} to access #{inspect container}")
end
end

defp translate_assertion({ op, _, [{ :access, _, [container, base] }] }) when negation?(op) do
quote do
container = unquote(container)
base = unquote(base)
assert(!container[base], "Expected #{inspect base} to not access #{inspect container}")
end
end

defp translate_assertion(expected) do
quote do
value = unquote(expected)
assert value, "Expected #{inspect value} to be true"
end
end

defp guess_expected_and_actual(left, right) do
case right do
match: { fun, i, _ } when is_integer(i) and (fun != :<<>> or fun != :{})
{ left, right }
else:
{ right, left }
end
end

defp assert_operator(operator, expected, actual, text) do
quote do
left = unquote(expected)
right = unquote(actual)
assert unquote(operator).(left, right),
"Expected #{inspect left} to be #{unquote(text)} #{inspect right}"
end
end
end
2 changes: 1 addition & 1 deletion lib/ex_unit/case.ex
Expand Up @@ -28,7 +28,7 @@ defmodule ExUnit.Case do
# ## Examples
#
# test "true is equal to true" do
# assert true == true
# assert_equal true, true
# end
#
defmacro test(message, contents) do
Expand Down
36 changes: 18 additions & 18 deletions test/elixir/binary/chars_test.exs
Expand Up @@ -4,26 +4,26 @@ defmodule Binary.Chars.AtomTest do
use ExUnit.Case

test :basic do
assert to_binary(:foo) == "foo"
assert_equal "foo", to_binary(:foo)
end

test :empty do
assert to_binary(:"") == ""
assert_equal "", to_binary(:"")
end

test :true_false_nil do
assert to_binary(false) == "false"
assert to_binary(true) == "true"
assert to_binary(nil) == ""
assert_equal "false", to_binary(false)
assert_equal "true", to_binary(true)
assert_equal "", to_binary(nil)
end

test :with_uppercase do
assert to_binary(:fOO) == "fOO"
assert to_binary(:FOO) == "FOO"
assert_equal "fOO", to_binary(:fOO)
assert_equal "FOO", to_binary(:FOO)
end

test :reference_atom do
assert to_binary(Foo.Bar) == "__MAIN__.Foo.Bar"
assert_equal "__MAIN__.Foo.Bar", to_binary(Foo.Bar)
end
end

Expand All @@ -37,38 +37,38 @@ defmodule Binary.Chars.BitStringTest do
end

test :binary do
assert to_binary("foo") == "foo"
assert to_binary(<<?a, ?b, ?c>>) == "abc"
assert to_binary("我今天要学习.") == "我今天要学习."
assert_equal "foo", to_binary("foo")
assert_equal "abc", to_binary(<<?a, ?b, ?c>>)
assert_equal "我今天要学习.", to_binary("我今天要学习.")
end
end

defmodule Binary.Chars.NumberTest do
use ExUnit.Case

test :integer do
assert to_binary(100) == "100"
assert_equal "100", to_binary(100)
end

test :float do
assert to_binary(1.0) == "1.00000000000000000000e+00"
assert to_binary(1.0e10) == "1.00000000000000000000e+10"
assert to_binary(1.0e+10) == "1.00000000000000000000e+10"
assert_equal "1.00000000000000000000e+00", to_binary(1.0)
assert_equal "1.00000000000000000000e+10", to_binary(1.0e10)
assert_equal "1.00000000000000000000e+10", to_binary(1.0e+10)
end
end

defmodule Binary.Chars.ListTest do
use ExUnit.Case

test :basic do
assert to_binary([ 1, "b", 3 ]) == <<1,98,3>>
assert_equal <<1,98,3>>, to_binary([ 1, "b", 3 ])
end

test :printable do
assert to_binary('abc') == "abc"
assert_equal "abc" , to_binary('abc')
end

test :empty do
assert to_binary([]) == ""
assert_equal "", to_binary([])
end
end

0 comments on commit 929f84f

Please sign in to comment.