Skip to content

Commit 5700196

Browse files
authored
Improve raise_error messages (#218)
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.
1 parent c685536 commit 5700196

File tree

4 files changed

+580
-231
lines changed

4 files changed

+580
-231
lines changed

lib/super_diff/rspec/matcher_text_builders/raise_error.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@ class RaiseError < Base
55
protected
66

77
def actual_phrase
8-
actual ? "Expected raised exception" : "Expected"
9-
end
10-
11-
def add_actual_value
12-
if actual
13-
template.add_text_in_color(actual_color) { actual }
8+
if actual == "exception-free block"
9+
"Expected"
1410
else
15-
template.add_text("block")
11+
"Expected raised exception"
1612
end
1713
end
1814
end

lib/super_diff/rspec/monkey_patches.rb

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -722,9 +722,15 @@ class RaiseError
722722
SuperDiff.insert_overrides(self, SuperDiff::RSpec::AugmentedMatcher)
723723

724724
SuperDiff.insert_overrides(self) do
725+
def expected_action_for_description
726+
"raise"
727+
end
728+
725729
def actual_for_matcher_text
726730
if @actual_error
727731
"#<#{@actual_error.class.name} #{@actual_error.message.inspect}>"
732+
else
733+
"exception-free block"
728734
end
729735
end
730736

@@ -733,10 +739,19 @@ def actual_for_diff
733739
end
734740

735741
def expected_for_matcher_text
736-
if @expected_message
737-
"#<#{describe_expected_error} #{description_of(@expected_message)}>"
742+
case @expected_message
743+
when nil
744+
if RSpec::Matchers.is_a_describable_matcher?(@expected_error)
745+
description_of(@expected_error)
746+
elsif @expected_error.is_a?(Regexp)
747+
"a kind of Exception with message matching #{description_of(@expected_error)}"
748+
else
749+
"a kind of #{@expected_error}"
750+
end
751+
when Regexp
752+
"#{described_expected_error} with message matching #{description_of(@expected_message)}"
738753
else
739-
"#<#{describe_expected_error}>"
754+
"#{described_expected_error} with message #{description_of(@expected_message)}"
740755
end
741756
end
742757

@@ -749,7 +764,7 @@ def diffable?
749764
end
750765

751766
def expected_action_for_failure_message
752-
@actual_error ? "match" : "raise error"
767+
@actual_error ? "match" : "raise"
753768
end
754769

755770
def matcher_text_builder_class
@@ -758,15 +773,11 @@ def matcher_text_builder_class
758773

759774
private
760775

761-
def describe_expected_error
762-
if @expected_error.is_a? Class
763-
@expected_error.name
764-
elsif @expected_error.is_a? Regexp
765-
"Exception #{description_of(@expected_error)}"
766-
elsif @expected_error.respond_to? :description
767-
@expected_error.description
776+
def described_expected_error
777+
if @expected_error.is_a?(Class)
778+
"a kind of #{@expected_error}"
768779
else
769-
SuperDiff.inspect_object(@expected_error, as_lines: false)
780+
description_of(@expected_error)
770781
end
771782
end
772783
end

0 commit comments

Comments
 (0)