File tree Expand file tree Collapse file tree 5 files changed +70
-8
lines changed
object_inspection/inspectors Expand file tree Collapse file tree 5 files changed +70
-8
lines changed Original file line number Diff line number Diff line change @@ -26,6 +26,12 @@ def self.a_hash_including_something?(value)
26
26
value . expecteds . first . is_a? ( ::Hash )
27
27
end
28
28
29
+ # HINT: `a_hash_including` is an alias of `include` in the rspec-expectations gem.
30
+ # `hash_including` is an argument matcher in the rspec-mocks gem.
31
+ def self . hash_including_something? ( value )
32
+ value . is_a? ( ::RSpec ::Mocks ::ArgumentMatchers ::HashIncludingMatcher )
33
+ end
34
+
29
35
def self . a_collection_including_something? ( value )
30
36
fuzzy_object? ( value ) &&
31
37
value . respond_to? ( :expecteds ) &&
Original file line number Diff line number Diff line change @@ -3,8 +3,10 @@ module RSpec
3
3
module Differs
4
4
class HashIncluding < SuperDiff ::Differs ::Hash
5
5
def self . applies_to? ( expected , actual )
6
- SuperDiff ::RSpec . a_hash_including_something? ( expected ) &&
7
- actual . is_a? ( ::Hash )
6
+ (
7
+ SuperDiff ::RSpec . a_hash_including_something? ( expected ) ||
8
+ SuperDiff ::RSpec . hash_including_something? ( expected )
9
+ ) && actual . is_a? ( ::Hash )
8
10
end
9
11
10
12
private
Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ module ObjectInspection
4
4
module Inspectors
5
5
class HashIncluding < SuperDiff ::ObjectInspection ::Inspectors ::Base
6
6
def self . applies_to? ( value )
7
- SuperDiff ::RSpec . a_hash_including_something? ( value )
7
+ SuperDiff ::RSpec . a_hash_including_something? ( value ) || SuperDiff :: RSpec . hash_including_something? ( value )
8
8
end
9
9
10
10
protected
@@ -13,9 +13,14 @@ def inspection_tree
13
13
SuperDiff ::ObjectInspection ::InspectionTree . new do
14
14
add_text "#<a hash including ("
15
15
16
- nested do |aliased_matcher |
16
+ nested do |value |
17
+ hash = if SuperDiff ::RSpec . a_hash_including_something? ( value )
18
+ value . expecteds . first
19
+ else
20
+ value . instance_variable_get ( :@expected )
21
+ end
17
22
insert_hash_inspection_of (
18
- aliased_matcher . expecteds . first ,
23
+ hash ,
19
24
initial_break : nil ,
20
25
)
21
26
end
Original file line number Diff line number Diff line change @@ -3,12 +3,19 @@ module RSpec
3
3
module OperationTreeBuilders
4
4
class HashIncluding < SuperDiff ::OperationTreeBuilders ::Hash
5
5
def self . applies_to? ( expected , actual )
6
- SuperDiff ::RSpec . a_hash_including_something? ( expected ) &&
7
- actual . is_a? ( ::Hash )
6
+ (
7
+ SuperDiff ::RSpec . a_hash_including_something? ( expected ) ||
8
+ SuperDiff ::RSpec . hash_including_something? ( expected )
9
+ ) && actual . is_a? ( ::Hash )
8
10
end
9
11
10
12
def initialize ( expected :, **rest )
11
- super ( expected : expected . expecteds . first , **rest )
13
+ hash = if SuperDiff ::RSpec . a_hash_including_something? ( expected )
14
+ expected . expecteds . first
15
+ else
16
+ expected . instance_variable_get ( :@expected )
17
+ end
18
+ super ( expected : hash , **rest )
12
19
end
13
20
14
21
private
Original file line number Diff line number Diff line change 341
341
end
342
342
end
343
343
344
+ # HINT: `a_hash_including` is an alias of `include` in the rspec-expectations gem.
345
+ # `hash_including` is an argument matcher in the rspec-mocks gem.
346
+ context "when the expected value is `hash-including-<something>`, not `a-hash-including-<something>`" do
347
+ it "produces the correct failure message" do
348
+ as_both_colored_and_uncolored do |color_enabled |
349
+ snippet = <<~TEST . strip
350
+ expected = hash_including(city: "Hill Valley")
351
+ actual = { city: "Burbank" }
352
+ expect(actual).to match(expected)
353
+ TEST
354
+ program = make_plain_test_program (
355
+ snippet ,
356
+ color_enabled : color_enabled ,
357
+ )
358
+
359
+ expected_output = build_expected_output (
360
+ color_enabled : color_enabled ,
361
+ snippet : %|expect(actual).to match(expected)| ,
362
+ expectation : proc {
363
+ line do
364
+ plain %|Expected |
365
+ actual %|{ city: "Burbank" }|
366
+ plain %| to match |
367
+ expected %|#<a hash including (city: "Hill Valley")>|
368
+ plain %|.|
369
+ end
370
+ } ,
371
+ diff : proc {
372
+ plain_line %| {|
373
+ expected_line %|- city: "Hill Valley"|
374
+ actual_line %|+ city: "Burbank"|
375
+ plain_line %| }|
376
+ } ,
377
+ )
378
+
379
+ expect ( program ) .
380
+ to produce_output_when_run ( expected_output ) .
381
+ in_color ( color_enabled )
382
+ end
383
+ end
384
+ end
385
+
344
386
context "when the expected value is a collection-including-<something>" do
345
387
context "that is small" do
346
388
it "produces the correct failure message when used in the positive" do
You can’t perform that action at this time.
0 commit comments