From e1674518fb50c5b77bc5f9fa87acff10cb00a468 Mon Sep 17 00:00:00 2001 From: Aleksei Magusev Date: Sun, 26 Jun 2016 12:03:59 +0200 Subject: [PATCH] Improve doctest failure messages for exception assertions (#4889) --- lib/ex_unit/lib/ex_unit/assertions.ex | 16 ++++++----- lib/ex_unit/lib/ex_unit/doc_test.ex | 30 +++++++++++++------- lib/ex_unit/test/ex_unit/assertions_test.exs | 8 +++--- lib/ex_unit/test/ex_unit/doc_test_test.exs | 8 ++++-- 4 files changed, 39 insertions(+), 23 deletions(-) diff --git a/lib/ex_unit/lib/ex_unit/assertions.ex b/lib/ex_unit/lib/ex_unit/assertions.ex index 2b05b211c3..a4e73185f0 100644 --- a/lib/ex_unit/lib/ex_unit/assertions.ex +++ b/lib/ex_unit/lib/ex_unit/assertions.ex @@ -476,17 +476,19 @@ defmodule ExUnit.Assertions do def assert_raise(exception, message, function) when is_function(function) do error = assert_raise(exception, function) - is_match = cond do + match? = cond do is_binary(message) -> Exception.message(error) == message Regex.regex?(message) -> Exception.message(error) =~ message end - msg = "Wrong message for #{inspect exception}\n" <> - "Expected:\n" <> - " #{inspect message}\n" <> - "Got:\n" <> - " #{inspect Exception.message(error)}" - assert is_match, message: msg + message = + "Wrong message for #{inspect exception}\n" <> + "expected:\n" <> + " #{inspect message}\n" <> + "actual:\n" <> + " #{inspect Exception.message(error)}" + + assert match?, message: message error end diff --git a/lib/ex_unit/lib/ex_unit/doc_test.ex b/lib/ex_unit/lib/ex_unit/doc_test.ex index ba472102af..8ad919ff09 100644 --- a/lib/ex_unit/lib/ex_unit/doc_test.ex +++ b/lib/ex_unit/lib/ex_unit/doc_test.ex @@ -330,25 +330,35 @@ defmodule ExUnit.DocTest do quote do stack = unquote(stack) - expr = unquote(String.trim(expr)) - spec = inspect(unquote(exception)) <> " with message " <> inspect(unquote(message)) + expr = unquote(String.trim(expr)) try do unquote(expr_ast) rescue error -> - unless error.__struct__ == unquote(exception) and - Exception.message(error) == unquote(message) do - got = inspect(error.__struct__) <> " with message " <> inspect(Exception.message(error)) - reraise ExUnit.AssertionError, - [message: "Doctest failed: expected exception #{spec} but got #{got}", - expr: expr], - stack + actual_exception = error.__struct__ + actual_message = Exception.message(error) + message = + cond do + actual_exception != unquote(exception) -> + "Doctest failed: expected exception #{inspect(unquote(exception))} but got #{inspect(actual_exception)} with message #{inspect(actual_message)}" + actual_message != unquote(message) -> + "Doctest failed: wrong message for #{inspect(actual_exception)}\n" <> + "expected:\n" <> + " #{inspect(unquote(message))}\n" <> + "actual:\n" <> + " #{inspect(actual_message)}" + true -> + nil + end + + if message do + reraise ExUnit.AssertionError, [message: message, expr: expr], stack end else _ -> reraise ExUnit.AssertionError, - [message: "Doctest failed: expected exception #{spec} but nothing was raised", + [message: "Doctest failed: expected exception #{inspect(unquote(exception))} but nothing was raised", expr: expr], stack end diff --git a/lib/ex_unit/test/ex_unit/assertions_test.exs b/lib/ex_unit/test/ex_unit/assertions_test.exs index 1c613c9bdb..ff96d80348 100644 --- a/lib/ex_unit/test/ex_unit/assertions_test.exs +++ b/lib/ex_unit/test/ex_unit/assertions_test.exs @@ -453,9 +453,9 @@ defmodule ExUnit.AssertionsTest do rescue error in [ExUnit.AssertionError] -> "Wrong message for RuntimeError" <> - "\nExpected:" <> + "\nexpected:" <> "\n \"foo\"" <> - "\nGot:" <> + "\nactual:" <> "\n \"bar\"" = error.message end @@ -466,9 +466,9 @@ defmodule ExUnit.AssertionsTest do rescue error in [ExUnit.AssertionError] -> "Wrong message for RuntimeError" <> - "\nExpected:" <> + "\nexpected:" <> "\n ~r/ba[zk]/" <> - "\nGot:" <> + "\nactual:" <> "\n \"bar\"" = error.message end diff --git a/lib/ex_unit/test/ex_unit/doc_test_test.exs b/lib/ex_unit/test/ex_unit/doc_test_test.exs index b97c2ea42c..b5d82fa194 100644 --- a/lib/ex_unit/test/ex_unit/doc_test_test.exs +++ b/lib/ex_unit/test/ex_unit/doc_test_test.exs @@ -298,7 +298,7 @@ defmodule ExUnit.DocTestTest do assert output =~ """ 5) test moduledoc at ExUnit.DocTestTest.Invalid (5) (ExUnit.DocTestTest.ActuallyCompiled) test/ex_unit/doc_test_test.exs:248 - Doctest failed: expected exception WhatIsThis with message "oops" but got RuntimeError with message "oops" + Doctest failed: expected exception WhatIsThis but got RuntimeError with message "oops" code: raise "oops" stacktrace: test/ex_unit/doc_test_test.exs:139: ExUnit.DocTestTest.Invalid (module) @@ -307,7 +307,11 @@ defmodule ExUnit.DocTestTest do assert output =~ """ 6) test moduledoc at ExUnit.DocTestTest.Invalid (6) (ExUnit.DocTestTest.ActuallyCompiled) test/ex_unit/doc_test_test.exs:248 - Doctest failed: expected exception RuntimeError with message "hello" but got RuntimeError with message "oops" + Doctest failed: wrong message for RuntimeError + expected: + "hello" + actual: + "oops" code: raise "oops" stacktrace: test/ex_unit/doc_test_test.exs:142: ExUnit.DocTestTest.Invalid (module)