Class: Oktest::AssertionObject
- Inherits:
-
Object
- Object
- Oktest::AssertionObject
show all
- Defined in:
- lib/oktest.rb
Constant Summary
collapse
- NOT_YET =
‘ok()` registers AssertionObject into this. `__done()` removes from this.
{}
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#!=(expected) ⇒ Object
-
#!~(expected) ⇒ Object
-
#<(expected) ⇒ Object
-
#<=(expected) ⇒ Object
-
#==(expected) ⇒ Object
-
#===(expected) ⇒ Object
-
#=~(expected) ⇒ Object
-
#>(expected) ⇒ Object
-
#>=(expected) ⇒ Object
-
#__assert(result) ⇒ Object
-
#attr(name, expected) ⇒ Object
-
#attrs(**keyvals) ⇒ Object
-
#dir_exist? ⇒ Boolean
-
#falsy? ⇒ Boolean
-
#file_exist? ⇒ Boolean
-
#in?(expected) ⇒ Boolean
-
#in_delta?(expected, delta) ⇒ Boolean
-
#initialize(actual, bool, location) ⇒ AssertionObject
constructor
A new instance of AssertionObject.
-
#keyval(key, expected) ⇒ Object
(also: #item)
-
#keyvals(keyvals = {}) ⇒ Object
(also: #items)
-
#length(n) ⇒ Object
-
#method_missing(method_name, *args) ⇒ Object
-
#NOT ⇒ Object
-
#not_exist? ⇒ Boolean
-
#raise!(errcls = nil, errmsg = nil, &b) ⇒ Object
-
#raise?(errcls = nil, errmsg = nil, subclass: false, &b) ⇒ Boolean
-
#same?(expected) ⇒ Boolean
-
#symlink_exist? ⇒ Boolean
-
#throw?(expected) ⇒ Boolean
-
#truthy? ⇒ Boolean
Constructor Details
#initialize(actual, bool, location) ⇒ AssertionObject
Returns a new instance of AssertionObject.
44
45
46
47
48
|
# File 'lib/oktest.rb', line 44
def initialize(actual, bool, location)
@actual = actual
@bool = bool
@location = location
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
# File 'lib/oktest.rb', line 233
def method_missing(method_name, *args)
__done()
method_name.to_s =~ /\?\z/ or
super
begin
ret = @actual.__send__(method_name, *args)
rescue NoMethodError, TypeError => exc
while !exc.backtrace.empty? && exc.backtrace[0].start_with?(__FILE__)
exc.backtrace.shift()
end
raise
end
if ret == true || ret == false
__assert(@bool == ret) {
args = args.empty? ? '' : "(#{args.collect {|x| x.inspect }.join(', ')})"
eq = @bool ? '' : ' == false'
"$<actual>.#{method_name}#{args}#{eq}: failed.\n"\
" $<actual>: #{@actual.inspect}"
}
else
raise TypeError, "ok(): #{@actual.class}##{method_name}() expected to return true or false, but got #{ret.inspect}."
end
self
end
|
Instance Attribute Details
#actual ⇒ Object
Returns the value of attribute actual.
50
51
52
|
# File 'lib/oktest.rb', line 50
def actual
@actual
end
|
#bool ⇒ Object
Returns the value of attribute bool.
50
51
52
|
# File 'lib/oktest.rb', line 50
def bool
@bool
end
|
#location ⇒ Object
Returns the value of attribute location.
50
51
52
|
# File 'lib/oktest.rb', line 50
def location
@location
end
|
Class Method Details
.report_not_yet ⇒ Object
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/oktest.rb', line 57
def self.report_not_yet()
return if NOT_YET.empty?
NOT_YET.each_value do |ass|
s = ass.location ? " (at #{ass.location})" : nil
$stderr.write "** warning: ok() is called but not tested yet#{s}.\n"
end
NOT_YET.clear()
end
|
Instance Method Details
#!=(expected) ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/oktest.rb', line 101
def !=(expected) __done()
__assert(@bool == (@actual != expected)) {
op = @bool ? '!=' : '=='
"$<actual> #{op} $<expected>: failed.\n"\
" $<actual>: #{@actual.inspect}\n"\
" $<expected>: #{expected.inspect}"
}
self
end
|
#!~(expected) ⇒ Object
195
196
197
198
199
200
201
202
|
# File 'lib/oktest.rb', line 195
def !~(expected) __done()
__assert_match(@actual !~ expected, '!~', '=~', expected)
self
end
|
#<(expected) ⇒ Object
155
156
157
158
159
160
161
162
|
# File 'lib/oktest.rb', line 155
def <(expected)
__done()
__assert_op(@actual < expected, '<', '>=', expected)
self
end
|
#<=(expected) ⇒ Object
164
165
166
167
168
169
170
171
|
# File 'lib/oktest.rb', line 164
def <=(expected)
__done()
__assert_op(@actual <= expected, '<=', '>', expected)
self
end
|
#==(expected) ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/oktest.rb', line 79
def ==(expected)
__done()
__assert(@bool == (@actual == expected)) {
if @bool && ! (@actual == expected) \
&& @actual.is_a?(String) && expected.is_a?(String) \
&& (@actual =~ /\n/ || expected =~ /\n/)
diff = Util.unified_diff(expected, @actual, "--- $<expected>\n+++ $<actual>\n")
"$<actual> == $<expected>: failed.\n#{diff}"
else
op = @bool ? '==' : '!='
"$<actual> #{op} $<expected>: failed.\n"\
" $<actual>: #{@actual.inspect}\n"\
" $<expected>: #{expected.inspect}"
end
}
self
end
|
#===(expected) ⇒ Object
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/oktest.rb', line 115
def ===(expected)
__done()
__assert(@bool == (@actual === expected)) {
s = "$<actual> === $<expected>"
s = "!(#{s})" unless @bool
"#{s}: failed.\n"\
" $<actual>: #{@actual.inspect}\n"\
" $<expected>: #{expected.inspect}"
}
self
end
|
#=~(expected) ⇒ Object
186
187
188
189
190
191
192
193
|
# File 'lib/oktest.rb', line 186
def =~(expected)
__done()
__assert_match(@actual =~ expected, '=~', '!~', expected)
self
end
|
#>(expected) ⇒ Object
137
138
139
140
141
142
143
144
|
# File 'lib/oktest.rb', line 137
def >(expected)
__done()
__assert_op(@actual > expected, '>', '<=', expected)
self
end
|
#>=(expected) ⇒ Object
146
147
148
149
150
151
152
153
|
# File 'lib/oktest.rb', line 146
def >=(expected)
__done()
__assert_op(@actual >= expected, '>=', '<', expected)
self
end
|
#__assert(result) ⇒ Object
68
69
70
|
# File 'lib/oktest.rb', line 68
def __assert(result)
raise FAIL_EXCEPTION, yield unless result
end
|
#attr(name, expected) ⇒ Object
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
# File 'lib/oktest.rb', line 411
def attr(name, expected)
__done()
val = @actual.__send__(name)
__assert(@bool == (expected == val)) {
op = @bool ? '==' : '!='
"$<actual>.#{name} #{op} $<expected>: failed.\n"\
" $<actual>.#{name}: #{val.inspect}\n"\
" $<expected>: #{expected.inspect}"\
}
self
end
|
#attrs(**keyvals) ⇒ Object
426
427
428
429
430
431
432
433
|
# File 'lib/oktest.rb', line 426
def attrs(**keyvals)
__done()
keyvals.each {|name, expected| attr(name, expected) }
self
end
|
#dir_exist? ⇒ Boolean
518
519
520
521
522
523
524
525
|
# File 'lib/oktest.rb', line 518
def dir_exist?
__done()
__assert_fs(File.directory?(@actual), "File.directory?($<actual>)")
self
end
|
#falsy? ⇒ Boolean
488
489
490
491
492
493
494
495
496
497
498
499
|
# File 'lib/oktest.rb', line 488
def falsy?
__done()
__assert(@bool == (!!@actual == false)) {
op = @bool ? '==' : '!='
"!!$<actual> #{op} false: failed.\n"\
" $<actual>: #{@actual.inspect}"
}
self
end
|
#file_exist? ⇒ Boolean
509
510
511
512
513
514
515
516
|
# File 'lib/oktest.rb', line 509
def file_exist?
__done()
__assert_fs(File.file?(@actual) , "File.file?($<actual>)")
self
end
|
#in?(expected) ⇒ Boolean
397
398
399
400
401
402
403
404
405
406
407
408
409
|
# File 'lib/oktest.rb', line 397
def in?(expected)
__done()
__assert(@bool == !! expected.include?(@actual)) {
eq = @bool ? '' : ' == false'
"$<expected>.include?($<actual>)#{eq}: failed.\n"\
" $<actual>: #{@actual.inspect}\n"\
" $<expected>: #{expected.inspect}"
}
self
end
|
#in_delta?(expected, delta) ⇒ Boolean
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
# File 'lib/oktest.rb', line 204
def in_delta?(expected, delta)
__done()
__assert(@bool == !!((@actual - expected).abs < delta)) {
eq = @bool ? '' : ' == false'
"($<actual> - $<expected>).abs < #{delta}#{eq}: failed.\n"\
" $<actual>: #{@actual.inspect}\n"\
" $<expected>: #{expected.inspect}\n"\
" ($<actual> - $<expected>).abs: #{(@actual - expected).abs.inspect}"
}
self
end
|
#keyval(key, expected) ⇒ Object
Also known as:
item
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
# File 'lib/oktest.rb', line 435
def keyval(key, expected)
__done()
val = @actual[key]
__assert(@bool == (expected == val)) {
op = @bool ? '==' : '!='
"$<actual>[#{key.inspect}] #{op} $<expected>: failed.\n"\
" $<actual>[#{key.inspect}]: #{val.inspect}\n"\
" $<expected>: #{expected.inspect}"\
}
self
end
|
#keyvals(keyvals = {}) ⇒ Object
Also known as:
items
451
452
453
454
455
456
457
458
|
# File 'lib/oktest.rb', line 451
def keyvals(keyvals={}) __done()
keyvals.each {|name, expected| keyval(name, expected) }
self
end
|
#length(n) ⇒ Object
461
462
463
464
465
466
467
468
469
470
471
472
473
|
# File 'lib/oktest.rb', line 461
def length(n)
__done()
__assert(@bool == (@actual.length == n)) {
op = @bool ? '==' : '!='
"$<actual>.length #{op} #{n}: failed.\n"\
" $<actual>.length: #{@actual.length}\n"\
" $<actual>: #{actual.inspect}"
}
self
end
|
#NOT ⇒ Object
72
73
74
75
76
77
|
# File 'lib/oktest.rb', line 72
def NOT()
@bool = ! @bool
self
end
|
#not_exist? ⇒ Boolean
536
537
538
539
540
541
542
543
544
545
546
|
# File 'lib/oktest.rb', line 536
def not_exist?
__done()
__assert(@bool == ! File.exist?(@actual)) {
"File.exist?($<actual>)#{@bool ? ' == false' : ''}: failed.\n"\
" $<actual>: #{@actual.inspect}"
}
self
end
|
#raise!(errcls = nil, errmsg = nil, &b) ⇒ Object
265
266
267
268
|
# File 'lib/oktest.rb', line 265
def raise!(errcls=nil, errmsg=nil, &b)
return raise?(errcls, errmsg, subclass: true, &b)
end
|
#raise?(errcls = nil, errmsg = nil, subclass: false, &b) ⇒ Boolean
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
# File 'lib/oktest.rb', line 270
def raise?(errcls=nil, errmsg=nil, subclass: false, &b)
__done()
if errmsg.nil? && ! errcls.nil? && ! (errcls.is_a?(Class) && errcls <= Exception)
errmsg = errcls
errcls = RuntimeError
end
proc_obj = @actual
exc = nil
if @bool
begin
proc_obj.call
rescue Exception => exc
if errcls.nil?
nil
elsif exc.class == errcls nil
elsif subclass && exc.class < errcls
nil
else
raise
end
else
__assert(false) { "Expected #{errcls} to be raised but nothing raised." }
end
if errmsg
__assert(errmsg === exc.message) {
op = errmsg.is_a?(Regexp) ? '=~' : '=='
"$<error_message> #{op} #{errmsg.inspect}: failed.\n"\
" $<error_message>: #{exc.message.inspect}"
}
end
yield exc if block_given?()
else
! errmsg or
raise ArgumentError, "#{errmsg.inspect}: NOT.raise?() can't take errmsg."
begin
proc_obj.call
rescue Exception => exc
if errcls.nil?
raise
elsif exc.class == errcls __assert(false) { "#{errcls.inspect} should not be raised but got #{exc.inspect}." }
elsif subclass && exc.class < errcls
__assert(false) { "#{errcls.inspect} should not be raised but got #{exc.inspect}." }
else
raise
end
else
nil
end
end
(class << proc_obj; self; end).class_eval { attr_accessor :exc }
proc_obj.exc = exc
self
end
|
#same?(expected) ⇒ Boolean
219
220
221
222
223
224
225
226
227
228
229
230
231
|
# File 'lib/oktest.rb', line 219
def same?(expected)
__done()
__assert(@bool == !! @actual.equal?(expected)) {
eq = @bool ? '' : ' == false'
"$<actual>.equal?($<expected>)#{eq}: failed.\n"\
" $<actual>: #{@actual.inspect}\n"\
" $<expected>: #{expected.inspect}\n"
}
self
end
|
#symlink_exist? ⇒ Boolean
527
528
529
530
531
532
533
534
|
# File 'lib/oktest.rb', line 527
def symlink_exist?
__done()
__assert_fs(File.symlink?(@actual), "File.symlink?($<actual>)")
self
end
|
#throw?(expected) ⇒ Boolean
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
# File 'lib/oktest.rb', line 350
def throw?(expected)
__done()
proc_obj = @actual
if @bool
expected != nil or
raise ArgumentError, "throw?(#{expected.inspect}): expected tag required."
begin
proc_obj.call
rescue UncaughtThrowError => exc
if exc.tag.equal?(expected)
nil
elsif exc.tag == expected
__assert(false) {
"Thrown tag #{exc.tag.inspect} is equal to but not same as expected.\n"\
" (`#{exc.tag.inspect}.equal?(#{expected.inspect})` should be true but not.)"
}
else
__assert(false) {
"#{expected.inspect} should be thrown but actually #{exc.tag.inspect} thrown."
}
end
else
__assert(false) { "#{expected.inspect} should be thrown but nothing thrown." }
end
else
expected == nil or
raise ArgumentError, "NOT.throw?(#{expected.inspect}): argument should be nil."
begin
proc_obj.call
rescue UncaughtThrowError => exc
__assert(false) {
"Nothing should be thrown but #{exc.tag.inspect} thrown."
}
end
end
self
end
|
#truthy? ⇒ Boolean
475
476
477
478
479
480
481
482
483
484
485
486
|
# File 'lib/oktest.rb', line 475
def truthy?
__done()
__assert(@bool == (!!@actual == true)) {
op = @bool ? '==' : '!='
"!!$<actual> #{op} true: failed.\n"\
" $<actual>: #{@actual.inspect}"
}
self
end
|