elixir-lang / elixir

Elixir is a dynamic, functional language for building scalable and maintainable applications
https://elixir-lang.org/
Apache License 2.0
24.34k stars 3.36k forks source link

Assert equal format #699

Closed orenbenkiki closed 11 years ago

orenbenkiki commented 11 years ago

Current format is: ** (ExUnit.AssertionError) Expected v1 to be equal to (==) v2

This is sub-optimal for two reasons:

A better way would be:

** (ExUnit.AssertionError)
Expected: v1
But got : v2

This makes it clear what was expected and what we got, and makes it trivial to compare the values visually to see the difference.

josevalim commented 11 years ago

This is what we want to do with #265. We need someone to work on it, write a proposal considering the different formats available, etc. Pull requests are welcome.

orenbenkiki commented 11 years ago

The issue #265 is somewhat different, it talks about showing the source of the expression that triggered the assertion failure. I find that having the stack trace is enough, myself.

This current issue (#699) is about the format of showing the values (not the source expression) that triggered the assertion. The following trivial patch would solve the issue:

diff --git a/lib/ex_unit/lib/ex_unit/assertions.ex b/lib/ex_unit/lib/ex_unit/assertions.ex
index b61ef1b..599adc7 100644
--- a/lib/ex_unit/lib/ex_unit/assertions.ex
+++ b/lib/ex_unit/lib/ex_unit/assertions.ex
@@ -157,7 +157,7 @@ defmodule ExUnit.Assertions do
       left  = unquote(expected)
       right = unquote(actual)
       assert unquote(operator).(left, right),
-        "Expected #{inspect left} to #{unquote(text)} #{inspect right}"
+        "Expected #{unquote(text)} to hold between:\nProvided value: #{inspect right}\nComputed value: #{inspect left}"
     end
   end

@@ -219,7 +219,8 @@ defmodule ExUnit.Assertions do
   def assert_raise(exception, message, function) when is_binary(message) and is_function(function) do
     error = assert_raise(exception, function)
     assert error.message == message,
-      "Expected #{inspect exception} to be raised with message #{inspect message}, got: #{error.message}"
+      "Expected #{inspect exception} was raised with unexpected message:\n" \
+      <> "Provided message: #{message}\nComputed message: #{error.message}"
     error
   end