ae 1.9.0 → 1.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/HISTORY.md +52 -0
- data/README.md +9 -1
- data/demo/03_assert.md +110 -8
- data/demo/05_expect.md +36 -4
- data/demo/07_matchers.md +2 -2
- data/demo/09_legacy.md +122 -0
- data/demo/10_adapters.md +48 -0
- data/lib/ae/adapter.rb +3 -4
- data/lib/ae/adapters/minitest.rb +35 -31
- data/lib/ae/adapters/testunit.rb +25 -8
- data/lib/ae/ansi.rb +9 -7
- data/lib/ae/assert.rb +1 -2
- data/lib/ae/assertor.rb +118 -57
- data/lib/ae/check.rb +14 -6
- data/lib/ae/core_ext/helpers.rb +21 -19
- data/lib/ae/legacy.rb +38 -28
- data/lib/ae/must.rb +2 -2
- data/lib/ae/pry.rb +8 -11
- data/lib/ae/should.rb +2 -2
- data/lib/ae/version.rb +1 -1
- metadata +31 -1
data/lib/ae/assertor.rb
CHANGED
|
@@ -59,8 +59,12 @@ module AE
|
|
|
59
59
|
# Basic assertion. This method by-passes all the Assertor fluent
|
|
60
60
|
# constructs and performs the underlying assertion procedure. It
|
|
61
61
|
# is used by Assertor as the end call of an assertion.
|
|
62
|
+
#
|
|
63
|
+
# The +error+ can be a message, an exception, or a Proc that
|
|
64
|
+
# returns either. A Proc is only called if the assertion fails,
|
|
65
|
+
# which saves building a message that is never shown.
|
|
62
66
|
def self.assert(pass, error=nil, negated=nil, backtrace=nil)
|
|
63
|
-
pass = negated ^ !!pass
|
|
67
|
+
pass = !!negated ^ !!pass
|
|
64
68
|
increment_counts(pass)
|
|
65
69
|
if !pass
|
|
66
70
|
backtrace = backtrace || caller
|
|
@@ -72,6 +76,7 @@ module AE
|
|
|
72
76
|
# The intent of the method is to raise an assertion failure
|
|
73
77
|
# class that the test framework supports.
|
|
74
78
|
def self.raise_assertion(error, negated, backtrace=nil)
|
|
79
|
+
error = error.call if ::Proc === error
|
|
75
80
|
if not ::Exception === error
|
|
76
81
|
error = assertion_error.new(error)
|
|
77
82
|
end
|
|
@@ -101,11 +106,30 @@ module AE
|
|
|
101
106
|
# end
|
|
102
107
|
#end
|
|
103
108
|
|
|
104
|
-
#
|
|
105
|
-
|
|
106
|
-
|
|
109
|
+
# Ruby's BasicObject has a few public methods of its own, which would
|
|
110
|
+
# answer a call themselves instead of making an assertion. Making
|
|
111
|
+
# these private sends them through #method_missing like any other
|
|
112
|
+
# method, so `x.assert.instance_eval { @a == 1 }` asserts something
|
|
113
|
+
# about `x`. Only #__send__ and #__id__ stay public, because Ruby
|
|
114
|
+
# relies on them.
|
|
115
|
+
private :==, :!, :!=, :equal?, :instance_eval, :instance_exec
|
|
116
|
+
|
|
117
|
+
# Methods with no arguments whose result is not true, false or nil
|
|
118
|
+
# get a warning, once per line, because the result is probably
|
|
119
|
+
# meant to be compared, as in `x.assert.size == 3`. That compares
|
|
120
|
+
# the assertion's own result, true, and checks nothing.
|
|
121
|
+
def self.chain_warning(where, message)
|
|
122
|
+
@chain_warnings ||= {}
|
|
123
|
+
return if @chain_warnings[where]
|
|
124
|
+
@chain_warnings[where] = true
|
|
125
|
+
::Kernel.warn("#{where}: warning: #{message}")
|
|
107
126
|
end
|
|
108
127
|
|
|
128
|
+
# Color diffs are skipped when the two strings are this large
|
|
129
|
+
# (the product of their lengths), because building one takes
|
|
130
|
+
# time and memory in proportion.
|
|
131
|
+
DIFF_LIMIT = 1_000_000
|
|
132
|
+
|
|
109
133
|
# New Assertor.
|
|
110
134
|
#
|
|
111
135
|
def initialize(delegate, opts={}) #, backtrace)
|
|
@@ -143,23 +167,22 @@ module AE
|
|
|
143
167
|
def assert(*args, &block)
|
|
144
168
|
return self if !block && args.empty?
|
|
145
169
|
|
|
146
|
-
target = args.shift unless block
|
|
147
|
-
error = nil
|
|
148
|
-
|
|
149
170
|
# Block
|
|
150
171
|
if block
|
|
151
|
-
match = args.shift
|
|
152
172
|
result = block.arity > 0 ? block.call(@delegate) : block.call
|
|
153
|
-
if
|
|
154
|
-
|
|
155
|
-
error = @message || "#{match.inspect} == #{result.inspect}"
|
|
173
|
+
if args.empty?
|
|
174
|
+
return __assert__(result) { @message || block.inspect }
|
|
156
175
|
else
|
|
157
|
-
|
|
158
|
-
|
|
176
|
+
# Any expected value counts, even false or nil.
|
|
177
|
+
match = args.shift
|
|
178
|
+
return __assert__(match == result) { @message || "#{__inspect__(match)} == #{__inspect__(result)}" }
|
|
159
179
|
end
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
target = args.shift
|
|
160
183
|
|
|
161
184
|
# Proc-style
|
|
162
|
-
|
|
185
|
+
if proc_assertion?(target)
|
|
163
186
|
pass, error = proc_apply(target)
|
|
164
187
|
|
|
165
188
|
# Assay-style assertions
|
|
@@ -192,26 +215,32 @@ module AE
|
|
|
192
215
|
error = nil
|
|
193
216
|
|
|
194
217
|
if block
|
|
195
|
-
|
|
218
|
+
# Any expected value counts, even false or nil.
|
|
219
|
+
match = args.empty? ? @delegate : args.shift # TODO: see above
|
|
196
220
|
if exception?(match)
|
|
221
|
+
# An exception instance stands for its class.
|
|
222
|
+
klass = ::Class === match ? match : match.class
|
|
197
223
|
$DEBUG, debug = false, $DEBUG # b/c it always spits-out a NameError
|
|
198
224
|
begin
|
|
199
225
|
block.arity > 0 ? block.call(@delegate) : block.call
|
|
200
226
|
pass = false
|
|
201
|
-
error = "#{
|
|
202
|
-
rescue
|
|
227
|
+
error = "#{klass} not raised"
|
|
228
|
+
rescue klass
|
|
203
229
|
pass = true
|
|
204
|
-
error = "#{
|
|
205
|
-
rescue ::
|
|
230
|
+
error = "#{klass} raised"
|
|
231
|
+
rescue ::SignalException, ::SystemExit, ::NoMemoryError
|
|
232
|
+
raise
|
|
233
|
+
rescue ::Exception => e
|
|
206
234
|
pass = false
|
|
207
|
-
error = "#{
|
|
235
|
+
error = "#{klass} expected but #{e.class} was raised: #{e.message}"
|
|
208
236
|
ensure
|
|
209
237
|
$DEBUG = debug
|
|
210
238
|
end
|
|
239
|
+
error = @message || error
|
|
211
240
|
else
|
|
212
|
-
result = block.arity > 0 ? block.call(@
|
|
241
|
+
result = block.arity > 0 ? block.call(@delegate) : block.call
|
|
213
242
|
pass = (match === result)
|
|
214
|
-
error = @message || "#{match
|
|
243
|
+
error = ::Kernel.proc { @message || "#{__inspect__(match)} === #{__inspect__(result)}" }
|
|
215
244
|
end
|
|
216
245
|
|
|
217
246
|
## Matcher
|
|
@@ -227,15 +256,15 @@ module AE
|
|
|
227
256
|
else
|
|
228
257
|
target = args.shift
|
|
229
258
|
pass = (target === @delegate)
|
|
230
|
-
error = @message || "#{target
|
|
259
|
+
error = ::Kernel.proc { @message || "#{__inspect__(target)} === #{__inspect__(@delegate)}" }
|
|
231
260
|
end
|
|
232
261
|
|
|
233
262
|
__assert__(pass, error)
|
|
234
263
|
end
|
|
235
264
|
|
|
236
|
-
#
|
|
265
|
+
# Fail, whether or not the assertion is negated.
|
|
237
266
|
def flunk(message=nil, backtrace=nil)
|
|
238
|
-
|
|
267
|
+
Assertor.assert(false, message || @message, false, backtrace || @backtrace)
|
|
239
268
|
end
|
|
240
269
|
|
|
241
270
|
# Ruby seems to have a quark in it's implementation whereby
|
|
@@ -246,8 +275,8 @@ module AE
|
|
|
246
275
|
end
|
|
247
276
|
|
|
248
277
|
#
|
|
249
|
-
def send(op, *a, &b)
|
|
250
|
-
method_missing(op, *a, &b)
|
|
278
|
+
def send(op, *a, **kw, &b)
|
|
279
|
+
method_missing(op, *a, **kw, &b)
|
|
251
280
|
end
|
|
252
281
|
|
|
253
282
|
#
|
|
@@ -259,19 +288,23 @@ module AE
|
|
|
259
288
|
|
|
260
289
|
# AE-STYLE ASSERTIONS
|
|
261
290
|
|
|
262
|
-
#
|
|
263
|
-
|
|
264
|
-
|
|
291
|
+
# Is `target` a Proc, or any other object that responds to #call?
|
|
292
|
+
# A Hash or Symbol is a value like any other, even though it has
|
|
293
|
+
# #to_proc.
|
|
294
|
+
def proc_assertion?(target)
|
|
295
|
+
::Proc === target || target.respond_to?(:call)
|
|
265
296
|
end
|
|
266
297
|
|
|
267
298
|
#
|
|
268
299
|
def proc_apply(target)
|
|
269
|
-
|
|
270
|
-
pass =
|
|
271
|
-
error =
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
300
|
+
arity = (::Proc === target || ::Method === target) ? target.arity : target.method(:call).arity
|
|
301
|
+
pass = arity != 0 ? target.call(@delegate) : target.call
|
|
302
|
+
error = ::Kernel.proc {
|
|
303
|
+
@message || (
|
|
304
|
+
to_s = target.method(:to_s)
|
|
305
|
+
to_s.arity == 0 ? to_s.call : to_s.call(@negated)
|
|
306
|
+
)
|
|
307
|
+
}
|
|
275
308
|
return pass, error
|
|
276
309
|
end
|
|
277
310
|
|
|
@@ -305,7 +338,7 @@ module AE
|
|
|
305
338
|
#
|
|
306
339
|
def rspec_matcher_apply(matcher)
|
|
307
340
|
pass = matcher.matches?(@delegate)
|
|
308
|
-
error = @message || rspec_matcher_message(matcher)
|
|
341
|
+
error = ::Kernel.proc { @message || rspec_matcher_message(matcher) }
|
|
309
342
|
return pass, error
|
|
310
343
|
end
|
|
311
344
|
|
|
@@ -335,27 +368,55 @@ module AE
|
|
|
335
368
|
# TODO: Should we use a more libreral determination of exception.
|
|
336
369
|
# e.g. <code>respond_to?(:exception)</code>.
|
|
337
370
|
|
|
338
|
-
# Is the +object+ an Exception or an instance of one?
|
|
371
|
+
# Is the +object+ an Exception class or an instance of one?
|
|
339
372
|
def exception?(object)
|
|
340
|
-
::Exception === object
|
|
373
|
+
::Exception === object || (::Class === object && object <= ::Exception)
|
|
341
374
|
end
|
|
342
375
|
|
|
343
376
|
# TODO: In future should probably be `@delegate.public_send(sym, *a, &b)`.
|
|
344
377
|
|
|
345
378
|
# Converts a missing method into an Assertion.
|
|
346
|
-
def method_missing(sym, *args, &block)
|
|
347
|
-
|
|
379
|
+
def method_missing(sym, *args, **kw, &block)
|
|
380
|
+
pass = @delegate.__send__(sym, *args, **kw, &block)
|
|
348
381
|
|
|
349
|
-
|
|
382
|
+
if args.empty? && kw.empty? && !block
|
|
383
|
+
__chain_warning__(sym, pass)
|
|
384
|
+
end
|
|
350
385
|
|
|
351
|
-
__assert__(pass
|
|
386
|
+
__assert__(pass) do
|
|
387
|
+
@message || compare_message(sym, *args) || generic_message(sym, *args, **kw)
|
|
388
|
+
end
|
|
352
389
|
end
|
|
353
390
|
|
|
354
391
|
# TODO: Can the handling of the message be simplified/improved?
|
|
355
392
|
|
|
356
|
-
# Simple assert.
|
|
357
|
-
|
|
358
|
-
|
|
393
|
+
# Simple assert. The failure message can be given as a block,
|
|
394
|
+
# which is only called if the assertion fails.
|
|
395
|
+
def __assert__(pass, error=nil, &message)
|
|
396
|
+
Assertor.assert(pass, error || message, @negated, @backtrace)
|
|
397
|
+
end
|
|
398
|
+
|
|
399
|
+
# Warn about `x.assert.size == 3`, which checks only that `size`
|
|
400
|
+
# is truthy. See Assertor.chain_warning.
|
|
401
|
+
def __chain_warning__(sym, result)
|
|
402
|
+
return if ::TrueClass === result || ::FalseClass === result || ::NilClass === result
|
|
403
|
+
name = sym.to_s
|
|
404
|
+
return unless /\A[a-z_]\w*\z/i.match?(name)
|
|
405
|
+
where = @backtrace.first.to_s.sub(/:in .*\z/, '')
|
|
406
|
+
value = __inspect__(result)
|
|
407
|
+
value = value[0, 40] + '...' if value.size > 43
|
|
408
|
+
Assertor.chain_warning(where,
|
|
409
|
+
"`#{name}` returned #{value}, so AE only checked that it was truthy. " \
|
|
410
|
+
"To compare the result, write `object.#{name}.assert == expected`."
|
|
411
|
+
)
|
|
412
|
+
end
|
|
413
|
+
|
|
414
|
+
# The object's #inspect, or a plain description of it if #inspect
|
|
415
|
+
# is missing or broken. A failure message shouldn't fail itself.
|
|
416
|
+
def __inspect__(object)
|
|
417
|
+
object.inspect
|
|
418
|
+
rescue ::StandardError
|
|
419
|
+
::Kernel.instance_method(:to_s).bind_call(object)
|
|
359
420
|
end
|
|
360
421
|
|
|
361
422
|
#
|
|
@@ -364,18 +425,17 @@ module AE
|
|
|
364
425
|
# Message to use when making a comparion assertion.
|
|
365
426
|
#
|
|
366
427
|
# NOTE: This message utilizes the ANSI gem to produce colorized
|
|
367
|
-
# comparisons
|
|
368
|
-
#
|
|
369
|
-
#
|
|
370
|
-
# which can be set in your test helper.
|
|
428
|
+
# comparisons, when standard output is a terminal and NO_COLOR is
|
|
429
|
+
# not set. To choose for yourself, set `AE.ansi = false` (or true)
|
|
430
|
+
# in your test helper.
|
|
371
431
|
#
|
|
372
432
|
# @param operator [Symbol] operator/method
|
|
373
433
|
#
|
|
374
|
-
# @see
|
|
434
|
+
# @see https://rubyworks.github.io/ansi
|
|
375
435
|
def compare_message(operator, *args, &blk)
|
|
376
436
|
return nil unless COMPARISON_OPERATORS.key?(operator)
|
|
377
437
|
prefix = ""
|
|
378
|
-
a, b = @delegate
|
|
438
|
+
a, b = __inspect__(@delegate), __inspect__(args.first)
|
|
379
439
|
if @negated
|
|
380
440
|
op = COMPARISON_OPERATORS[operator]
|
|
381
441
|
if op
|
|
@@ -384,7 +444,7 @@ module AE
|
|
|
384
444
|
prefix = "NOT "
|
|
385
445
|
end
|
|
386
446
|
end
|
|
387
|
-
if AE.ansi?
|
|
447
|
+
if AE.ansi? && a.size * b.size <= DIFF_LIMIT
|
|
388
448
|
diff = ::ANSI::Diff.new(a,b)
|
|
389
449
|
a = diff.diff1
|
|
390
450
|
b = diff.diff2
|
|
@@ -401,12 +461,13 @@ module AE
|
|
|
401
461
|
# @param op [Symbol] operator/method
|
|
402
462
|
#
|
|
403
463
|
# @return [String] message
|
|
404
|
-
def generic_message(op, *a, &b)
|
|
405
|
-
inspection = @delegate
|
|
464
|
+
def generic_message(op, *a, **kw, &b)
|
|
465
|
+
inspection = __inspect__(@delegate)
|
|
466
|
+
list = a.map { |x| __inspect__(x) } + kw.map { |k, v| "#{k}: #{__inspect__(v)}" }
|
|
406
467
|
if @negated
|
|
407
|
-
"! #{inspection} #{op} #{
|
|
468
|
+
"! #{inspection} #{op} #{list.join(',')}"
|
|
408
469
|
else
|
|
409
|
-
"#{inspection} #{op} #{
|
|
470
|
+
"#{inspection} #{op} #{list.join(',')}"
|
|
410
471
|
end
|
|
411
472
|
#self.class.message(m)[@delegate, *a] )
|
|
412
473
|
end
|
data/lib/ae/check.rb
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
require 'ae/assertor'
|
|
2
|
+
|
|
1
3
|
module AE
|
|
2
4
|
|
|
3
5
|
# The Ok mixin is a reusable assertion helper that
|
|
@@ -20,7 +22,7 @@ module AE
|
|
|
20
22
|
#
|
|
21
23
|
def message(&block)
|
|
22
24
|
if block
|
|
23
|
-
@message =
|
|
25
|
+
@message = block
|
|
24
26
|
end
|
|
25
27
|
@message
|
|
26
28
|
end
|
|
@@ -50,12 +52,18 @@ module AE
|
|
|
50
52
|
|
|
51
53
|
#
|
|
52
54
|
def ok!(*args)
|
|
53
|
-
|
|
55
|
+
check!(args, false, caller)
|
|
54
56
|
end
|
|
55
57
|
|
|
56
58
|
#
|
|
57
59
|
def no!(*args)
|
|
58
|
-
|
|
60
|
+
check!(args, true, caller)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Assert the check for the given arguments, negated or not.
|
|
64
|
+
# The backtrace says where the failure is reported.
|
|
65
|
+
def check!(args, negated, backtrace)
|
|
66
|
+
Assertor.assert(call(*args), ::Kernel.proc { to_s(*args) }, negated, backtrace)
|
|
59
67
|
end
|
|
60
68
|
end
|
|
61
69
|
|
|
@@ -80,7 +88,7 @@ module AE
|
|
|
80
88
|
# end
|
|
81
89
|
#
|
|
82
90
|
def self.define(name, &block)
|
|
83
|
-
table[name] = Check::Proc.new(name, &block)
|
|
91
|
+
table[name] = Check::Proc.new(:name=>name, &block)
|
|
84
92
|
end
|
|
85
93
|
|
|
86
94
|
#
|
|
@@ -127,12 +135,12 @@ module AE
|
|
|
127
135
|
|
|
128
136
|
#
|
|
129
137
|
def ok(*args)
|
|
130
|
-
__check__.
|
|
138
|
+
__check__.check!(args, false, caller)
|
|
131
139
|
end
|
|
132
140
|
|
|
133
141
|
#
|
|
134
142
|
def no(*args)
|
|
135
|
-
__check__.
|
|
143
|
+
__check__.check!(args, true, caller)
|
|
136
144
|
end
|
|
137
145
|
|
|
138
146
|
# Returns the current check.
|
data/lib/ae/core_ext/helpers.rb
CHANGED
|
@@ -40,13 +40,13 @@ module Kernel
|
|
|
40
40
|
end
|
|
41
41
|
end
|
|
42
42
|
|
|
43
|
-
# Word form for #=~. Also can take a block.
|
|
43
|
+
# Word form for #=~. Also can take a block. An object without
|
|
44
|
+
# #=~ matches nothing, as Object#=~ did before Ruby 3.2.
|
|
45
|
+
#
|
|
46
|
+
# Note that String, Symbol and Regexp have their own #match?.
|
|
44
47
|
def match?(value=NoArgument)
|
|
45
|
-
if block_given?
|
|
46
|
-
|
|
47
|
-
else
|
|
48
|
-
self =~ value
|
|
49
|
-
end
|
|
48
|
+
value = yield if block_given?
|
|
49
|
+
respond_to?(:=~) ? (self =~ value) : nil
|
|
50
50
|
end
|
|
51
51
|
|
|
52
52
|
# Broad equality.
|
|
@@ -54,12 +54,15 @@ module Kernel
|
|
|
54
54
|
equal?(x) || eql?(x) || self == x || self === x
|
|
55
55
|
end
|
|
56
56
|
|
|
57
|
-
# Can a message be sent to the receiver successfully?
|
|
58
|
-
|
|
57
|
+
# Can a message be sent to the receiver successfully? Only a
|
|
58
|
+
# missing +method+ counts as failure; a NoMethodError raised from
|
|
59
|
+
# inside the method is a bug, and is raised as usual.
|
|
60
|
+
def send?(method, *args, **kw, &block)
|
|
59
61
|
begin
|
|
60
|
-
__send__(method, *args, &block)
|
|
62
|
+
__send__(method, *args, **kw, &block)
|
|
61
63
|
true
|
|
62
|
-
rescue NoMethodError
|
|
64
|
+
rescue NoMethodError => error
|
|
65
|
+
raise unless error.name == method.to_sym && error.receiver.equal?(self)
|
|
63
66
|
false
|
|
64
67
|
end
|
|
65
68
|
end
|
|
@@ -112,9 +115,9 @@ class Numeric
|
|
|
112
115
|
# Alias for #in_delta.
|
|
113
116
|
alias_method :close?, :in_delta?
|
|
114
117
|
|
|
115
|
-
# Verify epsilon tolerance.
|
|
118
|
+
# Verify epsilon tolerance, relative to the smaller magnitude.
|
|
116
119
|
def in_epsilon?(orig, epsilon=0.001)
|
|
117
|
-
in_delta?(orig, [orig, self].min * epsilon)
|
|
120
|
+
in_delta?(orig, [orig.abs, self.abs].min * epsilon)
|
|
118
121
|
end
|
|
119
122
|
end
|
|
120
123
|
|
|
@@ -144,13 +147,13 @@ class Proc
|
|
|
144
147
|
end
|
|
145
148
|
end
|
|
146
149
|
|
|
147
|
-
#
|
|
150
|
+
# Does calling the procedure throw +sym+? Throwing another
|
|
151
|
+
# symbol counts as no; any error is raised as usual.
|
|
148
152
|
def throws?(sym, *args)
|
|
149
153
|
catch(sym) do
|
|
150
154
|
begin
|
|
151
155
|
call(*args)
|
|
152
|
-
rescue
|
|
153
|
-
rescue NameError # 1.8 exception
|
|
156
|
+
rescue UncaughtThrowError
|
|
154
157
|
end
|
|
155
158
|
return false
|
|
156
159
|
end
|
|
@@ -168,14 +171,13 @@ class Proc
|
|
|
168
171
|
end
|
|
169
172
|
|
|
170
173
|
class Symbol
|
|
171
|
-
# Does the block throw this symbol?
|
|
172
|
-
#
|
|
174
|
+
# Does the block throw this symbol? Throwing another symbol
|
|
175
|
+
# counts as no; any error is raised as usual.
|
|
173
176
|
def thrown?(*args)
|
|
174
177
|
catch(self) do
|
|
175
178
|
begin
|
|
176
179
|
yield(*args)
|
|
177
|
-
rescue
|
|
178
|
-
rescue NameError # 1.8 exception
|
|
180
|
+
rescue UncaughtThrowError
|
|
179
181
|
end
|
|
180
182
|
return false
|
|
181
183
|
end
|
data/lib/ae/legacy.rb
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
require 'ae/assertor'
|
|
2
|
+
|
|
1
3
|
module AE
|
|
2
4
|
|
|
3
5
|
module Legacy #:nodoc:
|
|
@@ -21,7 +23,8 @@ module AE
|
|
|
21
23
|
# @return nothing
|
|
22
24
|
def __assert__(test, msg=nil)
|
|
23
25
|
msg = "failed assertion (no message given)" unless msg
|
|
24
|
-
|
|
26
|
+
Assertor.assert(test, msg, false, caller[1..-1])
|
|
27
|
+
nil
|
|
25
28
|
end
|
|
26
29
|
|
|
27
30
|
private :__assert__
|
|
@@ -31,14 +34,18 @@ module AE
|
|
|
31
34
|
# @example
|
|
32
35
|
# assert [1, 2].include?(5)
|
|
33
36
|
#
|
|
34
|
-
#
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
Assertor.new(self, :backtrace=>caller) # TODO: Probably remove this!
|
|
37
|
+
# With a block and no test, it works like #assert_block.
|
|
38
|
+
#
|
|
39
|
+
# @return [Assertor] if neither `test` nor a block is given
|
|
40
|
+
def assert(*args, &block)
|
|
41
|
+
if args.empty?
|
|
42
|
+
return assert_block(&block) if block
|
|
43
|
+
return Assertor.new(self, :backtrace=>caller) # TODO: Probably remove this!
|
|
41
44
|
end
|
|
45
|
+
test, msg = *args
|
|
46
|
+
msg = "failed assertion (no message given)" unless msg
|
|
47
|
+
Assertor.assert(test, msg, false, caller)
|
|
48
|
+
nil
|
|
42
49
|
end
|
|
43
50
|
|
|
44
51
|
# Passes if the block yields true.
|
|
@@ -52,7 +59,7 @@ module AE
|
|
|
52
59
|
#
|
|
53
60
|
# @return nothing
|
|
54
61
|
def assert_block(msg=nil) # :yields:
|
|
55
|
-
test =
|
|
62
|
+
test = yield
|
|
56
63
|
msg = "assertion failed" unless msg
|
|
57
64
|
__assert__(test, msg)
|
|
58
65
|
end
|
|
@@ -98,8 +105,8 @@ module AE
|
|
|
98
105
|
#
|
|
99
106
|
# @return nothing
|
|
100
107
|
def assert_instance_of(cls, obj, msg=nil)
|
|
101
|
-
test = (cls
|
|
102
|
-
msg = "Expected #{obj} to be
|
|
108
|
+
test = obj.instance_of?(cls)
|
|
109
|
+
msg = "Expected #{obj.inspect} to be an instance of #{cls}" unless msg
|
|
103
110
|
__assert__(test, msg)
|
|
104
111
|
end
|
|
105
112
|
|
|
@@ -155,7 +162,7 @@ module AE
|
|
|
155
162
|
# @return nothing
|
|
156
163
|
def assert_no_match(exp, act, msg=nil)
|
|
157
164
|
test = (act !~ exp)
|
|
158
|
-
msg = "Expected #{act.inspect} to match #{exp.inspect}" unless msg
|
|
165
|
+
msg = "Expected #{act.inspect} not to match #{exp.inspect}" unless msg
|
|
159
166
|
__assert__(test, msg)
|
|
160
167
|
end
|
|
161
168
|
|
|
@@ -211,13 +218,14 @@ module AE
|
|
|
211
218
|
# @raise [Assertion] if test fails
|
|
212
219
|
#
|
|
213
220
|
# @return nothing
|
|
214
|
-
def assert_operator(o1, op, o2, msg=
|
|
221
|
+
def assert_operator(o1, op, o2, msg=nil)
|
|
215
222
|
test = o1.__send__(op, o2)
|
|
216
223
|
msg = "Expected #{o1}.#{op}(#{o2}) to be true" unless msg
|
|
217
224
|
__assert__(test, msg)
|
|
218
225
|
end
|
|
219
226
|
|
|
220
|
-
# Passes if the block raises one of the given exceptions
|
|
227
|
+
# Passes if the block raises one of the given exceptions,
|
|
228
|
+
# or a subclass of one. With none given, StandardError.
|
|
221
229
|
#
|
|
222
230
|
# @example
|
|
223
231
|
# assert_raise RuntimeError, LoadError do
|
|
@@ -226,19 +234,23 @@ module AE
|
|
|
226
234
|
#
|
|
227
235
|
# @raise [Assertion] if test fails
|
|
228
236
|
#
|
|
229
|
-
# @return
|
|
237
|
+
# @return [Exception] the exception raised
|
|
230
238
|
def assert_raises(*args)
|
|
231
|
-
msg
|
|
239
|
+
msg = (Module === args.last ? nil : args.pop)
|
|
240
|
+
exps = args.empty? ? [StandardError] : args
|
|
232
241
|
begin
|
|
233
242
|
yield
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
rescue Exception => e
|
|
237
|
-
test = (exp === e)
|
|
238
|
-
msg = "Expected #{exp} to be raised, but got #{e.class}" unless msg
|
|
239
|
-
__assert__(test, msg)
|
|
243
|
+
rescue *exps => e
|
|
244
|
+
Assertor.assert(true)
|
|
240
245
|
return e
|
|
246
|
+
rescue SignalException, SystemExit, NoMemoryError
|
|
247
|
+
raise
|
|
248
|
+
rescue Exception => e
|
|
249
|
+
msg = "Expected #{exps.join(' or ')} to be raised, but got #{e.class}: #{e.message}" unless msg
|
|
250
|
+
__assert__(false, msg)
|
|
241
251
|
end
|
|
252
|
+
msg = "Expected #{exps.join(' or ')} to be raised" unless msg
|
|
253
|
+
__assert__(false, msg)
|
|
242
254
|
end
|
|
243
255
|
|
|
244
256
|
alias_method :assert_raise, :assert_raises
|
|
@@ -320,19 +332,17 @@ module AE
|
|
|
320
332
|
#
|
|
321
333
|
# @return nothing
|
|
322
334
|
def assert_throws(sym, msg=nil)
|
|
323
|
-
|
|
335
|
+
default = "Expected #{sym.inspect} to have been thrown"
|
|
324
336
|
test = true
|
|
325
337
|
catch(sym) do
|
|
326
338
|
begin
|
|
327
339
|
yield
|
|
328
|
-
rescue
|
|
329
|
-
default += ", not #{e.
|
|
330
|
-
rescue NameError => e # 1.8 exception
|
|
331
|
-
default += ", not #{e.name.inspect}"
|
|
340
|
+
rescue UncaughtThrowError => e
|
|
341
|
+
default += ", not #{e.tag.inspect}"
|
|
332
342
|
end
|
|
333
343
|
test = false
|
|
334
344
|
end
|
|
335
|
-
__assert__(test, msg)
|
|
345
|
+
__assert__(test, msg || default)
|
|
336
346
|
end
|
|
337
347
|
|
|
338
348
|
# Assert that an Array, or any other object the responds to #include?
|
data/lib/ae/must.rb
CHANGED
data/lib/ae/pry.rb
CHANGED
|
@@ -2,11 +2,6 @@ require 'ae/basic_object'
|
|
|
2
2
|
|
|
3
3
|
module Kernel
|
|
4
4
|
|
|
5
|
-
# TODO: Is th cache really neccessry?
|
|
6
|
-
|
|
7
|
-
#
|
|
8
|
-
$PRY_TABLE = {}
|
|
9
|
-
|
|
10
5
|
# Pry allows you to test private and protected methods
|
|
11
6
|
# thru a public-only interface.
|
|
12
7
|
#
|
|
@@ -19,21 +14,23 @@ module Kernel
|
|
|
19
14
|
#
|
|
20
15
|
# @return [Pry] pry functor
|
|
21
16
|
def pry
|
|
22
|
-
|
|
23
|
-
__send__(op, *a, &b)
|
|
17
|
+
Pry.new do |op, *a, **kw, &b|
|
|
18
|
+
__send__(op, *a, **kw, &b)
|
|
24
19
|
end
|
|
25
20
|
end
|
|
26
21
|
|
|
27
22
|
# Pry Functor
|
|
28
23
|
class Pry < AE::BasicObject
|
|
29
|
-
#
|
|
24
|
+
# Send these to the object too, rather than answering them here.
|
|
25
|
+
private :==, :!, :!=, :equal?, :instance_eval, :instance_exec
|
|
26
|
+
|
|
30
27
|
def initialize(&function)
|
|
31
28
|
@function = function
|
|
32
29
|
end
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
|
|
31
|
+
def method_missing(op, *a, **kw, &b)
|
|
32
|
+
@function.call(op, *a, **kw, &b)
|
|
35
33
|
end
|
|
36
34
|
end
|
|
37
35
|
|
|
38
36
|
end
|
|
39
|
-
|
data/lib/ae/should.rb
CHANGED
data/lib/ae/version.rb
CHANGED