Debian Codegraph @

rspec-expectations/spec/rspec/matchers/built_in/include_spec.rb

raw symbols SCIP
1 # This class fakes some behavior of
2 # ActiveSupport::HashWithIndifferentAccess.
3 # It doesn't convert recursively.
4 class FakeHashWithIndifferentAccess < Hash
5 class << self
6 def from_hash(hsh)
7 new_hsh = new
8 hsh.each do |key, value|
9 new_hsh[key] = value
10 end
11 new_hsh
12 end
13 end
14
15 def [](key)
16 super(key.to_s)
17 end
18
19 def []=(key, value)
20 super(key.to_s, value)
21 end
22
23 def key?(key)
24 super(key.to_s)
25 end
26
27 def to_hash
28 new_hsh = ::Hash.new
29 each do |key, value|
30 new_hsh[key] = value
31 end
32 new_hsh
33 end
34 end
35
36 # Some custom hashes don't support fuzzy matching
37 class CaseInsensitiveHash < Hash
38 class << self
39 def from_hash(hsh)
40 new_hsh = new
41 hsh.each do |key, value|
42 new_hsh[key] = value
43 end
44 new_hsh
45 end
46 end
47
48 def include?(key)
49 super(key.downcase)
50 end
51 alias :key? :include?
52 end
53
54 RSpec.describe "#include matcher" do
55 include RSpec::Support::Spec::DiffHelpers
56
57 it "is diffable" do
58 expect(include("a")).to be_diffable
59 end
60
61 shared_examples_for "a Hash target" do
62 def build_target(hsh)
63 hsh
64 end
65
66 def use_string_keys_in_failure_message?
67 false
68 end
69
70 def converted_key_hash_inspect(key, value)
71 if use_string_keys_in_failure_message?
72 hash_inspect({ key.to_s => value })
73 else
74 hash_inspect({ key => value })
75 end
76 end
77
78 it 'passes if target has the expected as a key' do
79 expect(build_target(:key => 'value')).to include(:key)
80 end
81
82 it 'passes if target has the expected as a key fuzzily matched' do
83 expect(build_target('KEY' => 'value')).to include(match(/key/i))
84 end
85
86 it "fails if target does not include expected" do
87 failure_string = %(expected #{converted_key_hash_inspect(:key, "value")} to include :other)
88
89 expect {
90 expect(build_target(:key => 'value')).to include(:other)
91 }.to fail_matching(failure_string)
92 end
93
94 it "fails if target doesn't have a key and we expect nil" do
95 expect {
96 expect(build_target({})).to include(:something => nil)
97 }.to fail_matching("expected {} to include #{hash_inspect({ :something => nil })}")
98 end
99
100 it 'works even when an entry in the hash overrides #send' do
101 hash = build_target(:key => 'value')
102 def hash.send; :sent; end
103 expect(hash).to include(hash)
104 end
105
106 it 'provides a valid diff' do
107 allow(RSpec::Matchers.configuration).to receive(:color?).and_return(false)
108
109 failure_string = if use_string_keys_in_failure_message?
110 dedent(<<-END)
111 |Diff:
112 |@@ #{one_line_header(3)} @@
113 |-:bar => 3,
114 |-:foo => 1,
115 |+"bar" => 2,
116 |+"foo" => 1,
117 END
118 else
119 diff = dedent(<<-END)
120 |Diff:
121 |@@ #{one_line_header(3)} @@
122 |-:bar => 3,
123 |+:bar => 2,
124 END
125 diff << "\n :foo => 1,\n" if Diff::LCS::VERSION.to_f < 1.4
126 diff
127 end
128
129 expect {
130 expect(build_target(:foo => 1, :bar => 2)).to include(:foo => 1, :bar => 3)
131 }.to fail_including(failure_string)
132 end
133
134 it 'provides a valid diff for fuzzy matchers' do
135 allow(RSpec::Matchers.configuration).to receive(:color?).and_return(false)
136
137 failure_string = if use_string_keys_in_failure_message?
138 dedent(<<-END)
139 |Diff:
140 |@@ #{one_line_header(3)} @@
141 |-(match /FOO/i) => 1,
142 |-:bar => 3,
143 |+"bar" => 2,
144 |+"foo" => 1,
145 END
146 else
147 dedent(<<-END)
148 |Diff:
149 |@@ #{one_line_header(3)} @@
150 |-(match /FOO/i) => 1,
151 |-:bar => 3,
152 |+:bar => 2,
153 |+:foo => 1,
154 END
155 end
156
157 expect {
158 expect(build_target(:foo => 1, :bar => 2)).to include(match(/FOO/i) => 1, :bar => 3)
159 }.to fail_including(failure_string)
160 end
161
162 it 'does not support count constraint' do
163 expect {
164 expect(build_target(:key => 'value')).to include(:other).once
165 }.to raise_error(NotImplementedError)
166 end
167 end
168
169 describe "expect(...).to include(with_no_args)" do
170 it "fails correctly" do
171 expect { expect([1, 2, 3]).to include }.to raise_error(ArgumentError)
172 end
173 end
174
175 describe "expect(...).to include(with_one_arg)" do
176 it_behaves_like "an RSpec value matcher", :valid_value => [1, 2], :invalid_value => [1] do
177 let(:matcher) { include(2) }
178 end
179
180 context "for an object that does not respond to `include?`" do
181 it 'fails gracefully' do
182 expect {
183 expect(5).to include(1)
184 }.to fail_matching("expected 5 to include 1, but it does not respond to `include?`")
185 expect {
186 expect(5).to include(1).once
187 }.to fail_matching("expected 5 to include 1 once, but it does not respond to `include?`")
188 end
189 end
190
191 context "for an arbitrary object that responds to `include?`" do
192 let(:container) { double("Container") }
193 before { allow(container).to receive(:include?) { |arg| arg == :stuff } }
194
195 it 'delegates to `include?`' do
196 expect(container).to include(:stuff)
197
198 expect {
199 expect(container).to include(:space)
200 }.to fail_matching("to include :space")
201 end
202
203 it 'refuses to count inclusions' do
204 expect {
205 expect(container).to include(:stuff).once
206 }.to raise_error(NotImplementedError, /Count constraints/)
207 end
208 end
209
210 context "for an arbitrary object that responds to `include?` and `to_hash`" do
211 it "delegates to `include?`" do
212 container = double("Container", :include? => true, :to_hash => { "foo" => "bar" })
213 expect(container).to receive(:include?).with("foo").and_return(true)
214 expect(container).to include("foo")
215 end
216 end
217
218 context "for a string target" do
219 it "passes if target includes expected" do
220 expect("abc").to include("a")
221 end
222
223 it "passes if target matches a regexp" do
224 expect("abc").to include(/[cba]{3}/)
225 end
226
227 it "fails if target does not include expected" do
228 expect {
229 expect("abc").to include("d")
230 }.to fail_matching("expected \"abc\" to include \"d\"")
231 end
232
233 it "includes a diff when actual is multiline" do
234 expect {
235 expect("abc\ndef").to include("g")
236 }.to fail_matching("expected \"abc\\ndef\" to include \"g\"\nDiff")
237 end
238
239 it "includes a diff when actual is multiline and there are multiple expecteds" do
240 expect {
241 expect("abc\ndef").to include("g", "h")
242 }.to fail_matching("expected \"abc\\ndef\" to include \"g\" and \"h\"\nDiff")
243 end
244
245 it "does not diff when lines match but are not an exact match" do
246 expect {
247 expect(" foo\nbar\nbazz").to include("foo", "bar", "gaz")
248 }.to fail_with(a_string_not_matching(/Diff/i))
249 end
250
251 context "with exact count" do
252 it 'fails if the string contains the wrong number of occurences' do
253 expect {
254 expect('foo bar foo').to include('foo').once
255 }.to fail_with(/expected "foo bar foo" to include "foo" once but it is included twice/)
256 expect {
257 expect('foo bar foo').to include(/[of]{3}/).once
258 }.to fail_with(/expected "foo bar foo" to include \/\[of\]\{3\}\/ once but it is included twice/)
259 end
260
261 it 'passes if the string contains the correct number of occurences' do
262 expect('fooo bar').to include('oo').once
263 expect('fooo bar').to include('o').thrice
264 expect('fooo ooo oo bar foo').to include('oo').exactly(4).times
265 expect('fooo ooo oo bar foo').to include(/fo{2}/).exactly(2).times
266 end
267 end
268
269 context "with at_least count" do
270 it 'passes if the search term is included at least the number of times' do
271 expect('foo bar foo').to include('foo').at_least(2).times
272 expect('foo bar foo foo').to include('foo').at_least(:twice)
273 end
274
275 it 'fails if the search term is included too few times' do
276 expect {
277 expect('foo bar foo').to include('foo').at_least(:thrice)
278 }.to fail_with(/expected "foo bar foo" to include "foo" at least 3 times but it is included twice/)
279 end
280 end
281
282 context "with at_most count" do
283 it 'passes if the search term is included at most the number of times' do
284 expect('foo bar foo').to include('foo').at_most(2).times
285 expect('foo bar').to include('foo').at_most(:twice)
286 end
287
288 it 'fails if the search term is included too many times' do
289 expect {
290 expect('foo bar foo foo').to include('foo').at_most(:twice)
291 }.to fail_with(/expected "foo bar foo foo" to include "foo" at most twice but it is included 3 times/)
292 end
293 end
294 end
295
296 context "for an array target" do
297 it "passes if target includes expected" do
298 expect([1, 2, 3]).to include(3)
299 end
300
301 it "passes if target includes expected fuzzily matched" do
302 expect(["A", "B", "C"]).to include(match(/a/i))
303 end
304
305 it "fails if target does not include expected" do
306 expect {
307 expect([1, 2, 3]).to include(4)
308 }.to fail_matching("expected [1, 2, 3] to include 4")
309 end
310
311 it 'fails when given differing null doubles' do
312 dbl_1 = double.as_null_object
313 dbl_2 = double.as_null_object
314
315 expect {
316 expect([dbl_1]).to include(dbl_2)
317 }.to fail_matching("expected [#{dbl_1.inspect}] to include")
318 end
319
320 it 'passes when given the same null double' do
321 dbl = double.as_null_object
322 expect([dbl]).to include(dbl)
323 end
324
325 context "with exact count" do
326 it 'fails if the block yields wrong number of times' do
327 expect {
328 expect([1, 2, 1]).to include(1).once
329 }.to fail_with('expected [1, 2, 1] to include 1 once but it is included twice')
330 expect {
331 expect([10, 20, 30]).to include(a_value_within(2).of(17)).twice
332 }.to fail_with('expected [10, 20, 30] to include (a value within 2 of 17) twice but it is included 0 times')
333 end
334
335 it 'passes if the block yields the specified number of times' do
336 expect([1, 2, 1]).to include(1).twice
337 expect([10, 20, 30]).to include(a_value_within(5).of(17)).once
338 expect([{ 'a' => 1 }]).to include('a' => 1).once
339 end
340 end
341
342 context "with at_least count" do
343 it 'passes if the search term is included at least the number of times' do
344 expect([1, 2, 1]).to include(1).at_least(2).times
345 expect([1, 2, 1, 1]).to include(1).at_least(:twice)
346 end
347
348 it 'fails if the search term is included too few times' do
349 expect {
350 expect([1, 2, 1]).to include(1).at_least(:thrice)
351 }.to fail_with('expected [1, 2, 1] to include 1 at least 3 times but it is included twice')
352 end
353 end
354
355 context "with at_most count" do
356 it 'passes if the search term is included at most the number of times' do
357 expect([1, 2, 1]).to include(1).at_most(2).times
358 expect([1, 2]).to include(1).at_most(:twice)
359 end
360
361 it 'fails if the search term is included too many times' do
362 expect {
363 expect([1, 2, 1, 1]).to include(1).at_most(:twice)
364 }.to fail_with('expected [1, 2, 1, 1] to include 1 at most twice but it is included 3 times')
365 end
366 end
367 end
368
369 context "for a hash target" do
370 it_behaves_like "a Hash target"
371 end
372
373 context "for a target that subclasses Hash to treat string/symbol keys interchangeably, but returns a raw hash from #to_hash" do
374 it_behaves_like "a Hash target" do
375 undef :build_target # to prevent "method redefined" warning
376 def build_target(hsh)
377 FakeHashWithIndifferentAccess.from_hash(hsh)
378 end
379
380 undef :use_string_keys_in_failure_message? # to prevent "method redefined" warning
381 def use_string_keys_in_failure_message?
382 true
383 end
384 end
385 end
386
387 context "for a target that subclasses Hash to perform custom key checks like downcasing" do
388 it_behaves_like "a Hash target" do
389 undef :build_target # to prevent "method redefined" warning
390 def build_target(hsh)
391 CaseInsensitiveHash.from_hash(hsh)
392 end
393 end
394 end
395
396 context "for a target that can pass for a hash" do
397 def build_target(hsh)
398 PseudoHash.new(hsh)
399 end
400
401 around do |example|
402 in_sub_process_if_possible do
403 require 'delegate'
404
405 class PseudoHash < SimpleDelegator
406 end
407
408 example.run
409 end
410 end
411
412 it_behaves_like "a Hash target"
413 end
414 end
415
416 describe "expect(...).to include(with, multiple, args)" do
417 it "has a description" do
418 matcher = include("a")
419 expect(matcher.description).to eq("include \"a\"")
420 end
421
422 context "for a string target" do
423 it "passes if target includes all items" do
424 expect("a string").to include("str", "a")
425 end
426
427 it "fails if target does not include one of the items" do
428 expect {
429 expect("a string").to include("str", "a", "foo")
430 }.to fail_matching('expected "a string" to include "foo"')
431 end
432
433 it "fails if target does not include two of the items" do
434 expect {
435 expect("a string").to include("nope", "a", "nada", "str")
436 }.to fail_matching('expected "a string" to include "nope" and "nada"')
437 end
438
439 it "fails if target does not include many of the items" do
440 expect {
441 expect("a string").to include("nope", "a", "nada", "nein", "ing", "str")
442 }.to fail_matching('expected "a string" to include "nope", "nada", and "nein"')
443 end
444 end
445
446 context "for an array target" do
447 it "passes if target includes all items" do
448 expect([1, 2, 3]).to include(1, 2, 3)
449 end
450
451 it "passes if target includes all items fuzzily matched" do
452 expect(["A", "B", "C"]).to include(match(/b/i), "A")
453 end
454
455 it "fails if target does not include one of the items" do
456 expect {
457 expect([1, 2, 3]).to include(1, 2, 4)
458 }.to fail_matching("expected [1, 2, 3] to include 4")
459 end
460
461 it "fails if target does not include two of the items" do
462 expect {
463 expect([1, 2, 3]).to include(5, 1, 2, 4)
464 }.to fail_matching("expected [1, 2, 3] to include 5 and 4")
465 end
466
467 it "fails if target does not include many of the items" do
468 expect {
469 expect([1, 2, 3]).to include(5, 1, 6, 2, 4)
470 }.to fail_matching("expected [1, 2, 3] to include 5, 6, and 4")
471 end
472
473 it 'correctly diffs lists of hashes' do
474 allow(RSpec::Matchers.configuration).to receive(:color?).and_return(false)
475
476 expect {
477 expect([
478 { :number => 1 },
479 { :number => 2 },
480 { :number => 3 }
481 ]).to include(
482 { :number => 1 },
483 { :number => 0 },
484 { :number => 3 }
485 )
486 }.to fail_including(
487 if RUBY_VERSION.to_f > 3.3
488 dedent(<<-END)
489 |Diff:
490 |@@ #{one_line_header} @@
491 |-[{number: 1}, {number: 0}, {number: 3}]
492 |+[{number: 1}, {number: 2}, {number: 3}]
493 END
494 else
495 dedent(<<-END)
496 |Diff:
497 |@@ #{one_line_header} @@
498 |-[{:number=>1}, {:number=>0}, {:number=>3}]
499 |+[{:number=>1}, {:number=>2}, {:number=>3}]
500 END
501 end
502 )
503 end
504 end
505
506 context "for a hash target" do
507 it 'passes if target includes all items as keys' do
508 expect({ :key => 'value', :other => 'value' }).to include(:key, :other)
509 end
510
511 it "passes if target includes all items as keys fuzzily matched" do
512 expect({ "A" => "B", "C" => "D" }).to include(match(/c/i), "A")
513 end
514
515 it 'fails if target does not include one of the items as a key' do
516 expect {
517 expect({ :key => 'value', :this => 'that' }).to include(:key, :nope, :this)
518 }.to fail_with(%r|expected #{hash_inspect_for_regex :key => "value", :this => "that"} to include :nope|)
519 end
520
521 it "fails if target does not include two of the items as keys" do
522 expect {
523 expect({ :key => 'value', :this => 'that' }).to include(:nada, :key, :nope, :this)
524 }.to fail_with(%r|expected #{hash_inspect_for_regex :key => "value", :this => "that"} to include :nada and :nope|)
525 end
526
527 it "fails if target does not include many of the items as keys" do
528 expect {
529 expect({ :key => 'value', :this => 'that' }).to include(:nada, :key, :nope, :negative, :this)
530 }.to fail_with(%r|expected #{hash_inspect_for_regex :key => "value", :this => "that"} to include :nada, :nope, and :negative|)
531 end
532 end
533
534 it 'does not implement count constraints' do
535 expect {
536 expect('').to include('foo', 'bar').once
537 }.to raise_error(NotImplementedError)
538 expect {
539 expect('').to include('foo', 'bar').at_least(:twice)
540 }.to raise_error(NotImplementedError)
541 expect {
542 expect('').to include('foo', 'bar').at_most(:twice)
543 }.to raise_error(NotImplementedError)
544 end
545 end
546
547 describe "expect(...).not_to include(expected)" do
548 context "for an object that does not respond to `include?`" do
549 it 'fails gracefully' do
550 expect {
551 expect(5).not_to include(1)
552 }.to fail_matching("expected 5 not to include 1, but it does not respond to `include?`")
553 end
554 end
555
556 context "for an arbitrary object that responds to `include?`" do
557 it 'delegates to `include?`' do
558 container = double("Container")
559 allow(container).to receive(:include?) { |arg| arg == :stuff }
560
561 expect(container).not_to include(:space)
562
563 expect {
564 expect(container).not_to include(:stuff)
565 }.to fail_matching("not to include :stuff")
566 end
567 end
568
569 context "for a string target" do
570 it "passes if target does not include expected" do
571 expect("abc").not_to include("d")
572 end
573
574 it "fails if target includes expected" do
575 expect {
576 expect("abc").not_to include("c")
577 }.to fail_with("expected \"abc\" not to include \"c\"")
578 end
579
580 context "with exact count" do
581 it 'passes if the block yields wrong number of times' do
582 expect('foo bar foo').not_to include('foo').once
583 end
584
585 it 'fails if the block yields the specified number of times' do
586 expect {
587 expect('fooo bar').not_to include('oo').once
588 }.to fail_with(/expected "fooo bar" not to include "oo" once but it is included once/)
589 end
590 end
591
592 context "with at_least count" do
593 it 'fails if the search term is included at least the number of times' do
594 expect {
595 expect('foo bar foo foo').not_to include('foo').at_least(:twice)
596 }.to fail_with(/expected "foo bar foo foo" not to include "foo" at least twice but it is included 3 times/)
597 end
598
599 it 'passes if the search term is included too few times' do
600 expect('foo bar foo').not_to include('foo').at_least(:thrice)
601 end
602 end
603
604 context "with at_most count" do
605 it 'fails if the search term is included at most the number of times' do
606 expect {
607 expect('foo bar').not_to include('foo').at_most(:twice)
608 }.to fail_with(/expected "foo bar" not to include "foo" at most twice but it is included once/)
609 end
610
611 it 'passes if the search term is included too many times' do
612 expect('foo bar foo foo').not_to include('foo').at_most(:twice)
613 end
614 end
615 end
616
617 context "for an array target" do
618 it "passes if target does not include expected" do
619 expect([1, 2, 3]).not_to include(4)
620 end
621
622 it "fails if target includes expected" do
623 expect {
624 expect([1, 2, 3]).not_to include(3)
625 }.to fail_with("expected [1, 2, 3] not to include 3")
626 end
627
628 it 'passes when given differing null doubles' do
629 expect([double.as_null_object]).not_to include(double.as_null_object)
630 end
631
632 it 'fails when given the same null double' do
633 dbl = double.as_null_object
634
635 expect {
636 expect([dbl]).not_to include(dbl)
637 }.to fail_matching("expected [#{dbl.inspect}] not to include")
638 end
639 end
640
641 context "for a hash target" do
642 it 'passes if target does not have the expected as a key' do
643 expect({ :other => 'value' }).not_to include(:key)
644 end
645
646 it "fails if target includes expected key" do
647 expect {
648 expect({ :key => 'value' }).not_to include(:key)
649 }.to fail_matching("expected #{hash_inspect({ :key => "value" })} not to include :key")
650 end
651 end
652
653 end
654
655 describe "expect(...).not_to include(with, multiple, args)" do
656 context "for a string target" do
657 it "passes if the target does not include any of the expected" do
658 expect("abc").not_to include("d", "e", "f")
659 end
660
661 it "fails if the target includes all of the expected" do
662 expect {
663 expect("abc").not_to include("c", "a")
664 }.to fail_with('expected "abc" not to include "c" and "a"')
665 end
666
667 it "fails if the target includes one (but not all) of the expected" do
668 expect {
669 expect("abc").not_to include("d", "a")
670 }.to fail_with('expected "abc" not to include "a"')
671 end
672
673 it "fails if the target includes two (but not all) of the expected" do
674 expect {
675 expect("abc").not_to include("d", "a", "b")
676 }.to fail_with('expected "abc" not to include "a" and "b"')
677 end
678
679 it "fails if the target includes many (but not all) of the expected" do
680 expect {
681 expect("abcd").not_to include("b", "d", "a", "f")
682 }.to fail_with('expected "abcd" not to include "b", "d", and "a"')
683 end
684 end
685
686 context "for a hash target" do
687 it "passes if it does not include any of the expected keys" do
688 expect({ :a => 1, :b => 2 }).not_to include(:c, :d)
689 end
690
691 it "fails if the target includes all of the expected keys" do
692 expect {
693 expect({ :a => 1, :b => 2 }).not_to include(:a, :b)
694 }.to fail_with(%r|expected #{hash_inspect_for_regex :a => 1, :b => 2} not to include :a and :b|)
695 end
696
697 it "fails if the target includes one (but not all) of the expected keys" do
698 expect {
699 expect({ :a => 1, :b => 2 }).not_to include(:d, :b)
700 }.to fail_with(%r|expected #{hash_inspect_for_regex :a => 1, :b => 2} not to include :b|)
701 end
702
703 it "fails if the target includes two (but not all) of the expected keys" do
704 expect {
705 expect({ :a => 1, :b => 2 }).not_to include(:a, :b, :c)
706 }.to fail_with(%r|expected #{hash_inspect_for_regex :a => 1, :b => 2} not to include :a and :b|)
707 end
708
709 it "fails if the target includes many (but not all) of the expected keys" do
710 expect {
711 expect({ :a => 1, :b => 2, :c => 3 }).not_to include(:b, :a, :c, :f)
712 }.to fail_with(%r|expected #{hash_inspect_for_regex :a => 1, :b => 2, :c => 3} not to include :b, :a, and :c|)
713 end
714 end
715
716 context "for an array target" do
717 it "passes if the target does not include any of the expected" do
718 expect([1, 2, 3]).not_to include(4, 5, 6)
719 end
720
721 it "fails if the target includes all of the expected" do
722 expect {
723 expect([1, 2, 3]).not_to include(3, 1)
724 }.to fail_with('expected [1, 2, 3] not to include 3 and 1')
725 end
726
727 it "fails if the target includes one (but not all) of the expected" do
728 expect {
729 expect([1, 2, 3]).not_to include(4, 1)
730 }.to fail_with('expected [1, 2, 3] not to include 1')
731 end
732
733 it "fails if the target includes two (but not all) of the expected" do
734 expect {
735 expect([1, 2, 3]).not_to include(4, 1, 2)
736 }.to fail_with('expected [1, 2, 3] not to include 1 and 2')
737 end
738
739 it "fails if the target includes many (but not all) of the expected" do
740 expect {
741 expect([1, 2, 3]).not_to include(5, 4, 2, 1, 3)
742 }.to fail_with('expected [1, 2, 3] not to include 2, 1, and 3')
743 end
744 end
745 end
746
747 describe "expect(...).to include(:key => value)" do
748 context 'for a hash target' do
749 it "passes if target includes the key/value pair" do
750 expect({ :key => 'value' }).to include(:key => 'value')
751 end
752
753 it "passes if target includes the key/value pair among others" do
754 expect({ :key => 'value', :other => 'different' }).to include(:key => 'value')
755 end
756
757 it "passes if target includes the key/value pair fuzzily matched among others", :if => (RUBY_VERSION.to_f > 1.8) do
758 hsh = { :key => 'value', :other => 'different' }
759
760 expect(hsh).to include(match(/KEY/i) => 'value')
761 expect(FakeHashWithIndifferentAccess.from_hash(hsh)).to include(match(/KEY/i) => 'value')
762 expect(CaseInsensitiveHash.from_hash(hsh)).to include(match(/KEY/i) => 'value')
763 end
764
765 it "fails if target has a different value for key" do
766 expect {
767 expect({ :key => 'different' }).to include(:key => 'value')
768 }.to fail_matching("expected #{hash_inspect({ :key => "different" })} to include #{hash_inspect({ :key => "value" })}")
769 end
770
771 it "fails if target has a different key" do
772 expect {
773 expect({ :other => 'value' }).to include(:key => 'value')
774 }.to fail_matching("expected #{hash_inspect({ :other => "value" })} to include #{hash_inspect({ :key => "value" })}")
775 end
776 end
777
778 context 'for a non-hash target' do
779 it "fails if the target does not contain the given hash" do
780 expect {
781 expect(['a', 'b']).to include(:key => 'value')
782 }.to fail_matching("expected [\"a\", \"b\"] to include #{hash_inspect({ :key => "value" })}")
783 end
784
785 it "passes if the target contains the given hash" do
786 expect(['a', { :key => 'value' }]).to include(:key => 'value')
787 end
788 end
789 end
790
791 describe "expect(...).not_to include(:key => value)" do
792 context 'for a hash target' do
793 it "fails if target includes the key/value pair" do
794 expect {
795 expect({ :key => 'value' }).not_to include(:key => 'value')
796 }.to fail_matching("expected #{hash_inspect({ :key => "value" })} not to include #{hash_inspect({ :key => "value" })}")
797 end
798
799 it "fails if target includes the key/value pair among others" do
800 expect {
801 expect({ :key => 'value', :other => 'different' }).not_to include(:key => 'value')
802 }.to fail_with(%r|expected #{hash_inspect_for_regex(:key => "value", :other => "different")} not to include #{hash_inspect_for_regex({ :key => "value" })}|)
803 end
804
805 it "passes if target has a different value for key" do
806 expect({ :key => 'different' }).not_to include(:key => 'value')
807 end
808
809 it "passes if target has a different key" do
810 expect({ :other => 'value' }).not_to include(:key => 'value')
811 end
812 end
813
814 context "for a non-hash target" do
815 it "passes if the target does not contain the given hash" do
816 expect(['a', 'b']).not_to include(:key => 'value')
817 end
818
819 it "fails if the target contains the given hash" do
820 expect {
821 expect(['a', { :key => 'value' }]).not_to include(:key => 'value')
822 }.to fail_matching("expected [\"a\", #{hash_inspect({ :key => "value" })}] not to include #{hash_inspect({ :key => "value" })}")
823 end
824 end
825 end
826
827 describe "expect(...).to include(:key1 => value1, :key2 => value2)" do
828 context 'for a hash target' do
829 it "passes if target includes the key/value pairs" do
830 expect({ :a => 1, :b => 2 }).to include(:b => 2, :a => 1)
831 end
832
833 it "passes if target includes the key/value pairs among others" do
834 expect({ :a => 1, :c => 3, :b => 2 }).to include(:b => 2, :a => 1)
835 end
836
837 it "fails if target has a different value for one of the keys" do
838 expect {
839 expect({ :a => 1, :b => 2 }).to include(:a => 2, :b => 2)
840 }.to fail_with(%r|expected #{hash_inspect_for_regex :a => 1, :b => 2} to include #{hash_inspect_for_regex :a => 2}|)
841 end
842
843 it "fails if target has a different value for both of the keys" do
844 expect {
845 expect({ :a => 1, :b => 1 }).to include(:a => 2, :b => 2)
846 }.to fail_with(%r|expected #{hash_inspect_for_regex :a => 1, :b => 1} to include #{hash_inspect_for_regex :a => 2, :b => 2}|)
847 end
848
849 it "fails if target lacks one of the keys" do
850 expect {
851 expect({ :a => 1, :b => 1 }).to include(:a => 1, :c => 1)
852 }.to fail_with(%r|expected #{hash_inspect_for_regex :a => 1, :b => 1} to include #{hash_inspect_for_regex :c => 1}|)
853 end
854
855 it "fails if target lacks both of the keys" do
856 expect {
857 expect({ :a => 1, :b => 1 }).to include(:c => 1, :d => 1)
858 }.to fail_with(%r|expected #{hash_inspect_for_regex :a => 1, :b => 1} to include #{hash_inspect_for_regex :c => 1, :d => 1}|)
859 end
860
861 it "fails if target lacks one of the keys and has a different value for another" do
862 expect {
863 expect({ :a => 1, :b => 2 }).to include(:c => 1, :b => 3)
864 }.to fail_with(%r|expected #{hash_inspect_for_regex :a => 1, :b => 2} to include #{hash_inspect_for_regex :c => 1, :b => 3}|)
865 end
866 end
867
868 context 'for a non-hash target' do
869 it "fails if the target does not contain the given hash" do
870 expect {
871 expect(['a', 'b']).to include(:a => 1, :b => 1)
872 }.to fail_with(%r|expected \["a", "b"\] to include #{hash_inspect_for_regex :a => 1, :b => 1}|)
873 end
874
875 it "passes if the target contains the given hash" do
876 expect(['a', { :a => 1, :b => 2 }]).to include(:a => 1, :b => 2)
877 end
878 end
879 end
880
881 describe "expect(...).not_to include(:key1 => value1, :key2 => value2)" do
882 context 'for a hash target' do
883 it "fails if target includes the key/value pairs" do
884 expect {
885 expect({ :a => 1, :b => 2 }).not_to include(:a => 1, :b => 2)
886 }.to fail_with(%r|expected #{hash_inspect_for_regex :a => 1, :b => 2} not to include #{hash_inspect_for_regex :a => 1, :b => 2}|)
887 end
888
889 it "fails if target includes the key/value pairs among others" do
890 hash = { :a => 1, :b => 2, :c => 3 }
891 expect {
892 expect(hash).not_to include(:a => 1, :b => 2)
893 }.to fail_with(%r|expected #{hash_inspect_for_regex :a => 1, :b => 2, :c => 3} not to include #{hash_inspect_for_regex :a => 1, :b => 2}|)
894 end
895
896 it "fails if target has a different value for one of the keys" do
897 expect {
898 expect({ :a => 1, :b => 2 }).not_to include(:a => 2, :b => 2)
899 }.to fail_with(%r|expected #{hash_inspect_for_regex :a => 1, :b => 2} not to include #{hash_inspect_for_regex :b => 2}|)
900 end
901
902 it "passes if target has a different value for both of the keys" do
903 expect({ :a => 1, :b => 1 }).not_to include(:a => 2, :b => 2)
904 end
905
906 it "fails if target lacks one of the keys" do
907 expect {
908 expect({ :a => 1, :b => 1 }).not_to include(:a => 1, :c => 1)
909 }.to fail_with(%r|expected #{hash_inspect_for_regex :a => 1, :b => 1} not to include #{hash_inspect_for_regex :a => 1}|)
910 end
911
912 it "passes if target lacks both of the keys" do
913 expect({ :a => 1, :b => 1 }).not_to include(:c => 1, :d => 1)
914 end
915 end
916
917 context 'for a non-hash target' do
918 it "passes if the target does not contain the given hash" do
919 expect(['a', 'b']).not_to include(:a => 1, :b => 1)
920 end
921
922 it "fails if the target contains the given hash" do
923 expect {
924 expect(['a', { :a => 1, :b => 2 }]).not_to include(:a => 1, :b => 2)
925 }.to fail_with(%r|expected \["a", #{hash_inspect_for_regex :a => 1, :b => 2}\] not to include #{hash_inspect_for_regex :a => 1, :b => 2}|)
926 end
927 end
928 end
929
930 describe "Composing matchers with `include`" do
931 RSpec::Matchers.define :a_string_containing do |expected|
932 match do |actual|
933 actual.include?(expected)
934 end
935
936 description do
937 "a string containing '#{expected}'"
938 end
939 end
940
941 describe "expect(array).to include(matcher)" do
942 it "passes when the matcher matches one of the values" do
943 expect([10, 20, 30]).to include( a_value_within(5).of(24) )
944 end
945
946 it 'provides a description' do
947 description = include( a_value_within(5).of(24) ).description
948 expect(description).to eq("include (a value within 5 of 24)")
949 end
950
951 it 'fails with a clear message when the matcher matches none of the values' do
952 expect {
953 expect([10, 30]).to include( a_value_within(5).of(24) )
954 }.to fail_with("expected [10, 30] to include (a value within 5 of 24)")
955 end
956
957 it 'works with comparison matchers' do
958 expect {
959 expect([100, 200]).to include(a_value < 90)
960 }.to fail_with("expected [100, 200] to include (a value < 90)")
961
962 expect([100, 200]).to include(a_value > 150)
963 end
964
965 it 'does not treat an object that only implements #matches? as a matcher' do
966 not_a_matcher = Struct.new(:value) do
967 def matches?(_)
968 fail "`matches?` should never be called"
969 end
970 end
971
972 expect([not_a_matcher.new("rspec.info")]).to include(not_a_matcher.new("rspec.info"))
973
974 expect {
975 expect([not_a_matcher.new("rspec.info")]).to include(not_a_matcher.new("foo.com"))
976 }.to fail_matching("expected [#{not_a_matcher.new("rspec.info").inspect}] to include")
977 end
978 end
979
980 describe "expect(array).to include(multiple, matcher, arguments)" do
981 it "passes if target includes items satisfying all matchers" do
982 expect(['foo', 'bar', 'baz']).to include(a_string_containing("ar"), a_string_containing('oo'))
983 end
984
985 it "fails if target does not include an item satisfying any one of the items" do
986 expect {
987 expect(['foo', 'bar', 'baz']).to include(a_string_containing("ar"), a_string_containing("abc"))
988 }.to fail_matching("expected #{['foo', 'bar', 'baz'].inspect} to include (a string containing 'abc')")
989 end
990 end
991
992 describe "expect(hash).to include(key => matcher)" do
993 it "passes when the matcher matches" do
994 expect(:a => 12).to include(:a => a_value_within(3).of(10))
995 end
996
997 it 'provides a description' do
998 description = include(:a => a_value_within(3).of(10)).description
999 expect(description).to eq(
1000 if RUBY_VERSION.to_f > 3.3
1001 "include {a: (a value within 3 of 10)}"
1002 else
1003 "include {:a => (a value within 3 of 10)}"
1004 end
1005 )
1006 end
1007
1008 it "fails with a clear message when the matcher does not match" do
1009 expect {
1010 expect(:a => 15).to include(:a => a_value_within(3).of(10))
1011 }.to fail_matching(
1012 if RUBY_VERSION.to_f > 3.3
1013 "expected {a: 15} to include {a: (a value within 3 of 10)}"
1014 else
1015 "expected {:a => 15} to include {:a => (a value within 3 of 10)}"
1016 end
1017 )
1018 end
1019 end
1020
1021 describe "expect(hash).to include(key_matcher)" do
1022 it "passes when the matcher matches a key", :if => (RUBY_VERSION.to_f > 1.8) do
1023 expect(:drink => "water", :food => "bread").to include(match(/foo/))
1024 end
1025
1026 it 'provides a description' do
1027 description = include(match(/foo/)).description
1028 expect(description).to eq("include (match /foo/)")
1029 end
1030
1031 it 'fails with a clear message when the matcher does not match', :if => (RUBY_VERSION.to_f > 1.8) do
1032 expect {
1033 expect(:drink => "water", :food => "bread").to include(match(/bar/))
1034 }.to fail_matching("expected #{hash_inspect({ :drink => "water", :food => "bread" })} to include (match /bar/)")
1035 end
1036 end
1037
1038 describe "expect(hash).to include(key_matcher => value)" do
1039 it "passes when the matcher matches a pair", :if => (RUBY_VERSION.to_f > 1.8) do
1040 expect(:drink => "water", :food => "bread").to include(match(/foo/) => "bread")
1041 end
1042
1043 it "passes when the matcher matches all pairs", :if => (RUBY_VERSION.to_f > 1.8) do
1044 expect(:drink => "water", :food => "bread").to include(match(/foo/) => "bread", match(/ink/) => "water")
1045 end
1046
1047 it "passes with a natural matcher", :if => (RUBY_VERSION.to_f > 1.8) do
1048 expect(:drink => "water", :food => "bread").to include(/foo/ => "bread")
1049 end
1050
1051 it "passes with a natural matcher", :if => (RUBY_VERSION.to_f > 1.8) do
1052 expect(:drink => "water", :food => "bread").to include(/foo/ => /read/)
1053 end
1054
1055 it 'provides a description' do
1056 description = include(match(/foo/) => "bread").description
1057 expect(description).to eq('include {(match /foo/) => "bread"}')
1058 end
1059
1060 it 'fails with a clear message when the value does not match', :if => (RUBY_VERSION.to_f > 1.8) do
1061 expect {
1062 expect(:drink => "water", :food => "bread").to include(match(/foo/) => "meat")
1063 }.to fail_matching("expected #{hash_inspect({ :drink => "water", :food => "bread" })} to include {(match /foo/) => \"meat\"}")
1064 end
1065
1066 it 'fails with a clear message when the matcher does not match', :if => (RUBY_VERSION.to_f > 1.8) do
1067 expect {
1068 expect(:drink => "water", :food => "bread").to include(match(/bar/) => "bread")
1069 }.to fail_matching("expected #{hash_inspect({ :drink => "water", :food => "bread" })} to include {(match /bar/) => \"bread\"}")
1070 end
1071
1072 it 'fails with a clear message when several matchers do not match', :if => (RUBY_VERSION.to_f > 1.8) do
1073 expect {
1074 expect(:drink => "water", :food => "bread").to include(match(/bar/) => "bread", match(/baz/) => "water")
1075 }.to fail_matching("expected #{hash_inspect({ :drink => "water", :food => "bread" })} to include {(match /bar/) => \"bread\", (match /baz/) => \"water\"}")
1076 end
1077 end
1078
1079 describe "expect(array).not_to include(multiple, matcher, arguments)" do
1080 it "passes if none of the target values satisfies any of the matchers" do
1081 expect(['foo', 'bar', 'baz']).not_to include(a_string_containing("gh"), a_string_containing('de'))
1082 end
1083
1084 it 'fails if all of the matchers are satisfied by one of the target values' do
1085 expect {
1086 expect(['foo', 'bar', 'baz']).not_to include(a_string_containing("ar"), a_string_containing('az'))
1087 }.to fail_matching("expected #{['foo', 'bar', 'baz'].inspect} not to include (a string containing 'ar') and (a string containing 'az')")
1088 end
1089
1090 it 'fails if the some (but not all) of the matchers are satisfied' do
1091 expect {
1092 expect(['foo', 'bar', 'baz']).not_to include(a_string_containing("ar"), a_string_containing('bz'))
1093 }.to fail_matching("expected #{['foo', 'bar', 'baz'].inspect} not to include (a string containing 'ar')")
1094 end
1095 end
1096 end
1097
1098 # `fail_including` uses the `include` matcher internally, and using a matcher
1099 # to test itself is potentially problematic, so just for this spec file we
1100 # use `fail_matching` instead, which converts to a regex instead.
1101 def fail_matching(message)
1102 raise_error(RSpec::Expectations::ExpectationNotMetError, /#{Regexp.escape(message)}/)
1103 end
1104 end