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, &b) ⇒ Object
-
#NOT ⇒ Object
-
#not_exist? ⇒ Boolean
-
#raise!(errcls = nil, errmsg = nil, &b) ⇒ Object
-
#raise?(errcls = nil, errmsg = nil, _subclass: false, &b) ⇒ Boolean
-
#raise_nothing? ⇒ 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.
47
48
49
50
51
|
# File 'lib/oktest.rb', line 47
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, &b) ⇒ Object
241
242
243
244
245
246
247
248
|
# File 'lib/oktest.rb', line 241
def method_missing(method_name, *args, **kwargs, &b)
return super unless method_name.to_s =~ /\?\z/
__method_missing(method_name, args, kwargs) {
@actual.__send__(method_name, *args, **kwargs, &b)
}
end
|
Instance Attribute Details
#actual ⇒ Object
Returns the value of attribute actual.
53
54
55
|
# File 'lib/oktest.rb', line 53
def actual
@actual
end
|
#bool ⇒ Object
Returns the value of attribute bool.
53
54
55
|
# File 'lib/oktest.rb', line 53
def bool
@bool
end
|
#location ⇒ Object
Returns the value of attribute location.
53
54
55
|
# File 'lib/oktest.rb', line 53
def location
@location
end
|
Class Method Details
.report_not_yet ⇒ Object
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/oktest.rb', line 60
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
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/oktest.rb', line 104
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
202
203
204
205
206
207
208
209
|
# File 'lib/oktest.rb', line 202
def !~(expected) __done()
__assert_match(@actual !~ expected, '!~', '=~', expected)
self
end
|
#<(expected) ⇒ Object
162
163
164
165
166
167
168
169
|
# File 'lib/oktest.rb', line 162
def <(expected)
__done()
__assert_op(@actual < expected, '<', '>=', expected)
self
end
|
#<=(expected) ⇒ Object
171
172
173
174
175
176
177
178
|
# File 'lib/oktest.rb', line 171
def <=(expected)
__done()
__assert_op(@actual <= expected, '<=', '>', expected)
self
end
|
#==(expected) ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/oktest.rb', line 82
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/oktest.rb', line 118
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
193
194
195
196
197
198
199
200
|
# File 'lib/oktest.rb', line 193
def =~(expected)
__done()
__assert_match(@actual =~ expected, '=~', '!~', expected)
self
end
|
#>(expected) ⇒ Object
144
145
146
147
148
149
150
151
|
# File 'lib/oktest.rb', line 144
def >(expected)
__done()
__assert_op(@actual > expected, '>', '<=', expected)
self
end
|
#>=(expected) ⇒ Object
153
154
155
156
157
158
159
160
|
# File 'lib/oktest.rb', line 153
def >=(expected)
__done()
__assert_op(@actual >= expected, '>=', '<', expected)
self
end
|
#__assert(result) ⇒ Object
71
72
73
|
# File 'lib/oktest.rb', line 71
def __assert(result)
raise FAIL_EXCEPTION, yield unless result
end
|
#attr(name, expected) ⇒ Object
457
458
459
460
461
462
463
464
465
466
467
468
469
470
|
# File 'lib/oktest.rb', line 457
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
472
473
474
475
476
477
478
479
|
# File 'lib/oktest.rb', line 472
def attrs(**keyvals)
__done()
keyvals.each {|name, expected| attr(name, expected) }
self
end
|
#dir_exist? ⇒ Boolean
564
565
566
567
568
569
570
571
|
# File 'lib/oktest.rb', line 564
def dir_exist?
__done()
__assert_fs(File.directory?(@actual), "File.directory?($<actual>)")
self
end
|
#falsy? ⇒ Boolean
534
535
536
537
538
539
540
541
542
543
544
545
|
# File 'lib/oktest.rb', line 534
def falsy?
__done()
__assert(@bool == (!!@actual == false)) {
op = @bool ? '==' : '!='
"!!$<actual> #{op} false: failed.\n"\
" $<actual>: #{@actual.inspect}"
}
self
end
|
#file_exist? ⇒ Boolean
555
556
557
558
559
560
561
562
|
# File 'lib/oktest.rb', line 555
def file_exist?
__done()
__assert_fs(File.file?(@actual) , "File.file?($<actual>)")
self
end
|
#in?(expected) ⇒ Boolean
443
444
445
446
447
448
449
450
451
452
453
454
455
|
# File 'lib/oktest.rb', line 443
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
# File 'lib/oktest.rb', line 211
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
|
# File 'lib/oktest.rb', line 481
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
497
498
499
500
501
502
503
504
|
# File 'lib/oktest.rb', line 497
def keyvals(keyvals={}) __done()
keyvals.each {|name, expected| keyval(name, expected) }
self
end
|
#length(n) ⇒ Object
507
508
509
510
511
512
513
514
515
516
517
518
519
|
# File 'lib/oktest.rb', line 507
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
75
76
77
78
79
80
|
# File 'lib/oktest.rb', line 75
def NOT()
@bool = ! @bool
self
end
|
#not_exist? ⇒ Boolean
582
583
584
585
586
587
588
589
590
591
592
|
# File 'lib/oktest.rb', line 582
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
299
300
301
302
|
# File 'lib/oktest.rb', line 299
def raise!(errcls=nil, errmsg=nil, &b)
return raise?(errcls, errmsg, _subclass: true, &b)
end
|
#raise?(errcls = nil, errmsg = nil, _subclass: false, &b) ⇒ Boolean
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
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
|
# File 'lib/oktest.rb', line 304
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
return exc
end
|
#raise_nothing? ⇒ Boolean
384
385
386
387
388
389
390
391
392
393
394
|
# File 'lib/oktest.rb', line 384
def raise_nothing?()
__done()
proc_obj = @actual
if @bool
proc_obj.call
else
raise OktestError, "`raise_nothing?()` is not available with `.NOT`."
end
end
|
#same?(expected) ⇒ Boolean
226
227
228
229
230
231
232
233
234
235
236
237
238
|
# File 'lib/oktest.rb', line 226
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
573
574
575
576
577
578
579
580
|
# File 'lib/oktest.rb', line 573
def symlink_exist?
__done()
__assert_fs(File.symlink?(@actual), "File.symlink?($<actual>)")
self
end
|
#throw?(expected) ⇒ Boolean
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
|
# File 'lib/oktest.rb', line 396
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
521
522
523
524
525
526
527
528
529
530
531
532
|
# File 'lib/oktest.rb', line 521
def truthy?
__done()
__assert(@bool == (!!@actual == true)) {
op = @bool ? '==' : '!='
"!!$<actual> #{op} true: failed.\n"\
" $<actual>: #{@actual.inspect}"
}
self
end
|