minitest-ok 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,878 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ###
4
+ ### $Release: 0.1.0 $
5
+ ### $Copyright: copyright(c) 2015 kuwata-lab.com all rights reserved $
6
+ ### $License: MIT License $
7
+ ###
8
+
9
+
10
+ require_relative '../test_helper'
11
+
12
+
13
+ describe Minitest::Ok::AssertionObject do
14
+
15
+ def should_not_raise
16
+ begin
17
+ yield
18
+ rescue Exception => ex
19
+ assert false, "Nothing should not be raised, but #{ex.class} raised.\n[Message] #{ex}"
20
+ end
21
+ end
22
+
23
+ def should_raise
24
+ begin
25
+ yield
26
+ rescue Minitest::Assertion => ex
27
+ return ex
28
+ else
29
+ assert false, "Assertion should be raised, but nothing raised."
30
+ end
31
+ end
32
+
33
+
34
+ describe '#==' do
35
+
36
+ it "calls assert_equal()" do
37
+ should_not_raise { ok {1+1} == 2 }
38
+ ex = should_raise { ok {1+1} == 3 }
39
+ msg = ("Expected: 3\n" +
40
+ " Actual: 2")
41
+ assert_equal msg, ex.message
42
+ end
43
+
44
+ it "calls refute_equal() after NOT() called." do
45
+ should_not_raise { ok {1+1}.NOT == 3 }
46
+ ex = should_raise { ok {1+1}.NOT == 2 }
47
+ msg = "Expected 2 to not be equal to 2."
48
+ assert_equal msg, ex.message
49
+ end
50
+
51
+ end
52
+
53
+
54
+ describe '#!=' do
55
+
56
+ it "calls refute_equal()." do
57
+ should_not_raise { ok {1+1} != 3 }
58
+ ex = should_raise { ok {1+1} != 2 }
59
+ msg = "Expected 2 to not be equal to 2."
60
+ assert_equal msg, ex.message
61
+ end
62
+
63
+ it "calls assert_equal() after NOT() called." do
64
+ should_not_raise { ok {1+1}.NOT != 2 }
65
+ ex = should_raise { ok {1+1}.NOT != 3 }
66
+ msg = ("Expected: 3\n" +
67
+ " Actual: 2")
68
+ assert_equal msg, ex.message
69
+ end
70
+
71
+ end
72
+
73
+
74
+ describe '#>' do
75
+
76
+ it "calls assert_operator(:>)." do
77
+ should_not_raise { ok {1+1} > 1 }
78
+ ex = should_raise { ok {1+1} > 2 }
79
+ ex = should_raise { ok {1+1} > 3 }
80
+ msg = "Expected 2 to be > 3."
81
+ assert_equal msg, ex.message
82
+ end
83
+
84
+ it "calls refute_operator(:>) after NOT() called." do
85
+ should_not_raise { ok {1+1}.NOT > 3 }
86
+ should_not_raise { ok {1+1}.NOT > 2 }
87
+ ex = should_raise { ok {1+1}.NOT > 1 }
88
+ msg = "Expected 2 to not be > 1."
89
+ assert_equal msg, ex.message
90
+ end
91
+
92
+ end
93
+
94
+
95
+ describe '#>=' do
96
+
97
+ it "calls assert_operator(:>=)." do
98
+ should_not_raise { ok {1+1} >= 1 }
99
+ should_not_raise { ok {1+1} >= 2 }
100
+ ex = should_raise { ok {1+1} >= 3 }
101
+ msg = "Expected 2 to be >= 3."
102
+ assert_equal msg, ex.message
103
+ end
104
+
105
+ it "calls refute_operator(:>=) after NOT() called." do
106
+ should_not_raise { ok {1+1}.NOT >= 3 }
107
+ ex = should_raise { ok {1+1}.NOT >= 2 }
108
+ ex = should_raise { ok {1+1}.NOT >= 1 }
109
+ msg = "Expected 2 to not be >= 1."
110
+ assert_equal msg, ex.message
111
+ end
112
+
113
+ end
114
+
115
+
116
+ describe '#<' do
117
+
118
+ it "calls assert_operator(:<)." do
119
+ should_not_raise { ok {1+1} < 3 }
120
+ ex = should_raise { ok {1+1} < 2 }
121
+ ex = should_raise { ok {1+1} < 1 }
122
+ msg = "Expected 2 to be < 1."
123
+ assert_equal msg, ex.message
124
+ end
125
+
126
+ it "calls refute_operator(:<) after NOT() called." do
127
+ should_not_raise { ok {1+1}.NOT < 1 }
128
+ should_not_raise { ok {1+1}.NOT < 2 }
129
+ ex = should_raise { ok {1+1}.NOT < 3 }
130
+ msg = "Expected 2 to not be < 3."
131
+ assert_equal msg, ex.message
132
+ end
133
+
134
+ end
135
+
136
+
137
+ describe '#<=' do
138
+
139
+ it "calls assert_operator(:<=)." do
140
+ should_not_raise { ok {1+1} <= 3 }
141
+ should_not_raise { ok {1+1} <= 2 }
142
+ ex = should_raise { ok {1+1} <= 1 }
143
+ msg = "Expected 2 to be <= 1."
144
+ assert_equal msg, ex.message
145
+ end
146
+
147
+ it "calls refute_operator(:<=) after NOT() called." do
148
+ should_not_raise { ok {1+1}.NOT <= 1 }
149
+ ex = should_raise { ok {1+1}.NOT <= 2 }
150
+ ex = should_raise { ok {1+1}.NOT <= 3 }
151
+ msg = "Expected 2 to not be <= 3."
152
+ assert_equal msg, ex.message
153
+ end
154
+
155
+ end
156
+
157
+
158
+ describe '#===' do
159
+
160
+ it "calls assert_operator(:===)." do
161
+ should_not_raise { ok {String} === 'foo' }
162
+ should_not_raise { ok {/\d+/ } === '123' }
163
+ ex = should_raise { ok {String} === 123 }
164
+ msg = 'Expected String to be === 123.'
165
+ assert_equal msg, ex.message
166
+ end
167
+
168
+ it "calls refuse_operator(:===) after NOT() called." do
169
+ should_not_raise { ok {String}.NOT === 123 }
170
+ should_not_raise { ok {/\d+/ }.NOT === 'abc' }
171
+ ex = should_raise { ok {String}.NOT === '123' }
172
+ msg = 'Expected String to not be === "123".'
173
+ assert_equal msg, ex.message
174
+ end
175
+
176
+ end
177
+
178
+
179
+ describe '#=~' do
180
+
181
+ it "calls assert_match()." do
182
+ should_not_raise { ok {"hom"} =~ /\w+/ }
183
+ ex = should_raise { ok {"hom"} =~ /\d+/ }
184
+ msg = 'Expected /\d+/ to match "hom".'
185
+ assert_equal msg, ex.message
186
+ end
187
+
188
+ it "calls refute_match() after NOT() called." do
189
+ should_not_raise { ok {"hom"}.NOT =~ /\d+/ }
190
+ ex = should_raise { ok {"hom"}.NOT =~ /\w+/ }
191
+ msg = 'Expected /\w+/ to not match "hom".'
192
+ assert_equal msg, ex.message
193
+ end
194
+
195
+ end
196
+
197
+
198
+ describe '#!~' do
199
+
200
+ it "calls refute_match()." do
201
+ should_not_raise { ok {"hom"} !~ /\d+/ }
202
+ ex = should_raise { ok {"hom"} !~ /\w+/ }
203
+ msg = 'Expected /\w+/ to not match "hom".'
204
+ assert_equal msg, ex.message
205
+ end
206
+
207
+ it "calls assert_match() after NOT() called." do
208
+ should_not_raise { ok {"hom"}.NOT !~ /\w+/ }
209
+ ex = should_raise { ok {"hom"}.NOT !~ /\d+/ }
210
+ msg = 'Expected /\d+/ to match "hom".'
211
+ assert_equal msg, ex.message
212
+ end
213
+
214
+ end
215
+
216
+
217
+ describe '#is_a?' do
218
+
219
+ it "calls assert_kind_of()." do
220
+ should_not_raise { ok {"hom"}.is_a?(String) }
221
+ ex = should_raise { ok {"hom"}.is_a?(Array) }
222
+ msg = 'Expected "hom" to be a kind of Array, not String.'
223
+ assert_equal msg, ex.message
224
+ end
225
+
226
+ it "calls refute_kind_of() after NOT() called." do
227
+ should_not_raise { ok {"hom"}.NOT.is_a?(Array) }
228
+ ex = should_raise { ok {"hom"}.NOT.is_a?(String) }
229
+ msg = 'Expected "hom" to not be a kind of String.'
230
+ assert_equal msg, ex.message
231
+ end
232
+
233
+ end
234
+
235
+
236
+ describe '#kind_of?' do
237
+
238
+ it "calls assert_kind_of()." do
239
+ should_not_raise { ok {"hom"}.kind_of?(String) }
240
+ ex = should_raise { ok {"hom"}.kind_of?(Array) }
241
+ msg = 'Expected "hom" to be a kind of Array, not String.'
242
+ assert_equal msg, ex.message
243
+ end
244
+
245
+ it "calls refute_kind_of() after NOT() called." do
246
+ should_not_raise { ok {"hom"}.NOT.kind_of?(Array) }
247
+ ex = should_raise { ok {"hom"}.NOT.kind_of?(String) }
248
+ msg = 'Expected "hom" to not be a kind of String.'
249
+ assert_equal msg, ex.message
250
+ end
251
+
252
+ end
253
+
254
+
255
+ describe '#instance_of?' do
256
+
257
+ it "calls assert_instance_of()." do
258
+ should_not_raise { ok {123}.instance_of?(Fixnum) }
259
+ ex = should_raise { ok {123}.instance_of?(Integer) }
260
+ msg = 'Expected 123 to be an instance of Integer, not Fixnum.'
261
+ assert_equal msg, ex.message
262
+ end
263
+
264
+ it "calls refute_instance_of() after NOT() called." do
265
+ should_not_raise { ok {123}.NOT.instance_of?(Integer) }
266
+ ex = should_raise { ok {123}.NOT.instance_of?(Fixnum) }
267
+ msg = 'Expected 123 to not be an instance of Fixnum.'
268
+ assert_equal msg, ex.message
269
+ end
270
+
271
+ end
272
+
273
+
274
+ describe '#same?' do
275
+
276
+ it "calls assert_same()." do
277
+ should_not_raise { ok {123}.same?(123) }
278
+ ex = should_raise { ok {[1]}.same?([1]) }
279
+ msg = /^Expected \[1\] \(oid=\d+\) to be the same as \[1\] \(oid=\d+\)\.$/
280
+ assert_match msg, ex.message
281
+ end
282
+
283
+ it "calls refute_same() after NOT() called." do
284
+ should_not_raise { ok {[1]}.NOT.same?([1]) }
285
+ ex = should_raise { ok {123}.NOT.same?(123) }
286
+ msg = /^Expected 123 \(oid=\d+\) to not be the same as 123 \(oid=\d+\)\.$/
287
+ assert_match msg, ex.message
288
+ end
289
+
290
+ end
291
+
292
+
293
+ describe '#empty?' do
294
+
295
+ it "calls assert_empty()." do
296
+ should_not_raise { ok {[ ]}.empty? }
297
+ ex = should_raise { ok {[1]}.empty? }
298
+ msg = "Expected [1] to be empty."
299
+ assert_equal msg, ex.message
300
+ end
301
+
302
+ it "calls refute_empty() after NOT() called." do
303
+ should_not_raise { ok {[1]}.NOT.empty? }
304
+ ex = should_raise { ok {[ ]}.NOT.empty? }
305
+ msg = "Expected [] to not be empty."
306
+ assert_equal msg, ex.message
307
+ end
308
+
309
+ end
310
+
311
+
312
+ describe '#in_delta?' do
313
+
314
+ it "calls assert_in_delta()." do
315
+ should_not_raise { ok {3.14159}.in_delta?(3.14, 0.01) }
316
+ ex = should_raise { ok {3.14159}.in_delta?(3.14, 0.001) }
317
+ msg = /^Expected \|?3\.14 - 3\.14159\|? \(0\.0015899\d+\) to be <=? 0\.001\.$/
318
+ assert_match msg, ex.message
319
+ end
320
+
321
+ it "calls refute_in_delta() after NOT() called." do
322
+ should_not_raise { ok {3.14159}.NOT.in_delta?(3.14, 0.001) }
323
+ ex = should_raise { ok {3.14159}.NOT.in_delta?(3.14, 0.01) }
324
+ msg = /^Expected \|?3\.14 - 3\.14159\|? \(0\.0015899\d+\) to not be <=? 0\.01\.$/
325
+ assert_match msg, ex.message
326
+ end
327
+
328
+ end
329
+
330
+
331
+ describe '#in_epsilon?' do
332
+
333
+ it "calls assert_in_epsilon()." do
334
+ should_not_raise { ok {3.14159}.in_epsilon?(3.14, 0.001) }
335
+ ex = should_raise { ok {3.14159}.in_epsilon?(3.14, 0.0001) }
336
+ msg = /^Expected \|?3\.14 - 3\.14159\|? \(0.00158999\d+\) to be <=? 0\.000314000\d+\.$/
337
+ assert_match msg, ex.message
338
+ end
339
+
340
+ it "calls refute_in_epsilon() after NOT() called." do
341
+ should_not_raise { ok {3.14159}.NOT.in_epsilon?(3.14, 0.0001) }
342
+ ex = should_raise { ok {3.14159}.NOT.in_epsilon?(3.14, 0.001) }
343
+ msg = /^Expected \|?3\.14 - 3\.14159\|? \(0.00158999\d+\) to not be <=? 0\.00314\.$/
344
+ assert_match msg, ex.message
345
+ end
346
+
347
+ end
348
+
349
+
350
+ describe '#raise?' do
351
+
352
+ it "calls assert_raises()." do
353
+ should_not_raise { ok {proc{1/0}}.raise?(ZeroDivisionError) }
354
+ ex = should_raise { ok {proc{1/1}}.raise?(ZeroDivisionError) }
355
+ msg = "ZeroDivisionError expected but nothing was raised."
356
+ assert_equal msg, ex.message
357
+ end
358
+
359
+ it "fails when exception raised after NOT() called." do
360
+ should_not_raise { ok {proc{1/1}}.NOT.raise?(ZeroDivisionError) }
361
+ #
362
+ ex = should_raise { ok {proc{1/0}}.NOT.raise?(ZeroDivisionError) }
363
+ msg = "Exception ZeroDivisionError raised unexpectedly."
364
+ assert_equal msg, ex.message
365
+ #
366
+ assert_raises(ZeroDivisionError) do
367
+ ok {proc{1/0}}.NOT.raise?(NoMethodError)
368
+ end
369
+ end
370
+
371
+ it "can take error message in addition to exception class." do
372
+ should_not_raise { ok {proc{1/0}}.raise?(ZeroDivisionError, "divided by 0") }
373
+ should_not_raise { ok {proc{1/0}}.raise?(ZeroDivisionError, /by [0]/) }
374
+ end
375
+
376
+ end
377
+
378
+
379
+ describe '#throw?' do
380
+
381
+ it "calls assert_throws()." do
382
+ should_not_raise { ok {proc { throw :exit }}.throw?(:exit) }
383
+ ex = should_raise { ok {proc { throw :exit }}.throw?(:finish) }
384
+ msg = "Expected :finish to have been thrown, not :exit."
385
+ assert_equal msg, ex.message
386
+ ex = should_raise { ok {proc { var = 1 + 1 }}.throw?(:exit) }
387
+ msg = "Expected :exit to have been thrown."
388
+ assert_equal msg, ex.message
389
+ end
390
+
391
+ it "raises error because refute_raise() is not defiend in Minitest." do
392
+ ex = assert_raises(RuntimeError) do
393
+ ok {proc { throw :exit }}.NOT.throw?(:finish)
394
+ end
395
+ msg = "NOT.throw? is unsupported because refute_throws() is not defined in Minitest."
396
+ assert_equal msg, ex.message
397
+ end
398
+
399
+ end
400
+
401
+
402
+ describe '#respond_to?' do
403
+
404
+ it "calls assert_respond_to()." do
405
+ should_not_raise { ok {[1]}.respond_to?(:each) }
406
+ ex = should_raise { ok {123}.respond_to?(:each) }
407
+ msg = "Expected 123 (Fixnum) to respond to #each."
408
+ assert_equal msg, ex.message
409
+ end
410
+
411
+ it "calls refute_respond_to() after NOT() called." do
412
+ should_not_raise { ok {[1]}.respond_to?(:each) }
413
+ ex = should_raise { ok {123}.respond_to?(:each) }
414
+ msg = "Expected 123 (Fixnum) to respond to #each."
415
+ assert_equal msg, ex.message
416
+ end
417
+
418
+ end
419
+
420
+
421
+ describe '#include?' do
422
+
423
+ it "calls assert_includes()." do
424
+ should_not_raise { ok {1..9}.include?(2) }
425
+ ex = should_raise { ok {1..9}.include?(0) }
426
+ msg = "Expected 1..9 to include 0."
427
+ assert_equal msg, ex.message
428
+ end
429
+
430
+ it "calls refute_includes() after NOT() called." do
431
+ should_not_raise { ok {1..9}.NOT.include?(0) }
432
+ ex = should_raise { ok {1..9}.NOT.include?(2) }
433
+ msg = "Expected 1..9 to not include 2."
434
+ assert_equal msg, ex.message
435
+ end
436
+
437
+ end
438
+
439
+
440
+ describe '#in?' do
441
+
442
+ it "calls assert_includes()." do
443
+ should_not_raise { ok {1}.in?(1..9) }
444
+ ex = should_raise { ok {0}.in?(1..9) }
445
+ msg = "Expected 1..9 to include 0."
446
+ assert_equal msg, ex.message
447
+ end
448
+
449
+ it "calls refute_includes() after NOT() called." do
450
+ should_not_raise { ok {0}.NOT.in?(1..9) }
451
+ ex = should_raise { ok {1}.NOT.in?(1..9) }
452
+ msg = "Expected 1..9 to not include 1."
453
+ assert_equal msg, ex.message
454
+ end
455
+
456
+ end
457
+
458
+
459
+ describe '#output?' do
460
+
461
+ it "calls assert_output()." do
462
+ should_not_raise { ok {proc {puts 123}}.output?("123\n", "") }
463
+ ex = should_raise { ok {proc {puts 123}}.output?("", "123\n") }
464
+ msg = [
465
+ 'In stderr.',
466
+ '--- expected',
467
+ '+++ actual',
468
+ '@@ -1,2 +1 @@',
469
+ '-"123',
470
+ '-"',
471
+ '+""',
472
+ '',
473
+ ].join("\n")
474
+ assert_equal msg, ex.message
475
+ end
476
+
477
+ it "raises error when called after NOT()." do
478
+ ex = assert_raises(RuntimeError) do
479
+ ok {proc { nil }}.NOT.output?("123", "")
480
+ end
481
+ msg = "use ok().silent? instead of ok().NOT.output?."
482
+ assert_equal msg, ex.message
483
+ end
484
+
485
+ end
486
+
487
+
488
+ describe '#silent?' do
489
+
490
+ it "calls assert_silent()." do
491
+ should_not_raise { ok {proc {nil}}.silent? }
492
+ ex = should_raise { ok {proc {puts 123}}.silent? }
493
+ msg = [
494
+ 'In stdout.',
495
+ '--- expected',
496
+ '+++ actual',
497
+ '@@ -1 +1,2 @@',
498
+ '-""',
499
+ '+"123',
500
+ '+"',
501
+ '',
502
+ ].join("\n")
503
+ assert_equal msg, ex.message
504
+ end
505
+
506
+ it "raises error when called after NOT()." do
507
+ ex = assert_raises(RuntimeError) do
508
+ ok {proc { puts 123 }}.NOT.silent?
509
+ end
510
+ msg = "use ok().output? instead of ok().NOT.silent?."
511
+ assert_equal msg, ex.message
512
+ end
513
+
514
+ end
515
+
516
+
517
+ describe '#frozen?' do
518
+
519
+ it "calls assert_predicate()." do
520
+ should_not_raise { ok {"".freeze}.frozen? }
521
+ ex = should_raise { ok {"".dup() }.frozen? }
522
+ msg = 'Expected "" to be frozen?.'
523
+ assert_equal msg, ex.message
524
+ end
525
+
526
+ it "calls refute_predicate() after NOT() called." do
527
+ should_not_raise { ok {"".dup() }.NOT.frozen? }
528
+ ex = should_raise { ok {"".freeze}.NOT.frozen? }
529
+ msg = 'Expected "" to not be frozen?.'
530
+ assert_equal msg, ex.message
531
+ end
532
+
533
+ end
534
+
535
+
536
+ describe '#tainted?' do
537
+
538
+ it "calls assert_predicate()." do
539
+ should_not_raise { ok {"".taint}.tainted? }
540
+ ex = should_raise { ok {"".dup()}.tainted? }
541
+ msg = 'Expected "" to be tainted?.'
542
+ assert_equal msg, ex.message
543
+ end
544
+
545
+ it "calls refute_predicate() after NOT() called." do
546
+ should_not_raise { ok {"".dup()}.NOT.tainted? }
547
+ ex = should_raise { ok {"".taint}.NOT.tainted? }
548
+ msg = 'Expected "" to not be tainted?.'
549
+ assert_equal msg, ex.message
550
+ end
551
+
552
+ end
553
+
554
+
555
+ describe '#instance_variable_defined?' do
556
+
557
+ def obj_with_x(x)
558
+ obj = Object.new
559
+ obj.instance_variable_set('@x', x)
560
+ return obj
561
+ end
562
+
563
+ it "calls assert()." do
564
+ obj = obj_with_x(10)
565
+ should_not_raise { ok {obj}.instance_variable_defined?('@x') }
566
+ ex = should_raise { ok {obj}.instance_variable_defined?('@y') }
567
+ msg = /^Expected #<Object:\w+ @x=10> to have instance variable \@y, but not\.$/
568
+ assert_match msg, ex.message
569
+ end
570
+
571
+ it "calls refute() after NOT() called." do
572
+ obj = obj_with_x(10)
573
+ should_not_raise { ok {obj}.NOT.instance_variable_defined?('@y') }
574
+ ex = should_raise { ok {obj}.NOT.instance_variable_defined?('@x') }
575
+ msg = /^Expected #<Object:\w+ @x=10> not to have instance variable \@x, but has it\.$/
576
+ assert_match msg, ex.message
577
+ end
578
+
579
+ end
580
+
581
+
582
+ describe '#method_missing()' do
583
+
584
+ it "calls super when not predicate." do
585
+ x = nil
586
+ ex = assert_raises(NoMethodError) do
587
+ (x = ok {nil}).append('bar')
588
+ end
589
+ x._mark_as_tested()
590
+ msg = /^undefined method `append' for \#<Mini[tT]est::Ok::AssertionObject:\w+>$/
591
+ assert_match msg, ex.message
592
+ #
593
+ ex = assert_raises(NoMethodError) do
594
+ ok {nil}.start_with?('bar')
595
+ end
596
+ msg = "undefined method `start_with?' for nil:NilClass"
597
+ assert_equal msg, ex.message
598
+ end
599
+
600
+ it "calles assert()." do
601
+ should_not_raise { ok {"foobar"}.start_with?('foo') }
602
+ ex = should_raise { ok {"foobar"}.start_with?('bar') }
603
+ msg = 'Expected "foobar".start_with?("bar") but failed.'
604
+ assert_equal msg, ex.message
605
+ #
606
+ should_not_raise { ok {[1,2,3]}.all? {|x| x <= 3 }}
607
+ should_not_raise { ok {[1,2,3]}.any? {|x| x % 2 == 0}}
608
+ end
609
+
610
+ it "calles refute() after NOT() called." do
611
+ should_not_raise { ok {"foobar"}.NOT.start_with?('bar') }
612
+ ex = should_raise { ok {"foobar"}.NOT.start_with?('foo') }
613
+ msg = 'Expected "foobar".start_with?("foo") to fail but succeeded.'
614
+ assert_equal msg, ex.message
615
+ end
616
+
617
+ it "calls corresponding 'assert_xxxx' when 'xxxx?' called." do
618
+ MiniTest::Assertions.class_eval do
619
+ def assert_utf8(str)
620
+ assert str.encoding == Encoding::UTF_8, "Expected utf-8 string but not."
621
+ end
622
+ end
623
+ utf8 = "foobar".force_encoding('utf-8')
624
+ ascii = "foobar".force_encoding('us-ascii')
625
+ should_not_raise { ok {utf8 }.utf8? }
626
+ ex = should_raise { ok {ascii}.utf8? }
627
+ msg = 'Expected utf-8 string but not.'
628
+ assert_equal msg, ex.message
629
+ end
630
+
631
+ it "calls corresponding 'refute_xxxx' when 'xxxx?' called after NOT() called." do
632
+ MiniTest::Assertions.class_eval do
633
+ def refute_utf8(str)
634
+ refute str.encoding == Encoding::UTF_8, "Expected non utf-8 string but it is."
635
+ end
636
+ end
637
+ utf8 = "foobar".force_encoding('utf-8')
638
+ ascii = "foobar".force_encoding('us-ascii')
639
+ should_not_raise { ok {ascii}.NOT.utf8? }
640
+ ex = should_raise { ok {utf8 }.NOT.utf8? }
641
+ msg = 'Expected non utf-8 string but it is.'
642
+ assert_equal msg, ex.message
643
+ end
644
+
645
+ end
646
+
647
+
648
+ describe '#truthy?' do
649
+
650
+ it "calles assert()." do
651
+ should_not_raise { ok {123}.truthy? }
652
+ ex = should_raise { ok {nil}.truthy? }
653
+ msg = 'Expected (!! nil) == true, but not.'
654
+ assert_equal msg, ex.message
655
+ end
656
+
657
+ it "calles refute() after NOT() called." do
658
+ should_not_raise { ok {nil}.NOT.truthy? }
659
+ ex = should_raise { ok {123}.NOT.truthy? }
660
+ msg = 'Expected (!! 123) == false, but not.'
661
+ assert_equal msg, ex.message
662
+ end
663
+
664
+ end
665
+
666
+
667
+ describe '#falthy?' do
668
+
669
+ it "calles refute()." do
670
+ should_not_raise { ok {nil}.falthy? }
671
+ ex = should_raise { ok {123}.falthy? }
672
+ msg = 'Expected (!! 123) == false, but not.'
673
+ assert_equal msg, ex.message
674
+ end
675
+
676
+ it "calles assert() after NOT() called." do
677
+ should_not_raise { ok {123}.NOT.falthy? }
678
+ ex = should_raise { ok {nil}.NOT.falthy? }
679
+ msg = 'Expected (!! nil) == true, but not.'
680
+ assert_equal msg, ex.message
681
+ end
682
+
683
+ end
684
+
685
+
686
+ describe '#attr' do
687
+
688
+ class User
689
+ def initialize(name, age)
690
+ @name, @age = name, age
691
+ end
692
+ attr_accessor :name, :age
693
+ end
694
+
695
+ it "calles assert_equal()." do
696
+ user = User.new('Haruhi', 16)
697
+ should_not_raise { ok {user}.attr(:name, 'Haruhi').attr(:age, 16) }
698
+ ex = should_raise { ok {user}.attr(:name, 'Haruhi').attr(:age, 12) }
699
+ msg = ("Expected <object>.age == <exected>, but failed.\n" +
700
+ " (object: #<User:0xXXXXXX @name=\"Haruhi\", @age=16>).\n" +
701
+ "Expected: 12\n" +
702
+ " Actual: 16")
703
+ assert_equal msg, ex.message.gsub(/<User:0x\w+/, '<User:0xXXXXXX')
704
+ end
705
+
706
+ it "calles refute_equal() after NOT() called." do
707
+ user = User.new('Haruhi', 16)
708
+ should_not_raise { ok {user}.NOT.attr(:name, 'Suzumiya').attr(:age, 12) }
709
+ ex = should_raise { ok {user}.NOT.attr(:name, 'Suzumiya').attr(:age, 16) }
710
+ msg = ("Expected <object>.age != <exected>, but failed.\n" +
711
+ " (object: #<User:0xXXXXXX @name=\"Haruhi\", @age=16>).\n" +
712
+ "Expected 16 to not be equal to 16.")
713
+ assert_equal msg, ex.message.gsub(/<User:0x\w+/, '<User:0xXXXXXX')
714
+ end
715
+
716
+ end
717
+
718
+
719
+ describe '#attrs' do
720
+
721
+ class User
722
+ def initialize(name, age)
723
+ @name, @age = name, age
724
+ end
725
+ attr_accessor :name, :age
726
+ end
727
+
728
+ it "calles assert_equal()." do
729
+ user = User.new('Haruhi', 16)
730
+ should_not_raise { ok {user}.attrs(name: 'Haruhi', age: 16) }
731
+ ex = should_raise { ok {user}.attrs(name: 'Haruhi', age: 12) }
732
+ msg = ("Expected <object>.age == <exected>, but failed.\n" +
733
+ " (object: #<User:0xXXXXXX @name=\"Haruhi\", @age=16>).\n" +
734
+ "Expected: 12\n" +
735
+ " Actual: 16")
736
+ assert_equal msg, ex.message.gsub(/<User:0x\w+/, '<User:0xXXXXXX')
737
+ end
738
+
739
+ it "calles refute_equal() after NOT() called." do
740
+ user = User.new('Haruhi', 16)
741
+ should_not_raise { ok {user}.NOT.attrs(name: 'Suzumiya', age: 12) }
742
+ ex = should_raise { ok {user}.NOT.attrs(name: 'Suzumiya', age: 16) }
743
+ msg = ("Expected <object>.age != <exected>, but failed.\n" +
744
+ " (object: #<User:0xXXXXXX @name=\"Haruhi\", @age=16>).\n" +
745
+ "Expected 16 to not be equal to 16.")
746
+ assert_equal msg, ex.message.gsub(/<User:0x\w+/, '<User:0xXXXXXX')
747
+ end
748
+
749
+ end
750
+
751
+
752
+ describe '#item' do
753
+
754
+ it "calles assert_equal()." do
755
+ user = {:name=>'Haruhi', :age=>16}
756
+ should_not_raise { ok {user}.item(:name, 'Haruhi').item(:age, 16) }
757
+ ex = should_raise { ok {user}.item(:name, 'Haruhi').item(:age, 12) }
758
+ msg = ("Expected <object>[:age] == <exected>, but failed.\n" +
759
+ " (object: {:name=>\"Haruhi\", :age=>16}).\n" +
760
+ "Expected: 12\n" +
761
+ " Actual: 16")
762
+ assert_equal msg, ex.message
763
+ end
764
+
765
+ it "calles refute_equal() after NOT() called." do
766
+ user = {:name=>'Haruhi', :age=>16}
767
+ should_not_raise { ok {user}.NOT.item(:name, 'Suzumiya').item(:age, 12) }
768
+ ex = should_raise { ok {user}.NOT.item(:name, 'Suzumiya').item(:age, 16) }
769
+ msg = ("Expected <object>[:age] != <exected>, but failed.\n" +
770
+ " (object: {:name=>\"Haruhi\", :age=>16}).\n" +
771
+ "Expected 16 to not be equal to 16.")
772
+ assert_equal msg, ex.message
773
+ end
774
+
775
+ end
776
+
777
+
778
+ describe '#items' do
779
+
780
+ it "calles assert_equal()." do
781
+ user = {:name=>'Haruhi', :age=>16}
782
+ should_not_raise { ok {user}.items(name: 'Haruhi', age: 16) }
783
+ ex = should_raise { ok {user}.items(name: 'Haruhi', age: 12) }
784
+ msg = ("Expected <object>[:age] == <exected>, but failed.\n" +
785
+ " (object: {:name=>\"Haruhi\", :age=>16}).\n" +
786
+ "Expected: 12\n" +
787
+ " Actual: 16")
788
+ assert_equal msg, ex.message
789
+ end
790
+
791
+ it "calles refute_equal() after NOT() called." do
792
+ user = {:name=>'Haruhi', :age=>16}
793
+ should_not_raise { ok {user}.NOT.items(name: 'Suzumiya', age: 12) }
794
+ ex = should_raise { ok {user}.NOT.items(name: 'Suzumiya', age: 16) }
795
+ msg = ("Expected <object>[:age] != <exected>, but failed.\n" +
796
+ " (object: {:name=>\"Haruhi\", :age=>16}).\n" +
797
+ "Expected 16 to not be equal to 16.")
798
+ assert_equal msg, ex.message
799
+ end
800
+
801
+ end
802
+
803
+
804
+ describe '#file_exist?' do
805
+
806
+ it "calles assert()." do
807
+ fpath = __FILE__
808
+ should_not_raise { ok {fpath}.file_exist? }
809
+ #
810
+ ex = should_raise { ok {'XXX'}.file_exist? }
811
+ msg = "File 'XXX' doesn't exist."
812
+ assert_equal msg, ex.message
813
+ #
814
+ ex = should_raise { ok {Dir.pwd}.file_exist? }
815
+ msg = "'#{Dir.pwd}' is not a file."
816
+ assert_equal msg, ex.message
817
+ end
818
+
819
+ it "calles assert() after NOT() called." do
820
+ fpath = __FILE__
821
+ should_not_raise { ok {'XXX'}.NOT.file_exist? }
822
+ ex = should_raise { ok {fpath}.NOT.file_exist? }
823
+ msg = "File '#{fpath}' exists unexpectedly."
824
+ assert_equal msg, ex.message
825
+ end
826
+
827
+ end
828
+
829
+
830
+ describe '#dir_exist?' do
831
+
832
+ it "calles assert()." do
833
+ dpath = Dir.pwd
834
+ should_not_raise { ok {dpath}.dir_exist? }
835
+ #
836
+ ex = should_raise { ok {'XXX'}.dir_exist? }
837
+ msg = "Directory 'XXX' doesn't exist."
838
+ assert_equal msg, ex.message
839
+ #
840
+ fpath = __FILE__
841
+ ex = should_raise { ok {fpath}.dir_exist? }
842
+ msg = "'#{fpath}' is not a directory."
843
+ assert_equal msg, ex.message
844
+ end
845
+
846
+ it "calles assert() after NOT() called." do
847
+ dpath = Dir.pwd
848
+ should_not_raise { ok {'XXX'}.NOT.dir_exist? }
849
+ ex = should_raise { ok {dpath}.NOT.dir_exist? }
850
+ msg = "Directory '#{dpath}' exists unexpectedly."
851
+ assert_equal msg, ex.message
852
+ end
853
+
854
+ end
855
+
856
+
857
+ describe '#not_exist?' do
858
+
859
+ it "calles assert()." do
860
+ fpath = __FILE__
861
+ should_not_raise { ok {'XXX'}.not_exist? }
862
+ ex = should_raise { ok {fpath}.not_exist? }
863
+ msg = "'#{fpath}' exists unexpectedly."
864
+ assert_equal msg, ex.message
865
+ end
866
+
867
+ it "calles assert() after NOT() called." do
868
+ fpath = __FILE__
869
+ should_not_raise { ok {fpath}.NOT.not_exist? }
870
+ ex = should_raise { ok {'XXX'}.NOT.not_exist? }
871
+ msg = "'XXX' doesn't exist."
872
+ assert_equal msg, ex.message
873
+ end
874
+
875
+ end
876
+
877
+
878
+ end