From 43c2bbdd7187e4fd4c00bc83610f72bff32a5af7 Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Fri, 10 Jun 2016 11:39:39 -0500 Subject: [PATCH] Show left and right hand values when equality fails For assertions that contain function calls, displaying just the expression is not helpful. For e.g. Assertion failed String.upcase("foo") == "Foo" The learner is given no extra information to help them learn. Sure they might consult the documentation (and should!), but perhaps a better experience would be for us to inform the learner about the values it encountered. Assertion failed String.upcase("foo") == "Foo" left: "FOO" right: "Foo" Learner: :bulb: ah hah! it upcases the whole string! :dizzy: --- lib/display/colours.ex | 2 ++ lib/display/failure.ex | 14 +++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/display/colours.ex b/lib/display/colours.ex index 632da64..d76a0fd 100644 --- a/lib/display/colours.ex +++ b/lib/display/colours.ex @@ -2,6 +2,7 @@ defmodule Display.Paint do def red(str), do: painter().red(str) def cyan(str), do: painter().cyan(str) def green(str), do: painter().green(str) + def yellow(str), do: painter.yellow(str) defp painter do case Mix.env do @@ -17,6 +18,7 @@ defmodule Display.Colours do def red(str), do: colourize(ANSI.red, str) def cyan(str), do: colourize(ANSI.cyan, str) def green(str), do: colourize(ANSI.green, str) + def yellow(str), do: colourize(ANSI.yellow, str) defp colourize(color, message) do Enum.join([color, message, ANSI.reset], "") diff --git a/lib/display/failure.ex b/lib/display/failure.ex index 7c8eacc..855feff 100644 --- a/lib/display/failure.ex +++ b/lib/display/failure.ex @@ -9,11 +9,12 @@ defmodule Display.Failure do #{Paint.red(message)} """ end - def format_failure(%{error: %ExUnit.AssertionError{expr: expr}, file: file, line: line}) do + def format_failure(%{error: %ExUnit.AssertionError{expr: expr} = error, file: file, line: line}) do """ #{Paint.cyan("Assertion failed in #{file}:#{line}")} #{Paint.red(Macro.to_string(expr))} """ + |> format_inequality(error) end def format_failure(%{error: error, file: file, line: line}) do """ @@ -22,6 +23,17 @@ defmodule Display.Failure do """ end + defp format_inequality(message, %{left: @no_value, right: @no_value}) do + message + end + defp format_inequality(message, %{left: left, right: right}) do + """ + #{message} + left: #{left |> inspect |> Paint.yellow} + right: #{right |> inspect |> Paint.yellow} + """ + end + defp format_error(error) do trace = System.stacktrace |> Enum.take(2) Paint.red(Exception.format(:error, error, trace))