rspec-expectations 2.14.5 → 2.99.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
@@ -25,14 +25,14 @@ describe "Matchers should be able to generate their own descriptions" do
25
25
  expect(RSpec::Matchers.generated_description).to eq "should not be empty"
26
26
  end
27
27
 
28
- it "expect(...).to be true" do
29
- expect(true).to be_true
30
- expect(RSpec::Matchers.generated_description).to eq "should be true"
28
+ it "expect(...).to be truthy" do
29
+ expect(true).to be_truthy
30
+ expect(RSpec::Matchers.generated_description).to eq "should be truthy"
31
31
  end
32
32
 
33
- it "expect(...).to be false" do
34
- expect(false).to be_false
35
- expect(RSpec::Matchers.generated_description).to eq "should be false"
33
+ it "expect(...).to be falsey" do
34
+ expect(false).to be_falsey
35
+ expect(RSpec::Matchers.generated_description).to eq "should be falsey"
36
36
  end
37
37
 
38
38
  it "expect(...).to be nil" do
@@ -92,19 +92,23 @@ describe "Matchers should be able to generate their own descriptions" do
92
92
  expect(RSpec::Matchers.generated_description).to eq 'should have taste for "wine", "cheese"'
93
93
  end
94
94
 
95
- it "expect(...).to have n items" do
96
- expect(team).to have(3).players
97
- expect(RSpec::Matchers.generated_description).to eq "should have 3 players"
98
- end
95
+ context "the deprecated collection cardinality matchers" do
96
+ before { allow_deprecation }
99
97
 
100
- it "expect(...).to have at least n items" do
101
- expect(team).to have_at_least(2).players
102
- expect(RSpec::Matchers.generated_description).to eq "should have at least 2 players"
103
- end
98
+ it "expect(...).to have n items" do
99
+ expect(team).to have(3).players
100
+ expect(RSpec::Matchers.generated_description).to eq "should have 3 players"
101
+ end
102
+
103
+ it "expect(...).to have at least n items" do
104
+ expect(team).to have_at_least(2).players
105
+ expect(RSpec::Matchers.generated_description).to eq "should have at least 2 players"
106
+ end
104
107
 
105
- it "expect(...).to have at most n items" do
106
- expect(team).to have_at_most(4).players
107
- expect(RSpec::Matchers.generated_description).to eq "should have at most 4 players"
108
+ it "expect(...).to have at most n items" do
109
+ expect(team).to have_at_most(4).players
110
+ expect(RSpec::Matchers.generated_description).to eq "should have at most 4 players"
111
+ end
108
112
  end
109
113
 
110
114
  it "expect(...).to include(x)" do
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ module RSpec
4
+ module Matchers
5
+ describe DifferentiateBlockMethodTypes do
6
+ let(:differentiator) do
7
+ DifferentiateBlockMethodTypes.new do
8
+ def some_instance_method_1; end
9
+ def self.some_singleton_method_1; end
10
+ define_method(:some_instance_method_2) { }
11
+
12
+ if RUBY_VERSION.to_f > 1.8
13
+ define_singleton_method(:some_singleton_method_2) { }
14
+ else
15
+ def self.some_singleton_method_2; end
16
+ end
17
+ end
18
+ end
19
+
20
+ it 'differentiates singleton method defs from instance method defs' do
21
+ expect(differentiator.instance_methods).to eq([:some_instance_method_1, :some_instance_method_2])
22
+ expect(differentiator.singleton_methods).to eq([:some_singleton_method_1, :some_singleton_method_2])
23
+ end
24
+
25
+ it 'passes the given args through to the block' do
26
+ expect { |b|
27
+ DifferentiateBlockMethodTypes.new(1, 2, &b)
28
+ }.to yield_with_args(1, 2)
29
+ end
30
+
31
+ it 'ignores unrecognized DSL methods called in the block' do
32
+ expect {
33
+ DifferentiateBlockMethodTypes.new { foo.bar; some_dsl { nested } }
34
+ }.not_to raise_error
35
+ end
36
+ end
37
+ end
38
+ end
39
+
@@ -28,7 +28,7 @@ module RSpec
28
28
  end.new
29
29
 
30
30
  expect(actual).to eq :anything # to trigger the matches? method
31
- expect(called).to be_true
31
+ expect(called).to be_truthy
32
32
  end
33
33
 
34
34
  it "describes itself" do
@@ -24,32 +24,6 @@ module RSpec
24
24
  expect(matcher.description).to eq "equal 1"
25
25
  end
26
26
 
27
- context "when the expected object is falsey in conditinal semantics" do
28
- it "describes itself with the expected object" do
29
- matcher = equal(nil)
30
- matcher.matches?(nil)
31
- expect(matcher.description).to eq "equal nil"
32
- end
33
- end
34
-
35
- context "when the expected object's #equal? always returns true" do
36
- let(:strange_string) do
37
- string = "foo"
38
-
39
- def string.equal?(other)
40
- true
41
- end
42
-
43
- string
44
- end
45
-
46
- it "describes itself with the expected object" do
47
- matcher = equal(strange_string)
48
- matcher.matches?(strange_string)
49
- expect(matcher.description).to eq 'equal "foo"'
50
- end
51
- end
52
-
53
27
  it "suggests the `eq` matcher on failure" do
54
28
  expected, actual = "1", "1"
55
29
  expect {
@@ -10,7 +10,10 @@ describe "have matcher" do
10
10
  end
11
11
  end
12
12
 
13
- before(:each) { stub_const("ActiveSupport::Inflector", inflector) }
13
+ before(:each) do
14
+ stub_const("ActiveSupport::Inflector", inflector)
15
+ allow_deprecation
16
+ end
14
17
 
15
18
  def create_collection_owner_with(n)
16
19
  owner = RSpec::Expectations::Helper::CollectionOwner.new
@@ -452,4 +455,301 @@ EOF
452
455
  end
453
456
  end
454
457
  end
458
+
459
+ context "deprecations for the have matcher" do
460
+ it "has the correct call site in the deprecation message" do
461
+ expect_deprecation_with_call_site(__FILE__, __LINE__ + 1)
462
+ expect([1, 2, 3]).to have(3).items
463
+ end
464
+
465
+ context "when the target is a collection" do
466
+ it "prints a specific message for the positive expectation format" do
467
+ expectation_expression = "expect(collection).to have(3).items"
468
+
469
+ message = "the rspec-collection_matchers gem " +
470
+ "or replace your expectation with something like " +
471
+ "`expect(collection.size).to eq(3)`"
472
+
473
+ expect(RSpec).to receive(:deprecate).with("`#{expectation_expression}`", :replacement => message)
474
+
475
+ expect([1, 2, 3]).to have(3).items
476
+ end
477
+
478
+ it "prints a specific message for the negative expectation format" do
479
+ expectation_expression = "expect(collection).not_to have(4).items"
480
+
481
+ message = "the rspec-collection_matchers gem " +
482
+ "or replace your expectation with something like " +
483
+ "`expect(collection.size).to_not eq(4)`"
484
+
485
+ expect(RSpec).to receive(:deprecate).with("`#{expectation_expression}`", :replacement => message)
486
+
487
+ expect([1, 2, 3]).to_not have(4).items
488
+ end
489
+ end
490
+
491
+ context "when the target owns a collection" do
492
+ class self::BagOfWords
493
+ attr_reader :words
494
+
495
+ def initialize(words)
496
+ @words = words
497
+ end
498
+ end
499
+
500
+ it "prints a specific message for the positive expectation format" do
501
+ expectation_expression = "expect(collection_owner).to have(3).words"
502
+
503
+ message = "the rspec-collection_matchers gem " +
504
+ "or replace your expectation with something like " +
505
+ "`expect(collection_owner.words.size).to eq(3)`"
506
+
507
+ expect(RSpec).to receive(:deprecate).with("`#{expectation_expression}`", :replacement => message)
508
+
509
+ target = BagOfWords.new(%w[foo bar baz])
510
+ expect(target).to have(3).words
511
+ end
512
+
513
+ it "prints a specific message for the negative expectation format" do
514
+ expectation_expression = "expect(collection_owner).not_to have(4).words"
515
+
516
+ message = "the rspec-collection_matchers gem " +
517
+ "or replace your expectation with something like " +
518
+ "`expect(collection_owner.words.size).to_not eq(4)`"
519
+
520
+ expect(RSpec).to receive(:deprecate).with("`#{expectation_expression}`", :replacement => message)
521
+
522
+ target = BagOfWords.new(%w[foo bar baz])
523
+ expect(target).to_not have(4).words
524
+ end
525
+ end
526
+
527
+ context "when the target is an enumerator" do
528
+ it "prints a specific message for the positive expectation format" do
529
+ target = %w[a b c].to_enum(:each)
530
+
531
+ expectation_expression = "expect(collection).to have(3).letters"
532
+
533
+ message = "the rspec-collection_matchers gem " +
534
+ "or replace your expectation with something like " +
535
+ "`expect(collection.count).to eq(3)`"
536
+
537
+ expect(RSpec).to receive(:deprecate).with("`#{expectation_expression}`", :replacement => message)
538
+
539
+ expect(target).to have(3).letters
540
+ end
541
+
542
+ it "prints a specific message for the negative expectation format" do
543
+ target = %w[a b c].to_enum(:each)
544
+
545
+ expectation_expression = "expect(collection).not_to have(4).letters"
546
+
547
+ message = "the rspec-collection_matchers gem " +
548
+ "or replace your expectation with something like " +
549
+ "`expect(collection.count).to_not eq(4)`"
550
+
551
+ expect(RSpec).to receive(:deprecate).with("`#{expectation_expression}`", :replacement => message)
552
+
553
+ expect(target).to_not have(4).letters
554
+ end
555
+ end
556
+ end
557
+
558
+ context "deprecations for the have_at_most matcher" do
559
+ it "has the correct call site in the deprecation message" do
560
+ expect_deprecation_with_call_site(__FILE__, __LINE__ + 1)
561
+ expect([1, 2, 3]).to have_at_most(3).items
562
+ end
563
+
564
+ context "when the target is a collection" do
565
+ it "prints a specific message for the positive expectation format" do
566
+ expectation_expression = "expect(collection).to have_at_most(3).items"
567
+
568
+ message = "the rspec-collection_matchers gem " +
569
+ "or replace your expectation with something like " +
570
+ "`expect(collection.size).to be <= 3`"
571
+
572
+ expect(RSpec).to receive(:deprecate).with("`#{expectation_expression}`", :replacement => message)
573
+
574
+ expect([1, 2, 3]).to have_at_most(3).items
575
+ end
576
+
577
+ it "prints a specific message for the negative expectation format" do
578
+ expectation_expression = "expect(collection).not_to have_at_most(2).items"
579
+
580
+ message = "the rspec-collection_matchers gem " +
581
+ "or replace your expectation with something like " +
582
+ "`expect(collection.size).to be > 2`"
583
+
584
+ expect(RSpec).to receive(:deprecate).with("`#{expectation_expression}`", :replacement => message)
585
+
586
+ expect([1, 2, 3]).to_not have_at_most(2).items
587
+ end
588
+ end
589
+
590
+ context "when the target owns a collection" do
591
+ class self::BagOfWords
592
+ attr_reader :words
593
+
594
+ def initialize(words)
595
+ @words = words
596
+ end
597
+ end
598
+
599
+ it "prints a specific message for the positive expectation format" do
600
+ expectation_expression = "expect(collection_owner).to have_at_most(3).words"
601
+
602
+ message = "the rspec-collection_matchers gem " +
603
+ "or replace your expectation with something like " +
604
+ "`expect(collection_owner.words.size).to be <= 3`"
605
+
606
+ expect(RSpec).to receive(:deprecate).with("`#{expectation_expression}`", :replacement => message)
607
+
608
+ target = BagOfWords.new(%w[foo bar baz])
609
+ expect(target).to have_at_most(3).words
610
+ end
611
+
612
+ it "prints a specific message for the negative expectation format" do
613
+ expectation_expression = "expect(collection_owner).not_to have_at_most(2).words"
614
+
615
+ message = "the rspec-collection_matchers gem " +
616
+ "or replace your expectation with something like " +
617
+ "`expect(collection_owner.words.size).to be > 2`"
618
+
619
+ expect(RSpec).to receive(:deprecate).with("`#{expectation_expression}`", :replacement => message)
620
+
621
+ target = BagOfWords.new(%w[foo bar baz])
622
+ expect(target).to_not have_at_most(2).words
623
+ end
624
+ end
625
+
626
+ context "when the target is an enumerator" do
627
+ it "prints a specific message for the positive expectation format" do
628
+ target = %w[a b c].to_enum(:each)
629
+
630
+ expectation_expression = "expect(collection).to have_at_most(3).letters"
631
+
632
+ message = "the rspec-collection_matchers gem " +
633
+ "or replace your expectation with something like " +
634
+ "`expect(collection.count).to be <= 3`"
635
+
636
+ expect(RSpec).to receive(:deprecate).with("`#{expectation_expression}`", :replacement => message)
637
+
638
+ expect(target).to have_at_most(3).letters
639
+ end
640
+
641
+ it "prints a specific message for the negative expectation format" do
642
+ target = %w[a b c].to_enum(:each)
643
+
644
+ expectation_expression = "expect(collection).not_to have_at_most(2).letters"
645
+
646
+ message = "the rspec-collection_matchers gem " +
647
+ "or replace your expectation with something like " +
648
+ "`expect(collection.count).to be > 2`"
649
+
650
+ expect(RSpec).to receive(:deprecate).with("`#{expectation_expression}`", :replacement => message)
651
+
652
+ expect(target).to_not have_at_most(2).letters
653
+ end
654
+ end
655
+ end
656
+
657
+ context "deprecations for the have_at_least matcher" do
658
+ it "has the correct call site in the deprecation message" do
659
+ expect_deprecation_with_call_site(__FILE__, __LINE__ + 1)
660
+ expect([1, 2, 3]).to have_at_least(3).items
661
+ end
662
+
663
+ context "when the target is a collection" do
664
+ it "prints a specific message for the positive expectation format" do
665
+ expectation_expression = "expect(collection).to have_at_least(3).items"
666
+
667
+ message = "the rspec-collection_matchers gem " +
668
+ "or replace your expectation with something like " +
669
+ "`expect(collection.size).to be >= 3`"
670
+
671
+ expect(RSpec).to receive(:deprecate).with("`#{expectation_expression}`", :replacement => message)
672
+
673
+ expect([1, 2, 3]).to have_at_least(3).items
674
+ end
675
+
676
+ it "prints a specific message for the negative expectation format" do
677
+ expectation_expression = "expect(collection).not_to have_at_least(4).items"
678
+
679
+ message = "the rspec-collection_matchers gem " +
680
+ "or replace your expectation with something like " +
681
+ "`expect(collection.size).to be < 4`"
682
+
683
+ expect(RSpec).to receive(:deprecate).with("`#{expectation_expression}`", :replacement => message)
684
+
685
+ expect([1, 2, 3]).to_not have_at_least(4).items
686
+ end
687
+ end
688
+
689
+ context "when the target owns a collection" do
690
+ class BagOfWords
691
+ attr_reader :words
692
+
693
+ def initialize(words)
694
+ @words = words
695
+ end
696
+ end
697
+
698
+ it "prints a specific message for the positive expectation format" do
699
+ expectation_expression = "expect(collection_owner).to have_at_least(3).words"
700
+
701
+ message = "the rspec-collection_matchers gem " +
702
+ "or replace your expectation with something like " +
703
+ "`expect(collection_owner.words.size).to be >= 3`"
704
+
705
+ expect(RSpec).to receive(:deprecate).with("`#{expectation_expression}`", :replacement => message)
706
+
707
+ target = BagOfWords.new(%w[foo bar baz])
708
+ expect(target).to have_at_least(3).words
709
+ end
710
+
711
+ it "prints a specific message for the negative expectation format" do
712
+ expectation_expression = "expect(collection_owner).not_to have_at_least(4).words"
713
+
714
+ message = "the rspec-collection_matchers gem " +
715
+ "or replace your expectation with something like " +
716
+ "`expect(collection_owner.words.size).to be < 4`"
717
+
718
+ expect(RSpec).to receive(:deprecate).with("`#{expectation_expression}`", :replacement => message)
719
+
720
+ target = BagOfWords.new(%w[foo bar baz])
721
+ expect(target).to_not have_at_least(4).words
722
+ end
723
+ end
724
+
725
+ context "when the target is an enumerator" do
726
+ it "prints a specific message for the positive expectation format" do
727
+ target = %w[a b c].to_enum(:each)
728
+
729
+ expectation_expression = "expect(collection).to have_at_least(3).letters"
730
+
731
+ message = "the rspec-collection_matchers gem " +
732
+ "or replace your expectation with something like " +
733
+ "`expect(collection.count).to be >= 3`"
734
+
735
+ expect(RSpec).to receive(:deprecate).with("`#{expectation_expression}`", :replacement => message)
736
+
737
+ expect(target).to have_at_least(3).letters
738
+ end
739
+
740
+ it "prints a specific message for the negative expectation format" do
741
+ target = %w[a b c].to_enum(:each)
742
+
743
+ expectation_expression = "expect(collection).not_to have_at_least(4).letters"
744
+
745
+ message = "the rspec-collection_matchers gem " +
746
+ "or replace your expectation with something like " +
747
+ "`expect(collection.count).to be < 4`"
748
+
749
+ expect(RSpec).to receive(:deprecate).with("`#{expectation_expression}`", :replacement => message)
750
+
751
+ expect(target).to_not have_at_least(4).letters
752
+ end
753
+ end
754
+ end
455
755
  end