Debian Codegraph @

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

raw symbols SCIP
1 module RSpec::Matchers::BuiltIn
2 RSpec.describe Compound do
3 include RSpec::Support::Spec::DiffHelpers
4
5 let(:matcher_without_diffable) { include("foo") }
6
7 before do
8 allow(RSpec::Matchers.configuration).to receive_messages(:color? => false)
9 allow(matcher_without_diffable).to receive(:diffable?).and_raise(NoMethodError)
10 end
11
12 shared_examples "making a copy" do |compound_method, copy_method|
13 context "when making a copy via `#{copy_method}`" do
14 it "uses a copy of the base matchers" do
15 matcher_1 = include(3)
16 matcher_2 = include(4)
17 compound = matcher_1.__send__(compound_method, matcher_2)
18 copy = compound.__send__(copy_method)
19
20 expect(copy).not_to equal(compound)
21 expect(copy.matcher_1).not_to equal(matcher_1)
22 expect(copy.matcher_1).to be_a(RSpec::Matchers::BuiltIn::Include)
23 expect(copy.matcher_1.expected).to eq([3])
24
25 expect(copy.matcher_2).not_to equal(matcher_2)
26 expect(copy.matcher_2).to be_a(RSpec::Matchers::BuiltIn::Include)
27 expect(copy.matcher_2.expected).to eq([4])
28 end
29
30 it "copies custom matchers properly so they can work even though they have singleton behavior" do
31 matcher_1 = custom_include(3)
32 matcher_2 = custom_include(3)
33 compound = matcher_1.__send__(compound_method, matcher_2)
34 copy = compound.__send__(copy_method)
35
36 expect(copy).not_to equal(compound)
37 expect(copy.matcher_1).not_to equal(matcher_1)
38 expect(copy.matcher_2).not_to equal(matcher_2)
39
40 expect([3]).to copy
41
42 expect { expect([4]).to copy }.to fail_including("expected [4]")
43 end
44 end
45 end
46
47 shared_examples "handles blocks properly" do |meth|
48 define_method :combine do |m1, m2|
49 m1.__send__(meth, m2)
50 end
51
52 context "when used with a block matcher" do
53 it 'executes the block only once, regardless of how many matchers are compounded' do
54 w, x, y, z = 0, 0, 0, 0
55 expect {
56 w += 1; x += 2; y += 3; z += 4
57 }.to( combine(
58 change { w }.to(1), combine(
59 change { x }.to(2), combine(
60 change { y }.to(3),
61 change { z }.to(4) ) ) )
62 )
63 end
64
65 context "does not work when combined with another non-block matcher" do
66 example "with the block matcher first" do
67 expect {
68 x = 0
69 expect { x += 2 }.to combine(change { x }.to(2), be_a(Proc))
70 }.to fail_with(/supports_block_expectations/)
71 end
72
73 example "with the block matcher last" do
74 expect {
75 x = 0
76 expect { x += 2 }.to combine(be_a(Proc), change { x }.to(2))
77 }.to fail_with(/supports_block_expectations/)
78 end
79 end
80
81 context "indicates block expectations are not supported when combined with a custom matcher that does not define `supports_block_expectations?" do
82 let(:non_block_matcher) do
83 Class.new do
84 include ::RSpec::Matchers::Composable
85 def matches?(*); true; end
86 end.new
87 end
88
89 example "with the block matcher first" do
90 compound = combine(change {}.to(2), non_block_matcher)
91 expect(compound.supports_block_expectations?).to be false
92 end
93
94 example "with the block matcher last" do
95 compound = combine(non_block_matcher, change {}.to(2))
96 expect(compound.supports_block_expectations?).to be false
97 end
98 end
99
100 context "forwards on any matcher block arguments as needed (such as for `yield_with_args`)" do
101 obj = Object.new
102 def obj.foo(print_bar=true)
103 yield "bar"
104 print "printing bar" if print_bar
105 end
106
107 example "with the matcher that passes block args first" do
108 call_count = 0
109
110 expect { |probe|
111 call_count += 1
112 obj.foo(&probe)
113 }.to combine(yield_with_args(/bar/), output("printing bar").to_stdout)
114
115 expect(call_count).to eq(1)
116 end
117
118 example "with the matcher that passes block args last" do
119 call_count = 0
120
121 expect { |probe|
122 call_count += 1
123 obj.foo(&probe)
124 }.to combine(output("printing bar").to_stdout, yield_with_args("bar"))
125
126 expect(call_count).to eq(1)
127 end
128
129 it "does not support two matchers that both pass arguments to the block" do
130 expect {
131 expect { |probe|
132 obj.foo(false, &probe)
133 }.to combine(yield_with_args(/bar/), yield_with_args("bar"))
134 }.to raise_error(/cannot be combined/)
135 end
136 end
137
138 context "when used with `raise_error` (which cannot match against a wrapped block)" do
139 it 'does not work when combined with `throw_symbol` (which also cannot match against a wrapped block)' do
140 expect {
141 expect {}.to combine(raise_error("boom"), throw_symbol(:foo))
142 }.to raise_error(/cannot be combined/)
143 end
144
145 it 'works when `raise_error` is first' do
146 x = 0
147 expect {
148 x += 2
149 raise "boom"
150 }.to combine(raise_error("boom"), change { x }.to(2))
151 end
152
153 it 'works when `raise_error` is last' do
154 x = 0
155 expect {
156 x += 2
157 raise "boom"
158 }.to combine(change { x }.to(2), raise_error("boom"))
159 end
160
161 context "with nested compound matchers" do
162 if meth == :or
163 def expect_block
164 @x = 0
165 expect do
166 print "a"
167
168 # for or we need `raise "boom"` and one other
169 # to be wrong, so that only the `output("a").to_stdout`
170 # is correct for these specs to cover the needed
171 # behavior.
172 @x += 3
173 raise "bom"
174 end
175 end
176 else
177 def expect_block
178 @x = 0
179 expect do
180 print "a"
181 @x += 2
182 raise "boom"
183 end
184 end
185 end
186
187 it 'works when `raise_error` is first in the first compound matcher' do
188 matcher = combine(
189 combine(raise_error("boom"), change { @x }.to(2)),
190 output("a").to_stdout
191 )
192
193 expect_block.to matcher
194 end
195
196 it 'works when `raise_error` is last in the first compound matcher' do
197 matcher = combine(
198 combine(change { @x }.to(2), raise_error("boom")),
199 output("a").to_stdout
200 )
201
202 expect_block.to matcher
203 end
204
205 it 'works when `raise_error` is first in the last compound matcher' do
206 matcher = combine(
207 change { @x }.to(2),
208 combine(raise_error("boom"), output("a").to_stdout)
209 )
210
211 expect_block.to matcher
212 end
213
214 it 'works when `raise_error` is last in the last compound matcher' do
215 matcher = combine(
216 change { @x }.to(2),
217 combine(output("a").to_stdout, raise_error("boom"))
218 )
219 expect_block.to matcher
220 end
221 end
222 end
223 end
224
225 context "when given a proc and non block matchers" do
226 it 'does not treat it as a block expectation expression' do
227 p = lambda {}
228 expect(p).to combine(be_a(Proc), be(p))
229
230 expect {
231 expect(p).to combine(be_a(Integer), eq(3))
232 }.to fail_including("expected: 3")
233 end
234 end
235 end
236
237 context "when used as a composable matcher" do
238 it 'can pass' do
239 expect(["food", "barn"]).to include(
240 a_string_starting_with("f").and(ending_with("d")),
241 a_string_starting_with("b").and(ending_with("n"))
242 )
243 end
244
245 it 'can fail' do
246 expect {
247 expect(["foo", "bar"]).to include(
248 a_string_starting_with("f").and(ending_with("d")),
249 a_string_starting_with("b").and(ending_with("n"))
250 )
251 }.to fail_including('expected ["foo", "bar"] to include (a string starting with "f" and ending with "d") and (a string starting with "b" and ending with "n")')
252 end
253
254 it 'provides a description' do
255 matcher = include(
256 a_string_starting_with("f").and(ending_with("d")),
257 a_string_starting_with("b").and(ending_with("n"))
258 )
259
260 expect(matcher.description).to eq('include (a string starting with "f" and ending with "d") and (a string starting with "b" and ending with "n")')
261 end
262 end
263
264 describe "expect(...).to custom_matcher.and(other_matcher)" do
265 if RUBY_VERSION.to_f > 1.8 # The example can be adjusted to be compatible with Ruby 1.8, but it is then not indicative of the problem
266 binding.eval(<<-CODE, __FILE__, __LINE__)
267 it "does not allow composing incompatible matchers" do
268 arr = []
269 expect {
270 expect { arr << :foo }
271 .to change { arr }.to be_one
272 .and change { arr }.to include(:foo) # There is a barely noticeable difference: the `.and` runs on the wrong matcher, `be_one` instead of `change`
273 }.to raise_error("Block and value matchers can't be combined in a compound expectation (be one, change result to include :foo)")
274 end
275 CODE
276 end
277
278 it "treats a matcher that does support value expectations correctly" do
279 expect([1, 2]).to include(1).and include(2)
280 end
281
282 it "treats a matcher that doesn't _specify_ as supporting value expectations" do
283 unsupporting_includes_class = Class.new(Object) do
284 def matches?(actual); actual.include?(2); end
285 end
286 unsupporting_includes = unsupporting_includes_class.new
287 expect([1, 2]).to include(1).and unsupporting_includes
288 end
289 end
290
291 describe "expect(...).to matcher.and(other_matcher)" do
292
293 it_behaves_like "an RSpec value matcher", :valid_value => 3, :invalid_value => 4, :disallows_negation => true do
294 let(:matcher) { eq(3).and be <= 3 }
295 end
296
297 context 'when using boolean AND `&` alias' do
298 it_behaves_like "an RSpec value matcher", :valid_value => 3, :invalid_value => 4, :disallows_negation => true do
299 let(:matcher) { eq(3) & be_a(Integer) }
300 end
301 end
302
303 include_examples "making a copy", :and, :dup
304 include_examples "making a copy", :and, :clone
305 it_behaves_like "handles blocks properly", :and
306
307 context 'when both matchers pass' do
308 it 'passes' do
309 expect(3).to eq(3).and be >= 2
310 end
311 end
312
313 it 'has a description composed of both matcher descriptions' do
314 matcher = eq(3).and be >= 2
315 expect(3).to matcher
316 expect(matcher.description).to eq("eq 3 and be >= 2")
317 end
318
319 context 'when only the first matcher fails' do
320 it "fails with the first matcher's failure message" do
321 expect {
322 expect(3).to eq(4).and be >= 2
323 }.to fail_with(dedent <<-EOS)
324 |
325 |expected: 4
326 | got: 3
327 |
328 |(compared using ==)
329 |
330 EOS
331 end
332 end
333
334 context 'when only the second matcher fails' do
335 it "fails with the second matcher's failure message" do
336 expect {
337 expect(3).to be_kind_of(Integer).and eq(4)
338 }.to fail_with(dedent <<-EOS)
339 |
340 |expected: 4
341 | got: 3
342 |
343 |(compared using ==)
344 |
345 EOS
346 end
347 end
348
349 context "when both matchers fail" do
350 context "when both matchers have multi-line failure messages" do
351 it 'fails with a well formatted message containing both sub-messages' do
352 expect {
353 expect(3).to eq(4).and be >= 8
354 }.to fail_with(dedent <<-EOS)
355 |
356 | expected: 4
357 | got: 3
358 |
359 | (compared using ==)
360 |
361 |...and:
362 |
363 | expected: >= 8
364 | got: 3
365 EOS
366 end
367 end
368
369 context "when both matchers have single-line failure messages" do
370 it 'still fails with a multi-line failure message because it reads better than keeping it on a single line' do
371 expect {
372 expect("foo").to start_with("a").and end_with("z")
373 }.to fail_with(dedent <<-EOS)
374 | expected "foo" to start with "a"
375 |
376 |...and:
377 |
378 | expected "foo" to end with "z"
379 EOS
380 end
381 end
382
383 context "when the first matcher has a multi-line failure message" do
384 it 'fails with a well formatted message containing both sub-messages' do
385 expect {
386 expect("foo").to eq(4).and end_with("z")
387 }.to fail_with(dedent <<-EOS)
388 |
389 | expected: 4
390 | got: "foo"
391 |
392 | (compared using ==)
393 |
394 |...and:
395 |
396 | expected "foo" to end with "z"
397 EOS
398 end
399 end
400
401 context "when the second matcher has a multi-line failure message" do
402 it 'fails with a well formatted message containing both sub-messages' do
403 expect {
404 expect("foo").to end_with("z").and eq(4)
405 }.to fail_with(dedent <<-EOS)
406 | expected "foo" to end with "z"
407 |
408 |...and:
409 |
410 | expected: 4
411 | got: "foo"
412 |
413 | (compared using ==)
414 |
415 EOS
416 end
417 end
418
419 context "when the first matcher is diffable" do
420 subject { include("foo").and be_a(String) }
421
422 it 'is diffable' do
423 expect(subject).to be_diffable
424 end
425
426 context "when only first matcher fails" do
427 it 'fails with a message containing a diff for first matcher' do
428 expected_failure = dedent(<<-EOS)
429 |Diff for (include "foo"):
430 |@@ #{::Diff::LCS::VERSION.to_f > 1.5 ? "-1 +1,2": "-1,2 +1,3"} @@
431 |-foo
432 |+baz
433 |+bar
434 EOS
435
436 expect {
437 expect(dedent(<<-EOS)).to subject
438 |baz
439 |bar
440 EOS
441 }.to fail_including(expected_failure)
442 end
443 end
444
445 context "when only second matcher fails" do
446 subject { include("baz").and be_an(Integer) }
447
448 it 'fails with a message not containing a diff for first matcher' do
449 expect {
450 expect(dedent(<<-EOS)).to subject
451 |baz
452 |bar
453 EOS
454 }.to fail_with(a_string_excluding "Diff")
455 end
456 end
457
458 context "when both matcher fail" do
459 subject { include("foo").and eq(35) }
460
461 it "fails with a message containing a diff with first matcher" do
462 expected_failure = dedent(<<-EOS)
463 | expected "baz\\nbar" to include "foo"
464 |
465 |...and:
466 |
467 | expected: 35
468 | got: "baz\\nbar"
469 |
470 | (compared using ==)
471 |
472 |Diff for (include "foo"):
473 |@@ #{::Diff::LCS::VERSION.to_f > 1.5 ? "-1 +1,2" : "-1,2 +1,3"} @@
474 |-foo
475 |+baz
476 |+bar
477 EOS
478
479 expect {
480 expect(dedent(<<-EOS)).to subject
481 |baz
482 |bar
483 EOS
484 }.to fail_including(expected_failure)
485 end
486 end
487 end
488
489 context "when the first matcher does not implement #diffable?" do
490 subject { matcher_without_diffable.and exist }
491
492 it 'is not diffable' do
493 expect(subject).not_to be_diffable
494 end
495 end
496
497 context "when the second matcher does not implement #diffable?" do
498 subject { exist.and matcher_without_diffable }
499
500 it 'is not diffable' do
501 expect(subject).not_to be_diffable
502 end
503 end
504
505 context "when the second matcher is diffable" do
506 subject { eq(35).and include("foo") }
507
508 it 'is diffable' do
509 expect(subject).to be_diffable
510 end
511
512 it 'fails with a message containing a diff for second matcher' do
513 expected_failure = dedent(<<-EOS)
514 | expected: 35
515 | got: "baz\\nbar"
516 |
517 | (compared using ==)
518 |
519 |...and:
520 |
521 | expected "baz\\nbar" to include "foo"
522 |Diff for (include "foo"):
523 |@@ #{::Diff::LCS::VERSION.to_f > 1.5 ? "-1 +1,2" : "-1,2 +1,3"} @@
524 |-foo
525 |+baz
526 |+bar
527 EOS
528
529 expect {
530 expect(dedent(<<-EOS)).to subject
531 |baz
532 |bar
533 EOS
534 }.to fail_including(expected_failure)
535 end
536 end
537
538 context "when both matchers are diffable" do
539 subject { include("bar").and include("foo") }
540
541 it 'is diffable' do
542 expect(subject).to be_diffable
543 end
544
545 it 'fails with a message containing diffs for both matcher' do
546 expected_failure = dedent(<<-EOS)
547 | expected "baz\\nbug" to include "bar"
548 |
549 |...and:
550 |
551 | expected "baz\\nbug" to include "foo"
552 |Diff for (include "bar"):
553 |@@ #{::Diff::LCS::VERSION.to_f > 1.5 ? "-1 +1,2": "-1,2 +1,3"} @@
554 |-bar
555 |+baz
556 |+bug
557 |
558 |Diff for (include "foo"):
559 |@@ #{::Diff::LCS::VERSION.to_f > 1.5 ? "-1 +1,2": "-1,2 +1,3"} @@
560 |-foo
561 |+baz
562 |+bug
563 EOS
564
565 expect {
566 expect(dedent(<<-EOS)).to subject
567 |baz
568 |bug
569 EOS
570 }.to fail do |error|
571 expect(error.message).to include(expected_failure)
572 end
573 end
574
575 context 'when matcher transforms the actual' do
576 context 'when the matcher redefines `actual`' do
577 matcher :eq_downcase do |expected|
578 match do |actual|
579 @matcher_internal_actual = actual.downcase
580 values_match? expected, @matcher_internal_actual
581 end
582
583 def actual
584 @matcher_internal_actual
585 end
586
587 diffable
588 end
589
590 it 'shows the redefined value in diff' do
591 expected_failure =
592 dedent(<<-EOS)
593 | expected "HELLO\\nWORLD" to eq downcase "bonjour\\nmonde"
594 |
595 |...and:
596 |
597 | expected "HELLO\\nWORLD" to eq downcase "hola\\nmon"
598 |Diff for (eq downcase "bonjour\\nmonde"):
599 |@@ #{one_line_header(3)} @@
600 |-bonjour
601 |-monde
602 |+hello
603 |+world
604 |
605 |Diff for (eq downcase "hola\\nmon"):
606 |@@ #{one_line_header(3)} @@
607 |-hola
608 |-mon
609 |+hello
610 |+world
611 EOS
612
613 expect {
614 expect(
615 "HELLO\nWORLD"
616 ).to eq_downcase("bonjour\nmonde").and eq_downcase("hola\nmon")
617 }.to fail do |error|
618 expect(error.message).to include(expected_failure)
619 end
620 end
621 end
622
623 context 'when the matcher reassigns `@actual`' do
624 matcher :eq_downcase do |expected|
625 match do |actual|
626 @actual = actual.downcase
627 values_match? expected, @actual
628 end
629
630 diffable
631 end
632
633 it 'shows the reassigned value in diff' do
634 expected_failure =
635 dedent(<<-EOS)
636 | expected "hello\\nworld" to eq downcase "bonjour\\nmonde"
637 |
638 |...and:
639 |
640 | expected "hello\\nworld" to eq downcase "hola\\nmon"
641 |Diff for (eq downcase "bonjour\\nmonde"):
642 |@@ #{one_line_header(3)} @@
643 |-bonjour
644 |-monde
645 |+hello
646 |+world
647 |
648 |Diff for (eq downcase "hola\\nmon"):
649 |@@ #{one_line_header(3)} @@
650 |-hola
651 |-mon
652 |+hello
653 |+world
654 EOS
655
656 expect {
657 expect(
658 "HELLO\nWORLD"
659 ).to eq_downcase("bonjour\nmonde").and eq_downcase("hola\nmon")
660 }.to fail do |error|
661 expect(error.message).to include(expected_failure)
662 end
663 end
664 end
665 end
666 end
667
668 context "when both matchers are not diffable" do
669 subject { be_a(String).and be_truthy }
670
671 it 'is not diffable' do
672 expect(subject).not_to be_diffable
673 end
674
675 it 'fails with a message not containing any diff' do
676 expect {
677 expect(35).to subject
678 }.to fail_with(a_string_excluding "Diff")
679 end
680 end
681 end
682 end
683
684 describe "expect(...).not_to matcher.and(other_matcher)" do
685 it "is not supported" do
686 expect {
687 expect(3).not_to eq(2).and be > 2
688 }.to raise_error(NotImplementedError, /matcher.and matcher` is not supported/)
689 end
690 end
691
692 describe "expect(...).to matcher.or(other_matcher)" do
693 it_behaves_like "an RSpec value matcher", :valid_value => 3, :invalid_value => 5, :disallows_negation => true do
694 let(:matcher) { eq(3).or eq(4) }
695 end
696
697 context 'when using boolean OR `|` alias' do
698 it_behaves_like "an RSpec value matcher", :valid_value => 3, :invalid_value => 5, :disallows_negation => true do
699 let(:matcher) { eq(3) | eq(4) }
700 end
701 end
702
703 include_examples "making a copy", :or, :dup
704 include_examples "making a copy", :or, :clone
705 it_behaves_like "handles blocks properly", :or
706
707 it 'has a description composed of both matcher descriptions' do
708 matcher = eq(3).or eq(4)
709 expect(3).to matcher
710 expect(matcher.description).to eq("eq 3 or eq 4")
711 end
712
713 context 'when both matchers pass' do
714 it 'passes' do
715 expect("foo").to start_with("f").or end_with("o")
716 end
717 end
718
719 context 'when only the first matcher passes' do
720 it 'passes' do
721 expect("foo").to start_with("f").or end_with("z")
722 end
723 end
724
725 context 'when only the last matcher passes' do
726 it 'passes' do
727 expect("foo").to start_with("a").or end_with("o")
728 end
729 end
730
731 context 'when both matchers fail' do
732 context "when both matchers have multi-line failure messages" do
733 it 'fails with a well formatted message containing both sub-messages' do
734 expect {
735 expect(3).to eq(4).or be >= 8
736 }.to fail_with(dedent <<-EOS)
737 |
738 | expected: 4
739 | got: 3
740 |
741 | (compared using ==)
742 |
743 |...or:
744 |
745 | expected: >= 8
746 | got: 3
747 EOS
748 end
749 end
750
751 context "when both matchers have single-line failure messages" do
752 it 'still fails with a multi-line failure message because it reads better than keeping it on a single line' do
753 expect {
754 expect("foo").to start_with("a").or end_with("z")
755 }.to fail_with(dedent <<-EOS)
756 | expected "foo" to start with "a"
757 |
758 |...or:
759 |
760 | expected "foo" to end with "z"
761 EOS
762 end
763 end
764
765 context "when the first matcher has a multi-line failure message" do
766 it 'fails with a well formatted message containing both sub-messages' do
767 expect {
768 expect("foo").to eq(4).or end_with("z")
769 }.to fail_with(dedent <<-EOS)
770 |
771 | expected: 4
772 | got: "foo"
773 |
774 | (compared using ==)
775 |
776 |...or:
777 |
778 | expected "foo" to end with "z"
779 EOS
780 end
781 end
782
783 context "when the second matcher has a multi-line failure message" do
784 it 'fails with a well formatted message containing both sub-messages' do
785 expect {
786 expect("foo").to end_with("z").or eq(4)
787 }.to fail_with(dedent <<-EOS)
788 | expected "foo" to end with "z"
789 |
790 |...or:
791 |
792 | expected: 4
793 | got: "foo"
794 |
795 | (compared using ==)
796 |
797 EOS
798 end
799 end
800 end
801
802 context "when first matcher is diffable" do
803 subject { include("foo").or eq(35) }
804
805 it "is diffable" do
806 expect(subject).to be_diffable
807 end
808
809 it 'fails with a message containing diff for first matcher' do
810 expected_failure = dedent(<<-EOS)
811 | expected "baz\\nbug" to include "foo"
812 |
813 |...or:
814 |
815 | expected: 35
816 | got: "baz\\nbug"
817 |
818 | (compared using ==)
819 |
820 |Diff for (include "foo"):
821 |@@ #{::Diff::LCS::VERSION.to_f > 1.5 ? "-1 +1,2": "-1,2 +1,3"} @@
822 |-foo
823 |+baz
824 |+bug
825 EOS
826
827 expect {
828 expect(dedent(<<-EOS)).to subject
829 |baz
830 |bug
831 EOS
832 }.to fail_including(expected_failure)
833 end
834 end
835
836 context "when second matcher is diffable" do
837 subject { eq(35).or include("foo") }
838
839 it "is diffable" do
840 expect(subject).to be_diffable
841 end
842
843 it 'fails with a message containing diff for second matcher' do
844 expected_failure = dedent(<<-EOS)
845 | expected: 35
846 | got: "baz\\nbug"
847 |
848 | (compared using ==)
849 |
850 |...or:
851 |
852 | expected "baz\\nbug" to include "foo"
853 |Diff for (include "foo"):
854 |@@ #{::Diff::LCS::VERSION.to_f > 1.5 ? "-1 +1,2": "-1,2 +1,3"} @@
855 |-foo
856 |+baz
857 |+bug
858 EOS
859
860 expect {
861 expect(dedent(<<-EOS)).to subject
862 |baz
863 |bug
864 EOS
865 }.to fail_including(expected_failure)
866 end
867 end
868
869 context "when both matchers are diffable" do
870 subject { include("foo").or include("buzz") }
871
872 it "is diffable" do
873 expect(subject).to be_diffable
874 end
875
876 it 'fails with a message containing diffs for both matcher' do
877 expected_failure = dedent(<<-EOS)
878 | expected "baz\\nbug" to include "foo"
879 |
880 |...or:
881 |
882 | expected "baz\\nbug" to include "buzz"
883 |Diff for (include "foo"):
884 |@@ #{::Diff::LCS::VERSION.to_f > 1.5 ? "-1 +1,2": "-1,2 +1,3"} @@
885 |-foo
886 |+baz
887 |+bug
888 |
889 |Diff for (include "buzz"):
890 |@@ #{::Diff::LCS::VERSION.to_f > 1.5 ? "-1 +1,2": "-1,2 +1,3"} @@
891 |-buzz
892 |+baz
893 |+bug
894 EOS
895
896 expect {
897 expect(dedent(<<-EOS)).to subject
898 |baz
899 |bug
900 EOS
901 }.to fail_including(expected_failure)
902 end
903 end
904
905 context "when both matchers are not diffable" do
906 subject { be_a(String).or be_an(Integer) }
907
908 it "is not diffable" do
909 expect(subject).not_to be_diffable
910 end
911
912 it 'fails with a message containing diffs for both matcher' do
913 expect {
914 expect(true).to subject
915 }.to fail_with(a_string_excluding "Diff")
916 end
917 end
918 end
919
920 context "when chaining many matchers together" do
921 it 'can pass appropriately' do
922 matcher = start_with("f").and end_with("z").or end_with("o")
923 expect("foo").to matcher
924 expect(matcher.description).to eq('start with "f" and end with "z" or end with "o"')
925 end
926
927 it 'fails with complete diffs if its matchers are diffable' do
928 matcher = include("bar").and include("buzz").or include("foo")
929
930 expected_failure =
931 if ::Diff::LCS::VERSION.to_f > 1.5
932 dedent(<<-EOS)
933 | expected "bug\\nsquash" to include "bar"
934 |
935 |...and:
936 |
937 | expected "bug\\nsquash" to include "buzz"
938 |
939 | ...or:
940 |
941 | expected "bug\\nsquash" to include "foo"
942 |Diff for (include "bar"):
943 |@@ -1 +1,2 @@
944 |-bar
945 |+bug
946 |+squash
947 |
948 |Diff for (include "buzz"):
949 |@@ -1 +1,2 @@
950 |-buzz
951 |+bug
952 |+squash
953 |
954 |Diff for (include "foo"):
955 |@@ -1 +1,2 @@
956 |-foo
957 |+bug
958 |+squash
959 EOS
960 else
961 dedent(<<-EOS)
962 | expected "bug\\nsquash" to include "bar"
963 |
964 |...and:
965 |
966 | expected "bug\\nsquash" to include "buzz"
967 |
968 | ...or:
969 |
970 | expected "bug\\nsquash" to include "foo"
971 |Diff for (include "bar"):
972 |@@ -1,2 +1,3 @@
973 |-bar
974 |+bug
975 |+squash
976 |
977 |Diff for (include "buzz"):
978 |@@ -1,2 +1,3 @@
979 |-buzz
980 |+bug
981 |+squash
982 |
983 |Diff for (include "foo"):
984 |@@ -1,2 +1,3 @@
985 |-foo
986 |+bug
987 |+squash
988 EOS
989 end
990
991 expect {
992 expect(dedent(<<-EOS)).to matcher
993 |bug
994 |squash
995 EOS
996 }.to fail do |error|
997 expect(error.message).to include(expected_failure)
998 end
999 end
1000
1001 it 'fails with a complete message' do
1002 expect {
1003 expect(3).to eq(1).and eq(2).and eq(3).and eq(4)
1004 }.to fail_with(dedent <<-EOS)
1005 |
1006 | expected: 1
1007 | got: 3
1008 |
1009 | (compared using ==)
1010 |
1011 |...and:
1012 |
1013 | expected: 2
1014 | got: 3
1015 |
1016 | (compared using ==)
1017 |
1018 | ...and:
1019 |
1020 | expected: 4
1021 | got: 3
1022 |
1023 | (compared using ==)
1024 |
1025 EOS
1026 end
1027 end
1028
1029 describe "expect(...).not_to matcher.or(other_matcher)" do
1030 it "is not supported" do
1031 expect {
1032 expect(3).not_to eq(2).or be > 2
1033 }.to raise_error(NotImplementedError, /matcher.or matcher` is not supported/)
1034 end
1035 end
1036
1037 describe "#expects_call_stack_jump?" do
1038 subject(:expects_call_stack_jump?) { matcher.expects_call_stack_jump? }
1039 let(:matcher) { described_class.new(matcher_1, matcher_2) }
1040 let(:matcher_1) { double("Matcher 1") }
1041 let(:matcher_2) { double("Matcher 2") }
1042
1043 context "when neither matcher expects a call-stack jump" do
1044 let(:matcher_1) { double("Matcher 1", :expects_call_stack_jump? => false) }
1045 let(:matcher_2) { double("Matcher 2", :expects_call_stack_jump? => false) }
1046 it { is_expected.to be_falsey }
1047 end
1048
1049 context "when one of the matchers expects a call-stack jump" do
1050 let(:matcher_1) { double("Matcher 1", :expects_call_stack_jump? => false) }
1051 let(:matcher_2) { double("Matcher 2", :expects_call_stack_jump? => true) }
1052 it { is_expected.to be_truthy }
1053 end
1054
1055 context "when neither matcher _specifies_ that it expects a call-stack jump" do
1056 let(:matcher_1) { Object.new }
1057 let(:matcher_2) { Object.new }
1058 it { is_expected.to be_falsey }
1059 end
1060 end
1061 end
1062 end