File: builtin_contracts_spec.rb

package info (click to toggle)
ruby-contracts 0.17-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 624 kB
  • sloc: ruby: 3,805; makefile: 4; sh: 2
file content (457 lines) | stat: -rw-r--r-- 11,669 bytes parent folder | download
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
RSpec.describe "Contracts:" do
  before :all do
    @o = GenericExample.new
  end

  def fails(&some)
    expect { some.call }.to raise_error(ContractError)
  end

  def passes(&some)
    expect { some.call }.to_not raise_error
  end

  describe "DescendantOf:" do
    it "should pass for Array" do
      passes { @o.enumerable_descendant_test(Array) }
    end

    it "should pass for a hash" do
      passes { @o.enumerable_descendant_test(Hash) }
    end

    it "should fail for a number class" do
      fails { @o.enumerable_descendant_test(Integer) }
    end

    it "should fail for a non-class" do
      fails { @o.enumerable_descendant_test(1) }
    end
  end

  describe "Num:" do
    it "should pass for Integers" do
      passes { @o.double(2) }
    end

    it "should pass for Floats" do
      passes { @o.double(2.2) }
    end

    it "should fail for nil and other data types" do
      fails { @o.double(nil) }
      fails { @o.double(:x) }
      fails { @o.double("x") }
      fails { @o.double(/x/) }
    end
  end

  describe "Pos:" do
    it "should pass for positive numbers" do
      passes { @o.pos_test(1) }
      passes { @o.pos_test(1.6) }
    end

    it "should fail for 0" do
      fails { @o.pos_test(0) }
    end

    it "should fail for negative numbers" do
      fails { @o.pos_test(-1) }
      fails { @o.pos_test(-1.6) }
    end

    it "should fail for nil and other data types" do
      fails { @o.pos_test(nil) }
      fails { @o.pos_test(:x)  }
      fails { @o.pos_test("x") }
      fails { @o.pos_test(/x/) }
    end
  end

  describe "Neg:" do
    it "should pass for negative numbers" do
      passes { @o.neg_test(-1) }
      passes { @o.neg_test(-1.6) }
    end

    it "should fail for 0" do
      fails { @o.neg_test(0) }
    end

    it "should fail for positive numbers" do
      fails { @o.neg_test(1) }
      fails { @o.neg_test(1.6) }
    end

    it "should fail for nil and other data types" do
      fails { @o.neg_test(nil) }
      fails { @o.neg_test(:x)  }
      fails { @o.neg_test("x") }
      fails { @o.neg_test(/x/) }
    end
  end

  describe "Nat:" do
    it "should pass for 0" do
      passes { @o.nat_test(0) }
    end

    it "should pass for positive whole numbers" do
      passes { @o.nat_test(1) }
    end

    it "should fail for positive non-whole numbers" do
      fails { @o.nat_test(1.5) }
    end

    it "should fail for negative numbers" do
      fails { @o.nat_test(-1) }
      fails { @o.nat_test(-1.6) }
    end

    it "should fail for nil and other data types" do
      fails { @o.nat_test(nil) }
      fails { @o.nat_test(:x)  }
      fails { @o.nat_test("x") }
      fails { @o.nat_test(/x/) }
    end
  end

  describe "Any:" do
    it "should pass for numbers" do
      passes { @o.show(1) }
    end
    it "should pass for strings" do
      passes { @o.show("bad") }
    end
    it "should pass for procs" do
      passes { @o.show(lambda {}) }
    end
    it "should pass for nil" do
      passes { @o.show(nil) }
    end
  end

  describe "None:" do
    it "should fail for numbers" do
      fails { @o.fail_all(1) }
    end
    it "should fail for strings" do
      fails { @o.fail_all("bad") }
    end
    it "should fail for procs" do
      fails { @o.fail_all(lambda {}) }
    end
    it "should fail for nil" do
      fails { @o.fail_all(nil) }
    end
  end

  describe "Or:" do
    it "should pass for nums" do
      passes { @o.num_or_string(1) }
    end

    it "should pass for strings" do
      passes { @o.num_or_string("bad") }
    end

    it "should fail for nil" do
      fails { @o.num_or_string(nil) }
    end
  end

  describe "Xor:" do
    it "should pass for an object with a method :good" do
      passes { @o.xor_test(A.new) }
    end

    it "should pass for an object with a method :bad" do
      passes { @o.xor_test(B.new) }
    end

    it "should fail for an object with neither method" do
      fails { @o.xor_test(1) }
    end

    it "should fail for an object with both methods :good and :bad" do
      fails { @o.xor_test(F.new) }
    end
  end

  describe "And:" do
    it "should pass for an object of class A that has a method :good" do
      passes { @o.and_test(A.new) }
    end

    it "should fail for an object that has a method :good but isn't of class A" do
      fails { @o.and_test(F.new) }
    end
  end

  describe "Enum:" do
    it "should pass for an object that is included" do
      passes { @o.enum_test(:a) }
    end

    it "should fail for an object that is not included" do
      fails { @o.enum_test(:z) }
    end
  end

  describe "RespondTo:" do
    it "should pass for an object that responds to :good" do
      passes { @o.responds_test(A.new) }
    end

    it "should fail for an object that doesn't respond to :good" do
      fails { @o.responds_test(B.new) }
    end
  end

  describe "Send:" do
    it "should pass for an object that returns true for method :good" do
      passes { @o.send_test(A.new) }
    end

    it "should fail for an object that returns false for method :good" do
      fails { @o.send_test(F.new) }
    end
  end

  describe "Exactly:" do
    it "should pass for an object that is exactly a Parent" do
      passes { @o.exactly_test(Parent.new) }
    end

    it "should fail for an object that inherits from Parent" do
      fails { @o.exactly_test(Child.new) }
    end

    it "should fail for an object that is not related to Parent at all" do
      fails { @o.exactly_test(A.new) }
    end
  end

  describe "Eq:" do
    it "should pass for a class" do
      passes { @o.eq_class_test(Foo) }
    end

    it "should pass for a module" do
      passes { @o.eq_module_test(Bar) }
    end

    it "should pass for other values" do
      passes { @o.eq_value_test(Baz) }
    end

    it "should fail when not equal" do
      fails { @o.eq_class_test(Bar) }
    end

    it "should fail when given instance of class" do
      fails { @o.eq_class_test(Foo.new) }
    end
  end

  describe "Not:" do
    it "should pass for an argument that isn't nil" do
      passes { @o.not_nil(1) }
    end

    it "should fail for nil" do
      fails { @o.not_nil(nil) }
    end
  end

  describe "ArrayOf:" do
    it "should pass for an array of nums" do
      passes { @o.product([1, 2, 3]) }
    end

    it "should fail for an array with one non-num" do
      fails { @o.product([1, 2, 3, "bad"]) }
    end

    it "should fail for a non-array" do
      fails { @o.product(1) }
    end
  end

  describe "RangeOf:" do
    require "date"
    it "should pass for a range of nums" do
      passes { @o.first_in_range_num(3..10) }
    end

    it "should pass for a range of dates" do
      d1 = Date.today
      d2 = d1 + 18
      passes { @o.first_in_range_date(d1..d2) }
    end

    it "should fail for a non-range" do
      fails { @o.first_in_range_num("foo") }
      fails { @o.first_in_range_num(:foo) }
      fails { @o.first_in_range_num(5) }
      fails { @o.first_in_range_num(nil) }
    end

    it "should fail for a range with incorrect data type" do
      fails { @o.first_in_range_num("a".."z") }
    end

    it "should fail for a badly-defined range" do
      # For some reason, Ruby 2.0.0 allows (date .. number) as a range.
      # Perhaps other Ruby versions do too.
      # Note that (date .. string) gives ArgumentError.
      # This test guards against ranges with inconsistent data types.
      begin
        d1 = Date.today
        fails { @o.first_in_range_date(d1..10) }
        fails { @o.first_in_range_num(d1..10) }
      rescue ArgumentError
        # If Ruby doesn't like the range, we ignore the test.
        :nop
      end
    end
  end

  describe "SetOf:" do
    it "should pass for a set of nums" do
      passes { @o.product_from_set(Set.new([1, 2, 3])) }
    end

    it "should fail for an array with one non-num" do
      fails { @o.product_from_set(Set.new([1, 2, 3, "bad"])) }
    end

    it "should fail for a non-array" do
      fails { @o.product_from_set(1) }
    end
  end

  describe "Bool:" do
    it "should pass for an argument that is a boolean" do
      passes { @o.bool_test(true) }
      passes { @o.bool_test(false) }
    end

    it "should fail for nil" do
      fails { @o.bool_test(nil) }
    end
  end

  describe "Maybe:" do
    it "should pass for nums" do
      expect(@o.maybe_double(1)).to eq(2)
    end

    it "should pass for nils" do
      expect(@o.maybe_double(nil)).to eq(nil)
    end

    it "should fail for strings" do
      fails { @o.maybe_double("foo") }
    end
  end

  describe "KeywordArgs:" do
    it "should pass for exact correct input" do
      passes { @o.person_keywordargs(:name => "calvin", :age => 10) }
    end

    it "should fail if some keys don't have contracts" do
      fails { @o.person_keywordargs(:name => "calvin", :age => 10, :foo => "bar") }
    end

    it "should fail if a key with a contract on it isn't provided" do
      fails { @o.person_keywordargs(:name => "calvin") }
    end

    it "should fail for incorrect input" do
      fails { @o.person_keywordargs(:name => 50, :age => 10) }
      fails { @o.hash_keywordargs(:hash => nil) }
      fails { @o.hash_keywordargs(:hash => 1) }
    end
  end

  describe "Optional:" do
    it "can't be used outside of KeywordArgs" do
      expect do
        BareOptionalContractUsed.new.something(3, 5)
      end.to raise_error(ArgumentError, Contracts::Optional::UNABLE_TO_USE_OUTSIDE_OF_OPT_HASH)
    end
  end

  describe "HashOf:" do
    it "doesn't allow to specify multiple key-value pairs with pretty syntax" do
      expect do
        Class.new do
          include Contracts::Core

          Contract Contracts::HashOf[Symbol => String, Contracts::Num => Contracts::Num] => nil
          def something(hash)
            # ...
          end
        end
      end.to raise_error(ArgumentError, "You should provide only one key-value pair to HashOf contract")
    end

    context "given a fulfilled contract" do
      it { expect(@o.gives_max_value({ :panda => 1, :bamboo => 2 })).to eq(2) }
      it { expect(@o.pretty_gives_max_value({ :panda => 1, :bamboo => 2 })).to eq(2) }
    end

    context "given an unfulfilled contract" do
      it { fails { @o.gives_max_value({ :panda => "1", :bamboo => "2" }) }  }
      it { fails { @o.gives_max_value(nil) } }
      it { fails { @o.gives_max_value(1) } }
      it { fails { @o.pretty_gives_max_value({ :panda => "1", :bamboo => "2" }) } }
    end

    describe "#to_s" do
      context "given Symbol => String" do
        it { expect(Contracts::HashOf[Symbol, String].to_s).to eq("Hash<Symbol, String>") }
      end

      context "given String => Num" do
        it { expect(Contracts::HashOf[String, Contracts::Num].to_s).to eq("Hash<String, Contracts::Builtin::Num>") }
      end
    end
  end

  describe "StrictHash:" do
    context "when given an exact correct input" do
      it "does not raise an error" do
        passes { @o.strict_person({ :name => "calvin", :age => 10 }) }
      end
    end

    context "when given an input with correct keys but wrong types" do
      it "raises an error" do
        fails { @o.strict_person({ :name => "calvin", :age => "10" }) }
      end
    end

    context "when given an input with missing keys" do
      it "raises an error" do
        fails { @o.strict_person({ :name => "calvin" }) }
      end
    end

    context "when given an input with extra keys" do
      it "raises an error" do
        fails { @o.strict_person({ :name => "calvin", :age => 10, :soft => true }) }
      end
    end

    context "when given not a hash" do
      it "raises an error" do
        fails { @o.strict_person(1337) }
      end
    end
  end
end