Skip to content

Commit 937a5d3

Browse files
committed
Ensure that descriptions for RSpec matchers are correct
1 parent 6839ec9 commit 937a5d3

21 files changed

+418
-137
lines changed

lib/super_diff/rspec/augmented_matcher.rb

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,55 @@ def build_failure_message(negated:)
2929
matcher_text_builder.call(negated: negated)
3030
end
3131

32-
def actual_for_failure_message
32+
def matcher_text_builder
33+
@_matcher_text_builder ||=
34+
matcher_text_builder_class.new(matcher_text_builder_args)
35+
end
36+
37+
def matcher_text_builder_class
38+
RSpec::MatcherTextBuilders::Base
39+
end
40+
41+
def matcher_text_builder_args
42+
{
43+
actual: actual_for_matcher_text,
44+
expected_for_failure_message: expected_for_failure_message,
45+
expected_for_description: expected_for_description,
46+
expected_action_for_failure_message: expected_action_for_failure_message,
47+
expected_action_for_description: expected_action_for_description,
48+
}
49+
end
50+
51+
def actual_for_matcher_text
3352
description_of(actual)
3453
end
3554

36-
def expected_for_failure_message
55+
def expected_for_matcher_text
3756
description_of(expected)
3857
end
3958

40-
def matcher_text_builder
41-
@_matcher_text_builder ||=
42-
matcher_text_builder_class.new(
43-
actual: actual_for_failure_message,
44-
expected: expected_for_failure_message,
45-
expected_action: expected_action,
46-
)
59+
def expected_for_failure_message
60+
expected_for_matcher_text
4761
end
4862

49-
def matcher_text_builder_class
50-
MatcherTextBuilders::Base
63+
def expected_for_description
64+
expected_for_matcher_text
5165
end
5266

5367
private
5468

55-
def expected_action
69+
def expected_action_for_matcher_text
5670
::RSpec::Matchers::EnglishPhrasing.split_words(self.class.matcher_name)
5771
end
5872

73+
def expected_action_for_failure_message
74+
expected_action_for_matcher_text
75+
end
76+
77+
def expected_action_for_description
78+
expected_action_for_matcher_text
79+
end
80+
5981
def readable_list_of(things)
6082
::RSpec::Matchers::EnglishPhrasing.list(things)
6183
end

lib/super_diff/rspec/matcher_text_builders/base.rb

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,19 @@ module SuperDiff
22
module RSpec
33
module MatcherTextBuilders
44
class Base
5-
def initialize(actual:, expected:, expected_action:)
5+
def initialize(
6+
actual:,
7+
expected_for_failure_message:,
8+
expected_for_description:,
9+
expected_action_for_failure_message:,
10+
expected_action_for_description:
11+
)
612
@actual = actual
7-
@expected = expected
8-
@expected_action = expected_action
13+
@expected_for_failure_message = expected_for_failure_message
14+
@expected_for_description = expected_for_description
15+
@expected_action_for_failure_message =
16+
expected_action_for_failure_message
17+
@expected_action_for_description = expected_action_for_description
918

1019
@negated = nil
1120
@template = MatcherTextTemplate.new
@@ -20,16 +29,17 @@ def call(negated:)
2029

2130
def matcher_description
2231
template = MatcherTextTemplate.new do |t|
23-
t.add_text expected_action
24-
add_expected_value_to(t)
32+
t.add_text expected_action_for_description
33+
add_expected_value_to_description(t)
34+
add_extra_after_expected_to(t)
2535
end
2636

2737
Csi.decolorize(template.to_s(as_single_line: true))
2838
end
2939

3040
protected
3141

32-
def add_extra_after_expected
42+
def add_extra_after_expected_to(template)
3343
end
3444

3545
def add_extra_after_error
@@ -45,7 +55,14 @@ def expected_color
4555

4656
private
4757

48-
attr_reader :expected, :actual, :expected_action, :template
58+
attr_reader(
59+
:actual,
60+
:expected_for_failure_message,
61+
:expected_for_description,
62+
:expected_action_for_failure_message,
63+
:expected_action_for_description,
64+
:template,
65+
)
4966

5067
def negated?
5168
@negated
@@ -55,7 +72,7 @@ def fill_template
5572
add_actual_section
5673
template.add_break
5774
template.insert expected_section
58-
add_extra_after_expected
75+
add_extra_after_expected_to(template)
5976
template.add_text_in_singleline_mode "."
6077
add_extra_after_error
6178
end
@@ -83,17 +100,33 @@ def expected_section
83100
t.add_text_in_multiline_mode do
84101
expected_phrase.to_s.rjust(phrase_width)
85102
end
86-
add_expected_value_to(t)
103+
add_expected_value_to_failure_message(t)
87104
end
88105
end
89106

90-
def add_expected_value_to(template)
91-
template.add_text " "
92-
template.add_text_in_color(expected_color, expected)
107+
def add_expected_value_to_failure_message(template)
108+
if respond_to?(:add_expected_value_to, true)
109+
add_expected_value_to(template, expected_for_failure_message)
110+
else
111+
template.add_text " "
112+
template.add_text_in_color(
113+
expected_color,
114+
expected_for_failure_message,
115+
)
116+
end
117+
end
118+
119+
def add_expected_value_to_description(template)
120+
if respond_to?(:add_expected_value_to, true)
121+
add_expected_value_to(template, expected_for_description)
122+
else
123+
template.add_text " "
124+
template.add_text_in_color(expected_color, expected_for_description)
125+
end
93126
end
94127

95128
def expected_phrase
96-
"#{to_or_not_to} #{expected_action}"
129+
"#{to_or_not_to} #{expected_action_for_failure_message}"
97130
end
98131

99132
def to_or_not_to

lib/super_diff/rspec/matcher_text_builders/be_predicate.rb

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,9 @@ def initialize(
1414
@expected_predicate_method_name = expected_predicate_method_name
1515
end
1616

17-
def matcher_description
18-
template = MatcherTextTemplate.new do |t|
19-
t.add_text "return true for"
20-
add_expected_value_to(t)
21-
end
22-
23-
Csi.decolorize(template.to_s(as_single_line: true))
24-
end
25-
2617
protected
2718

28-
def expected_action
19+
def expected_action_for_failure_message
2920
if predicate_accessible?
3021
"return true for"
3122
elsif private_predicate?
@@ -49,7 +40,7 @@ def expected_color
4940
:magenta
5041
end
5142

52-
def add_expected_value_to(template)
43+
def add_expected_value_to(template, expected)
5344
template.add_text " "
5445
template.add_text_in_color(expected_color, "`#{expected}?`")
5546
template.add_text " or "

lib/super_diff/rspec/matcher_text_builders/contain_exactly.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module MatcherTextBuilders
44
class ContainExactly < Base
55
protected
66

7-
def add_expected_value_to(template)
7+
def add_expected_value_to(template, expected)
88
template.add_text " "
99
template.add_list_in_color(expected_color, expected)
1010
end

lib/super_diff/rspec/matcher_text_builders/respond_to.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,24 @@ def initialize(
1818

1919
protected
2020

21-
def add_expected_value_to(template)
21+
def add_expected_value_to(template, expected)
2222
template.add_text " "
2323
template.add_list_in_color(expected_color, expected)
2424
end
2525

26-
def add_extra_after_expected
26+
def add_extra_after_expected_to(template)
2727
if expected_arity
28-
add_arity_clause
28+
add_arity_clause_to(template)
2929
end
3030

3131
if arbitrary_keywords?
32-
add_arbitrary_keywords_clause
32+
add_arbitrary_keywords_clause_to(template)
3333
elsif has_expected_keywords?
34-
add_keywords_clause
34+
add_keywords_clause_to(template)
3535
end
3636

3737
if unlimited_arguments?
38-
add_unlimited_arguments_clause
38+
add_unlimited_arguments_clause_to(template)
3939
end
4040
end
4141

@@ -55,27 +55,27 @@ def unlimited_arguments?
5555
@unlimited_arguments
5656
end
5757

58-
def add_arity_clause
58+
def add_arity_clause_to(template)
5959
template.add_text " with "
6060
template.add_text_in_color :red, expected_arity
6161
template.add_text " "
6262
template.add_text pluralize("argument", expected_arity)
6363
end
6464

65-
def add_arbitrary_keywords_clause
65+
def add_arbitrary_keywords_clause_to(template)
6666
template.add_text " with "
6767
template.add_text_in_color :red, "any"
6868
template.add_text " keywords"
6969
end
7070

71-
def add_keywords_clause
71+
def add_keywords_clause_to(template)
7272
template.add_text " with "
7373
template.add_text pluralize("keyword", expected_keywords.length)
7474
template.add_text " "
7575
template.add_list_in_color :red, expected_keywords
7676
end
7777

78-
def add_unlimited_arguments_clause
78+
def add_unlimited_arguments_clause_to(template)
7979
if arbitrary_keywords? || has_expected_keywords?
8080
template.add_text " and "
8181
else

0 commit comments

Comments
 (0)