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,845 @@
1
+ require 'spec_helper'
2
+
3
+ describe MustBe do
4
+ include MustBeExampleHelper
5
+
6
+ shared_examples_for "*_raise in case of bad arguments" do
7
+ context "when called with an exception type and something other"\
8
+ " than nil, a string, or a regexp" do
9
+ it "should raise TypeError" do
10
+ expect do
11
+ :it.send(the_method_name, RangeError, :not_nil_string_or_regexp) {}
12
+ end.should raise_error(TypeError, "nil, string, or regexp required")
13
+ end
14
+ end
15
+
16
+ context "when called with more than two arguments" do
17
+ it "should raise ArgumentError" do
18
+ expect do
19
+ :it.send(the_method_name, RangeError, "message", "trouble") {}
20
+ end.should raise_error(ArgumentError,
21
+ "wrong number of arguments (3 for 2)")
22
+ end
23
+ end
24
+
25
+ context "when called with two string arguments" do
26
+ it "should raise TypeError" do
27
+ expect do
28
+ :it.send(the_method_name, "message", "second_message") {}
29
+ end.should raise_error(TypeError, "exception class expected")
30
+ end
31
+ end
32
+
33
+ context "when called with a class which does not extend Exception" do
34
+ it "should raise TypeError" do
35
+ expect do
36
+ :it.send(the_method_name, Range) {}
37
+ end.should raise_error(TypeError, "exception class expected")
38
+ end
39
+ end
40
+
41
+ context "when called with something other than an exception type,"\
42
+ " nil, string, or regexp" do
43
+ it "should raise TypeError" do
44
+ expect do
45
+ :it.send(the_method_name, :not_an_error_type) {}
46
+ end.should raise_error(TypeError,
47
+ "exception class expected")
48
+ end
49
+ end
50
+ end
51
+
52
+ describe '#must_raise' do
53
+ let(:the_method_name) { :must_raise }
54
+ it_should_behave_like "*_raise in case of bad arguments"
55
+
56
+ context "when called with no arguments" do
57
+ it "should not notify if any exception is raised" do
58
+ expect do
59
+ :it.must_raise { raise Exception }
60
+ end.should raise_error(Exception)
61
+ should_not notify
62
+ end
63
+
64
+ it "should notify if no exception is raised" do
65
+ called = false
66
+ :it.must_raise{called = true; :result}.should == :result
67
+ called.should be_true
68
+ should notify(":it.must_raise {}, but nothing was raised")
69
+ end
70
+ end
71
+
72
+ context "when called with an exception type" do
73
+ it "should not notify if an exception of the same type is raised" do
74
+ expect do
75
+ :it.must_raise(TypeError) { raise TypeError }
76
+ end.should raise_error
77
+ should_not notify
78
+ end
79
+
80
+ it "should notify if no exception is raised" do
81
+ :it.must_raise(TypeError) {}
82
+ should notify(":it.must_raise(TypeError) {}, but nothing was raised")
83
+ end
84
+
85
+ it "should notify if a different exception type is raised" do
86
+ expect do
87
+ :it.must_raise(TypeError) { raise ArgumentError }
88
+ end.should raise_error
89
+ should notify(":it.must_raise(TypeError) {},"\
90
+ " but ArgumentError was raised")
91
+ end
92
+ end
93
+
94
+ context "when called with a string" do
95
+ it "should not notify if an exception with the same message is"\
96
+ " raised" do
97
+ expect do
98
+ :it.must_raise("message") { raise "message" }
99
+ end.should raise_error
100
+ should_not notify
101
+ end
102
+
103
+ it "should notify if no exception is raised" do
104
+ :it.must_raise("message") {}
105
+ should notify(":it.must_raise(\"message\") {}, but nothing"\
106
+ " was raised")
107
+ end
108
+
109
+ it "should notify if an exception with a different message is raised" do
110
+ expect do
111
+ :it.must_raise("message") { raise "wrong" }
112
+ end.should raise_error
113
+ should notify(":it.must_raise(\"message\") {},"\
114
+ " but RuntimeError with message \"wrong\" was raised")
115
+ end
116
+ end
117
+
118
+ context "when called with a regexp" do
119
+ it "should not notify if an exception with matching message is"\
120
+ " raised" do
121
+ expect do
122
+ :it.must_raise(/message/) { raise "some message" }
123
+ end.should raise_error
124
+ should_not notify
125
+ end
126
+
127
+ it "should notify if no exception is raised" do
128
+ :it.must_raise(/message/) {}
129
+ should notify(":it.must_raise(/message/) {}, but nothing was raised")
130
+ end
131
+
132
+ it "should notify if an exception with non-matching message"\
133
+ " is raised" do
134
+ expect do
135
+ :it.must_raise(/message/) { raise "mess" }
136
+ end.should raise_error
137
+ should notify(":it.must_raise(/message/) {},"\
138
+ " but RuntimeError with message \"mess\" was raised")
139
+ end
140
+ end
141
+
142
+ context "when called with nil" do
143
+ it "should not notify if any exception is raised" do
144
+ expect do
145
+ :it.must_raise(nil) { raise Exception }
146
+ end.should raise_error(Exception)
147
+ should_not notify
148
+ end
149
+
150
+ it "should notify if no exception is raised" do
151
+ :it.must_raise(nil) {}
152
+ should notify(":it.must_raise(nil) {}, but nothing was raised")
153
+ end
154
+ end
155
+
156
+ context "when called with an exception type and a string" do
157
+ it "should not notify if an exception of the same type with"\
158
+ " the same message is raised" do
159
+ expect do
160
+ :it.must_raise(TypeError, "oops") { raise TypeError, "oops" }
161
+ end.should raise_error
162
+ should_not notify
163
+ end
164
+
165
+ it "should notify if no exception is raised" do
166
+ :it.must_raise(TypeError, "oops") {}
167
+ should notify(":it.must_raise(TypeError, \"oops\") {},"\
168
+ " but nothing was raised")
169
+ end
170
+
171
+ it "should notify if an exception of a different type is raised" do
172
+ expect do
173
+ :it.must_raise(TypeError, "oops") { raise ArgumentError, "wrong" }
174
+ end.should raise_error
175
+ should notify(":it.must_raise(TypeError, \"oops\") {},"\
176
+ " but ArgumentError was raised")
177
+ end
178
+
179
+ it "should notify if an exception of the same type"\
180
+ " but with a different message is raised" do
181
+ expect do
182
+ :it.must_raise(TypeError, "oops") { raise TypeError, "wrong" }
183
+ end.should raise_error
184
+ should notify(":it.must_raise(TypeError, \"oops\") {},"\
185
+ " but TypeError with message \"wrong\" was raised")
186
+ end
187
+ end
188
+
189
+ context "when called with an exception type and a regexp" do
190
+ it "should not notify if an exception of the same type with"\
191
+ " matching message is raised" do
192
+ expect do
193
+ :it.must_raise(TypeError, /oops/) { raise TypeError, "oops" }
194
+ end.should raise_error
195
+ should_not notify
196
+ end
197
+
198
+ it "should notify if no exception is raised" do
199
+ :it.must_raise(TypeError, /oops/) {}
200
+ should notify(":it.must_raise(TypeError, /oops/) {},"\
201
+ " but nothing was raised")
202
+ end
203
+
204
+ it "should notify if an exception of a different type is raised" do
205
+ expect do
206
+ :it.must_raise(TypeError, /oops/) { raise ArgumentError, "wrong" }
207
+ end.should raise_error
208
+ should notify(":it.must_raise(TypeError, /oops/) {},"\
209
+ " but ArgumentError was raised")
210
+ end
211
+
212
+ it "should notify if an exception of the same type"\
213
+ " but with a non-matching message is raised" do
214
+ expect do
215
+ :it.must_raise(TypeError, /oops/) { raise TypeError, "wrong" }
216
+ end.should raise_error
217
+ should notify(":it.must_raise(TypeError, /oops/) {},"\
218
+ " but TypeError with message \"wrong\" was raised")
219
+ end
220
+ end
221
+
222
+ context "when called with an exception type and nil" do
223
+ it "should not notify if an exception of the same type is raised" do
224
+ expect do
225
+ :it.must_raise(TypeError, nil) { raise TypeError }
226
+ end.should raise_error
227
+ should_not notify
228
+ end
229
+
230
+ it "should notify if no exception is raised" do
231
+ :it.must_raise(TypeError, nil) {}
232
+ should notify(":it.must_raise(TypeError, nil) {},"\
233
+ " but nothing was raised")
234
+ end
235
+
236
+ it "should notify if a different exception type is raised" do
237
+ expect do
238
+ :it.must_raise(TypeError, nil) { raise ArgumentError }
239
+ end.should raise_error
240
+ should notify(":it.must_raise(TypeError, nil) {},"\
241
+ " but ArgumentError was raised")
242
+ end
243
+ end
244
+
245
+ describe "after disabled" do
246
+ before_disable_after_enable
247
+
248
+ it "should just yield" do
249
+ did_yield = false
250
+ :it.must_raise { did_yield = true }
251
+ did_yield.should be_true
252
+ end
253
+ end
254
+ end
255
+
256
+ describe '#must_not_raise' do
257
+ let(:the_method_name) { :must_not_raise }
258
+ it_should_behave_like "*_raise in case of bad arguments"
259
+
260
+ context "when called with no arguments" do
261
+ it "should notify if any exception is raised" do
262
+ expect do
263
+ :it.must_not_raise { raise Exception }
264
+ end.should raise_error(Exception)
265
+ should notify(":it.must_not_raise {}, but raised Exception")
266
+ end
267
+
268
+ it "should not notify if no exception is raised" do
269
+ called = false
270
+ :it.must_not_raise {called = true; :result}.should == :result
271
+ called.should be_true
272
+ should_not notify
273
+ end
274
+ end
275
+
276
+ context "when called with an exception type" do
277
+ it "should notify if an exception of the same type is raised" do
278
+ expect do
279
+ :it.must_not_raise(TypeError) { raise TypeError }
280
+ end.should raise_error
281
+ should notify(":it.must_not_raise(TypeError) {},"\
282
+ " but raised TypeError")
283
+ end
284
+
285
+ it "should not notify if no exception is raised" do
286
+ :it.must_not_raise(TypeError) {}
287
+ should_not notify
288
+ end
289
+
290
+ it "should not notify if a different exception type is raised" do
291
+ expect do
292
+ :it.must_not_raise(TypeError) { raise ArgumentError }
293
+ end.should raise_error
294
+ should_not notify
295
+ end
296
+ end
297
+
298
+ context "when called with a string" do
299
+ it "should notify if an exception with the same message is raised" do
300
+ expect do
301
+ :it.must_not_raise("message") { raise "message" }
302
+ end.should raise_error
303
+ should notify(":it.must_not_raise(\"message\") {},"\
304
+ " but raised RuntimeError with message \"message\"")
305
+ end
306
+
307
+ it "should not notify if no exception is raised" do
308
+ :it.must_not_raise("message") {}
309
+ should_not notify
310
+ end
311
+
312
+ it "should not notify if an exception with a different message is"\
313
+ " raised" do
314
+ expect do
315
+ :it.must_not_raise("message") { raise "wrong" }
316
+ end.should raise_error
317
+ should_not notify
318
+ end
319
+ end
320
+
321
+ context "when called with a regexp" do
322
+ it "should notify if an exception with matching message is raised" do
323
+ expect do
324
+ :it.must_not_raise(/message/) { raise "some message" }
325
+ end.should raise_error
326
+ should notify(":it.must_not_raise(/message/) {},"\
327
+ " but raised RuntimeError with message \"some message\"")
328
+ end
329
+
330
+ it "should not notify if no exception is raised" do
331
+ :it.must_not_raise(/message/) {}
332
+ should_not notify
333
+ end
334
+
335
+ it "should not notify if an exception with non-matching message"\
336
+ " is raised" do
337
+ expect do
338
+ :it.must_not_raise(/message/) { raise "mess" }
339
+ end.should raise_error
340
+ should_not notify
341
+ end
342
+ end
343
+
344
+ context "when called with nil" do
345
+ it "should notify if any exception is raised" do
346
+ expect do
347
+ :it.must_not_raise(nil) { raise Exception }
348
+ end.should raise_error(Exception)
349
+ should notify(":it.must_not_raise(nil) {}, but raised Exception")
350
+ end
351
+
352
+ it "should not notify if no exception is raised" do
353
+ :it.must_not_raise(nil) {}
354
+ should_not notify
355
+ end
356
+ end
357
+
358
+ context "when called with an exception type and a string" do
359
+ it "should notify if an exception of the same type with"\
360
+ " the same message is raised" do
361
+ expect do
362
+ :it.must_not_raise(TypeError, "oops") { raise TypeError, "oops" }
363
+ end.should raise_error
364
+ should notify(":it.must_not_raise(TypeError, \"oops\") {},"\
365
+ " but raised TypeError with message \"oops\"")
366
+ end
367
+
368
+ it "should not notify if no exception is raised" do
369
+ :it.must_not_raise(TypeError, "oops") {}
370
+ should_not notify
371
+ end
372
+
373
+ it "should not notify if an exception of a different type is raised" do
374
+ expect do
375
+ :it.must_not_raise(TypeError, "oops") { raise ArgumentError, "grr" }
376
+ end.should raise_error
377
+ should_not notify
378
+ end
379
+
380
+ it "should not notify if an exception of the same type"\
381
+ " but with a different message is raised" do
382
+ expect do
383
+ :it.must_not_raise(TypeError, "oops") { raise TypeError, "wrong" }
384
+ end.should raise_error
385
+ should_not notify
386
+ end
387
+ end
388
+
389
+ context "when called with an exception type and a regexp" do
390
+ it "should notify if an exception of the same type with"\
391
+ " matching message is raised" do
392
+ expect do
393
+ :it.must_not_raise(TypeError, /oops/) { raise TypeError, "oops" }
394
+ end.should raise_error
395
+ should notify(":it.must_not_raise(TypeError, /oops/) {},"\
396
+ " but raised TypeError with message \"oops\"")
397
+ end
398
+
399
+ it "should not notify if no exception is raised" do
400
+ :it.must_not_raise(TypeError, /oops/) {}
401
+ should_not notify
402
+ end
403
+
404
+ it "should not notify if an exception of a different type is raised" do
405
+ expect do
406
+ :it.must_not_raise(TypeError, /oops/) { raise ArgumentError, "grr" }
407
+ end.should raise_error
408
+ should_not notify
409
+ end
410
+
411
+ it "should not notify if an exception of the same type"\
412
+ " but with a non-matching message is raised" do
413
+ expect do
414
+ :it.must_not_raise(TypeError, /oops/) { raise TypeError, "wrong" }
415
+ end.should raise_error
416
+ should_not notify
417
+ end
418
+ end
419
+
420
+ context "when called with an exception type and nil" do
421
+ it "should notify if an exception of the same type is raised" do
422
+ expect do
423
+ :it.must_not_raise(TypeError, nil) { raise TypeError }
424
+ end.should raise_error
425
+ should notify(":it.must_not_raise(TypeError, nil) {},"\
426
+ " but raised TypeError")
427
+ end
428
+
429
+ it "should not notify if no exception is raised" do
430
+ :it.must_not_raise(TypeError, nil) {}
431
+ should_not notify
432
+ end
433
+
434
+ it "should not notify if a different exception type is raised" do
435
+ expect do
436
+ :it.must_not_raise(TypeError, nil) { raise ArgumentError }
437
+ end.should raise_error
438
+ should_not notify
439
+ end
440
+ end
441
+ end
442
+
443
+ shared_examples_for "*_throw in case of bad arguments" do
444
+ context "when called with more than two arguments" do
445
+ it "should raise ArgumentError" do
446
+ expect do
447
+ :it.send(the_method_name, :symbol, :object, :other) {}
448
+ end.should raise_error(ArgumentError,
449
+ "wrong number of arguments (3 for 2)")
450
+ end
451
+ end
452
+ end
453
+
454
+ describe '#must_throw' do
455
+ let(:the_method_name) { :must_throw }
456
+ it_should_behave_like "*_throw in case of bad arguments"
457
+
458
+ context "when block returns normally" do
459
+ it "should notify and return the result" do
460
+ :it.must_throw{:result}.should == :result
461
+ should notify(":it.must_throw {}, but did not throw")
462
+ end
463
+ end
464
+
465
+ context "when block throws exception" do
466
+ it "should notify and reraise" do
467
+ expect do
468
+ :it.must_throw { raise }
469
+ end.should raise_error(RuntimeError, "")
470
+ should notify(":it.must_throw {}, but raised RuntimeError")
471
+ end
472
+
473
+ context "when MustBe.notifier raises" do
474
+ before do
475
+ @notifier = MustBe.notifier
476
+ MustBe.notifier = lambda {|note| true }
477
+ end
478
+
479
+ after do
480
+ MustBe.notifier = @notifier
481
+ end
482
+
483
+ it "should raise without transparently" do
484
+ expect do
485
+ :it.must_throw { raise }
486
+ end.should raise_error(Note,
487
+ ":it.must_throw {}, but raised RuntimeError")
488
+ end
489
+
490
+ it "should notify with proper message (Ruby 1.8 regession)" do
491
+ # Otherwise, the problem does not show up.
492
+ MustBe.send(:class_variable_set, :@@must_throw__installed, false)
493
+ expect do
494
+ :it.must_throw { throw :ball }
495
+ end.should raise_error(Note, /:it.must_throw \{\}, but raised/)
496
+ end
497
+ end
498
+ end
499
+
500
+ context "when block throws uncaught symbol" do
501
+ it "should notify and reraise" do
502
+ expect do
503
+ :it.must_throw { throw :uncaught }
504
+ end.should raise_error(/uncaught throw/)
505
+ if RUBY_VERSION < "1.9"
506
+ should notify(":it.must_throw {}, but raised NameError")
507
+ else
508
+ should notify(":it.must_throw {}, but raised ArgumentError")
509
+ end
510
+ end
511
+ end
512
+
513
+ context "when block throws" do
514
+ context "when called with no arguments" do
515
+ it "should not notify" do
516
+ expect do
517
+ :it.must_throw { throw :ball }
518
+ end.should throw_symbol(:ball)
519
+ should_not notify
520
+ end
521
+ end
522
+
523
+ context "when called with tag" do
524
+ it "should not notify if tag equals thrown tag" do
525
+ expect do
526
+ :it.must_throw(:ball) { throw :ball }
527
+ end.should throw_symbol(:ball)
528
+ should_not notify
529
+ end
530
+
531
+ it "should notify if tag does not equal thrown tag" do
532
+ expect do
533
+ :it.must_throw(:pie) { throw :ball }
534
+ end.should throw_symbol(:ball)
535
+ should notify(":it.must_throw(:pie) {}, but threw :ball")
536
+ end
537
+
538
+ context "when checked against object" do
539
+ it "should notify" do
540
+ expect do
541
+ :it.must_throw(:ball, :fiercely) { throw :ball }
542
+ end.should throw_symbol(:ball)
543
+ should notify(":it.must_throw(:ball, :fiercely) {},"\
544
+ " but threw :ball")
545
+ end
546
+
547
+ it "should notify even if checked object is nil" do
548
+ expect do
549
+ :it.must_throw(:ball, nil) { throw :ball }
550
+ end.should throw_symbol(:ball)
551
+ should notify(":it.must_throw(:ball, nil) {},"\
552
+ " but threw :ball")
553
+ end
554
+ end
555
+ end
556
+
557
+ context "when called with tag and object" do
558
+ it "should not notify if tag equals thrown tag and"\
559
+ " object equals thrown object" do
560
+ expect do
561
+ :it.must_throw(:ball, :gently) { throw :ball, :gently }
562
+ end.should throw_symbol(:ball, :gently)
563
+ should_not notify
564
+ end
565
+
566
+ it "should notify if tag does not equal thrown tag" do
567
+ expect do
568
+ :it.must_throw(:pie, :gently) { throw :ball, :gently }
569
+ end.should throw_symbol(:ball, :gently)
570
+ should notify(":it.must_throw(:pie, :gently) {},"\
571
+ " but threw :ball, :gently")
572
+ end
573
+
574
+ it "should notify if object does not equal thrown object" do
575
+ expect do
576
+ :it.must_throw(:ball, :fiercely) { throw :ball, :gently }
577
+ end.should throw_symbol(:ball, :gently)
578
+ should notify(":it.must_throw(:ball, :fiercely) {},"\
579
+ " but threw :ball, :gently")
580
+ end
581
+
582
+ context "when checked object is nil" do
583
+ it "should not notify if thrown object is nil" do
584
+ expect do
585
+ :it.must_throw(:ball, nil) { throw :ball, nil }
586
+ end.should throw_symbol(:ball)
587
+ should_not notify
588
+ end
589
+
590
+ it "should notify if thrown object is not nil" do
591
+ expect do
592
+ :it.must_throw(:ball, nil) { throw :ball, :gently }
593
+ end.should throw_symbol(:ball)
594
+ should notify(":it.must_throw(:ball, nil) {},"\
595
+ " but threw :ball, :gently")
596
+ end
597
+ end
598
+ end
599
+ end
600
+
601
+ describe "after disabled" do
602
+ before_disable_after_enable
603
+
604
+ it "should just yield" do
605
+ did_yield = false
606
+ :it.must_throw { did_yield = true }
607
+ did_yield.should be_true
608
+ end
609
+ end
610
+
611
+ describe "safety" do
612
+ it "should be unrelated to previous throw" do
613
+ catch(:ball) { throw :ball }
614
+ :it.must_throw {}
615
+ should notify(":it.must_throw {}, but did not throw")
616
+ end
617
+
618
+ it "should ignore nested catches" do
619
+ :it.must_throw do
620
+ catch(:money) { throw :money }
621
+ end
622
+ should notify(":it.must_throw {}, but did not throw")
623
+ end
624
+
625
+ it "should allow for deeply nested #must_throw" do
626
+ note = nil
627
+ outer_note = nil
628
+ expect do
629
+ :it.must_throw do
630
+ begin
631
+ :it.must_throw(:party) do
632
+ begin
633
+ :it.must_throw(:ball, :fiercely) do
634
+ throw :ball, :gently
635
+ end
636
+ ensure
637
+ note = @note
638
+ @note = nil
639
+ end
640
+ end
641
+ ensure
642
+ outer_note = @note
643
+ @note = nil
644
+ end
645
+ end
646
+ end.should throw_symbol(:ball)
647
+ note.message.should == ":it.must_throw(:ball, :fiercely) {},"\
648
+ " but threw :ball, :gently"
649
+ outer_note.message.should == ":it.must_throw(:party) {},"\
650
+ " but threw :ball, :gently"
651
+ should_not notify
652
+ end
653
+
654
+ it "should ignore caught nested #must_throw" do
655
+ note = nil
656
+ :it.must_throw do
657
+ note = must_check do
658
+ catch(:money) do
659
+ :it.must_throw(:party) { throw :money }
660
+ end
661
+ end
662
+ end
663
+ note.message.should == ":it.must_throw(:party) {}, but threw :money"
664
+ should notify(":it.must_throw {}, but did not throw")
665
+ end
666
+
667
+ it "should be error safe" do
668
+ :it.must_throw do
669
+ begin
670
+ throw :uncaught
671
+ rescue NameError, ArgumentError
672
+ end
673
+ end
674
+ should notify(":it.must_throw {}, but did not throw")
675
+ end
676
+
677
+ if RUBY_VERSION > "1.9"
678
+ it "should be fiber safe" do
679
+ got_to_end = false
680
+ fiber = Fiber.new do
681
+ note = must_check do
682
+ catch :ball do
683
+ :it.must_throw(:party) do
684
+ begin
685
+ throw :ball
686
+ ensure
687
+ Fiber.yield
688
+ end
689
+ end
690
+ end
691
+ end
692
+ note.message.should == ":it.must_throw(:party) {},"\
693
+ " but threw :ball"
694
+ got_to_end = true
695
+ end
696
+
697
+ :it.must_throw do
698
+ fiber.resume
699
+ end
700
+ fiber.resume
701
+
702
+ got_to_end.should be_true
703
+ should notify(":it.must_throw {}, but did not throw")
704
+ end
705
+ end
706
+ end
707
+ end
708
+
709
+ describe '#must_not_throw' do
710
+ let(:the_method_name) { :must_not_throw }
711
+ it_should_behave_like "*_throw in case of bad arguments"
712
+
713
+ context "when block returns normally" do
714
+ it "should not notify and should return the result" do
715
+ :it.must_not_throw{:result}.should == :result
716
+ should_not notify
717
+ end
718
+ end
719
+
720
+ context "when block raises exception" do
721
+ it "should not notify and should reraise" do
722
+ expect do
723
+ :it.must_not_throw { throw :uncaught }
724
+ end.should raise_error(/uncaught throw/)
725
+ should_not notify
726
+ end
727
+ end
728
+
729
+ context "when block throws" do
730
+ context "when called with no arguments" do
731
+ it "should notify" do
732
+ expect do
733
+ :it.must_not_throw { throw :ball }
734
+ end.should throw_symbol(:ball)
735
+ should notify(":it.must_not_throw {}, but threw :ball")
736
+ end
737
+ end
738
+
739
+ context "when called with tag" do
740
+ it "should notify if tag equals thrown tag" do
741
+ expect do
742
+ :it.must_not_throw(:ball) { throw :ball }
743
+ end.should throw_symbol(:ball)
744
+ should notify(":it.must_not_throw(:ball) {}, but threw :ball")
745
+ end
746
+
747
+ it "should not notify if tag does not equal thrown tag" do
748
+ expect do
749
+ :it.must_not_throw(:pie) { throw :ball }
750
+ end.should throw_symbol(:ball)
751
+ should_not notify
752
+ end
753
+
754
+ context "when checked against object" do
755
+ it "should not notify" do
756
+ expect do
757
+ :it.must_not_throw(:ball, :fiercely) { throw :ball }
758
+ end.should throw_symbol(:ball)
759
+ should_not notify(":it.must_not_throw(:ball, :fiercely) {},"\
760
+ " but threw :ball")
761
+ end
762
+
763
+ it "should not notify even if checked object is nil" do
764
+ expect do
765
+ :it.must_not_throw(:ball, nil) { throw :ball }
766
+ end.should throw_symbol(:ball)
767
+ should_not notify
768
+ end
769
+ end
770
+ end
771
+
772
+ context "when called with tag and object" do
773
+ it "should notify if tag equals thrown tag and"\
774
+ " object equals thrown object" do
775
+ expect do
776
+ :it.must_not_throw(:ball, :gently) { throw :ball, :gently }
777
+ end.should throw_symbol(:ball, :gently)
778
+ should notify(":it.must_not_throw(:ball, :gently) {},"\
779
+ " but threw :ball, :gently")
780
+ end
781
+
782
+ it "should not notify if tag does not equal thrown tag" do
783
+ expect do
784
+ :it.must_not_throw(:pie, :gently) { throw :ball, :gently }
785
+ end.should throw_symbol(:ball, :gently)
786
+ should_not notify
787
+ end
788
+
789
+ it "should not notify if object does not equal thrown object" do
790
+ expect do
791
+ :it.must_not_throw(:ball, :fiercely) { throw :ball, :gently }
792
+ end.should throw_symbol(:ball, :gently)
793
+ should_not notify
794
+ end
795
+
796
+ context "when checked object is nil" do
797
+ it "should notify if thrown object is nil" do
798
+ expect do
799
+ :it.must_not_throw(:ball, nil) { throw :ball, nil }
800
+ end.should throw_symbol(:ball)
801
+ should notify(":it.must_not_throw(:ball, nil) {},"\
802
+ " but threw :ball, nil")
803
+ end
804
+
805
+ it "should not notify if thrown object is not nil" do
806
+ expect do
807
+ :it.must_not_throw(:ball, nil) { throw :ball, :gently }
808
+ end.should throw_symbol(:ball)
809
+ should_not notify
810
+ end
811
+ end
812
+ end
813
+ end
814
+
815
+ describe "safety" do
816
+ it "should safely interact with #must_throw" do
817
+ note = nil
818
+ outer_note = nil
819
+ expect do
820
+ :it.must_not_throw do
821
+ begin
822
+ :it.must_throw(:ball) do
823
+ begin
824
+ :it.must_not_throw(:ball, :gently) do
825
+ throw :ball, :gently
826
+ end
827
+ ensure
828
+ note = @note
829
+ @note = nil
830
+ end
831
+ end
832
+ ensure
833
+ outer_note = @note
834
+ @note = nil
835
+ end
836
+ end
837
+ end.should throw_symbol(:ball)
838
+ note.message.should == ":it.must_not_throw(:ball, :gently) {},"\
839
+ " but threw :ball, :gently"
840
+ outer_note.should == nil
841
+ should notify(":it.must_not_throw {}, but threw :ball, :gently")
842
+ end
843
+ end
844
+ end
845
+ end