super_diff 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (206) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +117 -89
  3. data/lib/super_diff.rb +33 -47
  4. data/lib/super_diff/active_record.rb +41 -0
  5. data/lib/super_diff/active_record/diff_formatters.rb +10 -0
  6. data/lib/super_diff/active_record/diff_formatters/active_record_relation.rb +23 -0
  7. data/lib/super_diff/active_record/differs.rb +10 -0
  8. data/lib/super_diff/active_record/differs/active_record_relation.rb +30 -0
  9. data/lib/super_diff/active_record/object_inspection.rb +14 -0
  10. data/lib/super_diff/active_record/object_inspection/inspectors.rb +16 -0
  11. data/lib/super_diff/active_record/object_inspection/inspectors/active_record_model.rb +38 -0
  12. data/lib/super_diff/active_record/object_inspection/inspectors/active_record_relation.rb +18 -0
  13. data/lib/super_diff/active_record/object_inspection/map_extension.rb +18 -0
  14. data/lib/super_diff/active_record/operation_sequences.rb +10 -0
  15. data/lib/super_diff/active_record/operation_sequences/active_record_relation.rb +16 -0
  16. data/lib/super_diff/active_record/operational_sequencers.rb +14 -0
  17. data/lib/super_diff/active_record/operational_sequencers/active_record_model.rb +19 -0
  18. data/lib/super_diff/active_record/operational_sequencers/active_record_relation.rb +24 -0
  19. data/lib/super_diff/active_support.rb +33 -0
  20. data/lib/super_diff/active_support/diff_formatters.rb +10 -0
  21. data/lib/super_diff/active_support/diff_formatters/hash_with_indifferent_access.rb +36 -0
  22. data/lib/super_diff/active_support/differs.rb +10 -0
  23. data/lib/super_diff/active_support/differs/hash_with_indifferent_access.rb +36 -0
  24. data/lib/super_diff/active_support/object_inspection.rb +14 -0
  25. data/lib/super_diff/active_support/object_inspection/inspectors.rb +12 -0
  26. data/lib/super_diff/active_support/object_inspection/inspectors/hash_with_indifferent_access.rb +18 -0
  27. data/lib/super_diff/active_support/object_inspection/map_extension.rb +15 -0
  28. data/lib/super_diff/active_support/operation_sequences.rb +10 -0
  29. data/lib/super_diff/active_support/operation_sequences/hash_with_indifferent_access.rb +16 -0
  30. data/lib/super_diff/active_support/operational_sequencers.rb +10 -0
  31. data/lib/super_diff/active_support/operational_sequencers/hash_with_indifferent_access.rb +21 -0
  32. data/lib/super_diff/colorized_document_extensions.rb +17 -0
  33. data/lib/super_diff/csi.rb +45 -15
  34. data/lib/super_diff/csi/bold_sequence.rb +9 -0
  35. data/lib/super_diff/csi/color.rb +62 -0
  36. data/lib/super_diff/csi/color_sequence_block.rb +28 -0
  37. data/lib/super_diff/csi/colorized_document.rb +72 -0
  38. data/lib/super_diff/csi/document.rb +183 -0
  39. data/lib/super_diff/csi/eight_bit_color.rb +72 -26
  40. data/lib/super_diff/csi/four_bit_color.rb +63 -29
  41. data/lib/super_diff/csi/twenty_four_bit_color.rb +79 -18
  42. data/lib/super_diff/csi/uncolorized_document.rb +29 -0
  43. data/lib/super_diff/diff_formatter.rb +10 -15
  44. data/lib/super_diff/diff_formatters.rb +10 -1
  45. data/lib/super_diff/diff_formatters/base.rb +12 -17
  46. data/lib/super_diff/diff_formatters/collection.rb +81 -50
  47. data/lib/super_diff/diff_formatters/{object.rb → custom_object.rb} +12 -9
  48. data/lib/super_diff/diff_formatters/default_object.rb +48 -0
  49. data/lib/super_diff/diff_formatters/multiline_string.rb +31 -0
  50. data/lib/super_diff/differ.rb +35 -32
  51. data/lib/super_diff/differs.rb +16 -1
  52. data/lib/super_diff/differs/array.rb +2 -2
  53. data/lib/super_diff/differs/base.rb +11 -21
  54. data/lib/super_diff/differs/custom_object.rb +26 -0
  55. data/lib/super_diff/differs/default_object.rb +25 -0
  56. data/lib/super_diff/differs/empty.rb +1 -1
  57. data/lib/super_diff/differs/hash.rb +2 -2
  58. data/lib/super_diff/differs/{multi_line_string.rb → multiline_string.rb} +6 -5
  59. data/lib/super_diff/equality_matcher.rb +9 -22
  60. data/lib/super_diff/equality_matchers.rb +19 -1
  61. data/lib/super_diff/equality_matchers/array.rb +6 -4
  62. data/lib/super_diff/equality_matchers/base.rb +8 -16
  63. data/lib/super_diff/equality_matchers/default.rb +60 -0
  64. data/lib/super_diff/equality_matchers/hash.rb +6 -4
  65. data/lib/super_diff/equality_matchers/{multi_line_string.rb → multiline_string.rb} +9 -6
  66. data/lib/super_diff/equality_matchers/primitive.rb +34 -0
  67. data/lib/super_diff/equality_matchers/{single_line_string.rb → singleline_string.rb} +7 -5
  68. data/lib/super_diff/helpers.rb +17 -81
  69. data/lib/super_diff/no_differ_available_error.rb +22 -0
  70. data/lib/super_diff/{errors.rb → no_operational_sequencer_available_error.rb} +0 -0
  71. data/lib/super_diff/object_inspection.rb +24 -0
  72. data/lib/super_diff/object_inspection/inspection_tree.rb +144 -0
  73. data/lib/super_diff/object_inspection/inspector.rb +27 -0
  74. data/lib/super_diff/object_inspection/inspectors.rb +18 -0
  75. data/lib/super_diff/object_inspection/inspectors/array.rb +22 -0
  76. data/lib/super_diff/object_inspection/inspectors/custom_object.rb +27 -0
  77. data/lib/super_diff/object_inspection/inspectors/default_object.rb +47 -0
  78. data/lib/super_diff/object_inspection/inspectors/hash.rb +22 -0
  79. data/lib/super_diff/object_inspection/inspectors/primitive.rb +13 -0
  80. data/lib/super_diff/object_inspection/inspectors/string.rb +13 -0
  81. data/lib/super_diff/object_inspection/map.rb +28 -0
  82. data/lib/super_diff/object_inspection/nodes.rb +49 -0
  83. data/lib/super_diff/object_inspection/nodes/base.rb +86 -0
  84. data/lib/super_diff/object_inspection/nodes/break.rb +15 -0
  85. data/lib/super_diff/object_inspection/nodes/inspection.rb +15 -0
  86. data/lib/super_diff/object_inspection/nodes/nesting.rb +16 -0
  87. data/lib/super_diff/object_inspection/nodes/text.rb +15 -0
  88. data/lib/super_diff/object_inspection/nodes/when_empty.rb +30 -0
  89. data/lib/super_diff/object_inspection/nodes/when_multiline.rb +22 -0
  90. data/lib/super_diff/object_inspection/nodes/when_non_empty.rb +30 -0
  91. data/lib/super_diff/object_inspection/nodes/when_singleline.rb +24 -0
  92. data/lib/super_diff/operation_sequences.rb +9 -0
  93. data/lib/super_diff/operation_sequences/base.rb +1 -1
  94. data/lib/super_diff/operation_sequences/{object.rb → custom_object.rb} +4 -3
  95. data/lib/super_diff/operation_sequences/default_object.rb +25 -0
  96. data/lib/super_diff/operational_sequencer.rb +23 -18
  97. data/lib/super_diff/operational_sequencers.rb +12 -1
  98. data/lib/super_diff/operational_sequencers/array.rb +65 -62
  99. data/lib/super_diff/operational_sequencers/base.rb +18 -26
  100. data/lib/super_diff/operational_sequencers/custom_object.rb +35 -0
  101. data/lib/super_diff/operational_sequencers/{object.rb → default_object.rb} +21 -11
  102. data/lib/super_diff/operational_sequencers/hash.rb +8 -5
  103. data/lib/super_diff/operational_sequencers/{multi_line_string.rb → multiline_string.rb} +11 -6
  104. data/lib/super_diff/operations.rb +6 -0
  105. data/lib/super_diff/operations/binary_operation.rb +14 -34
  106. data/lib/super_diff/operations/unary_operation.rb +11 -2
  107. data/lib/super_diff/rails.rb +1 -0
  108. data/lib/super_diff/recursion_guard.rb +47 -0
  109. data/lib/super_diff/rspec-rails.rb +2 -0
  110. data/lib/super_diff/rspec.rb +52 -8
  111. data/lib/super_diff/rspec/augmented_matcher.rb +98 -0
  112. data/lib/super_diff/rspec/configuration.rb +31 -0
  113. data/lib/super_diff/rspec/differ.rb +60 -16
  114. data/lib/super_diff/rspec/differs.rb +13 -0
  115. data/lib/super_diff/rspec/differs/collection_containing_exactly.rb +23 -0
  116. data/lib/super_diff/rspec/differs/partial_array.rb +22 -0
  117. data/lib/super_diff/rspec/differs/partial_hash.rb +22 -0
  118. data/lib/super_diff/rspec/differs/partial_object.rb +22 -0
  119. data/lib/super_diff/rspec/matcher_text_builders.rb +24 -0
  120. data/lib/super_diff/rspec/matcher_text_builders/base.rb +155 -0
  121. data/lib/super_diff/rspec/matcher_text_builders/be_predicate.rb +78 -0
  122. data/lib/super_diff/rspec/matcher_text_builders/contain_exactly.rb +14 -0
  123. data/lib/super_diff/rspec/matcher_text_builders/match.rb +23 -0
  124. data/lib/super_diff/rspec/matcher_text_builders/raise_error.rb +13 -0
  125. data/lib/super_diff/rspec/matcher_text_builders/respond_to.rb +99 -0
  126. data/lib/super_diff/rspec/matcher_text_template.rb +240 -0
  127. data/lib/super_diff/rspec/monkey_patches.rb +601 -98
  128. data/lib/super_diff/rspec/object_inspection.rb +8 -0
  129. data/lib/super_diff/rspec/object_inspection/inspectors.rb +24 -0
  130. data/lib/super_diff/rspec/object_inspection/inspectors/collection_containing_exactly.rb +19 -0
  131. data/lib/super_diff/rspec/object_inspection/inspectors/partial_array.rb +22 -0
  132. data/lib/super_diff/rspec/object_inspection/inspectors/partial_hash.rb +21 -0
  133. data/lib/super_diff/rspec/object_inspection/inspectors/partial_object.rb +21 -0
  134. data/lib/super_diff/rspec/object_inspection/map_extension.rb +23 -0
  135. data/lib/super_diff/rspec/operational_sequencers.rb +22 -0
  136. data/lib/super_diff/rspec/operational_sequencers/collection_containing_exactly.rb +97 -0
  137. data/lib/super_diff/rspec/operational_sequencers/partial_array.rb +23 -0
  138. data/lib/super_diff/rspec/operational_sequencers/partial_hash.rb +32 -0
  139. data/lib/super_diff/rspec/operational_sequencers/partial_object.rb +64 -0
  140. data/lib/super_diff/version.rb +1 -1
  141. data/spec/examples.txt +328 -46
  142. data/spec/integration/rails/active_record_spec.rb +19 -0
  143. data/spec/integration/rails/hash_with_indifferent_access_spec.rb +19 -0
  144. data/spec/integration/rspec/be_falsey_matcher_spec.rb +53 -0
  145. data/spec/integration/rspec/be_matcher_spec.rb +565 -0
  146. data/spec/integration/rspec/be_nil_matcher_spec.rb +53 -0
  147. data/spec/integration/rspec/be_predicate_matcher_spec.rb +546 -0
  148. data/spec/integration/rspec/be_truthy_matcher_spec.rb +57 -0
  149. data/spec/integration/rspec/contain_exactly_matcher_spec.rb +368 -0
  150. data/spec/integration/rspec/eq_matcher_spec.rb +874 -0
  151. data/spec/integration/rspec/have_attributes_matcher_spec.rb +299 -0
  152. data/spec/integration/rspec/include_matcher_spec.rb +350 -0
  153. data/spec/integration/rspec/match_matcher_spec.rb +1258 -0
  154. data/spec/integration/rspec/raise_error_matcher_spec.rb +350 -0
  155. data/spec/integration/rspec/respond_to_matcher_spec.rb +994 -0
  156. data/spec/integration/rspec/unhandled_errors_spec.rb +94 -0
  157. data/spec/spec_helper.rb +19 -4
  158. data/spec/support/colorizer.rb +9 -0
  159. data/spec/support/command_runner.rb +4 -0
  160. data/spec/support/integration/helpers.rb +179 -0
  161. data/spec/support/integration/matchers/produce_output_when_run_matcher.rb +79 -41
  162. data/spec/support/models/a.rb +11 -0
  163. data/spec/support/models/active_record/person.rb +26 -0
  164. data/spec/support/models/active_record/shipping_address.rb +29 -0
  165. data/spec/support/models/customer.rb +24 -0
  166. data/spec/support/models/empty_class.rb +6 -0
  167. data/spec/support/models/item.rb +10 -0
  168. data/spec/support/models/order.rb +9 -0
  169. data/spec/support/models/person.rb +20 -0
  170. data/spec/support/models/player.rb +33 -0
  171. data/spec/support/models/shipping_address.rb +34 -0
  172. data/spec/support/ruby_versions.rb +7 -0
  173. data/spec/support/shared_examples/active_record.rb +338 -0
  174. data/spec/support/shared_examples/hash_with_indifferent_access.rb +233 -0
  175. data/spec/unit/equality_matcher_spec.rb +579 -171
  176. data/spec/unit/object_inspection_spec.rb +1092 -0
  177. data/spec/unit/rspec/matchers/be_compared_to_spec.rb +23 -0
  178. data/spec/unit/rspec/matchers/be_falsey_spec.rb +9 -0
  179. data/spec/unit/rspec/matchers/be_nil_spec.rb +9 -0
  180. data/spec/unit/rspec/matchers/be_predicate_spec.rb +31 -0
  181. data/spec/unit/rspec/matchers/be_spec.rb +17 -0
  182. data/spec/unit/rspec/matchers/be_truthy_spec.rb +9 -0
  183. data/spec/unit/rspec/matchers/contain_exactly_spec.rb +11 -0
  184. data/spec/unit/rspec/matchers/eq_spec.rb +9 -0
  185. data/spec/unit/rspec/matchers/have_attributes_spec.rb +11 -0
  186. data/spec/unit/rspec/matchers/include_spec.rb +21 -0
  187. data/spec/unit/rspec/matchers/match_spec.rb +9 -0
  188. data/spec/unit/rspec/matchers/raise_error_spec.rb +29 -0
  189. data/spec/unit/rspec/matchers/respond_to_spec.rb +78 -0
  190. data/super_diff.gemspec +4 -2
  191. metadata +231 -34
  192. data/lib/super_diff/csi/color_helper.rb +0 -52
  193. data/lib/super_diff/csi/eight_bit_sequence.rb +0 -27
  194. data/lib/super_diff/csi/four_bit_sequence.rb +0 -24
  195. data/lib/super_diff/csi/sequence.rb +0 -22
  196. data/lib/super_diff/csi/twenty_four_bit_sequence.rb +0 -27
  197. data/lib/super_diff/diff_formatters/multi_line_string.rb +0 -31
  198. data/lib/super_diff/differs/object.rb +0 -68
  199. data/lib/super_diff/equality_matchers/object.rb +0 -18
  200. data/lib/super_diff/value_inspection.rb +0 -11
  201. data/spec/integration/rspec_spec.rb +0 -261
  202. data/spec/support/color_helper.rb +0 -49
  203. data/spec/support/person.rb +0 -23
  204. data/spec/support/person_diff_formatter.rb +0 -15
  205. data/spec/support/person_operation_sequence.rb +0 -14
  206. data/spec/support/person_operational_sequencer.rb +0 -19
@@ -0,0 +1,53 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe "Integration with RSpec's #be_nil matcher", type: :integration do
4
+ it "produces the correct failure message when used the positive" do
5
+ as_both_colored_and_uncolored do |color_enabled|
6
+ snippet = %|expect(:foo).to be_nil|
7
+ program = make_plain_test_program(snippet, color_enabled: color_enabled)
8
+
9
+ expected_output = build_expected_output(
10
+ color_enabled: color_enabled,
11
+ snippet: %|expect(:foo).to be_nil|,
12
+ expectation: proc {
13
+ line do
14
+ plain "Expected "
15
+ beta %|:foo|
16
+ plain " to be "
17
+ alpha %|nil|
18
+ plain "."
19
+ end
20
+ },
21
+ )
22
+
23
+ expect(program).
24
+ to produce_output_when_run(expected_output).
25
+ in_color(color_enabled)
26
+ end
27
+ end
28
+
29
+ it "produces the correct failure message when used in the negative" do
30
+ as_both_colored_and_uncolored do |color_enabled|
31
+ snippet = %|expect(nil).not_to be_nil|
32
+ program = make_plain_test_program(snippet, color_enabled: color_enabled)
33
+
34
+ expected_output = build_expected_output(
35
+ color_enabled: color_enabled,
36
+ snippet: %|expect(nil).not_to be_nil|,
37
+ expectation: proc {
38
+ line do
39
+ plain "Expected "
40
+ beta %|nil|
41
+ plain " not to be "
42
+ alpha %|nil|
43
+ plain "."
44
+ end
45
+ },
46
+ )
47
+
48
+ expect(program).
49
+ to produce_output_when_run(expected_output).
50
+ in_color(color_enabled)
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,546 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe "Integration with RSpec's #be_* matcher", type: :integration do
4
+ # rubocop:disable Metrics/BlockLength
5
+ ["be", "be_a", "be_an"].each do |prefix|
6
+ # rubocop:enable Metrics/BlockLength
7
+ context "using #{prefix}_<predicate>" do
8
+ context "when the predicate method doesn't exist on the object" do
9
+ context "when the inspected version of the actual value is short" do
10
+ it "produces the correct failure message" do
11
+ as_both_colored_and_uncolored do |color_enabled|
12
+ snippet = %|expect(:foo).to #{prefix}_strong|
13
+ program = make_plain_test_program(
14
+ snippet,
15
+ color_enabled: color_enabled,
16
+ )
17
+
18
+ expected_output = build_expected_output(
19
+ color_enabled: color_enabled,
20
+ snippet: snippet,
21
+ expectation: proc {
22
+ line do
23
+ plain "Expected "
24
+ beta %|:foo|
25
+ plain " to respond to "
26
+ alpha %|`strong?`|
27
+ plain " or "
28
+ alpha %|`strongs?`|
29
+ plain "."
30
+ end
31
+ },
32
+ )
33
+
34
+ expect(program).
35
+ to produce_output_when_run(expected_output).
36
+ in_color(color_enabled)
37
+ end
38
+ end
39
+ end
40
+
41
+ context "when the inspected version of the actual value is long" do
42
+ it "produces the correct failure message" do
43
+ as_both_colored_and_uncolored do |color_enabled|
44
+ snippet = <<~TEST.strip
45
+ hash = { foo: "bar", baz: "qux", blargh: "foz", fizz: "buzz" }
46
+ expect(hash).to #{prefix}_strong
47
+ TEST
48
+ program = make_plain_test_program(
49
+ snippet,
50
+ color_enabled: color_enabled,
51
+ )
52
+
53
+ expected_output = build_expected_output(
54
+ color_enabled: color_enabled,
55
+ snippet: %|expect(hash).to #{prefix}_strong|,
56
+ newline_before_expectation: true,
57
+ expectation: proc {
58
+ line do
59
+ plain " Expected "
60
+ beta %|{ foo: "bar", baz: "qux", blargh: "foz", fizz: "buzz" }|
61
+ end
62
+
63
+ line do
64
+ plain "to respond to "
65
+ alpha %|`strong?`|
66
+ plain " or "
67
+ alpha %|`strongs?`|
68
+ end
69
+ },
70
+ )
71
+
72
+ expect(program).
73
+ to produce_output_when_run(expected_output).
74
+ in_color(color_enabled)
75
+ end
76
+ end
77
+ end
78
+ end
79
+
80
+ context "when the predicate method exists on the object" do
81
+ context "but is private" do
82
+ context "when the inspected version of the actual value is short" do
83
+ it "produces the correct failure message" do
84
+ as_both_colored_and_uncolored do |color_enabled|
85
+ snippet = <<~TEST.strip
86
+ class Foo
87
+ private def strong?; end
88
+ end
89
+
90
+ expect(Foo.new).to #{prefix}_strong
91
+ TEST
92
+ program = make_plain_test_program(
93
+ snippet,
94
+ color_enabled: color_enabled,
95
+ )
96
+
97
+ expected_output = build_expected_output(
98
+ color_enabled: color_enabled,
99
+ snippet: %|expect(Foo.new).to #{prefix}_strong|,
100
+ expectation: proc {
101
+ line do
102
+ plain "Expected "
103
+ beta %|#<Foo>|
104
+ plain " to have a public method "
105
+ alpha %|`strong?`|
106
+ plain " or "
107
+ alpha %|`strongs?`|
108
+ plain "."
109
+ end
110
+ },
111
+ )
112
+
113
+ expect(program).
114
+ to produce_output_when_run(expected_output).
115
+ in_color(color_enabled).
116
+ removing_object_ids
117
+ end
118
+ end
119
+ end
120
+
121
+ context "when the inspected version of the actual value is long" do
122
+ it "produces the correct failure message" do
123
+ as_both_colored_and_uncolored do |color_enabled|
124
+ snippet = <<~TEST.strip
125
+ hash = { foo: "bar", baz: "qux", blargh: "foz", fizz: "buzz" }
126
+
127
+ class << hash
128
+ private def strong?; end
129
+ end
130
+
131
+ expect(hash).to #{prefix}_strong
132
+ TEST
133
+ program = make_plain_test_program(
134
+ snippet,
135
+ color_enabled: color_enabled,
136
+ )
137
+
138
+ expected_output = build_expected_output(
139
+ color_enabled: color_enabled,
140
+ snippet: %|expect(hash).to #{prefix}_strong|,
141
+ newline_before_expectation: true,
142
+ expectation: proc {
143
+ line do
144
+ plain " Expected "
145
+ beta %|{ foo: "bar", baz: "qux", blargh: "foz", fizz: "buzz" }|
146
+ end
147
+
148
+ line do
149
+ plain "to have a public method "
150
+ alpha %|`strong?`|
151
+ plain " or "
152
+ alpha %|`strongs?`|
153
+ end
154
+ },
155
+ )
156
+
157
+ expect(program).
158
+ to produce_output_when_run(expected_output).
159
+ in_color(color_enabled).
160
+ removing_object_ids
161
+ end
162
+ end
163
+ end
164
+ end
165
+
166
+ context "and is public" do
167
+ context "and returns false" do
168
+ context "but is called #true?" do
169
+ context "when the inspected version of the actual value is short" do
170
+ it "produces the correct failure message" do
171
+ as_both_colored_and_uncolored do |color_enabled|
172
+ snippet = <<~TEST.strip
173
+ class Foo
174
+ def true?; false; end
175
+ end
176
+
177
+ expect(Foo.new).to #{prefix}_true
178
+ TEST
179
+ program = make_plain_test_program(
180
+ snippet,
181
+ color_enabled: color_enabled,
182
+ )
183
+
184
+ expected_output = build_expected_output(
185
+ color_enabled: color_enabled,
186
+ snippet: %|expect(Foo.new).to #{prefix}_true|,
187
+ newline_before_expectation: true,
188
+ expectation: proc {
189
+ line do
190
+ plain "Expected "
191
+ beta %|#<Foo>|
192
+ plain " to return a truthy result for "
193
+ alpha %|`true?`|
194
+ plain " or "
195
+ alpha %|`trues?`|
196
+ plain "."
197
+ end
198
+
199
+ newline
200
+
201
+ line do
202
+ plain "(Perhaps you want to use "
203
+ blue "`be(true)`"
204
+ plain " or "
205
+ blue "`be_truthy`"
206
+ plain " instead?)"
207
+ end
208
+ },
209
+ )
210
+
211
+ expect(program).
212
+ to produce_output_when_run(expected_output).
213
+ in_color(color_enabled).
214
+ removing_object_ids
215
+ end
216
+ end
217
+ end
218
+
219
+ context "when the inspected version of the actual value is long" do
220
+ it "produces the correct failure message" do
221
+ as_both_colored_and_uncolored do |color_enabled|
222
+ snippet = <<~TEST.strip
223
+ hash = {
224
+ foo: "bar",
225
+ baz: "qux",
226
+ blargh: "foz",
227
+ fizz: "buzz"
228
+ }
229
+
230
+ class << hash
231
+ def true?; false; end
232
+ end
233
+
234
+ expect(hash).to #{prefix}_true
235
+ TEST
236
+ program = make_plain_test_program(
237
+ snippet,
238
+ color_enabled: color_enabled,
239
+ )
240
+
241
+ expected_output = build_expected_output(
242
+ color_enabled: color_enabled,
243
+ snippet: %|expect(hash).to #{prefix}_true|,
244
+ newline_before_expectation: true,
245
+ expectation: proc {
246
+ line do
247
+ plain " Expected "
248
+ beta %|{ foo: "bar", baz: "qux", blargh: "foz", fizz: "buzz" }|
249
+ end
250
+
251
+ line do
252
+ plain "to return a truthy result for "
253
+ alpha %|`true?`|
254
+ plain " or "
255
+ alpha %|`trues?`|
256
+ end
257
+
258
+ newline
259
+
260
+ line do
261
+ plain "(Perhaps you want to use "
262
+ blue "`be(true)`"
263
+ plain " or "
264
+ blue "`be_truthy`"
265
+ plain " instead?)"
266
+ end
267
+ },
268
+ )
269
+
270
+ expect(program).
271
+ to produce_output_when_run(expected_output).
272
+ in_color(color_enabled).
273
+ removing_object_ids
274
+ end
275
+ end
276
+ end
277
+ end
278
+
279
+ context "but is called #false?" do
280
+ it "produces the correct failure message" do
281
+ as_both_colored_and_uncolored do |color_enabled|
282
+ snippet = <<~TEST.strip
283
+ class X
284
+ def false?; false; end
285
+ end
286
+
287
+ expect(X.new).to #{prefix}_false
288
+ TEST
289
+ program = make_plain_test_program(
290
+ snippet,
291
+ color_enabled: color_enabled,
292
+ )
293
+
294
+ expected_output = build_expected_output(
295
+ color_enabled: color_enabled,
296
+ snippet: %|expect(X.new).to #{prefix}_false|,
297
+ newline_before_expectation: true,
298
+ expectation: proc {
299
+ line do
300
+ plain "Expected "
301
+ beta %|#<X>|
302
+ plain " to return a truthy result for "
303
+ alpha %|`false?`|
304
+ plain " or "
305
+ alpha %|`falses?`|
306
+ plain "."
307
+ end
308
+ },
309
+ )
310
+
311
+ expect(program).
312
+ to produce_output_when_run(expected_output).
313
+ in_color(color_enabled).
314
+ removing_object_ids
315
+ end
316
+ end
317
+ end
318
+
319
+ context "and is called neither #true? nor #false?" do
320
+ context "and is singular" do
321
+ context "when the inspected version of the actual value is short" do
322
+ it "produces the correct failure message" do
323
+ as_both_colored_and_uncolored do |color_enabled|
324
+ snippet = <<~TEST.strip
325
+ class X
326
+ def y?; false; end
327
+ end
328
+
329
+ expect(X.new).to #{prefix}_y
330
+ TEST
331
+ program = make_plain_test_program(
332
+ snippet,
333
+ color_enabled: color_enabled,
334
+ )
335
+
336
+ expected_output = build_expected_output(
337
+ color_enabled: color_enabled,
338
+ snippet: %|expect(X.new).to #{prefix}_y|,
339
+ expectation: proc {
340
+ line do
341
+ plain "Expected "
342
+ beta %|#<X>|
343
+ plain " to return a truthy result for "
344
+ alpha %|`y?`|
345
+ plain " or "
346
+ alpha %|`ys?`|
347
+ plain "."
348
+ end
349
+ },
350
+ )
351
+
352
+ expect(program).
353
+ to produce_output_when_run(expected_output).
354
+ in_color(color_enabled).
355
+ removing_object_ids
356
+ end
357
+ end
358
+ end
359
+
360
+ context "when the inspected version of the actual value is long" do
361
+ it "produces the correct failure message" do
362
+ as_both_colored_and_uncolored do |color_enabled|
363
+ snippet = <<~TEST.strip
364
+ hash = {
365
+ foo: "bar",
366
+ baz: "qux",
367
+ blargh: "foz",
368
+ fizz: "buzz",
369
+ aaaaaa: "bbbbbb"
370
+ }
371
+
372
+ class << hash
373
+ def y?; false; end
374
+ end
375
+
376
+ expect(hash).to #{prefix}_y
377
+ TEST
378
+ program = make_plain_test_program(
379
+ snippet,
380
+ color_enabled: color_enabled,
381
+ )
382
+
383
+ expected_output = build_expected_output(
384
+ color_enabled: color_enabled,
385
+ snippet: %|expect(hash).to #{prefix}_y|,
386
+ newline_before_expectation: true,
387
+ expectation: proc {
388
+ line do
389
+ plain " Expected "
390
+ beta %|{ foo: "bar", baz: "qux", blargh: "foz", fizz: "buzz", aaaaaa: "bbbbbb" }|
391
+ end
392
+
393
+ line do
394
+ plain "to return a truthy result for "
395
+ alpha %|`y?`|
396
+ plain " or "
397
+ alpha %|`ys?`|
398
+ end
399
+ },
400
+ )
401
+
402
+ expect(program).
403
+ to produce_output_when_run(expected_output).
404
+ in_color(color_enabled).
405
+ removing_object_ids
406
+ end
407
+ end
408
+ end
409
+ end
410
+
411
+ context "and is plural" do
412
+ context "when the inspected version of the actual value is short" do
413
+ it "produces the correct failure message" do
414
+ as_both_colored_and_uncolored do |color_enabled|
415
+ snippet = <<~TEST.strip
416
+ class X
417
+ def ys?; false; end
418
+ end
419
+
420
+ expect(X.new).to #{prefix}_y
421
+ TEST
422
+ program = make_plain_test_program(
423
+ snippet,
424
+ color_enabled: color_enabled,
425
+ )
426
+
427
+ expected_output = build_expected_output(
428
+ color_enabled: color_enabled,
429
+ snippet: %|expect(X.new).to #{prefix}_y|,
430
+ expectation: proc {
431
+ line do
432
+ plain "Expected "
433
+ beta %|#<X>|
434
+ plain " to return a truthy result for "
435
+ alpha %|`y?`|
436
+ plain " or "
437
+ alpha %|`ys?`|
438
+ plain "."
439
+ end
440
+ },
441
+ )
442
+
443
+ expect(program).
444
+ to produce_output_when_run(expected_output).
445
+ in_color(color_enabled).
446
+ removing_object_ids
447
+ end
448
+ end
449
+ end
450
+
451
+ context "when the inspected version of the actual value is long" do
452
+ it "produces the correct failure message" do
453
+ as_both_colored_and_uncolored do |color_enabled|
454
+ snippet = <<~TEST.strip
455
+ hash = {
456
+ foo: "bar",
457
+ baz: "qux",
458
+ blargh: "foz",
459
+ fizz: "buzz",
460
+ aaaaaa: "bbbbbb"
461
+ }
462
+
463
+ class << hash
464
+ def ys?; false; end
465
+ end
466
+
467
+ expect(hash).to #{prefix}_y
468
+ TEST
469
+ program = make_plain_test_program(
470
+ snippet,
471
+ color_enabled: color_enabled,
472
+ )
473
+
474
+ expected_output = build_expected_output(
475
+ color_enabled: color_enabled,
476
+ snippet: %|expect(hash).to #{prefix}_y|,
477
+ newline_before_expectation: true,
478
+ expectation: proc {
479
+ line do
480
+ plain " Expected "
481
+ beta %|{ foo: "bar", baz: "qux", blargh: "foz", fizz: "buzz", aaaaaa: "bbbbbb" }|
482
+ end
483
+
484
+ line do
485
+ plain "to return a truthy result for "
486
+ alpha %|`y?`|
487
+ plain " or "
488
+ alpha %|`ys?`|
489
+ end
490
+ },
491
+ )
492
+
493
+ expect(program).
494
+ to produce_output_when_run(expected_output).
495
+ in_color(color_enabled).
496
+ removing_object_ids
497
+ end
498
+ end
499
+ end
500
+ end
501
+ end
502
+ end
503
+
504
+ context "and returns true" do
505
+ it "produces the correct failure message when used in the negative" do
506
+ as_both_colored_and_uncolored do |color_enabled|
507
+ snippet = <<~TEST.strip
508
+ class Foo
509
+ def strong?; true; end
510
+ end
511
+
512
+ expect(Foo.new).not_to #{prefix}_strong
513
+ TEST
514
+ program = make_plain_test_program(
515
+ snippet,
516
+ color_enabled: color_enabled,
517
+ )
518
+
519
+ expected_output = build_expected_output(
520
+ color_enabled: color_enabled,
521
+ snippet: %|expect(Foo.new).not_to #{prefix}_strong|,
522
+ expectation: proc {
523
+ line do
524
+ plain "Expected "
525
+ beta %|#<Foo>|
526
+ plain " not to return a truthy result for "
527
+ alpha %|`strong?`|
528
+ plain " or "
529
+ alpha %|`strongs?`|
530
+ plain "."
531
+ end
532
+ },
533
+ )
534
+
535
+ expect(program).
536
+ to produce_output_when_run(expected_output).
537
+ in_color(color_enabled).
538
+ removing_object_ids
539
+ end
540
+ end
541
+ end
542
+ end
543
+ end
544
+ end
545
+ end
546
+ end