section9-unittest 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,772 @@
1
+ ###
2
+ ### $Release: 0.0.1 $
3
+ ### $Copyright: copyright(c) 2011 kuwata-lab.com all rights reserved $
4
+ ### $License: MIT License $
5
+ ###
6
+
7
+ File.instance_eval do
8
+ require join(dirname(expand_path(__FILE__)), "initialize.rb")
9
+ end
10
+
11
+
12
+ class UnitTest_TestCase_TC < TC
13
+
14
+ it "DESCRIPTION1" do
15
+ end
16
+
17
+ describe "TARGET1" do
18
+ describe "DETAIL1" do
19
+ it "DESC1" do
20
+ end
21
+ end
22
+ end
23
+
24
+ def test_it_method_defines_test_method_with_description
25
+ arr = self.methods.grep(/DESCRIPTION1/)
26
+ #assert_equal [:'test_001: DESCRIPTION1'], arr
27
+ assert_equal 1, arr.length
28
+ assert_equal 'test_001: DESCRIPTION1', arr.first.to_s
29
+ end
30
+
31
+ def test_describe_method_adds_prefix_to_test_method_name
32
+ arr = self.methods.grep(/TARGET1/)
33
+ #assert_equal [:'test_002: [TARGET1 > DETAIL1] DESC1'], arr
34
+ assert_equal 1, arr.length
35
+ assert_equal 'test_002: [TARGET1 > DETAIL1] DESC1', arr.first.to_s
36
+ end
37
+
38
+ def test_verify_method_returns_VerifyObject_instance
39
+ item = verify_(nil)
40
+ assert_same Section9::UnitTest::VerifyObject, item.class
41
+ __clear_registered()
42
+ end
43
+
44
+ def test_verify_NOT_method_returns_VerifyNotObject_instance
45
+ item = verify_(nil).NOT
46
+ assert_same Section9::UnitTest::VerifyNotObject, item.class
47
+ __clear_registered()
48
+ end
49
+
50
+ def test_verify_method_registers_self
51
+ item = verify_(nil)
52
+ d = item.instance_variable_get("@testcase").instance_variable_get("@__not_yet")
53
+ assert d.key?(item.__id__)
54
+ item == nil
55
+ assert ! d.key?(item.__id__)
56
+ end
57
+
58
+ def test_verify_NOT_method_registers_self_into_NOT_YET
59
+ item = verify_(nil).NOT
60
+ d = item.instance_variable_get("@testcase").instance_variable_get("@__not_yet")
61
+ assert d.key?(item.__id__)
62
+ item == false
63
+ assert ! d.key?(item.__id__)
64
+ end
65
+
66
+ def test_teardown_reports_verify_objects_not_tested_yet
67
+ bkup = $stderr
68
+ s = ""
69
+ def s.write(arg); self << arg; end
70
+ $stderr = s
71
+ verify_(nil) ; line1 = __LINE__
72
+ verify_(nil).NOT ; line2 = __LINE__
73
+ teardown()
74
+ expected = /\*\* warning: verify\(\) is called but not tested yet \(at #{__FILE__}:#{line1}(?::in `.*?')?\)\n/
75
+ assert_match expected, s
76
+ expected = /\*\* warning: verify\(\) is called but not tested yet \(at #{__FILE__}:#{line2}(?::in `.*?')?\)\n/
77
+ assert_match expected, s
78
+ ensure
79
+ $stderr = bkup
80
+ end
81
+
82
+ def test_recorder_returns_Recorder_object
83
+ assert_instance_of Section9::Recorder, recorder
84
+ end
85
+
86
+ end
87
+
88
+
89
+ class VerifyBaseObject_TC < TC
90
+
91
+ it "has been removed boolean methods." do
92
+ obj = Section9::UnitTest::VerifyBaseObject.new
93
+ assert_equal ["equal?"], obj.methods.grep(/\?$/).collect {|x| x.to_s }
94
+ end
95
+
96
+ end
97
+
98
+
99
+ class VerifyObject_TC < TC
100
+
101
+ MINITEST_P = defined?(MiniTest)
102
+ ASSERTION = MINITEST_P ? MiniTest::Assertion : Test::Unit::AssertionFailedError # :nodoc:
103
+
104
+ def _should_pass
105
+ assert_nothing_raised { yield }
106
+ end
107
+
108
+ def _should_fail(errmsg=nil, linenum=nil)
109
+ ex = assert_raise(ASSERTION) { yield }
110
+ if errmsg
111
+ errmsg.is_a?(Regexp) ? assert_match(errmsg, ex.message) \
112
+ : assert_equal(errmsg, ex.message)
113
+ end
114
+ if linenum
115
+ ex.backtrace[0] =~ /(.*:\d+)/
116
+ assert_equal "#{__FILE__}:#{linenum}", $1
117
+ end
118
+ end
119
+
120
+ def _get_failed
121
+ begin
122
+ yield
123
+ rescue ASSERTION => ex
124
+ return ex
125
+ end
126
+ flunk "assertion shoul be failed but not."
127
+ end
128
+
129
+ def _verify_location(ex, linenum)
130
+ ex.backtrace[0] =~ /(.*:\d+)/
131
+ assert_equal "#{__FILE__}:#{linenum}", $1
132
+ end
133
+
134
+ describe "#==" do
135
+ it "raises nothing when passed." do
136
+ _should_pass { verify_("Joe") == "Joe" }
137
+ end
138
+ it "raises assertion error when failed." do
139
+ errmsg = "<\"Jet\"> expected but was\n<\"Joe\">."
140
+ _should_fail(errmsg) { verify_("Joe") == "Jet" }
141
+ end
142
+ it "is available with NOT." do
143
+ _should_pass { verify_("Joe").NOT == "Jet" }
144
+ errmsg = "<\"Joe\"> expected to be != to\n<\"Joe\">."
145
+ _should_fail(errmsg) { verify_("Joe").NOT == "Joe" }
146
+ end
147
+ it "sets backtrace when failed." do
148
+ ex = _get_failed { verify_("Joe") == "Jet" }
149
+ _verify_location ex, __LINE__ - 1
150
+ end
151
+ it "shows context diff when two texts are different." do
152
+ act = <<'END'
153
+ Haruhi
154
+ Mikuru
155
+ Yuki
156
+ END
157
+ exp = <<'END'
158
+ Haruhi
159
+ Michiru
160
+ Yuki
161
+ END
162
+ expected = <<'END'.chomp
163
+ --- expected
164
+ +++ actual
165
+ @@ -1,3 +1,3 @@
166
+ Haruhi
167
+ -Michiru
168
+ +Mikuru
169
+ Yuki
170
+ END
171
+ ex = _get_failed { verify_(act) == exp }
172
+ verify_(ex.message).include?(expected)
173
+ end
174
+ end
175
+
176
+ describe "#!=" do
177
+ it "raises nothing when passed." do
178
+ _should_pass { verify_("Joe") != "Jet" }
179
+ end
180
+ it "raises assertion error when failed." do
181
+ errmsg = "<\"Joe\"> expected to be != to\n<\"Joe\">."
182
+ _should_fail(errmsg) { verify_("Joe") != "Joe" }
183
+ end
184
+ it "is available with NOT." do
185
+ _should_pass { verify_("Joe").NOT != "Joe" }
186
+ errmsg = "<\"Jet\"> expected but was\n<\"Joe\">."
187
+ _should_fail(errmsg) { verify_("Joe").NOT != "Jet" }
188
+ end
189
+ it "sets backtrace when failed." do
190
+ ex = _get_failed { verify_("Joe") != "Joe" }
191
+ _verify_location ex, __LINE__ - 1
192
+ end
193
+ end if RUBY_VERSION >= "1.9"
194
+
195
+ describe "#>" do
196
+ msg = MINITEST_P ? "Expected %s to be %s %s." : "<%s> expected to be\n%s\n<%s>."
197
+ it "raises nothing when passed." do
198
+ _should_pass { verify_(2) > 1 }
199
+ end
200
+ it "raises assertion error when failed." do
201
+ _should_fail(msg % [1, '>', 1]) { verify_(1) > 1 }
202
+ _should_fail(msg % [1, '>', 2]) { verify_(1) > 2 }
203
+ end
204
+ it "is available with NOT." do
205
+ _should_pass { verify_(1).NOT > 1 }
206
+ _should_pass { verify_(1).NOT > 2 }
207
+ _should_fail(msg % [2, '<=', 1]) { verify_(2).NOT > 1 }
208
+ end
209
+ it "sets backtrace when failed." do
210
+ ex = begin verify_(1) > 2
211
+ rescue ASSERTION => ex; ex end
212
+ _verify_location ex, __LINE__ - 2
213
+ end
214
+ end
215
+
216
+ describe "#>=" do
217
+ msg = MINITEST_P ? "Expected %s to be %s %s." : "<%s> expected to be\n%s\n<%s>."
218
+ it "raises nothing when passed." do
219
+ _should_pass { verify_(1) >= 1 }
220
+ _should_pass { verify_(2) >= 1 }
221
+ end
222
+ it "raises assertion error when failed." do
223
+ _should_fail(msg % [1, '>=', 2]) { verify_(1) >= 2 }
224
+ end
225
+ it "is available with NOT." do
226
+ _should_pass { verify_(1).NOT >= 2 }
227
+ _should_fail(msg % [1, '<', 1]) { verify_(1).NOT >= 1 }
228
+ _should_fail(msg % [2, '<', 1]) { verify_(2).NOT >= 1 }
229
+ end
230
+ it "sets backtrace when failed." do
231
+ ex = begin verify_(1) >= 2
232
+ rescue ASSERTION => ex; ex end
233
+ _verify_location ex, __LINE__ - 2
234
+ end
235
+ end
236
+
237
+ describe "#<" do
238
+ msg = MINITEST_P ? "Expected %s to be %s %s." : "<%s> expected to be\n%s\n<%s>."
239
+ it "raises nothing when passed." do
240
+ _should_pass { verify_(1) < 2 }
241
+ end
242
+ it "raises assertion error when failed." do
243
+ _should_fail(msg % [1, '<', 1]) { verify_(1) < 1 }
244
+ _should_fail(msg % [2, '<', 1]) { verify_(2) < 1 }
245
+ end
246
+ it "is available with NOT." do
247
+ _should_pass { verify_(1).NOT < 1 }
248
+ _should_pass { verify_(2).NOT < 1 }
249
+ _should_fail(msg % [1, '>=', 2]) { verify_(1).NOT < 2 }
250
+ end
251
+ it "sets backtrace when failed." do
252
+ ex = begin verify_(1) < 1
253
+ rescue ASSERTION => ex; ex end
254
+ _verify_location ex, __LINE__ - 2
255
+ end
256
+ end
257
+
258
+ describe "#<=" do
259
+ msg = MINITEST_P ? "Expected %s to be %s %s." : "<%s> expected to be\n%s\n<%s>."
260
+ it "raises nothing when passed." do
261
+ _should_pass { verify_(1) <= 1 }
262
+ _should_pass { verify_(1) <= 2 }
263
+ end
264
+ it "raises assertion error when failed." do
265
+ _should_fail(msg % [2, '<=', 1]) { verify_(2) <= 1 }
266
+ end
267
+ it "is available with NOT." do
268
+ _should_pass { verify_(2).NOT <= 1 }
269
+ _should_fail(msg % [1, '>', 1]) { verify_(1).NOT <= 1 }
270
+ _should_fail(msg % [1, '>', 2]) { verify_(1).NOT <= 2 }
271
+ end
272
+ it "sets backtrace when failed." do
273
+ ex = begin verify_(2) <= 1
274
+ rescue ASSERTION => ex; ex end
275
+ _verify_location ex, __LINE__ - 2
276
+ end
277
+ end
278
+
279
+ describe "#=~" do
280
+ it "raises nothing when passed." do
281
+ _should_pass { verify_("009") =~ /^\d+$/ }
282
+ end
283
+ it "raises assertion error when failed." do
284
+ msg = MINITEST_P ? "Expected /^\\d+$/ to match \"Joe\"." \
285
+ : "<\"Joe\"> expected to be =~\n</^\\d+$/>."
286
+ _should_fail(msg) { verify_("Joe") =~ /^\d+$/ }
287
+ end
288
+ it "is available with NOT." do
289
+ _should_pass { verify_("Joe").NOT =~ /^\d+$/ }
290
+ _should_fail("</^\\d+$/> expected to not match\n<\"009\">.") { verify_("009").NOT =~ /^\d+$/ }
291
+ end
292
+ it "sets backtrace when failed." do
293
+ ex = begin verify_("Joe") =~ /^\d+$/
294
+ rescue ASSERTION => ex; ex end
295
+ _verify_location ex, __LINE__ - 2
296
+ end
297
+ end
298
+
299
+ describe "#!~" do
300
+ it "raises nothing when passed." do
301
+ _should_pass { verify_("Joe") !~ /^\d+$/ }
302
+ end
303
+ it "raises assertion error when failed." do
304
+ #msg = MINITEST_P ? "Expected /^\\d+$/ to not match \"009\"." \
305
+ # : "<\"009\"> expected to not match\n</^\\d+$/>."
306
+ msg = MINITEST_P ? "</^\\d+$/> expected to not match\n<\"009\">." \
307
+ : "<\"009\"> expected to not match\n</^\\d+$/>."
308
+ _should_fail(msg) { verify_("009") !~ /^\d+$/ }
309
+ end
310
+ it "is available with NOT." do
311
+ msg = MINITEST_P ? "Expected /^\\d+$/ to match \"Joe\"." \
312
+ : "</^\\d+$/> expected to be =~\n<\"Joe\">."
313
+ _should_pass { verify_("Joe").NOT =~ /^\d+$/ }
314
+ _should_fail(msg) { verify_("Joe").NOT !~ /^\d+$/ }
315
+ end
316
+ it "sets backtrace when failed." do
317
+ ex = begin verify_("009") !~ /^\d+$/
318
+ rescue ASSERTION => ex; ex end
319
+ _verify_location ex, __LINE__ - 2
320
+ end
321
+ end if RUBY_VERSION >= "1.9"
322
+
323
+ describe "#nil?" do
324
+ it "raises nothing when passed." do
325
+ _should_pass { verify_(nil).nil? }
326
+ end
327
+ it "raises assertion error when failed." do
328
+ msg = MINITEST_P ? "Expected \"Joe\" to be nil." \
329
+ : "<nil> expected but was\n<\"Joe\">."
330
+ _should_fail(msg) { verify_("Joe").nil? }
331
+ end
332
+ it "is available with NOT." do
333
+ _should_pass { verify_("Joe").NOT.nil? }
334
+ errmsg = "<nil> expected to not be nil." # OK?
335
+ _should_fail(errmsg) { verify_(nil).NOT.nil? }
336
+ end
337
+ it "sets backtrace when failed." do
338
+ ex = begin verify_("Joe").nil?
339
+ rescue ASSERTION => ex; ex end
340
+ _verify_location ex, __LINE__ - 2
341
+ end
342
+ end
343
+
344
+ describe "#same?" do
345
+ it "raises nothing when passed." do
346
+ _should_pass { verify_(:Joe).same?(:Joe) }
347
+ end
348
+ it "raises assertion error when failed." do
349
+ msg = MINITEST_P ? /^Expected "Joe" \((oid=\d+|0x[0-9a-f]+)?\) to be the same as "Joe" \((oid=\d+|0x[0-9a-f]+)?\d+\)\.$/ \
350
+ : /^<"Joe">\nwith id <.*?> expected to be equal\? to\n<"Joe">\nwith id <.*?>\.$/
351
+ _should_fail(msg) { verify_("Joe").same?("Joe") }
352
+ end
353
+ it "is available with NOT." do
354
+ _should_pass { verify_("Joe").NOT.same?("Joe") }
355
+ msg = /^<:Joe>\nwith id <\d+> expected to not be equal\? to\n<:Joe>\nwith id <\d+>\./ # OK?
356
+ _should_fail(msg) { verify_(:Joe).NOT.same? :Joe }
357
+ end
358
+ it "sets backtrace when failed." do
359
+ ex = begin verify_("Joe").same?("Joe")
360
+ rescue ASSERTION => ex; ex end
361
+ _verify_location ex, __LINE__ - 2
362
+ end
363
+ end
364
+
365
+ describe "#kind_of?" do
366
+ it "raises nothing when passed." do
367
+ _should_pass { verify_(123).kind_of?(Numeric) }
368
+ end
369
+ it "raises assertion error when failed." do
370
+ msg = MINITEST_P ? "Expected :Joe to be a kind of String, not Symbol." \
371
+ : "<:Joe>\nexpected to be kind_of?\n<String> but was\n<Symbol>."
372
+ _should_fail(msg) { verify_(:Joe).kind_of?(String) }
373
+ end
374
+ it "is available with NOT." do
375
+ _should_pass { verify_(:Joe).NOT.kind_of?(String) }
376
+ msg = "<\"Joe\">\nexpected not to be kind_of?\n<String> but was\n<String>." # OK?
377
+ _should_fail(msg) { verify_("Joe").NOT.kind_of?(String) }
378
+ end
379
+ it "sets backtrace when failed." do
380
+ ex = begin verify_(:Joe).kind_of?(String)
381
+ rescue ASSERTION => ex; ex end
382
+ _verify_location ex, __LINE__ - 2
383
+ end
384
+ end
385
+
386
+ describe "#instance_of?" do
387
+ it "raises nothing when passed." do
388
+ _should_pass { verify_(123).instance_of?(Fixnum) }
389
+ end
390
+ it "raises assertion error when failed." do
391
+ msg = MINITEST_P ? "Expected 123 to be an instance of Numeric, not Fixnum." \
392
+ : "<123> expected to be an instance of\n<Numeric> but was\n<Fixnum>."
393
+ _should_fail(msg) { verify_(123).instance_of?(Numeric) }
394
+ end
395
+ it "is available with NOT." do
396
+ _should_pass { verify_(123).NOT.instance_of?(Numeric) }
397
+ errmsg = "<123>\nexpected not to be instance_of?\n<Fixnum> but was." # OK?
398
+ _should_fail(errmsg) { verify_(123).NOT.instance_of?(Fixnum) }
399
+ end
400
+ it "sets backtrace when failed." do
401
+ ex = begin verify_(123).instance_of?(Numeric)
402
+ rescue ASSERTION => ex; ex end
403
+ _verify_location ex, __LINE__ - 2
404
+ end
405
+ end
406
+
407
+ describe "#in_delta?" do
408
+ it "raises nothing when passed." do
409
+ _should_pass { verify_(3.1415).in_delta?(3.141, 0.001) }
410
+ end
411
+ it "raises assertion error when failed." do
412
+ msg = MINITEST_P ? "Expected 3.141 - 3.1415 (0.000500000000000167) to be < 0.0001." \
413
+ : "<3.141> and\n<3.1415> expected to be within\n<0.0001> of each other."
414
+ _should_fail(msg) { verify_(3.1415).in_delta?(3.141, 0.0001) }
415
+ end
416
+ it "is available with NOT." do
417
+ _should_pass { verify_(3.1415).NOT.in_delta?(3.141, 0.0001) }
418
+ errmsg = "<3.141> and\n<3.1415> expected to be without\n<0.0001> of each other."
419
+ _should_fail(errmsg) { verify_(3.1415).NOT.in_delta?(3.141, 0.001) }
420
+ end
421
+ it "sets backtrace when failed." do
422
+ ex = begin verify_(3.1415).in_delta?(3.141, 0.0001)
423
+ rescue ASSERTION => ex; ex end
424
+ _verify_location ex, __LINE__ - 2
425
+ end
426
+ end
427
+
428
+ describe "#respond_to?" do
429
+ it "raises nothing when passed." do
430
+ _should_pass { verify_("").respond_to?(:empty?) }
431
+ end
432
+ it "raises assertion error when failed." do
433
+ msg = MINITEST_P ? "Expected 123 (Fixnum) to respond to #empty?."\
434
+ : "<123>\nof type <Fixnum>\nexpected to respond_to?<:empty?>."
435
+ _should_fail(msg) { verify_(123).respond_to?(:empty?) }
436
+ end
437
+ it "is available with NOT." do
438
+ _should_pass { verify_(123).NOT.respond_to?(:empty?) }
439
+ errmsg = "Expected \"\" not to respond to :empty?."
440
+ _should_fail(errmsg) { verify_("").NOT.respond_to?(:empty?) }
441
+ end
442
+ it "sets backtrace when failed." do
443
+ ex = begin verify_(123).respond_to?(:empty?)
444
+ rescue ASSERTION => ex; ex end
445
+ _verify_location ex, __LINE__ - 2
446
+ end
447
+ end
448
+
449
+ describe "#raise?" do
450
+ errmsg = "undefined method `reverse' for :Joe:Symbol"
451
+ it "raises nothing when passed." do
452
+ _should_pass { verify_(proc { :Joe.reverse }).raise?(NoMethodError, errmsg) }
453
+ end
454
+ it "can accept regexp as error message pattern." do
455
+ rexp = /undefined method `reverse' for :Joe:Symbol/
456
+ _should_pass { verify_(proc { :Joe.reverse }).raise?(NoMethodError, rexp) }
457
+ end
458
+ it "can accept block instead of proc object." do
459
+ _should_pass { verify_ { :Joe.reverse }.raise?(NoMethodError, errmsg) }
460
+ end
461
+ it "return exception object raised." do
462
+ ex = verify_(proc { :Joe.reverse }).raise?(NoMethodError, errmsg)
463
+ assert_kind_of NoMethodError, ex
464
+ assert_equal errmsg, ex.message
465
+ end
466
+ it "sets exception object." do
467
+ pr = proc { :Joe.reverse }
468
+ verify_(pr).raise?(NoMethodError, errmsg)
469
+ assert_kind_of NoMethodError, pr.exception
470
+ assert_equal errmsg, pr.exception.message
471
+ end
472
+ it "raises assertion error when failed." do
473
+ msg = MINITEST_P ? "NoMethodError expected but nothing was raised." \
474
+ : "<NoMethodError> exception expected but none was thrown."
475
+ _should_fail(msg) { verify_(proc { :Joe.to_s }).raise?(NoMethodError) }
476
+ msg = "<\"MISSING\"> expected but was\n<\"undefined method `reverse' for :Joe:Symbol\">."
477
+ _should_fail(msg) { verify_(proc { :Joe.reverse }).raise?(NoMethodError, 'MISSING') }
478
+ end
479
+ it "is available with NOT." do
480
+ _should_pass { verify_(proc { :Joe.to_s }).NOT.raise? }
481
+ msg = MINITEST_P ? "Exception raised:\n<#<NoMethodError: undefined method `reverse' for :Joe:Symbol>>." \
482
+ : /Exception raised:\nClass: <NoMethodError>\n/
483
+ _should_fail(msg) { verify_(proc { :Joe.reverse }).NOT.raise? }
484
+ end
485
+ it "sets backtrace when failed." do
486
+ ex = begin verify_(proc { :Joe.to_s }).raise?(NoMethodError)
487
+ rescue ASSERTION => ex; ex end
488
+ _verify_location ex, __LINE__ - 2
489
+ end
490
+ end
491
+
492
+ describe "#throw?" do
493
+ it "raises nothing when passed." do
494
+ _should_pass { verify_ { throw :Jet }.throw?(:Jet) }
495
+ end
496
+ it "raises assertion error when failed." do
497
+ msg = MINITEST_P ? "Expected :Joe to have been thrown, not :Jet." \
498
+ : "<:Joe> expected to be thrown but\n<:Jet> was thrown."
499
+ _should_fail(msg) { verify_ { throw :Jet }.throw?(:Joe) }
500
+ end
501
+ it "raise assertion error when nothing thrown." do
502
+ msg = MINITEST_P ? "Expected :Joe to have been thrown." \
503
+ : "<:Joe> should have been thrown."
504
+ _should_fail(msg) { verify_ { nil }.throw?(:Joe) }
505
+ end
506
+ it "is available with NOT." do
507
+ _should_pass { verify_ { nil }.NOT.throw? }
508
+ msg = MINITEST_P ? "nothing should be thrown but uncaught throw :Jet" \
509
+ : "nothing should be thrown but uncaught throw `Jet'"
510
+ _should_fail(msg) { verify_ { throw :Jet }.NOT.throw? }
511
+ end
512
+ it "sets backtrace when failed." do
513
+ ex = begin verify_ { throw :Jet }.throw?(:Joe)
514
+ rescue ASSERTION => ex; ex end
515
+ _verify_location ex, __LINE__ - 2
516
+ end
517
+ end
518
+
519
+ describe "#file_exist?" do
520
+ it "raises nothing when passed." do
521
+ _should_pass { verify_(__FILE__).file_exist? }
522
+ end
523
+ it "raises assertion error when failed." do
524
+ errmsg = "File '_not_found' not exist."
525
+ _should_fail(errmsg) { verify_("_not_found").file_exist? }
526
+ end
527
+ it "is not available with NOT." do
528
+ ex = assert_raise(NoMethodError) { verify_("_not_found").NOT.file_exist? }
529
+ assert_equal "'file_exist?()' is not available with '.NOT'. please use '.NOT.exist?' instead.", ex.message
530
+ end
531
+ it "sets backtrace when failed." do
532
+ ex = begin verify_("_not_found").file_exist?
533
+ rescue ASSERTION => ex; ex end
534
+ _verify_location ex, __LINE__ - 2
535
+ end
536
+ end
537
+
538
+ describe "#dir_exist?" do
539
+ it "raises nothing when passed." do
540
+ _should_pass { verify_('.').dir_exist? }
541
+ end
542
+ it "raises assertion error when failed." do
543
+ errmsg = "Directory '_not_found' not exist."
544
+ _should_fail(errmsg) { verify_("_not_found").dir_exist? }
545
+ end
546
+ it "is not available with NOT." do
547
+ ex = assert_raise(NoMethodError) { verify_("_not_found").NOT.dir_exist? }
548
+ assert_equal "'dir_exist?()' is not available with '.NOT'. please use '.NOT.exist?' instead.", ex.message
549
+ end
550
+ it "sets backtrace when failed." do
551
+ ex = begin verify_("_not_found").dir_exist?
552
+ rescue ASSERTION => ex; ex end
553
+ _verify_location ex, __LINE__ - 2
554
+ end
555
+ end
556
+
557
+ describe "#exist?" do
558
+ it "is available only with NOT." do
559
+ _should_pass { verify_("_not_found").NOT.exist? }
560
+ msg = "File '#{__FILE__}' exists unexpectedly."
561
+ _should_fail(msg) { verify_(__FILE__).NOT.exist? }
562
+ ex = assert_raise(NoMethodError) { verify_(__FILE__).exist? }
563
+ assert_equal "'exist?()' is available only with '.NOT'. please use 'file_exist?' or 'dir_exist?' instead.", ex.message
564
+ end
565
+ it "sets backtrace when failed." do
566
+ ex = begin verify_(__FILE__).NOT.exist?
567
+ rescue ASSERTION => ex; ex end
568
+ _verify_location ex, __LINE__ - 2
569
+ end
570
+ end
571
+
572
+
573
+ describe '#called?' do
574
+ require 'section9/recorder'
575
+
576
+ class Calc
577
+ def average(*nums) # average() calls total()
578
+ return total(*nums) / nums.length
579
+ end
580
+ def total(*nums)
581
+ t = 0
582
+ nums.each {|n| t += n }
583
+ return t # or return nums.sum
584
+ end
585
+ end
586
+
587
+ def rec
588
+ unless @rec
589
+ @calc = Calc.new
590
+ @rec = Section9::Recorder.new
591
+ @rec.record_method(@calc, :total, :average)
592
+ avg = @calc.average(10, 20, 30, 40) #=> 25
593
+ end
594
+ @rec
595
+ end
596
+
597
+ it "accepts Array object." do
598
+ assert_nothing_raised do
599
+ verify_(rec[0]).called?([@calc, :average, [10, 20, 30, 40], 25])
600
+ end
601
+ end
602
+ it "accepts Hash object." do
603
+ assert_nothing_raised do
604
+ verify_(rec[0]).called?(:obj=>@calc, :name=>:average, :args=>[10, 20, 30, 40], :ret=>25)
605
+ end
606
+ end
607
+ it "raises assertion error when not matched to array argument." do
608
+ #
609
+ ex = assert_raise ASSERTION do
610
+ verify_(rec[0]).called?([nil, :average, [10, 20, 30, 40], 25])
611
+ end
612
+ msg = %r`receiver object: .\n<nil> expected but was\n<#<VerifyObject_TC::Calc:0x[0-9a-f]*>>.`
613
+ assert_match msg, ex.message
614
+ #
615
+ ex = assert_raise ASSERTION do
616
+ verify_(rec[0]).called?([@calc, :total, [10, 20, 30, 40], 25])
617
+ end
618
+ assert_equal "method name: .\n<:total> expected but was\n<:average>.", ex.message
619
+ #
620
+ ex = assert_raise ASSERTION do
621
+ verify_(rec[0]).called?([@calc, :average, [10, 20, 30, 41], 25])
622
+ end
623
+ assert_equal "arguments: .\n<[10, 20, 30, 41]> expected but was\n<[10, 20, 30, 40]>.", ex.message
624
+ #
625
+ ex = assert_raise ASSERTION do
626
+ verify_(rec[0]).called?([@calc, :average, [10, 20, 30, 40], 26])
627
+ end
628
+ assert_equal "return value: .\n<26> expected but was\n<25>.", ex.message
629
+ #
630
+ end
631
+ it "raises assertion error when not matched to hash argument." do
632
+ #
633
+ ex = assert_raise ASSERTION do
634
+ verify_(rec[0]).called?(:obj=>nil)
635
+ end
636
+ assert_match "receiver object:", ex.message
637
+ #
638
+ ex = assert_raise ASSERTION do
639
+ verify_(rec[0]).called?(:name=>:total)
640
+ end
641
+ assert_equal "method name: .\n<:total> expected but was\n<:average>.", ex.message
642
+ #
643
+ ex = assert_raise ASSERTION do
644
+ verify_(rec[0]).called?(:args=>[10, 20, 30, 41])
645
+ end
646
+ assert_equal "arguments: .\n<[10, 20, 30, 41]> expected but was\n<[10, 20, 30, 40]>.", ex.message
647
+ #
648
+ ex = assert_raise ASSERTION do
649
+ verify_(rec[0]).called?(:ret=>26)
650
+ end
651
+ assert_equal "return value: .\n<26> expected but was\n<25>.", ex.message
652
+ #
653
+ end
654
+ it "sets backtrace when raise assertion error." do
655
+ begin
656
+ linenum = __LINE__ + 1
657
+ verify_(rec[0]).called?([@calc, :average, [10, 20, 30, 40], 26])
658
+ rescue ASSERTION => ex
659
+ end
660
+ s = "#{__FILE__}:#{linenum}"
661
+ verify_(ex.backtrace.first) =~ /^#{__FILE__}:#{linenum}/
662
+ end
663
+ it "is not available with '.NOT'." do
664
+ ex = assert_raise(NoMethodError) do
665
+ verify_(rec[0]).NOT.called?(:ret=>25)
666
+ end
667
+ assert_equal "'called?()' is not available with '.NOT'.", ex.message
668
+ end
669
+ end
670
+
671
+ describe "#method_missing" do
672
+ dot = RUBY_VERSION >= '1.9' ? '' : '.'
673
+ it "sends methods to actual and check results." do
674
+ ex = assert_raise(ASSERTION) { verify_("009").empty? }
675
+ msg = "$actual.empty?() == true: failed.\n actual: \"009\"#{dot}"
676
+ assert_equal msg, ex.message
677
+ assert_nothing_raised { verify_("").empty? }
678
+ end
679
+ it "is available with .NOT" do
680
+ assert_nothing_raised { verify_("009").NOT.empty? }
681
+ ex = assert_raise(ASSERTION) { verify_("").NOT.empty? }
682
+ msg = "$actual.empty?() == false: failed.\n actual: \"\"#{dot}"
683
+ assert_equal msg, ex.message
684
+ end
685
+ it "prints arguments if they are passed." do
686
+ ex = assert_raise(ASSERTION) { verify_([10,20,30]).include?(40) }
687
+ msg = "$actual.include?() == true: failed.\n actual: [10, 20, 30]\n args: 40#{dot}"
688
+ assert_equal msg, ex.message
689
+ #
690
+ ex = assert_raise(ASSERTION) { verify_([10,20,30]).NOT.include?(30) }
691
+ msg = "$actual.include?() == false: failed.\n actual: [10, 20, 30]\n args: 30#{dot}"
692
+ assert_equal msg, ex.message
693
+ end
694
+ it "throws NoMethodError when method name doesn't end with '?'." do
695
+ ex = assert_raise(NoMethodError) { verify_("009").size == 3 }
696
+ msg = "undefined method `size' for verify() object."
697
+ assert_equal msg, ex.message
698
+ #
699
+ ex = assert_raise(NoMethodError) { verify_("009").NOT.size == 3 }
700
+ msg = "undefined method `size' for verify().NOT object."
701
+ assert_equal msg, ex.message
702
+ end
703
+ it "raises NoMethodError when method name doesn't end with '?'." do
704
+ ex = assert_raise(NoMethodError) { verify_("009").size == 3 }
705
+ msg = "undefined method `size' for verify() object\."
706
+ assert_equal msg, ex.message
707
+ #
708
+ ex = assert_raise(NoMethodError) { verify_("009").NOT.size == 3 }
709
+ msg = "undefined method `size' for verify().NOT object."
710
+ assert_equal msg, ex.message
711
+ end
712
+ it "raises ArgumentError when method returned non-boolean value." do
713
+ s = ""
714
+ def s.blank?
715
+ self == "" ? 1 : 0
716
+ end
717
+ ex = assert_raise(ArgumentError) { verify_(s).blank? }
718
+ msg = "String#blank?(): expected to return true or false, but returned 1."
719
+ assert_equal msg, ex.message
720
+ end
721
+ end
722
+
723
+ end
724
+
725
+
726
+ class TestCaseHelper_TC < TC
727
+
728
+ describe "#teardown()" do
729
+ it "calls callbacks registerd with at_exit()." do
730
+ assert_equal nil, @_at_end_callbacks
731
+ called = false
732
+ at_end { called = true }
733
+ assert_equal false, called
734
+ teardown()
735
+ assert_equal true, called
736
+ end
737
+ it "clears array of callbacks." do
738
+ at_end { nil }
739
+ at_end { nil }
740
+ at_end { nil }
741
+ assert_equal 3, @_at_end_callbacks.length
742
+ teardown()
743
+ assert_equal 0, @_at_end_callbacks.length
744
+ end
745
+ it "calls revert() method of tmp object if exists." do
746
+ fname = "_tmp_file.txt"
747
+ tmp.file(fname, "009")
748
+ assert File.file?(fname)
749
+ teardown()
750
+ assert ! File.exist?(fname)
751
+ end
752
+ end
753
+
754
+ describe "#tmp()" do
755
+ it "returns Tmp object" do
756
+ assert_kind_of Section9::Tmp, tmp
757
+ end
758
+ end
759
+
760
+ describe "#at_exit()" do
761
+ it "registers callback which will be called by teardown() method." do
762
+ assert_equal nil, @_at_end_callbacks
763
+ called = false
764
+ at_end { called = true }
765
+ assert_kind_of Array, @_at_end_callbacks
766
+ assert_equal 1, @_at_end_callbacks.length
767
+ @_at_end_callbacks[0].call
768
+ assert_equal true, called
769
+ end
770
+ end
771
+
772
+ end