must_be 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,578 @@
1
+ require 'spec_helper'
2
+
3
+ describe MustBe do
4
+ include MustBeExampleHelper
5
+
6
+ describe '#must_be' do
7
+ context "when called with no arguments" do
8
+ it "should notify if receiver is nil" do
9
+ nil.must_be.should == nil
10
+ should notify("nil.must_be, but matches NilClass")
11
+ end
12
+
13
+ it "should notify if receiver is false" do
14
+ false.must_be.should == false
15
+ should notify("false.must_be, but matches FalseClass")
16
+ end
17
+ end
18
+
19
+ context "when called on 51" do
20
+ context "when called with no arguments" do
21
+ it "should not notify" do
22
+ 51.must_be.should == 51
23
+ should_not notify
24
+ end
25
+ end
26
+
27
+ context "when called with Numeric" do
28
+ it "should not notify" do
29
+ 51.must_be(Numeric).should == 51
30
+ should_not notify
31
+ end
32
+ end
33
+
34
+ context "when called with Float" do
35
+ it "should notify" do
36
+ 51.must_be(Float).should == 51
37
+ should notify("51.must_be(Float), but matches Fixnum")
38
+ end
39
+ end
40
+
41
+ context "when called with Comparable" do
42
+ it "should not notify" do
43
+ 51.must_be(Comparable).should == 51
44
+ should_not notify
45
+ end
46
+ end
47
+
48
+ context "when called with Enumerable" do
49
+ it "should notify" do
50
+ 51.must_be(Enumerable).should == 51
51
+ should notify("51.must_be(Enumerable), but matches Fixnum")
52
+ end
53
+ end
54
+
55
+ context "when called with Hash, Kernel, Object" do
56
+ it "should not notify" do
57
+ 51.must_be(Hash, Kernel, Object).should == 51
58
+ should_not notify
59
+ end
60
+ end
61
+
62
+ context "when called with String, Time, Array" do
63
+ it "should notify" do
64
+ 51.must_be(String, Time, Array).should == 51
65
+ should notify("51.must_be(String, Time, Array), but matches Fixnum")
66
+ end
67
+ end
68
+
69
+ context "when called with [1, 51]" do
70
+ it "should notify" do
71
+ 51.must_be([1, 51]).should == 51
72
+ should notify("51.must_be([1, 51]), but matches Fixnum")
73
+ end
74
+ end
75
+
76
+ context "when called with blocks" do
77
+ let(:zerop) { lambda &:zero? }
78
+ let(:oddp) { lambda &:odd? }
79
+
80
+ context "when called with zerop" do
81
+ it "should notify" do
82
+ 51.must_be(zerop).should == 51
83
+ should notify
84
+ end
85
+ end
86
+
87
+ context "when called with oddp" do
88
+ it "should not notify" do
89
+ 51.must_be(oddp).should == 51
90
+ should_not notify
91
+ end
92
+ end
93
+
94
+ context "when called with zerop, oddp" do
95
+ it "should not notify" do
96
+ 51.must_be(zerop, oddp).should == 51
97
+ should_not notify
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
103
+
104
+ describe '#must_not_be' do
105
+ context "when called with no arguments" do
106
+ it "should not notify if receiver is nil" do
107
+ nil.must_not_be.should == nil
108
+ should_not notify
109
+ end
110
+
111
+ it "should not notify if receiver is false" do
112
+ false.must_not_be.should == false
113
+ should_not notify
114
+ end
115
+ end
116
+
117
+ context "when called on 51" do
118
+ context "when called with no arguments" do
119
+ it "should notify" do
120
+ 51.must_not_be.should == 51
121
+ should notify("51.must_not_be, but matches Fixnum")
122
+ end
123
+ end
124
+
125
+ context "when called with Numeric" do
126
+ it "should notify" do
127
+ 51.must_not_be(Numeric)
128
+ should notify("51.must_not_be(Numeric), but matches Fixnum")
129
+ end
130
+ end
131
+
132
+ context "when called with Float" do
133
+ it "should not notify" do
134
+ 51.must_not_be(Float).should == 51
135
+ should_not notify
136
+ end
137
+ end
138
+
139
+ context "when called with Comparable" do
140
+ it "should notify" do
141
+ 51.must_not_be(Comparable)
142
+ should notify("51.must_not_be(Comparable), but matches Fixnum")
143
+ end
144
+ end
145
+
146
+ context "when called with Enumerable" do
147
+ it "should not notify" do
148
+ 51.must_not_be(Enumerable).should == 51
149
+ should_not notify
150
+ end
151
+ end
152
+
153
+ context "when called with Hash, Kernel, Object" do
154
+ it "should notify" do
155
+ 51.must_not_be(Hash, Kernel, Object).should == 51
156
+ should notify("51.must_not_be(Hash, Kernel, Object),"\
157
+ " but matches Fixnum")
158
+ end
159
+ end
160
+
161
+ context "when called with String, Time, Array" do
162
+ it "should not notify" do
163
+ 51.must_not_be(String, Time, Array).should == 51
164
+ should_not notify
165
+ end
166
+ end
167
+
168
+ context "when called with [1, 51]" do
169
+ it "should notify" do
170
+ 51.must_not_be([1, 51]).should == 51
171
+ should_not notify
172
+ end
173
+ end
174
+
175
+ context "when called with blocks" do
176
+ let(:zerop) { lambda &:zero? }
177
+ let(:oddp) { lambda &:odd? }
178
+
179
+ context "when called with zerop" do
180
+ it "should not notify" do
181
+ 51.must_not_be(zerop).should == 51
182
+ should_not notify
183
+ end
184
+ end
185
+
186
+ context "when called with oddp" do
187
+ it "should notify" do
188
+ 51.must_not_be(oddp).should == 51
189
+ should notify
190
+ end
191
+ end
192
+
193
+ context "when called with zerop, oddp" do
194
+ it "should notify" do
195
+ 51.must_not_be(zerop, oddp).should == 51
196
+ should notify
197
+ end
198
+ end
199
+ end
200
+ end
201
+ end
202
+
203
+ shared_examples_for "*_be_a in case of bad arguments" do
204
+ context "when called with no arguments" do
205
+ it "should raise ArgumentError" do
206
+ expect do
207
+ 51.send(the_method_name)
208
+ end.should raise_error(ArgumentError,
209
+ "wrong number of arguments (0 for 1)")
210
+ end
211
+ end
212
+
213
+ context "when called with more than one message" do
214
+ it "should raise TypeError" do
215
+ expect do
216
+ 51.send(the_method_name, Object, :extra_message, :usual_message)
217
+ end.should raise_error(TypeError, "class or module required")
218
+ end
219
+ end
220
+
221
+ context "when called with a non-module in the middle of the argument"\
222
+ " list" do
223
+ it "should raise TypeError" do
224
+ expect do
225
+ 51.send(the_method_name, Object, :not_a_module, Kernel)
226
+ end.should raise_error(TypeError, "class or module required")
227
+ end
228
+ end
229
+
230
+ context "when called with just one non-module argument" do
231
+ it "should raise TypeError" do
232
+ expect do
233
+ 51.send(the_method_name, :not_a_module)
234
+ end.should raise_error(TypeError, "class or module required")
235
+ end
236
+ end
237
+ end
238
+
239
+ describe '#must_be_a' do
240
+ let(:the_method_name) { :must_be_a }
241
+ it_should_behave_like "*_be_a in case of bad arguments"
242
+
243
+ context "when called on 51" do
244
+ context "when called with Float" do
245
+ it "should notify" do
246
+ 51.must_be_a(Float)
247
+ should notify("51.must_be_a(Float), but is a Fixnum")
248
+ end
249
+ end
250
+
251
+ context "after disabling" do
252
+ before_disable_after_enable
253
+
254
+ context "when called with Float" do
255
+ it "should not notify" do
256
+ 51.must_be_a(Float)
257
+ should_not notify
258
+ end
259
+ end
260
+ end
261
+
262
+ context "when called with Kernel, Comparable" do
263
+ it "should not notify" do
264
+ 51.must_be_a(Kernel, Comparable)
265
+ should_not notify
266
+ end
267
+ end
268
+
269
+ context "when called with Float, Integer" do
270
+ it "should not notify" do
271
+ 51.must_be_a(Float, Integer)
272
+ should_not notify
273
+ end
274
+ end
275
+
276
+ context "when called with Float, :message" do
277
+ it "should notify" do
278
+ 51.must_be_a(Float, :message)
279
+ should notify("51.must_be_a(Float, :message), but is a Fixnum")
280
+ end
281
+ end
282
+ end
283
+ end
284
+
285
+ describe '#must_not_be_a' do
286
+ let(:the_method_name) { :must_not_be_a }
287
+ it_should_behave_like "*_be_a in case of bad arguments"
288
+
289
+ context "when called on 51" do
290
+ context "when called with Float, Enumerable" do
291
+ it "should notify" do
292
+ 51.must_not_be_a(Float, Enumerable)
293
+ should_not notify
294
+ end
295
+ end
296
+
297
+ context "when called with Kernel, Comparable" do
298
+ it "should notify" do
299
+ 51.must_not_be_a(Kernel, Comparable)
300
+ should notify("51.must_not_be_a(Kernel, Comparable),"\
301
+ " but is a Fixnum")
302
+ end
303
+ end
304
+
305
+ context "when called with Float, Integer" do
306
+ it "should notify" do
307
+ 51.must_not_be_a(Float, Integer)
308
+ should notify("51.must_not_be_a(Float, Integer),"\
309
+ " but is a Fixnum")
310
+ end
311
+ end
312
+
313
+ context "when called with Numeric, :message" do
314
+ it "should notify" do
315
+ 51.must_not_be_a(Numeric, :message)
316
+ should notify("51.must_not_be_a(Numeric, :message),"\
317
+ " but is a Fixnum")
318
+ end
319
+ end
320
+ end
321
+ end
322
+
323
+ shared_examples_for "*_be_in in case of bad arguments" do
324
+ context "when called with :does_not_respond_to_include?" do
325
+ it "should raise NoMethodError" do
326
+ expect do
327
+ "hi".send(the_method_name, :does_not_respond_to_include?)
328
+ end.should raise_error(NoMethodError)
329
+ end
330
+ end
331
+ end
332
+
333
+ describe '#must_be_in' do
334
+ let(:the_method_name) { :must_be_in }
335
+ it_should_behave_like "*_be_in in case of bad arguments"
336
+
337
+ context "when called on \"hi\"" do
338
+ context "when called with empty array" do
339
+ it "should notify" do
340
+ "hi".must_be_in([]).should == "hi"
341
+ should notify(%{"hi".must_be_in([])})
342
+ end
343
+ end
344
+
345
+ context "when called with an array which does not include it" do
346
+ it "should notify" do
347
+ "hi".must_be_in(["happy", "helper", "hiccup"]).should == "hi"
348
+ should notify(
349
+ %{"hi".must_be_in(["happy", "helper", "hiccup"])})
350
+ end
351
+ end
352
+
353
+ context "when called with a range which includes it" do
354
+ it "should not notify" do
355
+ "hi".must_be_in("ah".."oh").should == "hi"
356
+ should_not notify
357
+ end
358
+ end
359
+
360
+ context "when called with a string which includes it" do
361
+ it "should not notify" do
362
+ "hi".must_be_in("wishing").should == "hi"
363
+ should_not notify
364
+ end
365
+ end
366
+
367
+ context "when called with no arguments" do
368
+ it "should notify" do
369
+ :yep.must_be_in
370
+ should notify(":yep.must_be_in")
371
+ end
372
+ end
373
+
374
+ context "when called with multiple arguments" do
375
+ it "should not notify if any argument equals the receiver" do
376
+ :yep.must_be_in(:okay, :yep, :fine)
377
+ should_not notify
378
+ end
379
+
380
+ it "should notify if no argument equals the receiver" do
381
+ :yep.must_be_in(:nope, :sorry, :negatory)
382
+ should notify(":yep.must_be_in(:nope, :sorry, :negatory)")
383
+ end
384
+ end
385
+ end
386
+ end
387
+
388
+ describe '#must_not_be_in' do
389
+ let(:the_method_name) { :must_not_be_in }
390
+ it_should_behave_like "*_be_in in case of bad arguments"
391
+
392
+ context "when called on \"hi\"" do
393
+ context "when called with empty array" do
394
+ it "should not notify" do
395
+ "hi".must_not_be_in([]).should == "hi"
396
+ should_not notify
397
+ end
398
+ end
399
+
400
+ context "when called with an array which does not include it" do
401
+ it "should not notify" do
402
+ "hi".must_not_be_in(["happy", "helper", "hiccup"]).should == "hi"
403
+ should_not notify
404
+ end
405
+ end
406
+
407
+ context "when called with a range which includes it" do
408
+ it "should notify" do
409
+ "hi".must_not_be_in("ah".."oh").should == "hi"
410
+ should notify(%{"hi".must_not_be_in("ah".."oh")})
411
+ end
412
+ end
413
+
414
+ context "when called with a string which includes it" do
415
+ it "should notify" do
416
+ "hi".must_not_be_in("wishing").should == "hi"
417
+ should notify(%{"hi".must_not_be_in("wishing")})
418
+ end
419
+ end
420
+
421
+ context "when called with no arguments" do
422
+ it "should not notify" do
423
+ :yep.must_not_be_in
424
+ should_not notify
425
+ end
426
+ end
427
+
428
+ context "when called with multiple arguments" do
429
+ it "should notify if any argument equals the receiver" do
430
+ :yep.must_not_be_in(:okay, :yep, :fine)
431
+ should notify(":yep.must_not_be_in(:okay, :yep, :fine)")
432
+ end
433
+
434
+ it "should not notify if no argument equals the receiver" do
435
+ :yep.must_not_be_in(:nope, :sorry, :negatory)
436
+ should_not notify
437
+ end
438
+ end
439
+ end
440
+ end
441
+
442
+ it_should_have_must_be_value_assertion :must_be_nil, nil
443
+ it_should_have_must_not_be_value_assertion :must_not_be_nil, nil
444
+ it_should_have_must_be_value_assertion :must_be_true, true
445
+ it_should_have_must_be_value_assertion :must_be_false, false
446
+
447
+ describe '#must_be_boolean' do
448
+ it "should not notify if receiver is true" do
449
+ true.must_be_boolean.should == true
450
+ should_not notify
451
+ end
452
+
453
+ it "should not notify if receiver is false" do
454
+ false.must_be_boolean.should == false
455
+ should_not notify
456
+ end
457
+
458
+ it "should notify if receiver is nil" do
459
+ nil.must_be_boolean.should == nil
460
+ should notify("nil.must_be_boolean")
461
+ end
462
+
463
+ it "should notify if receiver is zero" do
464
+ 0.must_be_boolean.should == 0
465
+ should notify("0.must_be_boolean")
466
+ end
467
+ end
468
+
469
+ shared_examples_for "*_be_close in case of bad arguments" do
470
+ it "should raise ArgumentError if delta cannot be compared" do
471
+ expect do
472
+ 200.0.send(the_method_name, 2.0, :a_little)
473
+ end.should raise_error(ArgumentError)
474
+ end
475
+
476
+ it "should raise TypeError if expected cannot be subtracted from"\
477
+ " receiver" do
478
+ expect do
479
+ 200.0.send(the_method_name, :some)
480
+ end.should raise_error(TypeError)
481
+ end
482
+
483
+ it "should raise NoMethodError if receiver does not respond to :-" do
484
+ expect do
485
+ :lots.send(the_method_name, 2.0)
486
+ end.should raise_error(NoMethodError)
487
+ end
488
+
489
+ it "should rasie NoMethodError if (receiver - expected) does not"\
490
+ " respond to :abs" do
491
+ expect do
492
+ Time.new.send(the_method_name, 2.0, :five)
493
+ end.should raise_error(NoMethodError)
494
+ end
495
+ end
496
+
497
+ describe '#must_be_close' do
498
+ let(:the_method_name) { :must_be_close }
499
+ it_should_behave_like "*_be_close in case of bad arguments"
500
+
501
+ it "should not notify if receiver is the same" do
502
+ 2.0.must_be_close(2.0).should == 2.0
503
+ should_not notify
504
+ end
505
+
506
+ it "should not notify if receiver is a bit greater" do
507
+ 2.01.must_be_close(2.0).should == 2.01
508
+ should_not notify
509
+ end
510
+
511
+ it "should not notify if receiver is a bit less" do
512
+ 1.91.must_be_close(2.0).should == 1.91
513
+ should_not notify
514
+ end
515
+
516
+ it "should notify if receiver is much greater" do
517
+ 200.0.must_be_close(2.0, 20.0).should == 200.0
518
+ should notify("200.0.must_be_close(2.0, 20.0), difference is 198.0")
519
+ end
520
+
521
+ it "should notify if receiver is much less" do
522
+ -200.0.must_be_close(2.0, 20.0).should == -200.0
523
+ should notify("-200.0.must_be_close(2.0, 20.0), difference is 202.0")
524
+ end
525
+ end
526
+
527
+ describe '#must_not_be_close' do
528
+ let(:the_method_name) { :must_not_be_close }
529
+ it_should_behave_like "*_be_close in case of bad arguments"
530
+
531
+ it "should notify if receiver is the same" do
532
+ 2.0.must_not_be_close(2.0).should == 2.0
533
+ should notify("2.0.must_not_be_close(2.0, 0.1)")
534
+ end
535
+
536
+ it "should notify if receiver is a bit greater" do
537
+ 2.01.must_not_be_close(2.0).should == 2.01
538
+ should notify("2.01.must_not_be_close(2.0, 0.1)")
539
+ end
540
+
541
+ it "should notify if receiver is a bit less" do
542
+ 1.91.must_not_be_close(2.0).should == 1.91
543
+ should notify("1.91.must_not_be_close(2.0, 0.1)")
544
+ end
545
+
546
+ it "should not notify if receiver is much greater" do
547
+ 200.0.must_not_be_close(2.0, 20.0).should == 200.0
548
+ should_not notify
549
+ end
550
+
551
+ it "should not notify if receiver is much less" do
552
+ -200.0.must_not_be_close(2.0, 20.0).should == -200.0
553
+ should_not notify
554
+ end
555
+ end
556
+ end
557
+
558
+ ### Proc Case Equality Patch ###
559
+
560
+ describe "case equality patch" do
561
+ describe 'Proc#===' do
562
+ it "should work the same as Proc#call" do
563
+ is_less_than_three = lambda {|x| x < 3 }
564
+ is_less_than_three.should === 2
565
+ is_less_than_three.should_not === 7
566
+
567
+ is_even = lambda &:even?
568
+ is_even.should === 2
569
+ is_even.should_not === 7
570
+ end
571
+
572
+ if RUBY_VERSION < "1.9"
573
+ it "should alias Proc#call" do
574
+ Proc.instance_method(:===).should == Proc.instance_method(:call)
575
+ end
576
+ end
577
+ end
578
+ end