assay 0.4.1 → 0.5.0
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/.yardopts +7 -0
- data/HISTORY.md +128 -0
- data/LICENSE.txt +22 -0
- data/README.md +287 -0
- data/demo/ae/01_overview.md +98 -0
- data/demo/ae/03_assert.md +398 -0
- data/demo/ae/04_subjunctive.md +100 -0
- data/demo/ae/05_expect.md +136 -0
- data/demo/ae/06_counts.md +33 -0
- data/demo/ae/07_matchers.md +34 -0
- data/demo/ae/08_check.md +61 -0
- data/demo/ae/09_assay.md +124 -0
- data/demo/ae/applique/setup.rb +11 -0
- data/{test → demo/applique}/helper.rb +8 -2
- data/demo/applique/setup.rb +1 -0
- data/demo/bang/01_bang.md +220 -0
- data/demo/core/00_introduction.md +2 -0
- data/demo/core/01_assay_classes/00_introduction.md +2 -0
- data/demo/core/01_assay_classes/01_nil_assay.md +35 -0
- data/demo/core/01_assay_classes/02_boolean_assay.md +35 -0
- data/demo/core/01_assay_classes/03_false_assay.md +19 -0
- data/demo/core/01_assay_classes/03_true_assay.md +20 -0
- data/demo/core/01_assay_classes/04_like_assay.md +19 -0
- data/demo/core/01_assay_classes/05_equal_assay.md +19 -0
- data/demo/core/01_assay_classes/06_unequal_assay.md +35 -0
- data/demo/core/01_assay_classes/07_equality_assay.md +19 -0
- data/demo/core/01_assay_classes/08_identity_assay.md +24 -0
- data/demo/core/01_assay_classes/09_case_assay.md +22 -0
- data/demo/core/01_assay_classes/10_match_assay.md +15 -0
- data/demo/core/01_assay_classes/11_nomatch_assay.md +16 -0
- data/demo/core/01_assay_classes/12_compare_assay.md +24 -0
- data/demo/core/01_assay_classes/13_less_assay.md +29 -0
- data/demo/core/01_assay_classes/14_more_assay.md +29 -0
- data/demo/core/01_assay_classes/15_less_equal_assay.md +30 -0
- data/demo/core/01_assay_classes/16_more_equal_assay.md +29 -0
- data/demo/core/01_assay_classes/17_close_assay.md +42 -0
- data/demo/core/01_assay_classes/17_within_assay.md +42 -0
- data/demo/core/01_assay_classes/18_kind_assay.md +17 -0
- data/demo/core/01_assay_classes/19_instance_assay.md +16 -0
- data/demo/core/01_assay_classes/20_include_assay.md +17 -0
- data/demo/core/01_assay_classes/21_empty_assay.md +15 -0
- data/demo/core/01_assay_classes/22_respond_assay.md +15 -0
- data/demo/core/01_assay_classes/23_execution_assay.md +40 -0
- data/demo/core/01_assay_classes/23_return_assay.md +37 -0
- data/demo/core/01_assay_classes/24_rescue_assay.md +40 -0
- data/demo/core/01_assay_classes/25_raise_assay.md +40 -0
- data/demo/core/01_assay_classes/26_throw_assay.md +47 -0
- data/demo/core/01_assay_classes/27_path_assay.md +20 -0
- data/demo/core/01_assay_classes/28_directory_assay.md +21 -0
- data/demo/core/01_assay_classes/29_file_assay.md +20 -0
- data/demo/core/01_assay_classes/30_output_assay.md +39 -0
- data/demo/core/01_assay_classes/31_stdout_assay.md +21 -0
- data/demo/core/01_assay_classes/32_stderr_assay.md +21 -0
- data/demo/core/02_lookup.md +25 -0
- data/demo/core/03_assertor.md +117 -0
- data/demo/core/04_assay.md +40 -0
- data/demo/fluidity/00_intro.md +7 -0
- data/demo/fluidity/01_should.md +52 -0
- data/demo/fluidity/02_must.md +58 -0
- data/demo/fluidity/03_assert.md +71 -0
- data/demo/fluidity/04_other.md +35 -0
- data/demo/fluidity/05_object_methods.md +102 -0
- data/demo/minitest/01_minitest_extensions.rdoc +217 -0
- data/demo/minitest/02_minitest_assertions.rdoc +250 -0
- data/demo/rspec/00_introduction.md +16 -0
- data/demo/rspec/01_should.md +24 -0
- data/demo/rspec/02_matchers.md +176 -0
- data/demo/rspec/03_have.md +51 -0
- data/demo/rspec/04_expect.md +49 -0
- data/demo/rspec/applique/setup.rb +2 -0
- data/demo/testunit/01_testunit_assertions.md +266 -0
- data/lib/assay/adapter/minitest.rb +32 -21
- data/lib/assay/adapter/testunit.rb +31 -38
- data/lib/assay/ae/assert.rb +77 -0
- data/lib/assay/ae/assertor.rb +453 -0
- data/lib/assay/ae/check.rb +154 -0
- data/lib/assay/ae/core_ext.rb +146 -0
- data/lib/assay/ae/expect.rb +72 -0
- data/lib/assay/ae/must.rb +64 -0
- data/lib/assay/ae/should.rb +58 -0
- data/lib/assay/ae/subjunctive.rb +66 -0
- data/lib/assay/ae.rb +29 -0
- data/lib/assay/assertable.rb +21 -5
- data/lib/assay/assertor.rb +4 -4
- data/lib/assay/bang.rb +147 -0
- data/lib/assay/close_assay.rb +2 -2
- data/lib/assay/core_ext/kernel.rb +4 -8
- data/lib/assay/fluidity/assert.rb +27 -0
- data/lib/assay/fluidity/grammar.rb +157 -0
- data/lib/assay/fluidity/is.rb +18 -0
- data/lib/assay/fluidity/must.rb +38 -0
- data/lib/assay/fluidity/should.rb +32 -0
- data/lib/assay/fluidity/will.rb +36 -0
- data/lib/assay/fluidity.rb +6 -0
- data/lib/assay/less_assay.rb +7 -1
- data/lib/assay/less_equal_assay.rb +7 -1
- data/lib/assay/minitest/assertions.rb +495 -0
- data/lib/assay/minitest/extensions.rb +496 -0
- data/lib/assay/minitest.rb +24 -0
- data/lib/assay/more_assay.rb +7 -1
- data/lib/assay/more_equal_assay.rb +7 -1
- data/lib/assay/rspec/expect.rb +151 -0
- data/lib/assay/rspec/have.rb +201 -0
- data/lib/assay/rspec/matchers.rb +272 -0
- data/lib/assay/rspec/operatics.rb +60 -0
- data/lib/assay/rspec/should.rb +49 -0
- data/lib/assay/rspec.rb +13 -0
- data/lib/assay/testunit/assertions.rb +484 -0
- data/lib/assay/testunit.rb +9 -0
- data/lib/assay/version.rb +3 -0
- data/lib/assay.rb +7 -21
- metadata +211 -80
- data/.ruby +0 -51
- data/COPYING.rdoc +0 -38
- data/HISTORY.rdoc +0 -65
- data/QED.rdoc +0 -1143
- data/README.rdoc +0 -173
- data/lib/assay.yml +0 -51
- data/test/case_compare_assay.rb +0 -59
- data/test/case_empty_assay.rb +0 -51
- data/test/case_equal_assay.rb +0 -53
- data/test/case_equality_assay.rb +0 -55
- data/test/case_false_assay.rb +0 -48
- data/test/case_identity_assay.rb +0 -51
- data/test/case_include_assay.rb +0 -51
- data/test/case_instance_assay.rb +0 -51
- data/test/case_kind_assay.rb +0 -51
- data/test/case_less_assay.rb +0 -53
- data/test/case_less_equal_assay.rb +0 -53
- data/test/case_like_assay.rb +0 -57
- data/test/case_match_assay.rb +0 -45
- data/test/case_more_assay.rb +0 -53
- data/test/case_more_equal_assay.rb +0 -53
- data/test/case_nil_assay.rb +0 -48
- data/test/case_nomatch_assay.rb +0 -47
- data/test/case_raise_assay.rb +0 -51
- data/test/case_respond_assay.rb +0 -51
- data/test/case_throw_assay.rb +0 -51
- data/test/case_true_assay.rb +0 -48
- data/test/case_unequal_assay.rb +0 -55
- data/test/case_within_assay.rb +0 -61
|
@@ -0,0 +1,453 @@
|
|
|
1
|
+
require_relative '../../assay'
|
|
2
|
+
require_relative 'core_ext'
|
|
3
|
+
|
|
4
|
+
module Assay; end
|
|
5
|
+
module Assay::AE
|
|
6
|
+
|
|
7
|
+
# Assertor is the class behind the AE notation. It is an *assertion
|
|
8
|
+
# functor*, a higher-order function: it takes whatever method is called
|
|
9
|
+
# on it, calls that method on the object under test, and asserts the
|
|
10
|
+
# result.
|
|
11
|
+
#
|
|
12
|
+
# x.assert == y # Assertor.new(x) receives `==`
|
|
13
|
+
#
|
|
14
|
+
# It is built on BasicObject, so that the methods every object has
|
|
15
|
+
# (`kind_of?`, `nil?`, `respond_to?`) are assertions too.
|
|
16
|
+
#
|
|
17
|
+
# When Assay has a class for the method, by its operator, a failure is
|
|
18
|
+
# raised as that class: `x.assert == y` fails with an EqualAssay. Any
|
|
19
|
+
# other method fails with a plain Assertion.
|
|
20
|
+
#
|
|
21
|
+
class Assertor < ::BasicObject
|
|
22
|
+
|
|
23
|
+
# Initial settings of assertion counts.
|
|
24
|
+
ZERO_COUNTS = {:total=>0, :pass=>0, :fail=>0}
|
|
25
|
+
|
|
26
|
+
# AE's own query methods, and the Assay class each one fails with.
|
|
27
|
+
# Methods that are an Assay class's operator need no entry.
|
|
28
|
+
ASSAYS = {
|
|
29
|
+
:eq? => :EqualAssay,
|
|
30
|
+
:case? => :CaseAssay,
|
|
31
|
+
:match? => :MatchAssay,
|
|
32
|
+
:equal? => :IdentityAssay,
|
|
33
|
+
:in_delta? => :WithinAssay,
|
|
34
|
+
:in_epsilon? => :CloseAssay,
|
|
35
|
+
:raises? => :RescueAssay,
|
|
36
|
+
:raised? => :RescueAssay,
|
|
37
|
+
:throws? => :ThrowAssay,
|
|
38
|
+
:thrown? => :ThrowAssay
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
# Returns the Hash used to track assertion counts. It is the global
|
|
42
|
+
# `$ASSERTION_COUNTS`, which every notation adds to.
|
|
43
|
+
def self.counts
|
|
44
|
+
$ASSERTION_COUNTS
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Reset assertion counts.
|
|
48
|
+
#
|
|
49
|
+
# reset - Hash which can be used to set counts manually (optional).
|
|
50
|
+
#
|
|
51
|
+
# Returns the Hash of previous counts.
|
|
52
|
+
def self.recount(reset=nil)
|
|
53
|
+
old_counts = counts.dup
|
|
54
|
+
if reset
|
|
55
|
+
reset.each do |type, value|
|
|
56
|
+
counts[type.to_sym] = value
|
|
57
|
+
end
|
|
58
|
+
else
|
|
59
|
+
counts.replace(ZERO_COUNTS.dup)
|
|
60
|
+
end
|
|
61
|
+
return old_counts
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Increment assertion counts. If +pass+ is +true+ then +:total+
|
|
65
|
+
# and +:pass+ are increased. If +pass+ if +false+ then +:total+
|
|
66
|
+
# and +:fail+ are incremented.
|
|
67
|
+
def self.increment_counts(pass)
|
|
68
|
+
counts[:total] += 1
|
|
69
|
+
if pass
|
|
70
|
+
counts[:pass] += 1
|
|
71
|
+
else
|
|
72
|
+
counts[:fail] += 1
|
|
73
|
+
end
|
|
74
|
+
return counts
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Basic assertion. This method by-passes all the Assertor fluent
|
|
78
|
+
# constructs and performs the underlying assertion procedure. It
|
|
79
|
+
# is used by Assertor as the end call of an assertion.
|
|
80
|
+
#
|
|
81
|
+
# The +error+ can be a message, an exception, or a Proc that
|
|
82
|
+
# returns either. A Proc is only called if the assertion fails,
|
|
83
|
+
# which saves building a message that is never shown.
|
|
84
|
+
#
|
|
85
|
+
# The +assay+ is the class of exception to raise for a message. It
|
|
86
|
+
# defaults to Assertion.
|
|
87
|
+
def self.assert(pass, error=nil, negated=nil, backtrace=nil, assay=nil)
|
|
88
|
+
pass = !!negated ^ !!pass
|
|
89
|
+
increment_counts(pass)
|
|
90
|
+
if !pass
|
|
91
|
+
backtrace = backtrace || ::Kernel.caller
|
|
92
|
+
raise_assertion(error, negated, backtrace, assay)
|
|
93
|
+
end
|
|
94
|
+
return pass
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Raise an assertion failure.
|
|
98
|
+
def self.raise_assertion(error, negated, backtrace=nil, assay=nil)
|
|
99
|
+
error = error.call if ::Proc === error
|
|
100
|
+
if not ::Exception === error
|
|
101
|
+
error = (assay || assertion_error).new(error)
|
|
102
|
+
end
|
|
103
|
+
error.set_negative(negated)
|
|
104
|
+
error.set_backtrace(backtrace || ::Kernel.caller)
|
|
105
|
+
error.set_assertion(true)
|
|
106
|
+
::Kernel.raise error
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# Returns the Exception class raised when an assertion fails and no
|
|
110
|
+
# Assay class covers it.
|
|
111
|
+
def self.assertion_error
|
|
112
|
+
::Assertion
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# The Assay class for a method name, if there is one.
|
|
116
|
+
def self.assay_for(sym)
|
|
117
|
+
if name = ASSAYS[sym]
|
|
118
|
+
::Object.const_get(name)
|
|
119
|
+
else
|
|
120
|
+
::Assertion.by_operator(sym)
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# BasicObject has a few public methods of its own, which would
|
|
125
|
+
# answer a call themselves instead of making an assertion. Making
|
|
126
|
+
# these private sends them through #method_missing like any other
|
|
127
|
+
# method, so `x.assert.instance_eval { @a == 1 }` asserts something
|
|
128
|
+
# about `x`. Only #__send__ and #__id__ stay public, because Ruby
|
|
129
|
+
# relies on them.
|
|
130
|
+
private :==, :!, :!=, :equal?, :instance_eval, :instance_exec
|
|
131
|
+
|
|
132
|
+
# Methods with no arguments whose result is not true, false or nil
|
|
133
|
+
# get a warning, once per line, because the result is probably
|
|
134
|
+
# meant to be compared, as in `x.assert.size == 3`. That compares
|
|
135
|
+
# the assertion's own result, true, and checks nothing.
|
|
136
|
+
def self.chain_warning(where, message)
|
|
137
|
+
@chain_warnings ||= {}
|
|
138
|
+
return if @chain_warnings[where]
|
|
139
|
+
@chain_warnings[where] = true
|
|
140
|
+
::Kernel.warn("#{where}: warning: #{message}")
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# Color diffs are skipped when the two strings are this large
|
|
144
|
+
# (the product of their lengths), because building one takes
|
|
145
|
+
# time and memory in proportion.
|
|
146
|
+
DIFF_LIMIT = 1_000_000
|
|
147
|
+
|
|
148
|
+
# New Assertor.
|
|
149
|
+
#
|
|
150
|
+
def initialize(delegate, opts={})
|
|
151
|
+
@delegate = delegate
|
|
152
|
+
@message = opts[:message]
|
|
153
|
+
@backtrace = opts[:backtrace] || ::Kernel.caller
|
|
154
|
+
@negated = !!opts[:negated]
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
# Negate the meaning of the assertion.
|
|
158
|
+
#
|
|
159
|
+
def not(msg=nil)
|
|
160
|
+
@negated = !@negated
|
|
161
|
+
@message = msg if msg
|
|
162
|
+
self
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# Internal assert, provides all functionality associated
|
|
166
|
+
# with external #assert method. (See Assert#assert)
|
|
167
|
+
#
|
|
168
|
+
# Returns +true+ or +false+ based on assertions success.
|
|
169
|
+
#
|
|
170
|
+
def assert(*args, &block)
|
|
171
|
+
return self if !block && args.empty?
|
|
172
|
+
|
|
173
|
+
# Block
|
|
174
|
+
if block
|
|
175
|
+
result = block.arity > 0 ? block.call(@delegate) : block.call
|
|
176
|
+
if args.empty?
|
|
177
|
+
return __assert__(result) { @message || block.inspect }
|
|
178
|
+
else
|
|
179
|
+
# Any expected value counts, even false or nil.
|
|
180
|
+
match = args.shift
|
|
181
|
+
return __assert__(match == result) { @message || "#{__inspect__(match)} == #{__inspect__(result)}" }
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
target = args.shift
|
|
186
|
+
|
|
187
|
+
# Assay matchers, such as `EqualAssay[10]`
|
|
188
|
+
if ::Assay::Assertor === target
|
|
189
|
+
return @negated ? (target !~ @delegate) : (target =~ @delegate)
|
|
190
|
+
|
|
191
|
+
# Proc-style
|
|
192
|
+
elsif proc_assertion?(target)
|
|
193
|
+
pass, error = proc_apply(target)
|
|
194
|
+
|
|
195
|
+
# RSpec-style matchers
|
|
196
|
+
elsif rspec_matcher?(target)
|
|
197
|
+
pass, error = rspec_matcher_apply(target)
|
|
198
|
+
|
|
199
|
+
# Truthiness
|
|
200
|
+
else
|
|
201
|
+
pass = target # truthiness
|
|
202
|
+
error = args.shift # optional message for TestUnit compatiability
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
__assert__(pass, error)
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
# Internal expect, provides all functionality associated
|
|
209
|
+
# with external #expect method. (See Expect#expect)
|
|
210
|
+
#
|
|
211
|
+
def expect(*args, &block)
|
|
212
|
+
return self if !block && args.empty? # same as #assert
|
|
213
|
+
|
|
214
|
+
pass = false
|
|
215
|
+
error = nil
|
|
216
|
+
assay = nil
|
|
217
|
+
|
|
218
|
+
if block
|
|
219
|
+
# Any expected value counts, even false or nil.
|
|
220
|
+
match = args.empty? ? @delegate : args.shift
|
|
221
|
+
if exception?(match)
|
|
222
|
+
# An exception instance stands for its class.
|
|
223
|
+
klass = ::Class === match ? match : match.class
|
|
224
|
+
assay = ::RescueAssay
|
|
225
|
+
$DEBUG, debug = false, $DEBUG # b/c it always spits-out a NameError
|
|
226
|
+
begin
|
|
227
|
+
block.arity > 0 ? block.call(@delegate) : block.call
|
|
228
|
+
pass = false
|
|
229
|
+
error = "#{klass} not raised"
|
|
230
|
+
rescue klass
|
|
231
|
+
pass = true
|
|
232
|
+
error = "#{klass} raised"
|
|
233
|
+
rescue ::SignalException, ::SystemExit, ::NoMemoryError
|
|
234
|
+
::Kernel.raise
|
|
235
|
+
rescue ::Exception => e
|
|
236
|
+
pass = false
|
|
237
|
+
error = "#{klass} expected but #{e.class} was raised: #{e.message}"
|
|
238
|
+
ensure
|
|
239
|
+
$DEBUG = debug
|
|
240
|
+
end
|
|
241
|
+
error = @message || error
|
|
242
|
+
else
|
|
243
|
+
result = block.arity > 0 ? block.call(@delegate) : block.call
|
|
244
|
+
pass = (match === result)
|
|
245
|
+
assay = ::CaseAssay
|
|
246
|
+
error = ::Kernel.proc { @message || "#{__inspect__(match)} === #{__inspect__(result)}" }
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
# Case Equality
|
|
250
|
+
else
|
|
251
|
+
target = args.shift
|
|
252
|
+
pass = (target === @delegate)
|
|
253
|
+
assay = ::CaseAssay
|
|
254
|
+
error = ::Kernel.proc { @message || "#{__inspect__(target)} === #{__inspect__(@delegate)}" }
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
__assert__(pass, error, assay)
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
# Fail, whether or not the assertion is negated.
|
|
261
|
+
def flunk(message=nil, backtrace=nil)
|
|
262
|
+
Assertor.assert(false, message || @message, false, backtrace || @backtrace)
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
# Without this Ruby skips #method_missing for `=~`.
|
|
266
|
+
def =~(match)
|
|
267
|
+
method_missing(:"=~", match)
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
#
|
|
271
|
+
def send(op, *a, **kw, &b)
|
|
272
|
+
method_missing(op, *a, **kw, &b)
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
#
|
|
276
|
+
def inspect
|
|
277
|
+
@delegate.inspect
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
private
|
|
281
|
+
|
|
282
|
+
# Is `target` a Proc, or any other object that responds to #call?
|
|
283
|
+
# A Hash or Symbol is a value like any other, even though it has
|
|
284
|
+
# #to_proc.
|
|
285
|
+
def proc_assertion?(target)
|
|
286
|
+
::Proc === target || target.respond_to?(:call)
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
#
|
|
290
|
+
def proc_apply(target)
|
|
291
|
+
arity = (::Proc === target || ::Method === target) ? target.arity : target.method(:call).arity
|
|
292
|
+
pass = arity != 0 ? target.call(@delegate) : target.call
|
|
293
|
+
error = ::Kernel.proc {
|
|
294
|
+
@message || (
|
|
295
|
+
to_s = target.method(:to_s)
|
|
296
|
+
to_s.arity == 0 ? to_s.call : to_s.call(@negated)
|
|
297
|
+
)
|
|
298
|
+
}
|
|
299
|
+
return pass, error
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
# Is `target` an RSpec-style matcher?
|
|
303
|
+
def rspec_matcher?(target)
|
|
304
|
+
target.respond_to?(:matches?)
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
#
|
|
308
|
+
def rspec_matcher_apply(matcher)
|
|
309
|
+
pass = matcher.matches?(@delegate)
|
|
310
|
+
error = ::Kernel.proc { @message || rspec_matcher_message(matcher) }
|
|
311
|
+
return pass, error
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
#
|
|
315
|
+
def rspec_matcher_message(matcher)
|
|
316
|
+
if @negated
|
|
317
|
+
if matcher.respond_to?(:failure_message_when_negated)
|
|
318
|
+
return matcher.failure_message_when_negated
|
|
319
|
+
end
|
|
320
|
+
if matcher.respond_to?(:failure_message_for_should_not)
|
|
321
|
+
return matcher.failure_message_for_should_not
|
|
322
|
+
end
|
|
323
|
+
if matcher.respond_to?(:negative_failure_message)
|
|
324
|
+
return matcher.negative_failure_message
|
|
325
|
+
end
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
if matcher.respond_to?(:failure_message)
|
|
329
|
+
return matcher.failure_message
|
|
330
|
+
end
|
|
331
|
+
if matcher.respond_to?(:failure_message_for_should)
|
|
332
|
+
return matcher.failure_message_for_should
|
|
333
|
+
end
|
|
334
|
+
|
|
335
|
+
return matcher.to_s
|
|
336
|
+
end
|
|
337
|
+
|
|
338
|
+
# Is the +object+ an Exception class or an instance of one?
|
|
339
|
+
def exception?(object)
|
|
340
|
+
::Exception === object || (::Class === object && object <= ::Exception)
|
|
341
|
+
end
|
|
342
|
+
|
|
343
|
+
# Converts a missing method into an assertion: the method is called
|
|
344
|
+
# on the object under test and its result asserted.
|
|
345
|
+
#
|
|
346
|
+
# If the object has no such method but Assay has an assertion by
|
|
347
|
+
# that operator or name (`x.assert.like?(y)`, `x.assert.kind_of(K)`),
|
|
348
|
+
# the Assay class makes the test.
|
|
349
|
+
def method_missing(sym, *args, **kw, &block)
|
|
350
|
+
assay = Assertor.assay_for(sym)
|
|
351
|
+
|
|
352
|
+
if !__responds__?(sym) && (found = assay || ::Assertion.by_name(sym))
|
|
353
|
+
assay = found
|
|
354
|
+
list = kw.empty? ? args : args + [kw]
|
|
355
|
+
pass = assay.pass?(@delegate, *list, &block)
|
|
356
|
+
else
|
|
357
|
+
pass = @delegate.__send__(sym, *args, **kw, &block)
|
|
358
|
+
|
|
359
|
+
if args.empty? && kw.empty? && !block
|
|
360
|
+
__chain_warning__(sym, pass)
|
|
361
|
+
end
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
__assert__(pass, nil, assay) do
|
|
365
|
+
@message || compare_message(sym, *args) || generic_message(sym, *args, **kw)
|
|
366
|
+
end
|
|
367
|
+
end
|
|
368
|
+
|
|
369
|
+
# Does the object under test have the method, public or not? An
|
|
370
|
+
# object that can't say is taken to have it.
|
|
371
|
+
def __responds__?(sym)
|
|
372
|
+
@delegate.respond_to?(sym, true)
|
|
373
|
+
rescue ::NoMethodError
|
|
374
|
+
true
|
|
375
|
+
end
|
|
376
|
+
|
|
377
|
+
# Simple assert. The failure message can be given as a block,
|
|
378
|
+
# which is only called if the assertion fails.
|
|
379
|
+
def __assert__(pass, error=nil, assay=nil, &message)
|
|
380
|
+
Assertor.assert(pass, error || message, @negated, @backtrace, assay)
|
|
381
|
+
end
|
|
382
|
+
|
|
383
|
+
# Warn about `x.assert.size == 3`, which checks only that `size`
|
|
384
|
+
# is truthy. See Assertor.chain_warning.
|
|
385
|
+
def __chain_warning__(sym, result)
|
|
386
|
+
return if ::TrueClass === result || ::FalseClass === result || ::NilClass === result
|
|
387
|
+
name = sym.to_s
|
|
388
|
+
return unless /\A[a-z_]\w*\z/i.match?(name)
|
|
389
|
+
where = @backtrace.first.to_s.sub(/:in .*\z/, '')
|
|
390
|
+
value = __inspect__(result)
|
|
391
|
+
value = value[0, 40] + '...' if value.size > 43
|
|
392
|
+
Assertor.chain_warning(where,
|
|
393
|
+
"`#{name}` returned #{value}, so only its truthiness was checked. " \
|
|
394
|
+
"To compare the result, write `object.#{name}.assert == expected`."
|
|
395
|
+
)
|
|
396
|
+
end
|
|
397
|
+
|
|
398
|
+
# The object's #inspect, or a plain description of it if #inspect
|
|
399
|
+
# is missing or broken. A failure message shouldn't fail itself.
|
|
400
|
+
def __inspect__(object)
|
|
401
|
+
object.inspect
|
|
402
|
+
rescue ::StandardError
|
|
403
|
+
::Kernel.instance_method(:to_s).bind_call(object)
|
|
404
|
+
end
|
|
405
|
+
|
|
406
|
+
#
|
|
407
|
+
COMPARISON_OPERATORS = { :"==" => :"!=" }
|
|
408
|
+
|
|
409
|
+
# Message to use when making a comparion assertion. With
|
|
410
|
+
# `Assay.color = true`, long values are shown with their differences
|
|
411
|
+
# colored.
|
|
412
|
+
#
|
|
413
|
+
# @param operator [Symbol] operator/method
|
|
414
|
+
def compare_message(operator, *args, &blk)
|
|
415
|
+
return nil unless COMPARISON_OPERATORS.key?(operator)
|
|
416
|
+
a, b = __inspect__(@delegate), __inspect__(args.first)
|
|
417
|
+
operator = COMPARISON_OPERATORS[operator] if @negated
|
|
418
|
+
if a.size > 13 or b.size > 13
|
|
419
|
+
if defined?($ansi) && $ansi && a.size * b.size <= DIFF_LIMIT
|
|
420
|
+
diff = ::ANSI::Diff.new(a,b)
|
|
421
|
+
a = diff.diff1
|
|
422
|
+
b = diff.diff2
|
|
423
|
+
end
|
|
424
|
+
"a #{operator} b\na) " + a + "\nb) " + b
|
|
425
|
+
else
|
|
426
|
+
"#{a} #{operator} #{b}"
|
|
427
|
+
end
|
|
428
|
+
end
|
|
429
|
+
|
|
430
|
+
# Puts together a suitable error message.
|
|
431
|
+
#
|
|
432
|
+
# @param op [Symbol] operator/method
|
|
433
|
+
#
|
|
434
|
+
# @return [String] message
|
|
435
|
+
def generic_message(op, *a, **kw, &b)
|
|
436
|
+
inspection = __inspect__(@delegate)
|
|
437
|
+
list = a.map { |x| __inspect__(x) } + kw.map { |k, v| "#{k}: #{__inspect__(v)}" }
|
|
438
|
+
if @negated
|
|
439
|
+
"! #{inspection} #{op} #{list.join(',')}"
|
|
440
|
+
else
|
|
441
|
+
"#{inspection} #{op} #{list.join(',')}"
|
|
442
|
+
end
|
|
443
|
+
end
|
|
444
|
+
|
|
445
|
+
# @see http://redmine.ruby-lang.org/issues/3768
|
|
446
|
+
def self.const_missing(const)
|
|
447
|
+
::Object.const_get(const)
|
|
448
|
+
end
|
|
449
|
+
end
|
|
450
|
+
|
|
451
|
+
end
|
|
452
|
+
|
|
453
|
+
# Copyright (c) 2008 Thomas Sawyer
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
require_relative 'assertor'
|
|
2
|
+
|
|
3
|
+
module Assay::AE
|
|
4
|
+
|
|
5
|
+
# The Ok mixin is a reusable assertion helper that
|
|
6
|
+
# makes it easy to construct parameterized assertions
|
|
7
|
+
# with an elegant syntax.
|
|
8
|
+
#
|
|
9
|
+
module Check
|
|
10
|
+
|
|
11
|
+
# The Check::Proc class encapsulates a labeled procedure
|
|
12
|
+
# for making assertions using the `ok`/`no` methods.
|
|
13
|
+
#
|
|
14
|
+
class Proc
|
|
15
|
+
# Setup new check procedure.
|
|
16
|
+
def initialize(options={}, &check)
|
|
17
|
+
@name = options[:name]
|
|
18
|
+
@message = options[:message] || @name
|
|
19
|
+
@check = check
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
#
|
|
23
|
+
def message(&block)
|
|
24
|
+
if block
|
|
25
|
+
@message = block
|
|
26
|
+
end
|
|
27
|
+
@message
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
#
|
|
31
|
+
def message=(msg)
|
|
32
|
+
@message = msg
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Call check procedure.
|
|
36
|
+
def call(*args)
|
|
37
|
+
@check.call(*args)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
#
|
|
41
|
+
def to_s(*args)
|
|
42
|
+
case @message
|
|
43
|
+
when nil
|
|
44
|
+
@name.to_s
|
|
45
|
+
when ::Proc
|
|
46
|
+
@message.call(*args)
|
|
47
|
+
else
|
|
48
|
+
# TODO: count %\S and apply `% args.map{|a|a.inspect}[0,count]`
|
|
49
|
+
@message.to_s
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
#
|
|
54
|
+
def ok!(*args)
|
|
55
|
+
check!(args, false, caller)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
#
|
|
59
|
+
def no!(*args)
|
|
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)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# TODO: Better way to customize error message so it can have
|
|
71
|
+
# arguments in the messages ?
|
|
72
|
+
|
|
73
|
+
# Built-in check procedures.
|
|
74
|
+
TABLE = {
|
|
75
|
+
:equality => Check::Proc.new(:message=>"should be equal"){|h| h.any?{|a,b| b==a}},
|
|
76
|
+
:case_equality => Check::Proc.new(:message=>"should be equal"){|h| h.any?{|a,b| b===a}}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
#
|
|
80
|
+
def self.table
|
|
81
|
+
@table ||= TABLE.dup
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Define a univerally available ok/no check.
|
|
85
|
+
#
|
|
86
|
+
# Assay::AE::Check.define(:palindrome) do |x|
|
|
87
|
+
# x.reverse == x
|
|
88
|
+
# end
|
|
89
|
+
#
|
|
90
|
+
def self.define(name, &block)
|
|
91
|
+
table[name] = Check::Proc.new(:name=>name, &block)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
#
|
|
95
|
+
def check_table
|
|
96
|
+
Check.table
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# Define an ok/no check procedure. A one-off procedure is defined
|
|
100
|
+
# with a block.
|
|
101
|
+
#
|
|
102
|
+
# check do |x, y|
|
|
103
|
+
# x == y
|
|
104
|
+
# end
|
|
105
|
+
#
|
|
106
|
+
# ok 1,1
|
|
107
|
+
# no 1,2
|
|
108
|
+
#
|
|
109
|
+
# The check method can also be used to define reusable checks.
|
|
110
|
+
#
|
|
111
|
+
# check(:palindrome) do |x|
|
|
112
|
+
# x.reverse == x
|
|
113
|
+
# end
|
|
114
|
+
#
|
|
115
|
+
# This will also cause the current check to be set.
|
|
116
|
+
# Later in the code, the check procedure can be restored
|
|
117
|
+
# by just passing the symbolic name.
|
|
118
|
+
#
|
|
119
|
+
# check :palindrome
|
|
120
|
+
#
|
|
121
|
+
# ok 'abracarba'
|
|
122
|
+
# no 'foolishness'
|
|
123
|
+
#
|
|
124
|
+
def check(name=nil, &block)
|
|
125
|
+
if name
|
|
126
|
+
if block
|
|
127
|
+
check_table[name] = Check::Proc.new(:name=>name, &block)
|
|
128
|
+
end
|
|
129
|
+
@__check__ = check_table[name]
|
|
130
|
+
else
|
|
131
|
+
#raise ArgumentError if block.arity == 0
|
|
132
|
+
@__check__ = Check::Proc.new(&block)
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
#
|
|
137
|
+
def ok(*args)
|
|
138
|
+
__check__.check!(args, false, caller)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
#
|
|
142
|
+
def no(*args)
|
|
143
|
+
__check__.check!(args, true, caller)
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# Returns the current check.
|
|
147
|
+
def __check__
|
|
148
|
+
@__check__ || check_table[:equality]
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
end
|
|
154
|
+
|