File: hash_including_matcher_spec.rb

package info (click to toggle)
ruby-rspec 3.9.0c2e2m1s3-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,612 kB
  • sloc: ruby: 67,456; sh: 1,572; makefile: 98
file content (102 lines) | stat: -rw-r--r-- 3,711 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
module RSpec
  module Mocks
    module ArgumentMatchers
      RSpec.describe HashIncludingMatcher do

        it "describes itself properly" do
          expect(HashIncludingMatcher.new(:a => 1).description).to eq "hash_including(:a=>1)"
        end

        it "describes passed matchers" do
          description = hash_including(:foo => fake_matcher(Object.new)).description

          expect(description).to include(MatcherHelpers.fake_matcher_description)
        end

        describe "passing" do
          it "matches the same hash" do
            expect(hash_including(:a => 1)).to be === {:a => 1}
          end

          it "matches a hash with extra stuff" do
            expect(hash_including(:a => 1)).to be === {:a => 1, :b => 2}
          end

          it "matches against classes inheriting from Hash" do
            expect(hash_including(Class.new(Hash)[:a, 1])).to be === {:a => 1}
          end

          describe "when matching against other matchers" do
            it "matches an int against anything()" do
              expect(hash_including(:a => anything, :b => 2)).to be === {:a => 1, :b => 2}
            end

            it "matches a string against anything()" do
              expect(hash_including(:a => anything, :b => 2)).to be === {:a => "1", :b => 2}
            end

            it 'can match against arbitrary objects that implement #===' do
              expect(hash_including(:a => /foo/)).to be === { :a => "foobar" }
            end
          end

          describe "when passed only keys or keys mixed with key/value pairs" do
            it "matches if the key is present" do
              expect(hash_including(:a)).to be === {:a => 1, :b => 2}
            end

            it "matches if more keys are present" do
              expect(hash_including(:a, :b)).to be === {:a => 1, :b => 2, :c => 3}
            end

            it "matches a string against a given key" do
              expect(hash_including(:a)).to be === {:a => "1", :b => 2}
            end

            it "matches if passed one key and one key/value pair" do
              expect(hash_including(:a, :b => 2)).to be === {:a => 1, :b => 2}
            end

            it "matches if passed many keys and one key/value pair" do
              expect(hash_including(:a, :b, :c => 3)).to be === {:a => 1, :b => 2, :c => 3, :d => 4}
            end

            it "matches if passed many keys and many key/value pairs" do
              expect(hash_including(:a, :b, :c => 3, :e => 5)).to be === {:a => 1, :b => 2, :c => 3, :d => 4, :e => 5}
            end
          end
        end

        describe "failing" do
          it "does not match a non-hash" do
            expect(hash_including(:a => 1)).not_to be === 1
          end

          it "does not match a hash with a missing key" do
            expect(hash_including(:a => 1)).not_to be === { :b => 2 }
          end

          it "does not match a hash with a missing key" do
            expect(hash_including(:a)).not_to be === { :b => 2 }
          end

          it "does not match an empty hash with a given key" do
            expect(hash_including(:a)).not_to be === {}
          end

          it "does not match a hash with a missing key when one pair is matching" do
            expect(hash_including(:a, :b => 2)).not_to be === { :b => 2 }
          end

          it "does not match a hash with an incorrect value" do
            expect(hash_including(:a => 1, :b => 2)).not_to be === { :a => 1, :b => 3 }
          end

          it "does not match when values are nil but keys are different" do
            expect(hash_including(:a => nil)).not_to be === { :b => nil }
          end
        end
      end
    end
  end
end