rubysl-test-unit 1.0.1 → 2.0.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/.travis.yml +3 -2
- data/lib/rubysl/test/unit/unit.rb +863 -267
- data/lib/rubysl/test/unit/version.rb +1 -1
- data/lib/test/unit/assertions.rb +281 -531
- data/lib/test/unit/parallel.rb +184 -0
- data/lib/test/unit/testcase.rb +17 -143
- data/rubysl-test-unit.gemspec +3 -1
- metadata +22 -39
- data/lib/test/unit/assertionfailederror.rb +0 -14
- data/lib/test/unit/autorunner.rb +0 -220
- data/lib/test/unit/collector.rb +0 -43
- data/lib/test/unit/collector/dir.rb +0 -107
- data/lib/test/unit/collector/objectspace.rb +0 -34
- data/lib/test/unit/error.rb +0 -56
- data/lib/test/unit/failure.rb +0 -51
- data/lib/test/unit/testresult.rb +0 -80
- data/lib/test/unit/testsuite.rb +0 -76
- data/lib/test/unit/ui/console/testrunner.rb +0 -127
- data/lib/test/unit/ui/fox/testrunner.rb +0 -268
- data/lib/test/unit/ui/gtk/testrunner.rb +0 -416
- data/lib/test/unit/ui/gtk2/testrunner.rb +0 -465
- data/lib/test/unit/ui/testrunnermediator.rb +0 -68
- data/lib/test/unit/ui/testrunnerutilities.rb +0 -46
- data/lib/test/unit/ui/tk/testrunner.rb +0 -260
- data/lib/test/unit/util/backtracefilter.rb +0 -40
- data/lib/test/unit/util/observable.rb +0 -90
- data/lib/test/unit/util/procwrapper.rb +0 -48
data/lib/test/unit/assertions.rb
CHANGED
@@ -1,458 +1,328 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
# License:: Ruby license.
|
4
|
-
|
5
|
-
require 'test/unit/assertionfailederror'
|
6
|
-
require 'test/unit/util/backtracefilter'
|
1
|
+
require 'minitest/unit'
|
2
|
+
require 'pp'
|
7
3
|
|
8
4
|
module Test
|
9
5
|
module Unit
|
6
|
+
module Assertions
|
7
|
+
include MiniTest::Assertions
|
10
8
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
#
|
15
|
-
# To include it in your own code and use its functionality, you simply
|
16
|
-
# need to rescue Test::Unit::AssertionFailedError. Additionally you may
|
17
|
-
# override add_assertion to get notified whenever an assertion is made.
|
18
|
-
#
|
19
|
-
# Notes:
|
20
|
-
# * The message to each assertion, if given, will be propagated with the
|
21
|
-
# failure.
|
22
|
-
# * It is easy to add your own assertions based on assert_block().
|
23
|
-
#
|
24
|
-
# = Example Custom Assertion
|
25
|
-
#
|
26
|
-
# def deny(boolean, message = nil)
|
27
|
-
# message = build_message message, '<?> is not false or nil.', boolean
|
28
|
-
# assert_block message do
|
29
|
-
# not boolean
|
30
|
-
# end
|
31
|
-
# end
|
9
|
+
def mu_pp(obj) #:nodoc:
|
10
|
+
obj.pretty_inspect.chomp
|
11
|
+
end
|
32
12
|
|
33
|
-
|
13
|
+
MINI_DIR = File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), "minitest") #:nodoc:
|
34
14
|
|
35
|
-
|
36
|
-
#
|
37
|
-
# block yields true.
|
15
|
+
# :call-seq:
|
16
|
+
# assert(test, [failure_message])
|
38
17
|
#
|
39
|
-
#
|
40
|
-
#
|
41
|
-
|
42
|
-
#
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
18
|
+
#Tests if +test+ is true.
|
19
|
+
#
|
20
|
+
#+msg+ may be a String or a Proc. If +msg+ is a String, it will be used
|
21
|
+
#as the failure message. Otherwise, the result of calling +msg+ will be
|
22
|
+
#used as the message if the assertion fails.
|
23
|
+
#
|
24
|
+
#If no +msg+ is given, a default message will be used.
|
25
|
+
#
|
26
|
+
# assert(false, "This was expected to be true")
|
27
|
+
def assert(test, *msgs)
|
28
|
+
case msg = msgs.first
|
29
|
+
when String, Proc
|
30
|
+
else
|
31
|
+
bt = caller.reject { |s| s.start_with?(MINI_DIR) }
|
32
|
+
raise ArgumentError, "assertion message must be String or Proc, but #{msg.class} was given.", bt
|
33
|
+
end unless msgs.empty?
|
34
|
+
super
|
51
35
|
end
|
52
36
|
|
53
|
-
|
54
|
-
#
|
37
|
+
# :call-seq:
|
38
|
+
# assert_block( failure_message = nil )
|
55
39
|
#
|
56
|
-
#
|
57
|
-
#
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
40
|
+
#Tests the result of the given block. If the block does not return true,
|
41
|
+
#the assertion will fail. The optional +failure_message+ argument is the same as in
|
42
|
+
#Assertions#assert.
|
43
|
+
#
|
44
|
+
# assert_block do
|
45
|
+
# [1, 2, 3].any? { |num| num < 1 }
|
46
|
+
# end
|
47
|
+
def assert_block(*msgs)
|
48
|
+
assert yield, *msgs
|
65
49
|
end
|
66
50
|
|
67
|
-
|
68
|
-
#
|
51
|
+
# :call-seq:
|
52
|
+
# assert_raise( *args, &block )
|
69
53
|
#
|
70
|
-
#
|
71
|
-
#
|
72
|
-
#
|
54
|
+
#Tests if the given block raises an exception. Acceptable exception
|
55
|
+
#types maye be given as optional arguments. If the last argument is a
|
56
|
+
#String, it will be used as the error message.
|
73
57
|
#
|
74
|
-
#
|
75
|
-
#
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
EOT
|
83
|
-
assert_block(full_message) { expected == actual }
|
58
|
+
# assert_raise do #Fails, no Exceptions are raised
|
59
|
+
# end
|
60
|
+
#
|
61
|
+
# assert_raise NameError do
|
62
|
+
# puts x #Raises NameError, so assertion succeeds
|
63
|
+
# end
|
64
|
+
def assert_raise(*args, &b)
|
65
|
+
assert_raises(*args, &b)
|
84
66
|
end
|
85
67
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
68
|
+
# :call-seq:
|
69
|
+
# assert_raise_with_message(exception, expected, msg = nil, &block)
|
70
|
+
#
|
71
|
+
#Tests if the given block raises an exception with the expected
|
72
|
+
#message.
|
73
|
+
#
|
74
|
+
# assert_raise_with_message(RuntimeError, "foo") do
|
75
|
+
# nil #Fails, no Exceptions are raised
|
76
|
+
# end
|
77
|
+
#
|
78
|
+
# assert_raise_with_message(RuntimeError, "foo") do
|
79
|
+
# raise ArgumentError, "foo" #Fails, different Exception is raised
|
80
|
+
# end
|
81
|
+
#
|
82
|
+
# assert_raise_with_message(RuntimeError, "foo") do
|
83
|
+
# raise "bar" #Fails, RuntimeError is raised but the message differs
|
84
|
+
# end
|
85
|
+
#
|
86
|
+
# assert_raise_with_message(RuntimeError, "foo") do
|
87
|
+
# raise "foo" #Raises RuntimeError with the message, so assertion succeeds
|
88
|
+
# end
|
89
|
+
def assert_raise_with_message(exception, expected, msg = nil)
|
90
|
+
case expected
|
91
|
+
when String
|
92
|
+
assert = :assert_equal
|
93
|
+
when Regexp
|
94
|
+
assert = :assert_match
|
95
|
+
else
|
96
|
+
raise TypeError, "Expected #{expected.inspect} to be a kind of String or Regexp, not #{expected.class}"
|
92
97
|
end
|
93
|
-
end
|
94
98
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
modules.any? {|mod| actual_exception.is_a?(mod)}
|
99
|
+
ex = assert_raise(exception, msg) {yield}
|
100
|
+
msg = message(msg, "") {"Expected Exception(#{exception}) was raised, but the message doesn't match"}
|
101
|
+
__send__(assert, expected, ex.message, msg)
|
99
102
|
end
|
100
103
|
|
101
|
-
|
102
|
-
#
|
104
|
+
# :call-seq:
|
105
|
+
# assert_nothing_raised( *args, &block )
|
103
106
|
#
|
104
|
-
#
|
105
|
-
#
|
106
|
-
#
|
107
|
-
#
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
107
|
+
#If any exceptions are given as arguments, the assertion will
|
108
|
+
#fail if one of those exceptions are raised. Otherwise, the test fails
|
109
|
+
#if any exceptions are raised.
|
110
|
+
#
|
111
|
+
#The final argument may be a failure message.
|
112
|
+
#
|
113
|
+
# assert_nothing_raised RuntimeError do
|
114
|
+
# raise Exception #Assertion passes, Exception is not a RuntimeError
|
115
|
+
# end
|
116
|
+
#
|
117
|
+
# assert_nothing_raised do
|
118
|
+
# raise Exception #Assertion fails
|
119
|
+
# end
|
120
|
+
def assert_nothing_raised(*args)
|
121
|
+
self._assertions += 1
|
122
|
+
if Module === args.last
|
123
|
+
msg = nil
|
124
|
+
else
|
125
|
+
msg = args.pop
|
126
|
+
end
|
127
|
+
begin
|
128
|
+
line = __LINE__; yield
|
129
|
+
rescue MiniTest::Skip
|
130
|
+
raise
|
131
|
+
rescue Exception => e
|
132
|
+
bt = e.backtrace
|
133
|
+
as = e.instance_of?(MiniTest::Assertion)
|
134
|
+
if as
|
135
|
+
ans = /\A#{Regexp.quote(__FILE__)}:#{line}:in /o
|
136
|
+
bt.reject! {|ln| ans =~ ln}
|
116
137
|
end
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
yield
|
124
|
-
rescue Exception => actual_exception
|
125
|
-
break
|
126
|
-
end
|
127
|
-
false
|
138
|
+
if ((args.empty? && !as) ||
|
139
|
+
args.any? {|a| a.instance_of?(Module) ? e.is_a?(a) : e.class == a })
|
140
|
+
msg = message(msg) { "Exception raised:\n<#{mu_pp(e)}>" }
|
141
|
+
raise MiniTest::Assertion, msg.call, bt
|
142
|
+
else
|
143
|
+
raise
|
128
144
|
end
|
129
|
-
full_message = build_message(message, "<?> exception expected but was\n?", expected, actual_exception)
|
130
|
-
assert_block(full_message) {_expected_exception?(actual_exception, exceptions, modules)}
|
131
|
-
actual_exception
|
132
145
|
end
|
146
|
+
nil
|
133
147
|
end
|
134
148
|
|
135
|
-
|
136
|
-
#
|
149
|
+
# :call-seq:
|
150
|
+
# assert_nothing_thrown( failure_message = nil, &block )
|
137
151
|
#
|
138
|
-
#
|
139
|
-
|
140
|
-
public
|
141
|
-
def assert_raises(*args, &block)
|
142
|
-
assert_raise(*args, &block)
|
143
|
-
end
|
144
|
-
|
145
|
-
##
|
146
|
-
# Passes if +object+ .instance_of? +klass+
|
147
|
-
#
|
148
|
-
# Example:
|
149
|
-
# assert_instance_of String, 'foo'
|
150
|
-
|
151
|
-
public
|
152
|
-
def assert_instance_of(klass, object, message="")
|
153
|
-
_wrap_assertion do
|
154
|
-
assert_equal(Class, klass.class, "assert_instance_of takes a Class as its first argument")
|
155
|
-
full_message = build_message(message, <<EOT, object, klass, object.class)
|
156
|
-
<?> expected to be an instance of
|
157
|
-
<?> but was
|
158
|
-
<?>.
|
159
|
-
EOT
|
160
|
-
assert_block(full_message){object.instance_of?(klass)}
|
161
|
-
end
|
162
|
-
end
|
163
|
-
|
164
|
-
##
|
165
|
-
# Passes if +object+ is nil.
|
152
|
+
#Fails if the given block uses a call to Kernel#throw.
|
166
153
|
#
|
167
|
-
#
|
168
|
-
# assert_nil [1, 2].uniq!
|
169
|
-
|
170
|
-
public
|
171
|
-
def assert_nil(object, message="")
|
172
|
-
assert_equal(nil, object, message)
|
173
|
-
end
|
174
|
-
|
175
|
-
##
|
176
|
-
# Passes if +object+ .kind_of? +klass+
|
154
|
+
#An optional failure message may be provided as the final argument.
|
177
155
|
#
|
178
|
-
#
|
179
|
-
#
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
156
|
+
# assert_nothing_thrown "Something was thrown!" do
|
157
|
+
# throw :problem?
|
158
|
+
# end
|
159
|
+
def assert_nothing_thrown(msg=nil)
|
160
|
+
begin
|
161
|
+
yield
|
162
|
+
rescue ArgumentError => error
|
163
|
+
raise error if /\Auncaught throw (.+)\z/m !~ error.message
|
164
|
+
msg = message(msg) { "<#{$1}> was thrown when nothing was expected" }
|
165
|
+
flunk(msg)
|
187
166
|
end
|
167
|
+
assert(true, "Expected nothing to be thrown")
|
188
168
|
end
|
189
169
|
|
190
|
-
|
191
|
-
#
|
170
|
+
# :call-seq:
|
171
|
+
# assert_equal( expected, actual, failure_message = nil )
|
192
172
|
#
|
193
|
-
#
|
194
|
-
# assert_respond_to 'bugbear', :slice
|
195
|
-
|
196
|
-
public
|
197
|
-
def assert_respond_to(object, method, message="")
|
198
|
-
_wrap_assertion do
|
199
|
-
full_message = build_message(nil, "<?>\ngiven as the method name argument to #assert_respond_to must be a Symbol or #respond_to\\?(:to_str).", method)
|
200
|
-
|
201
|
-
assert_block(full_message) do
|
202
|
-
method.kind_of?(Symbol) || method.respond_to?(:to_str)
|
203
|
-
end
|
204
|
-
full_message = build_message(message, <<EOT, object, object.class, method)
|
205
|
-
<?>
|
206
|
-
of type <?>
|
207
|
-
expected to respond_to\\?<?>.
|
208
|
-
EOT
|
209
|
-
assert_block(full_message) { object.respond_to?(method) }
|
210
|
-
end
|
211
|
-
end
|
212
|
-
|
213
|
-
##
|
214
|
-
# Passes if +string+ =~ +pattern+.
|
173
|
+
#Tests if +expected+ is equal to +actual+.
|
215
174
|
#
|
216
|
-
#
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
175
|
+
#An optional failure message may be provided as the final argument.
|
176
|
+
def assert_equal(exp, act, msg = nil)
|
177
|
+
msg = message(msg) {
|
178
|
+
exp_str = mu_pp(exp)
|
179
|
+
act_str = mu_pp(act)
|
180
|
+
exp_comment = ''
|
181
|
+
act_comment = ''
|
182
|
+
if exp_str == act_str
|
183
|
+
if (exp.is_a?(String) && act.is_a?(String)) ||
|
184
|
+
(exp.is_a?(Regexp) && act.is_a?(Regexp))
|
185
|
+
exp_comment = " (#{exp.encoding})"
|
186
|
+
act_comment = " (#{act.encoding})"
|
187
|
+
elsif exp.is_a?(Float) && act.is_a?(Float)
|
188
|
+
exp_str = "%\#.#{Float::DIG+2}g" % exp
|
189
|
+
act_str = "%\#.#{Float::DIG+2}g" % act
|
190
|
+
elsif exp.is_a?(Time) && act.is_a?(Time)
|
191
|
+
if exp.subsec * 1000_000_000 == exp.nsec
|
192
|
+
exp_comment = " (#{exp.nsec}[ns])"
|
193
|
+
else
|
194
|
+
exp_comment = " (subsec=#{exp.subsec})"
|
195
|
+
end
|
196
|
+
if act.subsec * 1000_000_000 == act.nsec
|
197
|
+
act_comment = " (#{act.nsec}[ns])"
|
198
|
+
else
|
199
|
+
act_comment = " (subsec=#{act.subsec})"
|
200
|
+
end
|
201
|
+
elsif exp.class != act.class
|
202
|
+
# a subclass of Range, for example.
|
203
|
+
exp_comment = " (#{exp.class})"
|
204
|
+
act_comment = " (#{act.class})"
|
205
|
+
end
|
206
|
+
elsif !Encoding.compatible?(exp_str, act_str)
|
207
|
+
if exp.is_a?(String) && act.is_a?(String)
|
208
|
+
exp_str = exp.dump
|
209
|
+
act_str = act.dump
|
210
|
+
exp_comment = " (#{exp.encoding})"
|
211
|
+
act_comment = " (#{act.encoding})"
|
225
212
|
else
|
226
|
-
|
213
|
+
exp_str = exp_str.dump
|
214
|
+
act_str = act_str.dump
|
215
|
+
end
|
227
216
|
end
|
228
|
-
|
229
|
-
|
230
|
-
|
217
|
+
"<#{exp_str}>#{exp_comment} expected but was\n<#{act_str}>#{act_comment}"
|
218
|
+
}
|
219
|
+
assert(exp == act, msg)
|
231
220
|
end
|
232
221
|
|
233
|
-
|
234
|
-
#
|
235
|
-
# instance).
|
222
|
+
# :call-seq:
|
223
|
+
# assert_not_nil( expression, failure_message = nil )
|
236
224
|
#
|
237
|
-
#
|
238
|
-
#
|
239
|
-
#
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
full_message = build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__)
|
244
|
-
<?>
|
245
|
-
with id <?> expected to be equal\\? to
|
246
|
-
<?>
|
247
|
-
with id <?>.
|
248
|
-
EOT
|
249
|
-
assert_block(full_message) { actual.equal?(expected) }
|
225
|
+
#Tests if +expression+ is not nil.
|
226
|
+
#
|
227
|
+
#An optional failure message may be provided as the final argument.
|
228
|
+
def assert_not_nil(exp, msg=nil)
|
229
|
+
msg = message(msg) { "<#{mu_pp(exp)}> expected to not be nil" }
|
230
|
+
assert(!exp.nil?, msg)
|
250
231
|
end
|
251
232
|
|
252
|
-
|
253
|
-
#
|
233
|
+
# :call-seq:
|
234
|
+
# assert_not_equal( expected, actual, failure_message = nil )
|
254
235
|
#
|
255
|
-
#
|
236
|
+
#Tests if +expected+ is not equal to +actual+.
|
256
237
|
#
|
257
|
-
#
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
def assert_operator(object1, operator, object2, message="")
|
262
|
-
_wrap_assertion do
|
263
|
-
full_message = build_message(nil, "<?>\ngiven as the operator for #assert_operator must be a Symbol or #respond_to\\?(:to_str).", operator)
|
264
|
-
assert_block(full_message){operator.kind_of?(Symbol) || operator.respond_to?(:to_str)}
|
265
|
-
full_message = build_message(message, <<EOT, object1, AssertionMessage.literal(operator), object2)
|
266
|
-
<?> expected to be
|
267
|
-
?
|
268
|
-
<?>.
|
269
|
-
EOT
|
270
|
-
assert_block(full_message) { object1.__send__(operator, object2) }
|
271
|
-
end
|
238
|
+
#An optional failure message may be provided as the final argument.
|
239
|
+
def assert_not_equal(exp, act, msg=nil)
|
240
|
+
msg = message(msg) { "<#{mu_pp(exp)}> expected to be != to\n<#{mu_pp(act)}>" }
|
241
|
+
assert(exp != act, msg)
|
272
242
|
end
|
273
243
|
|
274
|
-
|
275
|
-
#
|
244
|
+
# :call-seq:
|
245
|
+
# assert_no_match( regexp, string, failure_message = nil )
|
276
246
|
#
|
277
|
-
#
|
278
|
-
# assert_nothing_raised do
|
279
|
-
# [1, 2].uniq
|
280
|
-
# end
|
281
|
-
|
282
|
-
public
|
283
|
-
def assert_nothing_raised(*args)
|
284
|
-
_wrap_assertion do
|
285
|
-
if Module === args.last
|
286
|
-
message = ""
|
287
|
-
else
|
288
|
-
message = args.pop
|
289
|
-
end
|
290
|
-
exceptions, modules = _check_exception_class(args)
|
291
|
-
begin
|
292
|
-
yield
|
293
|
-
rescue Exception => e
|
294
|
-
if ((args.empty? && !e.instance_of?(AssertionFailedError)) ||
|
295
|
-
_expected_exception?(e, exceptions, modules))
|
296
|
-
assert_block(build_message(message, "Exception raised:\n?", e)){false}
|
297
|
-
else
|
298
|
-
raise
|
299
|
-
end
|
300
|
-
end
|
301
|
-
nil
|
302
|
-
end
|
303
|
-
end
|
304
|
-
|
305
|
-
##
|
306
|
-
# Flunk always fails.
|
247
|
+
#Tests if the given Regexp does not match a given String.
|
307
248
|
#
|
308
|
-
#
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
249
|
+
#An optional failure message may be provided as the final argument.
|
250
|
+
def assert_no_match(regexp, string, msg=nil)
|
251
|
+
assert_instance_of(Regexp, regexp, "The first argument to assert_no_match should be a Regexp.")
|
252
|
+
self._assertions -= 1
|
253
|
+
msg = message(msg) { "<#{mu_pp(regexp)}> expected to not match\n<#{mu_pp(string)}>" }
|
254
|
+
assert(regexp !~ string, msg)
|
314
255
|
end
|
315
256
|
|
316
|
-
|
317
|
-
#
|
257
|
+
# :call-seq:
|
258
|
+
# assert_not_same( expected, actual, failure_message = nil )
|
318
259
|
#
|
319
|
-
#
|
320
|
-
#
|
321
|
-
|
322
|
-
|
260
|
+
#Tests if +expected+ is not the same object as +actual+.
|
261
|
+
#This test uses Object#equal? to test equality.
|
262
|
+
#
|
263
|
+
#An optional failure message may be provided as the final argument.
|
264
|
+
#
|
265
|
+
# assert_not_same("x", "x") #Succeeds
|
323
266
|
def assert_not_same(expected, actual, message="")
|
324
|
-
|
267
|
+
msg = message(msg) { build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__) }
|
325
268
|
<?>
|
326
269
|
with id <?> expected to not be equal\\? to
|
327
270
|
<?>
|
328
271
|
with id <?>.
|
329
272
|
EOT
|
330
|
-
|
273
|
+
assert(!actual.equal?(expected), msg)
|
331
274
|
end
|
332
275
|
|
333
|
-
|
334
|
-
#
|
276
|
+
# :call-seq:
|
277
|
+
# assert_respond_to( object, method, failure_message = nil )
|
335
278
|
#
|
336
|
-
#
|
337
|
-
# assert_not_equal 'some string', 5
|
338
|
-
|
339
|
-
public
|
340
|
-
def assert_not_equal(expected, actual, message="")
|
341
|
-
full_message = build_message(message, "<?> expected to be != to\n<?>.", expected, actual)
|
342
|
-
assert_block(full_message) { expected != actual }
|
343
|
-
end
|
344
|
-
|
345
|
-
##
|
346
|
-
# Passes if ! +object+ .nil?
|
279
|
+
#Tests if the given Object responds to +method+.
|
347
280
|
#
|
348
|
-
#
|
349
|
-
# assert_not_nil '1 two 3'.sub!(/two/, '2')
|
350
|
-
|
351
|
-
public
|
352
|
-
def assert_not_nil(object, message="")
|
353
|
-
full_message = build_message(message, "<?> expected to not be nil.", object)
|
354
|
-
assert_block(full_message){!object.nil?}
|
355
|
-
end
|
356
|
-
|
357
|
-
##
|
358
|
-
# Passes if +regexp+ !~ +string+
|
281
|
+
#An optional failure message may be provided as the final argument.
|
359
282
|
#
|
360
|
-
#
|
361
|
-
#
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
assert_block(full_message) { regexp !~ string }
|
283
|
+
# assert_respond_to("hello", :reverse) #Succeeds
|
284
|
+
# assert_respond_to("hello", :does_not_exist) #Fails
|
285
|
+
def assert_respond_to obj, (meth, priv), msg = nil
|
286
|
+
if priv
|
287
|
+
msg = message(msg) {
|
288
|
+
"Expected #{mu_pp(obj)} (#{obj.class}) to respond to ##{meth}#{" privately" if priv}"
|
289
|
+
}
|
290
|
+
return assert obj.respond_to?(meth, priv), msg
|
369
291
|
end
|
292
|
+
#get rid of overcounting
|
293
|
+
super if !caller[0].rindex(MINI_DIR, 0) || !obj.respond_to?(meth)
|
370
294
|
end
|
371
295
|
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
##
|
376
|
-
# Passes if the block throws +expected_symbol+
|
296
|
+
# :call-seq:
|
297
|
+
# assert_send( +send_array+, failure_message = nil )
|
377
298
|
#
|
378
|
-
#
|
379
|
-
# assert_throws :done do
|
380
|
-
# throw :done
|
381
|
-
# end
|
382
|
-
|
383
|
-
public
|
384
|
-
def assert_throws(expected_symbol, message="", &proc)
|
385
|
-
_wrap_assertion do
|
386
|
-
assert_instance_of(Symbol, expected_symbol, "assert_throws expects the symbol that should be thrown for its first argument")
|
387
|
-
assert_block("Should have passed a block to assert_throws."){block_given?}
|
388
|
-
caught = true
|
389
|
-
begin
|
390
|
-
catch(expected_symbol) do
|
391
|
-
proc.call
|
392
|
-
caught = false
|
393
|
-
end
|
394
|
-
full_message = build_message(message, "<?> should have been thrown.", expected_symbol)
|
395
|
-
assert_block(full_message){caught}
|
396
|
-
rescue NameError, ThreadError => error
|
397
|
-
if UncaughtThrow[error.class] !~ error.message
|
398
|
-
raise error
|
399
|
-
end
|
400
|
-
full_message = build_message(message, "<?> expected to be thrown but\n<?> was thrown.", expected_symbol, $1.intern)
|
401
|
-
flunk(full_message)
|
402
|
-
end
|
403
|
-
end
|
404
|
-
end
|
405
|
-
|
406
|
-
##
|
407
|
-
# Passes if block does not throw anything.
|
299
|
+
# Passes if the method send returns a true value.
|
408
300
|
#
|
409
|
-
#
|
410
|
-
#
|
411
|
-
#
|
412
|
-
#
|
413
|
-
|
414
|
-
public
|
415
|
-
def assert_nothing_thrown(message="", &proc)
|
416
|
-
_wrap_assertion do
|
417
|
-
assert(block_given?, "Should have passed a block to assert_nothing_thrown")
|
418
|
-
begin
|
419
|
-
proc.call
|
420
|
-
rescue NameError, ThreadError => error
|
421
|
-
if UncaughtThrow[error.class] !~ error.message
|
422
|
-
raise error
|
423
|
-
end
|
424
|
-
full_message = build_message(message, "<?> was thrown when nothing was expected", $1.intern)
|
425
|
-
flunk(full_message)
|
426
|
-
end
|
427
|
-
assert(true, "Expected nothing to be thrown")
|
428
|
-
end
|
429
|
-
end
|
430
|
-
|
431
|
-
##
|
432
|
-
# Passes if +expected_float+ and +actual_float+ are equal
|
433
|
-
# within +delta+ tolerance.
|
301
|
+
# +send_array+ is composed of:
|
302
|
+
# * A receiver
|
303
|
+
# * A method
|
304
|
+
# * Arguments to the method
|
434
305
|
#
|
435
306
|
# Example:
|
436
|
-
#
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
307
|
+
# assert_send([[1, 2], :member?, 1]) # -> pass
|
308
|
+
# assert_send([[1, 2], :member?, 4]) # -> fail
|
309
|
+
def assert_send send_ary, m = nil
|
310
|
+
recv, msg, *args = send_ary
|
311
|
+
m = message(m) {
|
312
|
+
if args.empty?
|
313
|
+
argsstr = ""
|
314
|
+
else
|
315
|
+
(argsstr = mu_pp(args)).sub!(/\A\[(.*)\]\z/m, '(\1)')
|
443
316
|
end
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
<?> expected to be within
|
448
|
-
<?> of each other.
|
449
|
-
EOT
|
450
|
-
assert_block(full_message) { (expected_float.to_f - actual_float.to_f).abs <= delta.to_f }
|
451
|
-
end
|
317
|
+
"Expected #{mu_pp(recv)}.#{msg}#{argsstr} to return true"
|
318
|
+
}
|
319
|
+
assert recv.__send__(msg, *args), m
|
452
320
|
end
|
453
321
|
|
454
|
-
|
455
|
-
#
|
322
|
+
# :call-seq:
|
323
|
+
# assert_not_send( +send_array+, failure_message = nil )
|
324
|
+
#
|
325
|
+
# Passes if the method send doesn't return a true value.
|
456
326
|
#
|
457
327
|
# +send_array+ is composed of:
|
458
328
|
# * A receiver
|
@@ -460,163 +330,43 @@ EOT
|
|
460
330
|
# * Arguments to the method
|
461
331
|
#
|
462
332
|
# Example:
|
463
|
-
#
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
<?(?)> with a true value.
|
473
|
-
EOT
|
474
|
-
assert_block(full_message) { send_array[0].__send__(send_array[1], *send_array[2..-1]) }
|
475
|
-
end
|
476
|
-
end
|
477
|
-
|
478
|
-
##
|
479
|
-
# Builds a failure message. +head+ is added before the +template+ and
|
480
|
-
# +arguments+ replaces the '?'s positionally in the template.
|
481
|
-
|
482
|
-
public
|
483
|
-
def build_message(head, template=nil, *arguments)
|
484
|
-
template &&= template.chomp
|
485
|
-
return AssertionMessage.new(head, template, arguments)
|
486
|
-
end
|
487
|
-
|
488
|
-
private
|
489
|
-
def _wrap_assertion
|
490
|
-
@_assertion_wrapped ||= false
|
491
|
-
unless (@_assertion_wrapped)
|
492
|
-
@_assertion_wrapped = true
|
493
|
-
begin
|
494
|
-
add_assertion
|
495
|
-
return yield
|
496
|
-
ensure
|
497
|
-
@_assertion_wrapped = false
|
333
|
+
# assert_not_send([[1, 2], :member?, 1]) # -> fail
|
334
|
+
# assert_not_send([[1, 2], :member?, 4]) # -> pass
|
335
|
+
def assert_not_send send_ary, m = nil
|
336
|
+
recv, msg, *args = send_ary
|
337
|
+
m = message(m) {
|
338
|
+
if args.empty?
|
339
|
+
argsstr = ""
|
340
|
+
else
|
341
|
+
(argsstr = mu_pp(args)).sub!(/\A\[(.*)\]\z/m, '(\1)')
|
498
342
|
end
|
499
|
-
|
500
|
-
|
501
|
-
|
343
|
+
"Expected #{mu_pp(recv)}.#{msg}#{argsstr} to return false"
|
344
|
+
}
|
345
|
+
assert !recv.__send__(msg, *args), m
|
502
346
|
end
|
503
|
-
|
504
|
-
##
|
505
|
-
# Called whenever an assertion is made. Define this in classes that
|
506
|
-
# include Test::Unit::Assertions to record assertion counts.
|
507
347
|
|
508
|
-
|
509
|
-
|
348
|
+
ms = instance_methods(true).map {|sym| sym.to_s }
|
349
|
+
ms.grep(/\Arefute_/) do |m|
|
350
|
+
mname = ('assert_not_' << m.to_s[/.*?_(.*)/, 1])
|
351
|
+
alias_method(mname, m) unless ms.include? mname
|
510
352
|
end
|
353
|
+
alias assert_include assert_includes
|
354
|
+
alias assert_not_include assert_not_includes
|
511
355
|
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
public
|
517
|
-
def self.use_pp=(value)
|
518
|
-
AssertionMessage.use_pp = value
|
356
|
+
def build_message(head, template=nil, *arguments) #:nodoc:
|
357
|
+
template &&= template.chomp
|
358
|
+
template.gsub(/\G((?:[^\\]|\\.)*?)(\\)?\?/) { $1 + ($2 ? "?" : mu_pp(arguments.shift)) }
|
519
359
|
end
|
520
|
-
|
521
|
-
# :stopdoc:
|
522
|
-
|
523
|
-
class AssertionMessage
|
524
|
-
@use_pp = true
|
525
|
-
class << self
|
526
|
-
attr_accessor :use_pp
|
527
|
-
end
|
528
|
-
|
529
|
-
class Literal
|
530
|
-
def initialize(value)
|
531
|
-
@value = value
|
532
|
-
end
|
533
|
-
|
534
|
-
def inspect
|
535
|
-
@value.to_s
|
536
|
-
end
|
537
|
-
end
|
538
|
-
|
539
|
-
class Template
|
540
|
-
def self.create(string)
|
541
|
-
parts = (string ? string.scan(/(?=[^\\])\?|(?:\\\?|[^\?])+/m) : [])
|
542
|
-
self.new(parts)
|
543
|
-
end
|
544
|
-
|
545
|
-
attr_reader :count
|
546
|
-
|
547
|
-
def initialize(parts)
|
548
|
-
@parts = parts
|
549
|
-
@count = parts.find_all{|e| e == '?'}.size
|
550
|
-
end
|
551
|
-
|
552
|
-
def result(parameters)
|
553
|
-
raise "The number of parameters does not match the number of substitutions." if(parameters.size != count)
|
554
|
-
params = parameters.dup
|
555
|
-
@parts.collect{|e| e == '?' ? params.shift : e.gsub(/\\\?/m, '?')}.join('')
|
556
|
-
end
|
557
|
-
end
|
558
|
-
|
559
|
-
def self.literal(value)
|
560
|
-
Literal.new(value)
|
561
|
-
end
|
562
360
|
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
@template_string = template_string
|
568
|
-
@parameters = parameters
|
569
|
-
end
|
570
|
-
|
571
|
-
def convert(object)
|
572
|
-
case object
|
573
|
-
when Exception
|
574
|
-
<<EOM.chop
|
575
|
-
Class: <#{convert(object.class)}>
|
576
|
-
Message: <#{convert(object.message)}>
|
577
|
-
---Backtrace---
|
578
|
-
#{filter_backtrace(object.backtrace).join("\n")}
|
579
|
-
---------------
|
580
|
-
EOM
|
581
|
-
else
|
582
|
-
if(self.class.use_pp)
|
583
|
-
begin
|
584
|
-
require 'pp'
|
585
|
-
rescue LoadError
|
586
|
-
self.class.use_pp = false
|
587
|
-
return object.inspect
|
588
|
-
end unless(defined?(PP))
|
589
|
-
PP.pp(object, '').chomp
|
590
|
-
else
|
591
|
-
object.inspect
|
592
|
-
end
|
361
|
+
def message(msg = nil, *args, &default) # :nodoc:
|
362
|
+
if Proc === msg
|
363
|
+
super(nil, *args) do
|
364
|
+
[msg.call, (default.call if default)].compact.reject(&:empty?).join(".\n")
|
593
365
|
end
|
594
|
-
|
595
|
-
|
596
|
-
def template
|
597
|
-
@template ||= Template.create(@template_string)
|
598
|
-
end
|
599
|
-
|
600
|
-
def add_period(string)
|
601
|
-
(string =~ /\.\Z/ ? string : string + '.')
|
602
|
-
end
|
603
|
-
|
604
|
-
def to_s
|
605
|
-
message_parts = []
|
606
|
-
if (@head)
|
607
|
-
head = @head.to_s
|
608
|
-
unless(head.empty?)
|
609
|
-
message_parts << add_period(head)
|
610
|
-
end
|
611
|
-
end
|
612
|
-
tail = template.result(@parameters.collect{|e| convert(e)})
|
613
|
-
message_parts << tail unless(tail.empty?)
|
614
|
-
message_parts.join("\n")
|
366
|
+
else
|
367
|
+
super
|
615
368
|
end
|
616
369
|
end
|
617
|
-
|
618
|
-
# :startdoc:
|
619
|
-
|
620
370
|
end
|
621
371
|
end
|
622
372
|
end
|