Skip to content

Commit

Permalink
Improve raise_error messages (#218)
Browse files Browse the repository at this point in the history
This commit makes some minor changes to the `raise_error` matcher to
make the failure messages slightly more friendly and consistent with the
overall philosophy of matchers in this repo.

Here are the current messages in comparison with the new ones:

- In cases where the block raises no error, the wording changes from
"Expected block" to "Expected exception-free block", and "exception-free
block" is highlighted in the actual color. This is to emphasize the
actual behavior vs. the expected behavior and matches other matchers.
- In cases where only an expected error class is provided, the wording
changes so that instead of inspecting the expected error, the failure
message says "a kind of" followed by the class name.
- In cases where a message is provided, the expected class and message
are separated in the failure message.
- In cases where a regexp message is provided, the wording changes from
"with message #{message}" to "with message matching #{message}".

Additionally, the integration tests for `raise_error` have been reviewed
and missing tests have been filled in.
  • Loading branch information
mcmire authored Feb 10, 2024
1 parent c685536 commit 5700196
Show file tree
Hide file tree
Showing 4 changed files with 580 additions and 231 deletions.
10 changes: 3 additions & 7 deletions lib/super_diff/rspec/matcher_text_builders/raise_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ class RaiseError < Base
protected

def actual_phrase
actual ? "Expected raised exception" : "Expected"
end

def add_actual_value
if actual
template.add_text_in_color(actual_color) { actual }
if actual == "exception-free block"
"Expected"
else
template.add_text("block")
"Expected raised exception"
end
end
end
Expand Down
35 changes: 23 additions & 12 deletions lib/super_diff/rspec/monkey_patches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -722,9 +722,15 @@ class RaiseError
SuperDiff.insert_overrides(self, SuperDiff::RSpec::AugmentedMatcher)

SuperDiff.insert_overrides(self) do
def expected_action_for_description
"raise"
end

def actual_for_matcher_text
if @actual_error
"#<#{@actual_error.class.name} #{@actual_error.message.inspect}>"
else
"exception-free block"
end
end

Expand All @@ -733,10 +739,19 @@ def actual_for_diff
end

def expected_for_matcher_text
if @expected_message
"#<#{describe_expected_error} #{description_of(@expected_message)}>"
case @expected_message
when nil
if RSpec::Matchers.is_a_describable_matcher?(@expected_error)
description_of(@expected_error)
elsif @expected_error.is_a?(Regexp)
"a kind of Exception with message matching #{description_of(@expected_error)}"
else
"a kind of #{@expected_error}"
end
when Regexp
"#{described_expected_error} with message matching #{description_of(@expected_message)}"
else
"#<#{describe_expected_error}>"
"#{described_expected_error} with message #{description_of(@expected_message)}"
end
end

Expand All @@ -749,7 +764,7 @@ def diffable?
end

def expected_action_for_failure_message
@actual_error ? "match" : "raise error"
@actual_error ? "match" : "raise"
end

def matcher_text_builder_class
Expand All @@ -758,15 +773,11 @@ def matcher_text_builder_class

private

def describe_expected_error
if @expected_error.is_a? Class
@expected_error.name
elsif @expected_error.is_a? Regexp
"Exception #{description_of(@expected_error)}"
elsif @expected_error.respond_to? :description
@expected_error.description
def described_expected_error
if @expected_error.is_a?(Class)
"a kind of #{@expected_error}"
else
SuperDiff.inspect_object(@expected_error, as_lines: false)
description_of(@expected_error)
end
end
end
Expand Down
Loading

0 comments on commit 5700196

Please sign in to comment.