File: has_spec.rb

package info (click to toggle)
ruby-rspec 3.13.0c0e0m0s1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,856 kB
  • sloc: ruby: 70,868; sh: 1,423; makefile: 99
file content (211 lines) | stat: -rw-r--r-- 6,460 bytes parent folder | download | duplicates (2)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
RSpec.describe "expect(...).to have_sym(*args)" do
  it_behaves_like "an RSpec value matcher", :valid_value => { :a => 1 },
                                            :invalid_value => {} do
    let(:matcher) { have_key(:a) }
  end

  it "passes if #has_sym?(*args) returns true" do
    expect({ :a => "A" }).to have_key(:a)
  end

  if RSpec::Support::RubyFeatures.required_kw_args_supported?
    binding.eval(<<-CODE, __FILE__, __LINE__)
    it 'supports the use of required keyword arguments' do
      thing = Class.new { def has_keyword?(keyword:); keyword == 'a'; end }
      expect(thing.new).to have_keyword(keyword: 'a')
    end
    CODE
  end

  if RSpec::Support::RubyFeatures.kw_args_supported?
    binding.eval(<<-CODE, __FILE__, __LINE__)
    it 'supports the use of optional keyword arguments' do
      thing = Class.new { def has_keyword?(keyword: 'b'); keyword == 'a'; end }
      expect(thing.new).to have_keyword(keyword: 'a')
    end
    CODE
  end

  it "fails if #has_sym?(*args) returns false" do
    expect {
      expect({ :b => "B" }).to have_key(:a)
    }.to fail_with('expected `{:b=>"B"}.has_key?(:a)` to return true, got false')
  end

  obj_with_block_method = Object.new
  def obj_with_block_method.has_some_stuff?; yield; end

  it 'forwards the given `{ }` block on to the `has_xyz?` method' do
    expect(obj_with_block_method).to have_some_stuff { true }
    expect(obj_with_block_method).to_not have_some_stuff { false }
  end

  it 'forwards the given `do..end` block on to the `has_xyz?` method' do
    expect(obj_with_block_method).to have_some_stuff do
      true
    end

    expect(obj_with_block_method).to_not have_some_stuff do
      false
    end
  end

  it 'favors a curly brace block over a do...end one since it binds to the matcher method' do
    expect(obj_with_block_method).to have_some_stuff { true } do
      false
    end

    expect(obj_with_block_method).not_to have_some_stuff { false } do
      true
    end
  end

  it 'does not include any args in the failure message if no args were given to the matcher' do
    o = Object.new
    def o.has_some_stuff?; false; end
    expect {
      expect(o).to have_some_stuff
    }.to fail_with("expected `#{o.inspect}.has_some_stuff?` to return true, got false")
  end

  it 'includes multiple args in the failure message if multiple args were given to the matcher' do
    o = Object.new
    def o.has_some_stuff?(*_); false; end
    expect {
      expect(o).to have_some_stuff(:a, 7, "foo")
    }.to fail_with(%Q{expected `#{o.inspect}.has_some_stuff?(:a, 7, "foo")` to return true, got false})
  end

  it "fails if #has_sym?(*args) returns nil" do
    klass = Class.new do
      def has_foo?
      end
    end
    expect {
      expect(klass.new).to have_foo
    }.to fail_with(/expected `.*\.has_foo\?` to return true, got nil/)
  end

  it 'fails if #has_sym?(*args) is private' do
    klass = Class.new do
      private
      def has_foo?
        true
      end
    end
    expect { expect(klass.new).to have_foo }.to fail_with(/private/)
  end

  it "fails if target does not respond to #has_sym?" do
    expect {
      expect(Object.new).to have_key(:a)
    }.to fail_including('to respond to `has_key?`')
  end

  it "reraises an exception thrown in #has_sym?(*args)" do
    o = Object.new
    def o.has_sym?(*_args)
      raise "Funky exception"
    end
    expect {
      expect(o).to have_sym(:foo)
    }.to raise_error("Funky exception")
  end

  it 'allows composable aliases to be defined' do
    RSpec::Matchers.alias_matcher :an_object_having_sym, :have_sym
    o = Object.new
    def o.has_sym?(sym); sym == :foo; end

    expect(o).to an_object_having_sym(:foo)
    expect(o).not_to an_object_having_sym(:bar)

    expect(an_object_having_sym(:foo).description).to eq("an object having sym :foo")
  end

  it 'composes gracefully' do
    RSpec::Matchers.alias_matcher :an_object_having_foo, :have_foo

    expect([
      double,
      double(:has_foo? => false),
      double(:has_foo? => true),
    ]).to include an_object_having_foo
  end
end

RSpec.describe "expect(...).not_to have_sym(*args)" do
  it "passes if #has_sym?(*args) returns false" do
    expect({ :a => "A" }).not_to have_key(:b)
  end

  context "when strict_predicate_matchers is set to true" do
    it "fails when #has_sym? returns nil" do
      actual = double("actual", :has_foo? => nil)
      expect {
        expect(actual).not_to have_foo
      }.to fail_with("expected `#{actual.inspect}.has_foo?` to return false, got nil")
    end
  end

  context "when strict_predicate_matchers is set to false" do
    around do |example|
      RSpec::Expectations.configuration.strict_predicate_matchers = false
      example.run
      RSpec::Expectations.configuration.strict_predicate_matchers = true
    end

    it "passes if #has_sym?(*args) returns nil" do
      actual = double("actual", :has_foo? => nil)
      expect(actual).not_to have_foo
    end
  end

  it "fails if #has_sym?(*args) returns true" do
    expect {
      expect({ :a => "A" }).not_to have_key(:a)
    }.to fail_with('expected `{:a=>"A"}.has_key?(:a)` to return false, got true')
  end

  it "fails if target does not respond to #has_sym?" do
    expect {
      expect(Object.new).not_to have_key(:a)
    }.to fail_including('to respond to `has_key?`')
  end

  it "reraises an exception thrown in #has_sym?(*args)" do
    o = Object.new
    def o.has_sym?(*_args)
      raise "Funky exception"
    end
    expect {
      expect(o).not_to have_sym(:foo)
    }.to raise_error("Funky exception")
  end

  it 'does not include any args in the failure message if no args were given to the matcher' do
    o = Object.new
    def o.has_some_stuff?; true; end
    expect {
      expect(o).not_to have_some_stuff
    }.to fail_with("expected `#{o.inspect}.has_some_stuff?` to return false, got true")
  end

  it 'includes multiple args in the failure message if multiple args were given to the matcher' do
    o = Object.new
    def o.has_some_stuff?(*_); true; end
    expect {
      expect(o).not_to have_some_stuff(:a, 7, "foo")
    }.to fail_with(%Q{expected `#{o.inspect}.has_some_stuff?(:a, 7, "foo")` to return false, got true})
  end
end

RSpec.describe "has" do
  it "works when the target implements #send" do
    o = { :a => "A" }
    def o.send(*_args); raise "DOH! Library developers shouldn't use #send!" end
    expect {
      expect(o).to have_key(:a)
    }.not_to raise_error
  end
end