ae 1.8.2 → 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 +7 -0
- data/HISTORY.md +71 -0
- data/LICENSE.txt +24 -0
- data/README.md +34 -14
- data/demo/01_overview.md +92 -0
- data/demo/02_assertion.md +4 -0
- data/demo/03_assert.md +402 -0
- data/demo/04_subjunctive.md +100 -0
- data/demo/05_expect.md +136 -0
- data/demo/06_counts.md +33 -0
- data/demo/07_matchers.md +34 -0
- data/demo/08_check.md +61 -0
- data/demo/09_legacy.md +122 -0
- data/demo/10_adapters.md +48 -0
- data/demo/applique/ae.rb +1 -0
- data/lib/ae/adapter.rb +3 -4
- data/lib/ae/adapters/minitest.rb +35 -36
- data/lib/ae/adapters/testunit.rb +42 -56
- 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 +22 -20
- 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 -19
- metadata +67 -48
- data/.ruby +0 -0
- data/.yardopts +0 -7
- data/DEMO.rdoc +0 -736
- data/lib/ae.yml +0 -61
data/lib/ae/adapters/testunit.rb
CHANGED
|
@@ -2,70 +2,56 @@ require 'ae'
|
|
|
2
2
|
|
|
3
3
|
AE.assertion_error = ::Test::Unit::AssertionFailedError
|
|
4
4
|
|
|
5
|
-
# TestUnit uses #add_assertion on it's +result+ object to track counts.
|
|
6
|
-
# We capture the result object by overriding the TestCase#run method,
|
|
7
|
-
# store it in a global variable and then use it when AE increments
|
|
8
|
-
# assertion counts.
|
|
9
|
-
#
|
|
10
|
-
# In addition we teach #run to recognize any Exception class that
|
|
11
|
-
# responds to #assertion? in the affirmative as an assertion
|
|
12
|
-
# rather than an error.
|
|
13
|
-
#
|
|
14
5
|
module Test #:nodoc:
|
|
15
6
|
module Unit #:nodoc:
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
# and errors in result.
|
|
22
|
-
def run(result)
|
|
23
|
-
$_test_unit_result = result
|
|
24
|
-
yield(STARTED, name)
|
|
25
|
-
@_result = result
|
|
26
|
-
begin
|
|
27
|
-
setup
|
|
28
|
-
__send__(@method_name)
|
|
29
|
-
rescue AssertionFailedError => e
|
|
30
|
-
add_failure(e.message, e.backtrace)
|
|
31
|
-
rescue Exception => e
|
|
32
|
-
if e.respond_to?(:assertion?) && e.assertion?
|
|
33
|
-
add_failure(e.message, e.backtrace)
|
|
34
|
-
else
|
|
35
|
-
raise if PASSTHROUGH_EXCEPTIONS.include? $!.class
|
|
36
|
-
add_error($!)
|
|
37
|
-
end
|
|
38
|
-
ensure
|
|
39
|
-
begin
|
|
40
|
-
teardown
|
|
41
|
-
rescue AssertionFailedError => e
|
|
42
|
-
add_failure(e.message, e.backtrace)
|
|
43
|
-
rescue Exception => e
|
|
44
|
-
if e.respond_to?(:assertion?) && e.assertion?
|
|
45
|
-
add_failure(e.message, e.backtrace)
|
|
46
|
-
else
|
|
47
|
-
raise if PASSTHROUGH_EXCEPTIONS.include? $!.class
|
|
48
|
-
add_error($!)
|
|
49
|
-
end
|
|
50
|
-
end
|
|
7
|
+
|
|
8
|
+
module AEAssertionHandler
|
|
9
|
+
class << self
|
|
10
|
+
def included(base)
|
|
11
|
+
base.exception_handler(:handle_ae_assertion_error)
|
|
51
12
|
end
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
def handle_ae_assertion_error(exception)
|
|
17
|
+
return false unless exception.respond_to?(:assertion?) && exception.assertion?
|
|
18
|
+
problem_occurred
|
|
19
|
+
add_failure(exception.message, exception.backtrace)
|
|
20
|
+
true
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
class TestCase #:nodoc:
|
|
25
|
+
include AEAssertionHandler
|
|
55
26
|
end
|
|
56
27
|
end
|
|
57
28
|
end
|
|
58
29
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
30
|
+
module AE
|
|
31
|
+
# Test::Unit adapter. Each AE assertion is added to the running
|
|
32
|
+
# test's assertion count.
|
|
33
|
+
module TestUnitAdapter
|
|
34
|
+
# Notes the running test.
|
|
35
|
+
module Test
|
|
36
|
+
def run(*args, **kw, &block)
|
|
37
|
+
previous = ::Thread.current[:ae_testunit_test]
|
|
38
|
+
::Thread.current[:ae_testunit_test] = self
|
|
39
|
+
super
|
|
40
|
+
ensure
|
|
41
|
+
::Thread.current[:ae_testunit_test] = previous
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Adds each AE assertion to the running test's count.
|
|
46
|
+
module Counts
|
|
47
|
+
def increment_counts(pass)
|
|
48
|
+
test = ::Thread.current[:ae_testunit_test]
|
|
49
|
+
test.__send__(:add_assertion) if test
|
|
50
|
+
super
|
|
51
|
+
end
|
|
67
52
|
end
|
|
68
|
-
return counts
|
|
69
53
|
end
|
|
70
54
|
end
|
|
71
55
|
|
|
56
|
+
Test::Unit::TestCase.prepend(AE::TestUnitAdapter::Test)
|
|
57
|
+
AE::Assertor.singleton_class.prepend(AE::TestUnitAdapter::Counts)
|
data/lib/ae/ansi.rb
CHANGED
|
@@ -2,25 +2,27 @@ require 'ansi/diff'
|
|
|
2
2
|
|
|
3
3
|
module AE
|
|
4
4
|
|
|
5
|
-
#
|
|
6
|
-
|
|
5
|
+
# ANSI mode is automatic until it is set: on when standard output is
|
|
6
|
+
# a terminal and the NO_COLOR environment variable is not set.
|
|
7
|
+
@ansi = nil
|
|
7
8
|
|
|
8
9
|
# ANSI mode.
|
|
9
10
|
#
|
|
10
11
|
# @return [Boolean] ANSI mode.
|
|
11
12
|
def self.ansi?
|
|
12
|
-
@ansi
|
|
13
|
+
return @ansi unless @ansi.nil?
|
|
14
|
+
$stdout.respond_to?(:tty?) && $stdout.tty? && ENV.fetch('NO_COLOR', '').empty?
|
|
13
15
|
end
|
|
14
16
|
|
|
15
|
-
# To turn
|
|
16
|
-
# ansi to +false+ in your test helper.
|
|
17
|
+
# To turn ANSI colorized error messages on or off, set
|
|
18
|
+
# ansi to +true+ or +false+ in your test helper. Set it to
|
|
19
|
+
# +nil+ to go back to the automatic setting.
|
|
17
20
|
#
|
|
18
21
|
# @example
|
|
19
22
|
# AE.ansi = false
|
|
20
23
|
#
|
|
21
24
|
def self.ansi=(boolean)
|
|
22
|
-
@ansi = boolean
|
|
25
|
+
@ansi = boolean.nil? ? nil : !!boolean
|
|
23
26
|
end
|
|
24
27
|
|
|
25
28
|
end
|
|
26
|
-
|
data/lib/ae/assert.rb
CHANGED
|
@@ -73,8 +73,7 @@ module AE
|
|
|
73
73
|
# @raise [Assertion]
|
|
74
74
|
# Assertion error with given `message`.
|
|
75
75
|
def flunk(message=nil, backtrace=nil)
|
|
76
|
-
|
|
77
|
-
Assertor.assert(false, message, backtrace || caller)
|
|
76
|
+
Assertor.assert(false, message, false, backtrace || caller)
|
|
78
77
|
end
|
|
79
78
|
|
|
80
79
|
end
|
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
|
|
@@ -161,21 +164,20 @@ class Proc
|
|
|
161
164
|
# TODO: wrong place, change yield?
|
|
162
165
|
def change?
|
|
163
166
|
pre_result = yield
|
|
164
|
-
|
|
167
|
+
call
|
|
165
168
|
post_result = yield
|
|
166
169
|
pre_result != post_result
|
|
167
170
|
end
|
|
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
|