oktest 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/oktest.gemspec ADDED
@@ -0,0 +1,45 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ###
4
+ ### $Release: 1.0.0 $
5
+ ### $License: MIT License $
6
+ ### $Copyright: copyright(c) 2011-2021 kuwata-lab.com all rights reserved $
7
+ ###
8
+
9
+ require 'rubygems'
10
+
11
+ Gem::Specification.new do |s|
12
+ ## package information
13
+ s.name = "oktest"
14
+ s.author = "kwatch"
15
+ s.email = "kwatch@gmail.com"
16
+ s.version = "$Release: 1.0.0 $".split()[1]
17
+ s.license = "MIT"
18
+ s.platform = Gem::Platform::RUBY
19
+ s.homepage = "https://github.com/kwatch/oktest/tree/ruby"
20
+ s.summary = "new style testing library"
21
+ s.description = <<'END'
22
+ Oktest.rb is a new-style testing library for Ruby.
23
+
24
+ * `ok {actual} == expected` style assertion.
25
+ * **Fixture injection** inspired by dependency injection.
26
+ * Structured test specifications like RSpec.
27
+ * Filtering testcases by pattern or tags.
28
+ * Blue/red color instead of green/red for accesability.
29
+ * Small code size (about 2300 lines) and good performance.
30
+
31
+ See https://github.com/kwatch/oktest/tree/ruby/ruby for details.
32
+ END
33
+ s.required_ruby_version = ">= 2.3"
34
+ s.add_dependency "diff-lcs", "~> 1.0"
35
+ s.add_dependency "benry-recorder", "~> 1.0"
36
+
37
+ ## files
38
+ files = Dir['lib/oktest.rb', 'test/*.rb']
39
+ files += ['README.md', 'MIT-LICENSE', 'oktest.gemspec', 'Rakefile.rb']
40
+ files += ['benchmark/Rakefile.rb', 'benchmark/run_all.rb']
41
+ s.files = files
42
+ s.executables = ['oktest']
43
+ s.bindir = 'bin'
44
+ s.test_file = 'test/run_all.rb'
45
+ end
@@ -0,0 +1,817 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ###
4
+ ### $Release: 1.0.0 $
5
+ ### $Copyright: copyright(c) 2011-2021 kuwata-lab.com all rights reserved $
6
+ ### $License: MIT License $
7
+ ###
8
+
9
+ require_relative './initialize'
10
+
11
+
12
+ class AssertionObject_TC < TC
13
+ #include Test::Unit::Assertions
14
+ include Oktest::SpecHelper
15
+
16
+ def FAIL!(errmsg, &b)
17
+ failmsg = "expected to be failed, but succeeded unexpectedly."
18
+ return ERROR!(Oktest::FAIL_EXCEPTION, errmsg, failmsg, &b)
19
+ end
20
+
21
+ def ERROR!(errcls, errmsg=nil, _failmsg=nil, &b)
22
+ exc = nil
23
+ begin
24
+ yield
25
+ rescue Exception => exc_
26
+ exc = exc_
27
+ errcls == exc_.class or
28
+ raise
29
+ else
30
+ _failmsg ||= "#{errcls} expected to be raised, but nothing raised."
31
+ assert false, _failmsg
32
+ end
33
+ #
34
+ if errmsg
35
+ assert errmsg === exc.message,
36
+ ("unexpected error message:\n"\
37
+ " expected: #{errmsg}\n"\
38
+ " actual: #{exc.message}")
39
+ end
40
+ #
41
+ return exc
42
+ end
43
+
44
+ def PASS!()
45
+ yield
46
+ end
47
+
48
+ def should_return_self
49
+ obj = yield
50
+ assert obj.class == Oktest::AssertionObject
51
+ end
52
+
53
+
54
+ describe '.report_not_yet()' do
55
+ it "[!3nksf] reports if 'ok{}' called but assertion not performed." do
56
+ assert Oktest::AssertionObject::NOT_YET.empty?, "should be empty"
57
+ sout, serr = capture do
58
+ Oktest::AssertionObject.report_not_yet()
59
+ end
60
+ assert_eq sout, ""
61
+ assert_eq serr, ""
62
+ #
63
+ lineno = __LINE__ + 1
64
+ ok {1+1}
65
+ assert ! Oktest::AssertionObject::NOT_YET.empty?, "should not be empty"
66
+ sout, serr = capture { Oktest::AssertionObject.report_not_yet() }
67
+ expected = "** warning: ok() is called but not tested yet (at #{__FILE__}:#{lineno}:in"
68
+ assert_eq sout, ""
69
+ assert serr.start_with?(expected), "not matched"
70
+ end
71
+ it "[!f92q4] clears remained objects." do
72
+ ok {1+1}
73
+ assert ! Oktest::AssertionObject::NOT_YET.empty?, "should not be empty"
74
+ sout, serr = capture { Oktest::AssertionObject.report_not_yet() }
75
+ assert Oktest::AssertionObject::NOT_YET.empty?, "should be empty"
76
+ end
77
+ end
78
+
79
+ describe '.NOT()' do
80
+ it "[!g775v] returns self." do
81
+ begin
82
+ should_return_self { ok {1+1}.NOT }
83
+ ensure
84
+ Oktest::AssertionObject::NOT_YET.clear()
85
+ end
86
+ end
87
+ it "[!63dde] toggles internal boolean." do
88
+ begin
89
+ x = ok {1+1}
90
+ assert_eq x.bool, true
91
+ x.NOT
92
+ assert_eq x.bool, false
93
+ ensure
94
+ Oktest::AssertionObject::NOT_YET.clear()
95
+ end
96
+ end
97
+ end
98
+
99
+ describe '#==' do
100
+ it "[!c6p0e] returns self when passed." do
101
+ should_return_self { ok {1+1} == 2 }
102
+ end
103
+ it "[!1iun4] raises assertion error when failed." do
104
+ errmsg = "$<actual> == $<expected>: failed.\n"\
105
+ " $<actual>: 2\n"\
106
+ " $<expected>: 3"
107
+ FAIL!(errmsg) { ok {1+1} == 3 }
108
+ end
109
+ it "[!eyslp] is avaialbe with NOT." do
110
+ PASS! { ok {1+1}.NOT == 3 }
111
+ errmsg = "$<actual> != $<expected>: failed.\n"\
112
+ " $<actual>: 2\n"\
113
+ " $<expected>: 2"
114
+ FAIL!(errmsg) { ok {1+1}.NOT == 2 }
115
+ end
116
+ it "[!3xnqv] shows context diff when both actual and expected are text." do
117
+ expected = "Haruhi\nMikuru\nYuki\n"
118
+ actual = "Haruhi\nMichiru\nYuki\n"
119
+ errmsg = <<'END'
120
+ $<actual> == $<expected>: failed.
121
+ --- $<expected>
122
+ +++ $<actual>
123
+ @@ -1,4 +1,4 @@
124
+ Haruhi
125
+ -Mikuru
126
+ +Michiru
127
+ Yuki
128
+ END
129
+ #errmsg.gsub!(/1,4/, '1,3') if RUBY_VERSION < "1.9.2"
130
+ errmsg.gsub!(/1,4/, '1,3') unless defined?(Diff::LCS)
131
+ FAIL!(errmsg) { ok {actual} == expected }
132
+ end
133
+ end
134
+
135
+ describe '#!=' do
136
+ it "[!iakbb] returns self when passed." do
137
+ should_return_self { ok {1+1} != 3 }
138
+ end
139
+ it "[!90tfb] raises assertion error when failed." do
140
+ #errmsg = "<2> expected to be != to\n<2>."
141
+ errmsg = "$<actual> != $<expected>: failed.\n"\
142
+ " $<actual>: 2\n"\
143
+ " $<expected>: 2"
144
+ FAIL!(errmsg) { ok {1+1} != 2 }
145
+ end
146
+ it "[!l6afg] is avaialbe with NOT." do
147
+ PASS! { ok {1+1}.NOT != 2 }
148
+ #errmsg = "<3> expected but was\n<2>."
149
+ errmsg = "$<actual> == $<expected>: failed.\n"\
150
+ " $<actual>: 2\n"\
151
+ " $<expected>: 3"
152
+ FAIL!(errmsg) { ok {1+1}.NOT != 3 }
153
+ end
154
+ end #if RUBY_VERSION >= "1.9"
155
+
156
+ describe '#===' do
157
+ it "[!uh8bm] returns self when passed." do
158
+ should_return_self { ok {String} === 'str' }
159
+ end
160
+ it "[!42f6a] raises assertion error when failed." do
161
+ errmsg = "$<actual> === $<expected>: failed.\n"\
162
+ " $<actual>: Integer\n"\
163
+ " $<expected>: \"str\""
164
+ FAIL!(errmsg) { ok {Integer} === 'str' }
165
+ end
166
+ it "[!vhvyu] is avaialbe with NOT." do
167
+ PASS! { ok {Integer}.NOT === 'str' }
168
+ errmsg = "!($<actual> === $<expected>): failed.\n"\
169
+ " $<actual>: String\n"\
170
+ " $<expected>: \"str\""
171
+ FAIL!(errmsg) { ok {String}.NOT === 'str' }
172
+ end
173
+ end
174
+
175
+ describe '>' do
176
+ it "[!3j7ty] returns self when passed." do
177
+ should_return_self { ok {2} > 1 }
178
+ end
179
+ it "[!vjjuq] raises assertion error when failed." do
180
+ #errmsg = "Expected 2 to be > 2."
181
+ errmsg = "2 > 2: failed."
182
+ FAIL!(errmsg) { ok {2} > 2 }
183
+ errmsg = "1 > 2: failed."
184
+ FAIL!(errmsg) { ok {1} > 2 }
185
+ #
186
+ errmsg = "\"aaa\" > \"bbb\": failed."
187
+ FAIL!(errmsg) { ok {'aaa'} > 'bbb' }
188
+ end
189
+ it "[!73a0t] is avaialbe with NOT." do
190
+ PASS! { ok {2}.NOT > 2 }
191
+ #errmsg = "Expected 2 to be <= 1."
192
+ errmsg = "2 <= 1: failed."
193
+ FAIL!(errmsg) { ok {2}.NOT > 1 }
194
+ end
195
+ end
196
+
197
+ describe '>=' do
198
+ it "[!75iqw] returns self when passed." do
199
+ should_return_self { ok {2} >= 2 }
200
+ should_return_self { ok {2} >= 1 }
201
+ end
202
+ it "[!isdfc] raises assertion error when failed." do
203
+ #errmsg = "Expected 1 to be >= 2."
204
+ errmsg = "1 >= 2: failed."
205
+ FAIL!(errmsg) { ok {1} >= 2 }
206
+ #
207
+ errmsg = "\"aaa\" >= \"bbb\": failed."
208
+ FAIL!(errmsg) { ok {'aaa'} >= 'bbb' }
209
+ end
210
+ it "[!3dgmh] is avaialbe with NOT." do
211
+ PASS! { ok {1}.NOT >= 2 }
212
+ #errmsg = "Expected 2 to be < 2."
213
+ errmsg = "2 < 2: failed."
214
+ FAIL!(errmsg) { ok {2}.NOT >= 2 }
215
+ end
216
+ end
217
+
218
+ describe '<' do
219
+ it "[!vkwcc] returns self when passed." do
220
+ should_return_self { ok {1} < 2 }
221
+ end
222
+ it "[!ukqa0] raises assertion error when failed." do
223
+ #errmsg = "Expected 2 to be < 2."
224
+ errmsg = "2 < 2: failed."
225
+ FAIL!(errmsg) { ok {2} < 2 }
226
+ errmsg = "2 < 1: failed."
227
+ FAIL!(errmsg) { ok {2} < 1 }
228
+ #
229
+ errmsg = "\"bbb\" < \"aaa\": failed."
230
+ FAIL!(errmsg) { ok {'bbb'} < 'aaa' }
231
+ end
232
+ it "[!gwvdl] is avaialbe with NOT." do
233
+ PASS! { ok {2}.NOT < 2 }
234
+ #errmsg = "Expected 1 to be >= 2."
235
+ errmsg = "1 >= 2: failed."
236
+ FAIL!(errmsg) { ok {1}.NOT < 2 }
237
+ end
238
+ end
239
+
240
+ describe '<=' do
241
+ it "[!yk7t2] returns self when passed." do
242
+ should_return_self { ok {1} <= 2 }
243
+ should_return_self { ok {1} <= 1 }
244
+ end
245
+ it "[!ordwe] raises assertion error when failed." do
246
+ #errmsg = "Expected 2 to be <= 1."
247
+ errmsg = "2 <= 1: failed."
248
+ FAIL!(errmsg) { ok {2} <= 1 }
249
+ #
250
+ errmsg = "\"bbb\" <= \"aaa\": failed."
251
+ FAIL!(errmsg) { ok {'bbb'} <= 'aaa' }
252
+ end
253
+ it "[!mcb9w] is avaialbe with NOT." do
254
+ PASS! { ok {2}.NOT <= 1 }
255
+ #errmsg = "Expected 1 to be > 2."
256
+ errmsg = "1 > 2: failed."
257
+ FAIL!(errmsg) { ok {1}.NOT <= 2 }
258
+ end
259
+ end
260
+
261
+ describe '=~' do
262
+ it "[!acypf] returns self when passed." do
263
+ should_return_self { ok {'SOS'} =~ /^[A-Z]+$/ }
264
+ end
265
+ it "[!xkldu] raises assertion error when failed." do
266
+ #errmsg = 'Expected /^\\d+$/ to match "SOS".'
267
+ errmsg = "$<actual> =~ $<expected>: failed.\n"\
268
+ " $<expected>: /^\\d+$/\n"\
269
+ " $<actual>: <<'END'\n"\
270
+ "SOS\n"\
271
+ "END\n"
272
+ FAIL!(errmsg) { ok {"SOS\n"} =~ /^\d+$/ }
273
+ end
274
+ it "[!2aa6f] is avaialbe with NOT." do
275
+ PASS! { ok {'SOS'}.NOT =~ /^\d+$/ }
276
+ #errmsg = "</\\w+/> expected to not match\n<\"SOS\">."
277
+ errmsg = "$<actual> !~ $<expected>: failed.\n"\
278
+ " $<expected>: /\\w+/\n"\
279
+ " $<actual>: \"SOS\"\n"
280
+ FAIL!(errmsg) { ok {'SOS'}.NOT =~ /\w+/ }
281
+ end if false
282
+ end
283
+
284
+ describe '!~' do
285
+ it "[!xywdr] returns self when passed." do
286
+ should_return_self { ok {'SOS'} !~ /^\d+$/ }
287
+ end
288
+ it "[!58udu] raises assertion error when failed." do
289
+ #errmsg = "</^\\w+$/> expected to not match\n<\"SOS\">."
290
+ errmsg = "$<actual> !~ $<expected>: failed.\n"\
291
+ " $<expected>: /^\\w+$/\n"\
292
+ " $<actual>: \"SOS\"\n"
293
+ FAIL!(errmsg) { ok {'SOS'} !~ /^\w+$/ }
294
+ end
295
+ it "[!iuf5j] is avaialbe with NOT." do
296
+ PASS! { ok {'SOS'}.NOT !~ /^\w+$/ }
297
+ #errmsg = "Expected /\\d+/ to match \"SOS\"."
298
+ errmsg = "$<actual> =~ $<expected>: failed.\n"\
299
+ " $<expected>: /\\d+/\n"\
300
+ " $<actual>: <<'END'\nSOS\nEND\n"
301
+ FAIL!(errmsg) { ok {"SOS\n"}.NOT !~ /\d+/ }
302
+ end
303
+ end #if RUBY_VERSION >= "1.9"
304
+
305
+ describe '#in_delta?' do
306
+ it "[!m0791] returns self when passed." do
307
+ should_return_self { ok {3.14159}.in_delta?(3.141, 0.001) }
308
+ end
309
+ it "[!f3zui] raises assertion error when failed." do
310
+ errmsg = "($<actual> - $<expected>).abs < #{0.1}: failed.\n"\
311
+ " $<actual>: 1.375\n"\
312
+ " $<expected>: 1.5\n"\
313
+ " ($<actual> - $<expected>).abs: #{0.125}"
314
+ FAIL!(errmsg) { ok {1.375}.in_delta?(1.5, 0.1) }
315
+ end
316
+ it "[!t7liw] is avaialbe with NOT." do
317
+ PASS! { ok {1.375}.NOT.in_delta?(1.5, 0.1) }
318
+ errmsg = "($<actual> - $<expected>).abs < #{0.2} == false: failed.\n"\
319
+ " $<actual>: 1.375\n"\
320
+ " $<expected>: 1.5\n"\
321
+ " ($<actual> - $<expected>).abs: #{0.125}"
322
+ FAIL!(errmsg) { ok {1.375}.NOT.in_delta?(1.5, 0.2) }
323
+ end
324
+ end
325
+
326
+ describe '#same?' do
327
+ it "[!yk7zo] returns self when passed." do
328
+ should_return_self { ok {:SOS}.same?(:SOS) }
329
+ end
330
+ it "[!ozbf4] raises assertion error when failed." do
331
+ errmsg = "$<actual>.equal?($<expected>): failed.\n"\
332
+ " $<actual>: \"SOS\"\n"\
333
+ " $<expected>: \"SOS\"\n"
334
+ FAIL!(errmsg) { ok {'SOS'}.same?('SOS') }
335
+ end
336
+ it "[!dwtig] is avaialbe with NOT." do
337
+ PASS! { ok {'SOS'}.NOT.same? 'SOS' }
338
+ errmsg = "$<actual>.equal?($<expected>) == false: failed.\n"\
339
+ " $<actual>: :SOS\n"\
340
+ " $<expected>: :SOS\n"
341
+ FAIL!(errmsg) { ok {:SOS}.NOT.same?(:SOS) }
342
+ end
343
+ end
344
+
345
+ describe '#method_missing()' do
346
+ it "[!7bbrv] returns self when passed." do
347
+ should_return_self { ok {"file.png"}.end_with?(".png") }
348
+ end
349
+ it "[!yjnxb] enables to handle boolean methods." do
350
+ PASS! { ok {""}.empty? }
351
+ PASS! { ok {nil}.nil? }
352
+ PASS! { ok {1}.is_a?(Integer) }
353
+ end
354
+ it "[!ttow6] raises NoMethodError when not a boolean method." do
355
+ ERROR!(NoMethodError) do
356
+ ok {"a"}.start_with
357
+ end
358
+ end
359
+ it "[!f0ekh] skip top of backtrace when NoMethodError raised." do
360
+ exc = ERROR!(NoMethodError) do
361
+ ok {[1]}.start_with?(1)
362
+ end
363
+ assert exc.backtrace[0] !~ /\/oktest\.rbc?:/, "backtrace not skipped"
364
+ assert exc.backtrace[0].start_with?(__FILE__), "backtrace not skipped"
365
+ end
366
+ it "[!cun59] fails when boolean method failed returned false." do
367
+ errmsg = "$<actual>.empty?: failed.\n $<actual>: \"SOS\""
368
+ FAIL!(errmsg) { ok {"SOS"}.empty? }
369
+ errmsg = "$<actual>.nil?: failed.\n $<actual>: \"\""
370
+ FAIL!(errmsg) { ok {""}.nil? }
371
+ errmsg = "$<actual>.is_a?(Integer): failed.\n $<actual>: 3.14"
372
+ FAIL!(errmsg) { ok {3.14}.is_a?(Integer) }
373
+ end
374
+ it "[!4objh] is available with NOT." do
375
+ ok {"SOS"}.NOT.empty?
376
+ ok {"SOS"}.NOT.nil?
377
+ ok {"SOS"}.NOT.is_a?(Integer)
378
+ errmsg = "$<actual>.empty? == false: failed.\n $<actual>: \"\""
379
+ FAIL!(errmsg) { ok {""}.NOT.empty? }
380
+ errmsg = "$<actual>.nil? == false: failed.\n $<actual>: nil"
381
+ FAIL!(errmsg) { ok {nil}.NOT.nil? }
382
+ errmsg = "$<actual>.is_a?(Integer) == false: failed.\n $<actual>: 1"
383
+ FAIL!(errmsg) { ok {1}.NOT.is_a?(Integer) }
384
+ end
385
+ it "[!sljta] raises TypeError when boolean method returned non-boolean value." do
386
+ errmsg = "ok(): String#sos?() expected to return true or false, but got 1."
387
+ ERROR!(TypeError, errmsg) do
388
+ s = "SOS"
389
+ def s.sos?; return 1; end
390
+ ok {s}.sos?
391
+ end
392
+ end
393
+ end
394
+
395
+ describe '#raise?' do
396
+ it "[!y1b28] returns self when passed." do
397
+ pr = proc { "SOS".sos }
398
+ should_return_self { ok {pr}.raise?(NoMethodError, "undefined method `sos' for \"SOS\":String") }
399
+ end
400
+ it "[!2rnni] 1st argument can be error message string or rexp." do
401
+ pr = proc { raise "something wrong" }
402
+ PASS! { ok {pr}.raise?("something wrong") }
403
+ PASS! { ok {pr}.raise?(/something wrong/) }
404
+ #
405
+ pr = proc { raise StandardError, "something wrong" }
406
+ ERROR!(StandardError, "something wrong") { ok {pr}.raise?("something wrong") }
407
+ end
408
+ describe '[!dpv5g] when `ok{}` called...' do
409
+ it "[!yps62] assertion passes when expected exception raised." do
410
+ pr = proc { "SOS".sub() }
411
+ PASS! { ok {pr}.raise?(ArgumentError) }
412
+ pr = proc { 1/0 }
413
+ PASS! { ok {pr}.raise?(ZeroDivisionError) }
414
+ end
415
+ it "[!wbwdo] raises assertion error when nothing raised." do
416
+ pr = proc { nil }
417
+ errmsg = "Expected ArgumentError to be raised but nothing raised."
418
+ FAIL!(errmsg) { ok {pr}.raise?(ArgumentError) }
419
+ end
420
+ it "[!lq6jv] compares error class with '==' operator, not '.is_a?'." do
421
+ pr = proc { "SOS".foobar }
422
+ PASS! { ok {pr}.raise?(NoMethodError) }
423
+ assert NoMethodError < NameError, "NoMethodError extends NameError"
424
+ ERROR!(NoMethodError, /foobar/) { ok {pr}.raise?(NameError) }
425
+ end
426
+ it "[!hwg0z] compares error class with '.is_a?' if 'subclass: true' specified." do
427
+ pr = proc { "SOS".foobar }
428
+ PASS! { ok {pr}.raise?(NoMethodError, nil) }
429
+ assert NoMethodError < NameError, "NoMethodError extends NameError"
430
+ PASS! { ok {pr}.raise?(NameError, nil, subclass: true) }
431
+ end
432
+ it "[!4n3ed] reraises if exception is not matched to specified error class." do
433
+ pr = proc { "SOS".sos }
434
+ errmsg = "undefined method `sos' for \"SOS\":String"
435
+ ERROR!(NoMethodError, errmsg) { ok {pr}.raise?(ArgumentError) }
436
+ end
437
+ it "[!tpxlv] accepts string or regexp as error message." do
438
+ pr = proc { "SOS".sos }
439
+ PASS! { ok {pr}.raise?(NoMethodError, "undefined method `sos' for \"SOS\":String") }
440
+ pr = proc { "SOS".sos }
441
+ PASS! { ok {pr}.raise?(NoMethodError, /^undefined method `sos' for "SOS":String$/) }
442
+ end
443
+ it "[!4c6x3] not check exception class when nil specified as errcls." do
444
+ pr = proc { foobar() }
445
+ PASS! { ok {pr}.raise?(nil, /undefined method `foobar'/) }
446
+ pr = proc { 1/0 }
447
+ PASS! { ok {pr}.raise?(nil, "divided by 0") }
448
+ pr = proc { 1/0 }
449
+ PASS! { ok {pr}.raise?(nil) }
450
+ end
451
+ it "[!dq97o] if block given, call it with exception object." do
452
+ pr = proc { "SOS".foobar }
453
+ exc1 = nil
454
+ ok {pr}.raise?(NoMethodError) do |exc2|
455
+ exc1 = exc2
456
+ end
457
+ assert exc1 != nil
458
+ assert exc1.equal?(pr.exc)
459
+ end
460
+ end
461
+ describe '[!qkr3h] when `ok{}.NOT` called...' do
462
+ it "[!cownv] not support error message." do
463
+ pr = proc { raise "some error" }
464
+ errmsg = "\"some error\": NOT.raise?() can't take errmsg."
465
+ ERROR!(ArgumentError, errmsg) { ok {pr}.NOT.raise?(Exception, "some error") }
466
+ end
467
+ it "[!spzy2] is available with NOT." do
468
+ pr = proc { "SOS".length }
469
+ PASS! { ok {pr}.NOT.raise? }
470
+ end
471
+ it "[!a1a40] assertion passes when nothing raised." do
472
+ pr = proc { nil }
473
+ PASS! { ok {pr}.NOT.raise? }
474
+ end
475
+ it "[!61vtv] assertion fails when specified exception raised." do
476
+ pr = proc { "SOS".foobar }
477
+ errmsg = "NoMethodError should not be raised but got #<NoMethodError: undefined method `foobar' for \"SOS\":String>."
478
+ FAIL!(errmsg) { ok {pr}.NOT.raise?(NoMethodError) }
479
+ end
480
+ it "[!smprc] compares error class with '==' operator, not '.is_a?'." do
481
+ pr = proc { "SOS".foobar }
482
+ FAIL!(/foobar/) { ok {pr}.NOT.raise?(NoMethodError) }
483
+ assert NoMethodError < NameError, "NoMethodError extends NameError"
484
+ ERROR!(NoMethodError) { ok {pr}.NOT.raise?(NameError) }
485
+ end
486
+ it "[!34nd8] compares error class with '.is_a?' if 'subclass: true' specified." do
487
+ pr = proc { "SOS".foobar }
488
+ FAIL!(/foobar/) { ok {pr}.NOT.raise?(NoMethodError, nil) }
489
+ assert NoMethodError < NameError, "NoMethodError extends NameError"
490
+ FAIL!(/foobar/) { ok {pr}.NOT.raise?(NameError, nil, subclass: true) }
491
+ end
492
+ it "[!shxne] reraises exception if different from specified error class." do
493
+ pr = proc { 1/0 }
494
+ errmsg = "divided by 0"
495
+ ERROR!(ZeroDivisionError, errmsg) { ok {pr}.NOT.raise?(NoMethodError) }
496
+ end
497
+ it "[!36032] re-raises exception when errcls is nil." do
498
+ pr = proc { foobar() }
499
+ ERROR!(NoMethodError) { ok {pr}.NOT.raise?(nil) }
500
+ pr = proc { 1/0 }
501
+ ERROR!(ZeroDivisionError) { ok {pr}.NOT.raise?(nil) }
502
+ end
503
+ end
504
+ it "[!vnc6b] sets exception object into '#exc' attribute." do
505
+ pr = proc { "SOS".foobar }
506
+ assert !pr.respond_to?(:exc)
507
+ PASS! { ok {pr}.raise?(NoMethodError) }
508
+ assert pr.respond_to?(:exc)
509
+ assert pr.exc.is_a?(NoMethodError)
510
+ assert_eq pr.exc.message, "undefined method `foobar' for \"SOS\":String"
511
+ #
512
+ pr = proc { nil }
513
+ assert !pr.respond_to?(:exc)
514
+ PASS! { ok {pr}.NOT.raise?(NoMethodError) }
515
+ assert pr.respond_to?(:exc)
516
+ assert_eq pr.exc, nil
517
+ end
518
+ end
519
+
520
+ describe '#raise!' do
521
+ it "[!8k6ee] compares error class by '.is_a?' instead of '=='." do
522
+ pr = proc { "SOS".foobar }
523
+ PASS! { ok {pr}.raise!(NoMethodError) }
524
+ PASS! { ok {pr}.raise!(NameError) }
525
+ ERROR!(NoMethodError, /foobar/) { ok {pr}.raise?(NameError) }
526
+ end
527
+ end
528
+
529
+ describe '#thrown?' do
530
+ it "[!w7935] raises ArgumentError when arg of 'thrown?()' is nil." do
531
+ ERROR!(ArgumentError, "throw?(nil): expected tag required.") do
532
+ pr = proc { throw :sym1 }
533
+ ok {pr}.throw?(nil)
534
+ end
535
+ end
536
+ it "[!lglzr] assertion passes when expected symbol thrown." do
537
+ pr = proc { throw :sym2 }
538
+ ok {pr}.throw?(:sym2)
539
+ assert true, "ok"
540
+ end
541
+ it "[!gf9nx] assertion fails when thrown tag is equal to but not same as expected." do
542
+ pr = proc { throw "sym" }
543
+ expected = ("Thrown tag \"sym\" is equal to but not same as expected.\n"\
544
+ " (`\"sym\".equal?(\"sym\")` should be true but not.)")
545
+ FAIL!(expected) { ok {pr}.throw?("sym") }
546
+ end
547
+ it "[!flgwy] assertion fails when thrown tag is different from expectd." do
548
+ FAIL!(":sym4 should be thrown but actually :sym9 thrown.") do
549
+ pr = proc { throw :sym9 }
550
+ ok {pr}.throw?(:sym4)
551
+ end
552
+ end
553
+ it "[!9ik3x] assertion fails when nothing thrown." do
554
+ pr = proc { nil }
555
+ expected = ":sym5 should be thrown but nothing thrown."
556
+ FAIL!(expected) { ok {pr}.throw?(:sym5) }
557
+ end
558
+ it "[!m03vq] raises ArgumentError when non-nil arg passed to 'NOT.thrown?()'." do
559
+ ERROR!(ArgumentError, "NOT.throw?(:sym6): argument should be nil.") do
560
+ pr = proc { nil }
561
+ ok {pr}.NOT.throw?(:sym6)
562
+ end
563
+ end
564
+ it "[!kxizg] assertion fails when something thrown in 'NOT.throw?()'." do
565
+ pr = proc { throw :sym7 }
566
+ expected = "Nothing should be thrown but :sym7 thrown."
567
+ FAIL!(expected) { ok {pr}.NOT.throw?(nil) }
568
+ end
569
+ it "[!zq9h6] returns self when passed." do
570
+ pr = proc { throw :sym8 }
571
+ should_return_self { ok {pr}.throw?(:sym8) }
572
+ #
573
+ pr = proc { nil }
574
+ should_return_self { ok {pr}.NOT.throw?(nil) }
575
+ end
576
+ end
577
+
578
+ describe '#in?' do
579
+ it "[!jzoxg] returns self when passed." do
580
+ should_return_self { ok {3}.in?(1..5) }
581
+ end
582
+ it "[!9rm8g] raises assertion error when failed." do
583
+ errmsg = "$<expected>.include?($<actual>): failed.\n"\
584
+ " $<actual>: 3\n"\
585
+ " $<expected>: 1..2"
586
+ FAIL!(errmsg) { ok {3}.in?(1..2) }
587
+ end
588
+ it "[!singl] is available with NOT." do
589
+ PASS! { ok {3}.NOT.in?(1..2) }
590
+ errmsg = "$<expected>.include?($<actual>) == false: failed.\n"\
591
+ " $<actual>: 3\n"\
592
+ " $<expected>: 1..5"
593
+ FAIL!(errmsg) { ok {3}.NOT.in?(1..5) }
594
+ end
595
+ end
596
+
597
+ describe '#attr()' do
598
+ it "[!lz3lb] returns self when passed." do
599
+ should_return_self { ok {"SOS"}.attr(:length, 3) }
600
+ end
601
+ it "[!79tgn] raises assertion error when failed." do
602
+ errmsg = "$<actual>.size == $<expected>: failed.\n"\
603
+ " $<actual>.size: 3\n"\
604
+ " $<expected>: 2"
605
+ FAIL!(errmsg) { ok {"SOS"}.attr(:size, 2) }
606
+ end
607
+ it "[!cqnu3] is available with NOT." do
608
+ PASS! { ok {"SOS"}.NOT.attr(:length, 2) }
609
+ errmsg = "$<actual>.size != $<expected>: failed.\n"\
610
+ " $<actual>.size: 3\n"\
611
+ " $<expected>: 3"
612
+ FAIL!(errmsg) { ok {"SOS"}.NOT.attr(:size, 3) }
613
+ end
614
+ end
615
+
616
+ describe '#attrs()' do
617
+ it "[!rtq9f] returns self when passed." do
618
+ should_return_self { ok {"SOS"}.attrs(:length=>3, :size=>3) }
619
+ end
620
+ it "[!7ta0s] raises assertion error when failed." do
621
+ errmsg = "$<actual>.size == $<expected>: failed.\n"\
622
+ " $<actual>.size: 3\n"\
623
+ " $<expected>: 2"
624
+ FAIL!(errmsg) { ok {"SOS"}.attrs(:size=>2) }
625
+ end
626
+ it "[!s0pnk] is available with NOT." do
627
+ PASS! { ok {"SOS"}.NOT.attrs(:length=>2) }
628
+ errmsg = "$<actual>.size != $<expected>: failed.\n"\
629
+ " $<actual>.size: 3\n"\
630
+ " $<expected>: 3"
631
+ FAIL!(errmsg) { ok {"SOS"}.NOT.attrs(:size=>3) }
632
+ end
633
+ end
634
+
635
+ describe '#keyval()' do
636
+ it "[!byebv] returns self when passed." do
637
+ d = {'a'=>1}
638
+ should_return_self { ok {d}.keyval('a', 1) }
639
+ end
640
+ it "[!vtrlz] raises assertion error when failed." do
641
+ d = {'a'=>1}
642
+ errmsg = "$<actual>[\"a\"] == $<expected>: failed.\n"\
643
+ " $<actual>[\"a\"]: 1\n"\
644
+ " $<expected>: \"1\""
645
+ FAIL!(errmsg) { ok {d}.keyval('a', '1') }
646
+ end
647
+ it "[!mmpwz] is available with NOT." do
648
+ d = {'a'=>1}
649
+ PASS! { ok {d}.NOT.keyval('a', '1') }
650
+ errmsg = "$<actual>[\"a\"] != $<expected>: failed.\n"\
651
+ " $<actual>[\"a\"]: 1\n"\
652
+ " $<expected>: 1"
653
+ FAIL!(errmsg) { ok {d}.NOT.keyval('a', 1) }
654
+ end
655
+ end
656
+
657
+ describe '#keyvals()' do
658
+ it "[!vtw22] returns self when passed." do
659
+ d = {'a'=>1, 'b'=>2}
660
+ should_return_self { ok {d}.keyvals('a'=>1, 'b'=>2) }
661
+ end
662
+ it "[!fyvmn] raises assertion error when failed." do
663
+ d = {'a'=>1, 'b'=>2}
664
+ errmsg = "$<actual>[\"a\"] == $<expected>: failed.\n"\
665
+ " $<actual>[\"a\"]: 1\n"\
666
+ " $<expected>: \"1\""
667
+ FAIL!(errmsg) { ok {d}.keyvals('a'=>'1', 'b'=>2) }
668
+ end
669
+ it "[!js2j2] is available with NOT." do
670
+ d = {'a'=>1, 'b'=>2}
671
+ PASS! { ok {d}.NOT.keyvals('a'=>'1') }
672
+ errmsg = "$<actual>[\"a\"] != $<expected>: failed.\n"\
673
+ " $<actual>[\"a\"]: 1\n"\
674
+ " $<expected>: 1"
675
+ FAIL!(errmsg) { ok {d}.NOT.keyvals('a'=>1) }
676
+ end
677
+ end
678
+
679
+ describe '#length' do
680
+ it "[!l9vnv] returns self when passed." do
681
+ should_return_self { ok {"SOS"}.length(3) }
682
+ end
683
+ it "[!1y787] raises assertion error when failed." do
684
+ errmsg = "$<actual>.length == 5: failed.\n"\
685
+ " $<actual>.length: 3\n"\
686
+ " $<actual>: \"SOS\""
687
+ FAIL!(errmsg) { ok {"SOS"}.length(5) }
688
+ end
689
+ it "[!kryx2] is available with NOT." do
690
+ PASS! { ok {"SOS"}.NOT.length(5) }
691
+ errmsg = "$<actual>.length != 3: failed.\n"\
692
+ " $<actual>.length: 3\n"\
693
+ " $<actual>: \"SOS\""
694
+ FAIL!(errmsg) { ok {"SOS"}.NOT.length(3) }
695
+ end
696
+ end
697
+
698
+ describe '#truthy?' do
699
+ it "[!nhmuk] returns self when passed." do
700
+ should_return_self { ok {""}.truthy? }
701
+ end
702
+ it "[!3d94h] raises assertion error when failed." do
703
+ errmsg = "!!$<actual> == true: failed.\n"\
704
+ " $<actual>: nil"
705
+ FAIL!(errmsg) { ok {nil}.truthy? }
706
+ end
707
+ it "[!8rmgp] is available with NOT." do
708
+ PASS! { ok {nil}.NOT.truthy? }
709
+ errmsg = "!!$<actual> != true: failed.\n"\
710
+ " $<actual>: 0"
711
+ FAIL!(errmsg) { ok {0}.NOT.truthy? }
712
+ end
713
+ end
714
+
715
+ describe '#falsy?' do
716
+ it "[!w1vm6] returns self when passed." do
717
+ should_return_self { ok {nil}.falsy? }
718
+ end
719
+ it "[!7o48g] raises assertion error when failed." do
720
+ errmsg = "!!$<actual> == false: failed.\n"\
721
+ " $<actual>: 0"
722
+ FAIL!(errmsg) { ok {0}.falsy? }
723
+ end
724
+ it "[!i44q6] is available with NOT." do
725
+ PASS! { ok {0}.NOT.falsy? }
726
+ errmsg = "!!$<actual> != false: failed.\n"\
727
+ " $<actual>: nil"
728
+ FAIL!(errmsg) { ok {nil}.NOT.falsy? }
729
+ end
730
+ end
731
+
732
+ describe '#file_exist?' do
733
+ it "[!6bcpp] returns self when passed." do
734
+ should_return_self { ok {__FILE__}.file_exist? }
735
+ end
736
+ it "[!69bs0] raises assertion error when failed." do
737
+ errmsg = "File.file?($<actual>): failed.\n"\
738
+ " $<actual>: \".\""
739
+ FAIL!(errmsg) { ok {'.'}.file_exist? }
740
+ end
741
+ it "[!r1mze] is available with NOT." do
742
+ PASS! { ok {'.'}.NOT.file_exist? }
743
+ errmsg = "File.file?($<actual>) == false: failed.\n"\
744
+ " $<actual>: \"#{__FILE__}\""
745
+ FAIL!(errmsg) { ok {__FILE__}.NOT.file_exist? }
746
+ end
747
+ end
748
+
749
+ describe '#dir_exist?' do
750
+ it "[!8qe7u] returns self when passed." do
751
+ should_return_self { ok {'.'}.dir_exist? }
752
+ end
753
+ it "[!vfh7a] raises assertion error when failed." do
754
+ errmsg = "File.directory?($<actual>): failed.\n"\
755
+ " $<actual>: \"#{__FILE__}\""
756
+ FAIL!(errmsg) { ok {__FILE__}.dir_exist? }
757
+ end
758
+ it "[!qtllp] is available with NOT." do
759
+ PASS! { ok {__FILE__}.NOT.dir_exist? }
760
+ errmsg = "File.directory?($<actual>) == false: failed.\n"\
761
+ " $<actual>: \".\""
762
+ FAIL!(errmsg) { ok {'.'}.NOT.dir_exist? }
763
+ end
764
+ end
765
+
766
+ describe '#symlink_exist?' do
767
+ def with_symlink
768
+ linkname = "_sym_#{rand().to_s[2...7]}"
769
+ File.symlink(__FILE__, linkname)
770
+ yield linkname
771
+ ensure
772
+ File.unlink(linkname)
773
+ end
774
+ it "[!ugfi3] returns self when passed." do
775
+ with_symlink do |linkname|
776
+ should_return_self { ok {linkname}.symlink_exist? }
777
+ end
778
+ end
779
+ it "[!qwngl] raises assertion error when failed." do
780
+ with_symlink do |linkname|
781
+ errmsg = "File.symlink?($<actual>): failed.\n"\
782
+ " $<actual>: \"_not_exist\""
783
+ FAIL!(errmsg) { ok {'_not_exist'}.symlink_exist? }
784
+ errmsg = "File.symlink?($<actual>): failed.\n"\
785
+ " $<actual>: \".\""
786
+ FAIL!(errmsg) { ok {'.'}.symlink_exist? }
787
+ end
788
+ end
789
+ it "[!cgpbt] is available with NOT." do
790
+ with_symlink do |linkname|
791
+ PASS! { ok {'_not_exist'}.NOT.symlink_exist? }
792
+ PASS! { ok {'.'}.NOT.symlink_exist? }
793
+ errmsg = "File.symlink?($<actual>) == false: failed.\n"\
794
+ " $<actual>: \"#{linkname}\""
795
+ FAIL!(errmsg) { ok {linkname}.NOT.symlink_exist? }
796
+ end
797
+ end
798
+ end
799
+
800
+ describe '#not_exist?' do
801
+ it "[!1ujag] returns self when passed." do
802
+ should_return_self { ok {'_not_exist'}.not_exist? }
803
+ end
804
+ it "[!ja84s] raises assertion error when failed." do
805
+ errmsg = "File.exist?($<actual>) == false: failed.\n"\
806
+ " $<actual>: \".\""
807
+ FAIL!(errmsg) { ok {'.'}.not_exist? }
808
+ end
809
+ it "[!to5z3] is available with NOT." do
810
+ PASS! { ok {'.'}.NOT.not_exist? }
811
+ errmsg = "File.exist?($<actual>): failed.\n"\
812
+ " $<actual>: \"_not_exist\""
813
+ FAIL!(errmsg) { ok {'_not_exist'}.NOT.not_exist? }
814
+ end
815
+ end
816
+
817
+ end