File: argument_matchers_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 (459 lines) | stat: -rw-r--r-- 17,286 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
module RSpec
  module Mocks
    RSpec.describe "argument matchers matching" do
      let(:a_double) { double }

      after(:each, :reset => true) do
        reset a_double
      end

      describe "boolean" do
        it "accepts true as boolean" do
          expect(a_double).to receive(:random_call).with(boolean)
          a_double.random_call(true)
        end

        it "accepts false as boolean" do
          expect(a_double).to receive(:random_call).with(boolean)
          a_double.random_call(false)
        end

        it "rejects non boolean", :reset => true do
          expect(a_double).to receive(:random_call).with(boolean)
          expect {
            a_double.random_call("false")
          }.to fail_including "expected: (boolean)"
        end
      end

      describe "kind_of" do
        it "accepts fixnum as kind_of(Numeric)" do
          expect(a_double).to receive(:random_call).with(kind_of(Numeric))
          a_double.random_call(1)
        end

        it "accepts float as kind_of(Numeric)" do
          expect(a_double).to receive(:random_call).with(kind_of(Numeric))
          a_double.random_call(1.5)
        end

        it "handles non matching kinds nicely", :reset => true do
          expect(a_double).to receive(:random_call).with(kind_of(Numeric))
          expect {
            a_double.random_call(true)
          }.to fail_including "expected: (kind of Numeric)"
        end

        it "matches arguments that have defined `kind_of?` to return true" do
          fix_num = double(:kind_of? => true)
          expect(a_double).to receive(:random_call).with(kind_of(Numeric))
          a_double.random_call(fix_num)
        end

        it "handles a class thats overridden ===" do
          allow(Numeric).to receive(:===) { false }
          fix_num = double(:kind_of? => true)
          expect(a_double).to receive(:random_call).with(kind_of(Numeric))
          a_double.random_call(fix_num)
        end
      end

      describe "instance_of" do
        it "accepts float as instance_of(Float)" do
          expect(a_double).to receive(:random_call).with(instance_of(Float))
          a_double.random_call(1.1)
        end

        it "does NOT accept float as instance_of(Numeric)" do
          expect(a_double).not_to receive(:random_call).with(instance_of(Numeric))
          a_double.random_call(1.1)
        end

        it "does NOT accept integer as instance_of(Numeric)" do
          expect(a_double).not_to receive(:random_call).with(instance_of(Numeric))
          a_double.random_call(1)
        end

        it "rejects non numeric", :reset => true do
          expect(a_double).to receive(:random_call).with(an_instance_of(Numeric))
          expect { a_double.random_call("1") }.to fail
        end

        it "rejects non string", :reset => true do
          expect(a_double).to receive(:random_call).with(an_instance_of(String))
          expect { a_double.random_call(123) }.to fail
        end

        it "handles non matching instances nicely", :reset => true do
          expect(a_double).to receive(:random_call).with(instance_of(Numeric))
          expect {
            a_double.random_call(1.5)
          }.to fail_including "expected: (an_instance_of(Numeric))"
        end
      end

      describe "anything" do
        it "accepts string as anything" do
          expect(a_double).to receive(:random_call).with("a", anything, "c")
          a_double.random_call("a", "whatever", "c")
        end

        it "doesn't accept no arguments" do
          expect(a_double).to_not receive(:random_call).with(anything)
          a_double.random_call
        end

        it "handles non matching instances nicely", :reset => true do
          expect(a_double).to receive(:random_call).with(anything)
          expect { a_double.random_call }.to fail_including "expected: (anything)"
        end
      end

      describe "duck_type" do
        it "matches duck type with one method" do
          expect(a_double).to receive(:random_call).with(duck_type(:length))
          a_double.random_call([])
        end

        it "matches duck type with two methods" do
          expect(a_double).to receive(:random_call).with(duck_type(:abs, :div))
          a_double.random_call(1)
        end

        it "rejects goose when expecting a duck", :reset => true do
          expect(a_double).to receive(:random_call).with(duck_type(:abs, :div))
          expect {
            a_double.random_call("I don't respond to :abs or :div")
          }.to fail_including "expected: (duck_type(:abs, :div))"
        end
      end

      describe "any_args" do
        context "as the only arg passed to `with`" do
          before { expect(a_double).to receive(:random_call).with(any_args) }

          it "matches no args" do
            a_double.random_call
          end

          it "matches one arg" do
            a_double.random_call("a string")
          end

          it "matches many args" do
            a_double.random_call("a string", :other, 3)
          end
        end

        context "as the last of three args" do
          before { expect(a_double).to receive(:random_call).with(1, /foo/, any_args) }

          it "matches a call of two args when it matches the first two explicit args" do
            a_double.random_call(1, "food")
          end

          it "matches a call of three args when it matches the first two explicit args" do
            a_double.random_call(1, "food", :more)
          end

          it "matches a call of four args when it matches the first two explicit args" do
            a_double.random_call(1, "food", :more, :args)
          end

          it "does not match a call where the first two args do not match", :reset => true do
            expect { a_double.random_call(1, "bar", 2, 3) }.to fail_including "expected: (1, /foo/, *(any args))"
          end

          it "does not match a call of no args", :reset => true do
            expect { a_double.random_call }.to fail_including "expected: (1, /foo/, *(any args))"
          end
        end

        context "as the first of three args" do
          before { expect(a_double).to receive(:random_call).with(any_args, 1, /foo/) }

          it "matches a call of two args when it matches the last two explicit args" do
            a_double.random_call(1, "food")
          end

          it "matches a call of three args when it matches the last two explicit args" do
            a_double.random_call(nil, 1, "food")
          end

          it "matches a call of four args when it matches the last two explicit args" do
            a_double.random_call(:some, :args, 1, "food")
          end

          it "does not match a call where the last two args do not match", :reset => true do
            expect { a_double.random_call(1, "bar", 2, 3) }.to fail_including "expected: (*(any args), 1, /foo/)"
          end

          it "does not match a call of no args", :reset => true do
            expect { a_double.random_call }.to fail_including "expected: (*(any args), 1, /foo/)"
          end
        end

        context "as the middle of three args" do
          before { expect(a_double).to receive(:random_call).with(1, any_args, /foo/) }

          it "matches a call of two args when it matches the first and last args" do
            a_double.random_call(1, "food")
          end

          it "matches a call of three args when it matches the first and last args" do
            a_double.random_call(1, nil, "food")
          end

          it "matches a call of four args when it matches the first and last args" do
            a_double.random_call(1, :some, :args, "food")
          end

          it "does not match a call where the first and last args do not match", :reset => true do
            expect { a_double.random_call(nil, "bar", 2, 3) }.to fail_including "expected: (1, *(any args), /foo/)"
          end

          it "does not match a call of no args", :reset => true do
            expect { a_double.random_call }.to fail_including "expected: (1, *(any args), /foo/)"
          end
        end

        context "when passed twice" do
          it 'immediately signals that this is invalid', :reset => true do
            expect {
              expect(a_double).to receive(:random_call).with(any_args, 1, any_args)
            }.to raise_error(ArgumentError, /any_args/)
          end
        end
      end

      describe "no_args" do
        it "matches no args against no_args" do
          expect(a_double).to receive(:random_call).with(no_args)
          a_double.random_call
        end

        it "fails no_args with one arg", :reset => true do
          expect(a_double).to receive(:msg).with(no_args)
          expect { a_double.msg(37) }.to fail_including "expected: (no args)"
        end

        context "when passed with other arguments" do
          it 'immediately signals that this is invalid', :reset => true do
            expect {
              expect(a_double).to receive(:random_call).with(no_args, 3)
            }.to raise_error(ArgumentError, /no_args/)
          end
        end
      end

      describe "hash_including" do
        it "matches hash with hash_including same hash" do
          expect(a_double).to receive(:random_call).with(hash_including(:a => 1))
          a_double.random_call(:a => 1)
        end

        it "fails hash_including with missing key", :reset => true do
          expect(a_double).to receive(:random_call).with(hash_including(:a => 1))
          expect {
            a_double.random_call(:a => 2)
          }.to fail_including "expected: (hash_including(:a=>1))"
        end
      end

      describe "hash_excluding" do
        it "matches hash with hash_excluding same hash" do
          expect(a_double).to receive(:random_call).with(hash_excluding(:a => 1))
          a_double.random_call(:a => 2)
        end

        it "handles non matching instances nicely", :reset => true do
          expect(a_double).to receive(:random_call).with(hash_excluding(:a => 1))
          expect {
            a_double.random_call(:a => 1)
          }.to fail_including "expected: (hash_not_including(:a=>1))"
        end
      end

      describe "array_including" do
        it "matches array with array_including same array" do
          expect(a_double).to receive(:random_call).with(array_including(1, 2))
          a_double.random_call([1, 2])
        end

        it "fails array_including when args aren't array", :reset => true do
          expect(a_double).to receive(:msg).with(array_including(1, 2, 3))
          expect {
            a_double.msg(1, 2, 3)
          }.to fail_including "expected: (array_including(1, 2, 3))"
        end

        it "fails array_including when arg doesn't contain all elements", :reset => true do
          expect(a_double).to receive(:msg).with(array_including(1, 2, 3))
          expect {
            a_double.msg([1, 2])
          }.to fail_including "expected: (array_including(1, 2, 3))"
        end
      end

      context "handling arbitary matchers" do
        it "matches any arbitrary object using #===" do
          matcher = double
          expect(matcher).to receive(:===).with(4).and_return(true)

          expect(a_double).to receive(:foo).with(matcher)
          a_double.foo(4)
        end

        it "matches against a Matcher", :reset => true do
          expect(a_double).to receive(:msg).with(equal(3))
          # This spec is generating warnings on 1.8.7, not sure why so
          # this does with_isolated_stderr to kill them. @samphippen 3rd Jan 2013.
          expect { with_isolated_stderr { a_double.msg(37) } }.to fail_including "expected: (equal 3)"
        end

        it "fails when given an arbitrary object that returns false from #===", :reset => true do
          matcher = double
          expect(matcher).to receive(:===).with(4).at_least(:once).and_return(false)

          expect(a_double).to receive(:foo).with(matcher)

          expect { a_double.foo(4) }.to fail
        end
      end

      context "handling objects with a wrong definition of `==` that raises errors for other types" do
        Color = Struct.new(:r, :g, :b) do
          def ==(other)
            other.r == r && other.g == g && other.b == b
          end
        end

        before(:context) do
          expect { Color.new(0, 0, 0) == Object.new }.to raise_error(NoMethodError)
        end

        it 'matches against an equal instance of the same type' do
          expect(a_double).to receive(:random_call).with(Color.new(0, 0, 0))
          a_double.random_call(Color.new(0, 0, 0))
        end

        it 'fails when matched against an unequal instance of the same class', :reset do
          expect(a_double).to receive(:random_call).with(Color.new(0, 0, 0))
          expect { a_double.random_call(Color.new(0, 1, 0)) }.to fail
        end

        it 'can match multiple instances of the type against multiple equal instances of the type' do
          expect(a_double).to receive(:random_call).with(
            Color.new(0, 0, 0),
            Color.new(0, 1, 0)
          )

          a_double.random_call(
            Color.new(0, 0, 0),
            Color.new(0, 1, 0)
          )
        end
      end

      context "handling non-matcher arguments" do
        it "matches string against regexp" do
          expect(a_double).to receive(:random_call).with(/bcd/)
          a_double.random_call("abcde")
        end

        it "matches regexp against regexp" do
          expect(a_double).to receive(:random_call).with(/bcd/)
          a_double.random_call(/bcd/)
        end

        it "fails if regexp does not match submitted string", :reset => true do
          expect(a_double).to receive(:random_call).with(/bcd/)
          expect { a_double.random_call("abc") }.to fail
        end

        it "fails if regexp does not match submitted regexp", :reset => true do
          expect(a_double).to receive(:random_call).with(/bcd/)
          expect { a_double.random_call(/bcde/) }.to fail
        end

        it "matches against a hash submitted and received by value" do
          expect(a_double).to receive(:random_call).with(:a => "a", :b => "b")
          a_double.random_call(:a => "a", :b => "b")
        end

        it "matches against a hash submitted by reference and received by value" do
          opts = {:a => "a", :b => "b"}
          expect(a_double).to receive(:random_call).with(opts)
          a_double.random_call(:a => "a", :b => "b")
        end

        it "matches against a hash submitted by value and received by reference" do
          opts = {:a => "a", :b => "b"}
          expect(a_double).to receive(:random_call).with(:a => "a", :b => "b")
          a_double.random_call(opts)
        end

        it "fails for a hash w/ wrong values", :reset => true do
          expect(a_double).to receive(:random_call).with(:a => "b", :c => "d")
          expect do
            a_double.random_call(:a => "b", :c => "e")
          end.to fail_with(/expected: \(\{(:a=>\"b\", :c=>\"d\"|:c=>\"d\", :a=>\"b\")\}\)/)
        end

        it "fails for a hash w/ wrong keys", :reset => true do
          expect(a_double).to receive(:random_call).with(:a => "b", :c => "d")
          expect do
            a_double.random_call("a" => "b", "c" => "d")
          end.to fail_with(/expected: \(\{(:a=>\"b\", :c=>\"d\"|:c=>\"d\", :a=>\"b\")\}\)/)
        end

        it "matches a class against itself" do
          expect(a_double).to receive(:foo).with(Float)
          a_double.foo(Float)
        end

        it "fails a class against an unrelated class", :reset => true do
          expect(a_double).to receive(:foo).with(Float)
          expect { a_double.foo(Hash) }.to fail
        end

        it "matches a class against an instance of itself" do
          expect(a_double).to receive(:foo).with(Float)
          a_double.foo(3.3)
        end

        it "fails a class against an object of a different type", :reset => true do
          expect(a_double).to receive(:foo).with(Float)
          expect { a_double.foo(3) }.to fail
        end

        it "fails with zero arguments", :reset => true do
          expect do
            expect(a_double).to receive(:msg).with { |arg| expect(arg).to eq :received }
          end.to raise_error(ArgumentError, /must have at least one argument/)
        end

        it "fails with sensible message when args respond to #description", :reset => true do
          arg = double(:description => nil, :inspect => "my_thing")

          expect(a_double).to receive(:msg).with(3)
          expect { a_double.msg arg }.to fail_including "got: (my_thing)"
        end

        it "fails with sensible message when arg#description is nil", :reset => true do
          arg = double(:description => nil, :inspect => "my_thing")

          expect(a_double).to receive(:msg).with(arg)
          expect { a_double.msg 3 }.to fail_including "expected: (my_thing)"
        end

        it "fails with sensible message when arg#description is blank", :reset => true do
          arg = double(:description => "", :inspect => "my_thing")

          expect(a_double).to receive(:msg).with(arg)
          expect { a_double.msg 3 }.to fail_including "expected: (my_thing)"
        end
      end
    end
  end
end