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.
46
47
48
49
50
|
# File 'lib/oktest.rb', line 46
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
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
264
265
266
267
268
269
|
# File 'lib/oktest.rb', line 239
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.
52
53
54
|
# File 'lib/oktest.rb', line 52
def actual
@actual
end
|
#bool ⇒ Object
Returns the value of attribute bool.
52
53
54
|
# File 'lib/oktest.rb', line 52
def bool
@bool
end
|
#location ⇒ Object
Returns the value of attribute location.
52
53
54
|
# File 'lib/oktest.rb', line 52
def location
@location
end
|
Class Method Details
.report_not_yet ⇒ Object
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/oktest.rb', line 59
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
103
104
105
106
107
108
109
110
111
112
113
114
115
|
# File 'lib/oktest.rb', line 103
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
201
202
203
204
205
206
207
208
|
# File 'lib/oktest.rb', line 201
def !~(expected)
__done()
__assert_match(@actual !~ expected, '!~', '=~', expected)
self
end
|
#<(expected) ⇒ Object
161
162
163
164
165
166
167
168
|
# File 'lib/oktest.rb', line 161
def <(expected)
__done()
__assert_op(@actual < expected, '<', '>=', expected)
self
end
|
#<=(expected) ⇒ Object
170
171
172
173
174
175
176
177
|
# File 'lib/oktest.rb', line 170
def <=(expected)
__done()
__assert_op(@actual <= expected, '<=', '>', expected)
self
end
|
#==(expected) ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/oktest.rb', line 81
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/oktest.rb', line 117
def ===(expected)
__done()
if @bool == false && @actual.is_a?(Matcher)
raise OktestError, "negative `===` is not available with matcher object."
end
__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
192
193
194
195
196
197
198
199
|
# File 'lib/oktest.rb', line 192
def =~(expected)
__done()
__assert_match(@actual =~ expected, '=~', '!~', expected)
self
end
|
#>(expected) ⇒ Object
143
144
145
146
147
148
149
150
|
# File 'lib/oktest.rb', line 143
def >(expected)
__done()
__assert_op(@actual > expected, '>', '<=', expected)
self
end
|
#>=(expected) ⇒ Object
152
153
154
155
156
157
158
159
|
# File 'lib/oktest.rb', line 152
def >=(expected)
__done()
__assert_op(@actual >= expected, '>=', '<', expected)
self
end
|
#__assert(result) ⇒ Object
70
71
72
|
# File 'lib/oktest.rb', line 70
def __assert(result)
raise FAIL_EXCEPTION, yield unless result
end
|
#attr(name, expected) ⇒ Object
417
418
419
420
421
422
423
424
425
426
427
428
429
430
|
# File 'lib/oktest.rb', line 417
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
432
433
434
435
436
437
438
439
|
# File 'lib/oktest.rb', line 432
def attrs(**keyvals)
__done()
keyvals.each {|name, expected| attr(name, expected) }
self
end
|
#dir_exist? ⇒ Boolean
524
525
526
527
528
529
530
531
|
# File 'lib/oktest.rb', line 524
def dir_exist?
__done()
__assert_fs(File.directory?(@actual), "File.directory?($<actual>)")
self
end
|
#falsy? ⇒ Boolean
494
495
496
497
498
499
500
501
502
503
504
505
|
# File 'lib/oktest.rb', line 494
def falsy?
__done()
__assert(@bool == (!!@actual == false)) {
op = @bool ? '==' : '!='
"!!$<actual> #{op} false: failed.\n"\
" $<actual>: #{@actual.inspect}"
}
self
end
|
#file_exist? ⇒ Boolean
515
516
517
518
519
520
521
522
|
# File 'lib/oktest.rb', line 515
def file_exist?
__done()
__assert_fs(File.file?(@actual) , "File.file?($<actual>)")
self
end
|
#in?(expected) ⇒ Boolean
403
404
405
406
407
408
409
410
411
412
413
414
415
|
# File 'lib/oktest.rb', line 403
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
# File 'lib/oktest.rb', line 210
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
# File 'lib/oktest.rb', line 441
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
457
458
459
460
461
462
463
464
|
# File 'lib/oktest.rb', line 457
def keyvals(keyvals={})
__done()
keyvals.each {|name, expected| keyval(name, expected) }
self
end
|
#length(n) ⇒ Object
467
468
469
470
471
472
473
474
475
476
477
478
479
|
# File 'lib/oktest.rb', line 467
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
74
75
76
77
78
79
|
# File 'lib/oktest.rb', line 74
def NOT()
@bool = ! @bool
self
end
|
#not_exist? ⇒ Boolean
542
543
544
545
546
547
548
549
550
551
552
|
# File 'lib/oktest.rb', line 542
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
271
272
273
274
|
# File 'lib/oktest.rb', line 271
def raise!(errcls=nil, errmsg=nil, &b)
return raise?(errcls, errmsg, _subclass: true, &b)
end
|
#raise?(errcls = nil, errmsg = nil, _subclass: false, &b) ⇒ Boolean
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
349
350
351
352
353
354
|
# File 'lib/oktest.rb', line 276
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
225
226
227
228
229
230
231
232
233
234
235
236
237
|
# File 'lib/oktest.rb', line 225
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
533
534
535
536
537
538
539
540
|
# File 'lib/oktest.rb', line 533
def symlink_exist?
__done()
__assert_fs(File.symlink?(@actual), "File.symlink?($<actual>)")
self
end
|
#throw?(expected) ⇒ Boolean
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
396
397
398
399
400
401
|
# File 'lib/oktest.rb', line 356
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
481
482
483
484
485
486
487
488
489
490
491
492
|
# File 'lib/oktest.rb', line 481
def truthy?
__done()
__assert(@bool == (!!@actual == true)) {
op = @bool ? '==' : '!='
"!!$<actual> #{op} true: failed.\n"\
" $<actual>: #{@actual.inspect}"
}
self
end
|