minitest 4.7.5 → 5.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data.tar.gz.sig +0 -0
- data/.autotest +1 -2
- data/History.txt +72 -21
- data/Manifest.txt +6 -0
- data/README.txt +57 -60
- data/design_rationale.rb +1 -1
- data/lib/hoe/minitest.rb +9 -5
- data/lib/minitest.rb +639 -0
- data/lib/minitest/assertions.rb +643 -0
- data/lib/minitest/autorun.rb +6 -6
- data/lib/minitest/benchmark.rb +92 -85
- data/lib/minitest/expectations.rb +268 -0
- data/lib/minitest/hell.rb +3 -5
- data/lib/minitest/mock.rb +3 -4
- data/lib/minitest/parallel_each.rb +59 -12
- data/lib/minitest/pride.rb +3 -111
- data/lib/minitest/pride_plugin.rb +143 -0
- data/lib/minitest/spec.rb +44 -312
- data/lib/minitest/test.rb +287 -0
- data/lib/minitest/unit.rb +33 -1407
- data/test/minitest/metametameta.rb +33 -30
- data/test/minitest/test_minitest_benchmark.rb +11 -9
- data/test/minitest/test_minitest_mock.rb +46 -48
- data/test/minitest/test_minitest_reporter.rb +245 -0
- data/test/minitest/test_minitest_spec.rb +46 -63
- data/test/minitest/test_minitest_unit.rb +213 -232
- metadata +12 -5
- metadata.gz.sig +0 -0
@@ -0,0 +1,287 @@
|
|
1
|
+
module Minitest
|
2
|
+
##
|
3
|
+
# Subclass Test to create your own tests. Typically you'll want a
|
4
|
+
# Test subclass per implementation class.
|
5
|
+
#
|
6
|
+
# See Minitest::Assertions
|
7
|
+
|
8
|
+
class Test < Runnable
|
9
|
+
require "minitest/assertions"
|
10
|
+
include Minitest::Assertions
|
11
|
+
|
12
|
+
PASSTHROUGH_EXCEPTIONS = [NoMemoryError, SignalException, # :nodoc:
|
13
|
+
Interrupt, SystemExit]
|
14
|
+
|
15
|
+
##
|
16
|
+
# Call this at the top of your tests when you absolutely
|
17
|
+
# positively need to have ordered tests. In doing so, you're
|
18
|
+
# admitting that you suck and your tests are weak.
|
19
|
+
|
20
|
+
def self.i_suck_and_my_tests_are_order_dependent!
|
21
|
+
class << self
|
22
|
+
undef_method :test_order if method_defined? :test_order
|
23
|
+
define_method :test_order do :alpha end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
##
|
28
|
+
# Make diffs for this TestCase use #pretty_inspect so that diff
|
29
|
+
# in assert_equal can be more details. NOTE: this is much slower
|
30
|
+
# than the regular inspect but much more usable for complex
|
31
|
+
# objects.
|
32
|
+
|
33
|
+
def self.make_my_diffs_pretty!
|
34
|
+
require "pp"
|
35
|
+
|
36
|
+
define_method :mu_pp do |o|
|
37
|
+
o.pretty_inspect
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
##
|
42
|
+
# Call this at the top of your tests when you want to run your
|
43
|
+
# tests in parallel. In doing so, you're admitting that you rule
|
44
|
+
# and your tests are awesome.
|
45
|
+
|
46
|
+
def self.parallelize_me!
|
47
|
+
require "minitest/parallel_each"
|
48
|
+
|
49
|
+
class << self
|
50
|
+
undef_method :test_order if method_defined? :test_order
|
51
|
+
define_method :test_order do :parallel end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
##
|
56
|
+
# Returns all instance methods starting with "test_". Based on
|
57
|
+
# #test_order, the methods are either sorted, randomized
|
58
|
+
# (default), or run in parallel.
|
59
|
+
|
60
|
+
def self.runnable_methods
|
61
|
+
methods = methods_matching(/^test_/)
|
62
|
+
|
63
|
+
case self.test_order
|
64
|
+
when :parallel
|
65
|
+
max = methods.size
|
66
|
+
ParallelEach.new methods.sort.sort_by { rand max }
|
67
|
+
when :random then
|
68
|
+
max = methods.size
|
69
|
+
methods.sort.sort_by { rand max }
|
70
|
+
when :alpha, :sorted then
|
71
|
+
methods.sort
|
72
|
+
else
|
73
|
+
raise "Unknown test_order: #{self.test_order.inspect}"
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
##
|
78
|
+
# Defines the order to run tests (:random by default). Override
|
79
|
+
# this or use a convenience method to change it for your tests.
|
80
|
+
|
81
|
+
def self.test_order
|
82
|
+
:random
|
83
|
+
end
|
84
|
+
|
85
|
+
##
|
86
|
+
# The time it took to run this test.
|
87
|
+
|
88
|
+
attr_accessor :time
|
89
|
+
|
90
|
+
def dup # :nodoc:
|
91
|
+
obj = super
|
92
|
+
obj.time = self.time
|
93
|
+
obj
|
94
|
+
end
|
95
|
+
|
96
|
+
##
|
97
|
+
# Runs a single test with setup/teardown hooks.
|
98
|
+
|
99
|
+
def run
|
100
|
+
with_info_handler do
|
101
|
+
time_it do
|
102
|
+
capture_exceptions do
|
103
|
+
before_setup; setup; after_setup
|
104
|
+
|
105
|
+
self.send self.name
|
106
|
+
end
|
107
|
+
|
108
|
+
%w{ before_teardown teardown after_teardown }.each do |hook|
|
109
|
+
capture_exceptions do
|
110
|
+
self.send hook
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
self # per contract
|
117
|
+
end
|
118
|
+
|
119
|
+
##
|
120
|
+
# Provides before/after hooks for setup and teardown. These are
|
121
|
+
# meant for library writers, NOT for regular test authors. See
|
122
|
+
# #before_setup for an example.
|
123
|
+
|
124
|
+
module LifecycleHooks
|
125
|
+
|
126
|
+
##
|
127
|
+
# Runs before every test, before setup. This hook is meant for
|
128
|
+
# libraries to extend minitest. It is not meant to be used by
|
129
|
+
# test developers.
|
130
|
+
#
|
131
|
+
# As a simplistic example:
|
132
|
+
#
|
133
|
+
# module MyMinitestPlugin
|
134
|
+
# def before_setup
|
135
|
+
# super
|
136
|
+
# # ... stuff to do before setup is run
|
137
|
+
# end
|
138
|
+
#
|
139
|
+
# def after_setup
|
140
|
+
# # ... stuff to do after setup is run
|
141
|
+
# super
|
142
|
+
# end
|
143
|
+
#
|
144
|
+
# def before_teardown
|
145
|
+
# super
|
146
|
+
# # ... stuff to do before teardown is run
|
147
|
+
# end
|
148
|
+
#
|
149
|
+
# def after_teardown
|
150
|
+
# # ... stuff to do after teardown is run
|
151
|
+
# super
|
152
|
+
# end
|
153
|
+
# end
|
154
|
+
#
|
155
|
+
# class MiniTest::Test
|
156
|
+
# include MyMinitestPlugin
|
157
|
+
# end
|
158
|
+
|
159
|
+
def before_setup; end
|
160
|
+
|
161
|
+
##
|
162
|
+
# Runs before every test. Use this to set up before each test
|
163
|
+
# run.
|
164
|
+
|
165
|
+
def setup; end
|
166
|
+
|
167
|
+
##
|
168
|
+
# Runs before every test, after setup. This hook is meant for
|
169
|
+
# libraries to extend minitest. It is not meant to be used by
|
170
|
+
# test developers.
|
171
|
+
#
|
172
|
+
# See #before_setup for an example.
|
173
|
+
|
174
|
+
def after_setup; end
|
175
|
+
|
176
|
+
##
|
177
|
+
# Runs after every test, before teardown. This hook is meant for
|
178
|
+
# libraries to extend minitest. It is not meant to be used by
|
179
|
+
# test developers.
|
180
|
+
#
|
181
|
+
# See #before_setup for an example.
|
182
|
+
|
183
|
+
def before_teardown; end
|
184
|
+
|
185
|
+
##
|
186
|
+
# Runs after every test. Use this to clean up after each test
|
187
|
+
# run.
|
188
|
+
|
189
|
+
def teardown; end
|
190
|
+
|
191
|
+
##
|
192
|
+
# Runs after every test, after teardown. This hook is meant for
|
193
|
+
# libraries to extend minitest. It is not meant to be used by
|
194
|
+
# test developers.
|
195
|
+
#
|
196
|
+
# See #before_setup for an example.
|
197
|
+
|
198
|
+
def after_teardown; end
|
199
|
+
end # LifecycleHooks
|
200
|
+
|
201
|
+
def capture_exceptions # :nodoc:
|
202
|
+
begin
|
203
|
+
yield
|
204
|
+
rescue *PASSTHROUGH_EXCEPTIONS
|
205
|
+
raise
|
206
|
+
rescue Assertion => e
|
207
|
+
self.failures << e
|
208
|
+
rescue Exception => e
|
209
|
+
self.failures << UnexpectedError.new(e)
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
##
|
214
|
+
# Did this run error?
|
215
|
+
|
216
|
+
def error?
|
217
|
+
self.failures.any? { |f| UnexpectedError === f }
|
218
|
+
end
|
219
|
+
|
220
|
+
##
|
221
|
+
# The location identifier of this test.
|
222
|
+
|
223
|
+
def location
|
224
|
+
loc = " [#{self.failure.location}]" unless passed? or error?
|
225
|
+
"#{self.class}##{self.name}#{loc}"
|
226
|
+
end
|
227
|
+
|
228
|
+
##
|
229
|
+
# Did this run pass?
|
230
|
+
#
|
231
|
+
# Note: skipped runs are not considered passing, but they don't
|
232
|
+
# cause the process to exit non-zero.
|
233
|
+
|
234
|
+
def passed?
|
235
|
+
not self.failure
|
236
|
+
end
|
237
|
+
|
238
|
+
##
|
239
|
+
# Returns ".", "F", or "E" based on the result of the run.
|
240
|
+
|
241
|
+
def result_code
|
242
|
+
self.failure and self.failure.result_code or "."
|
243
|
+
end
|
244
|
+
|
245
|
+
##
|
246
|
+
# Was this run skipped?
|
247
|
+
|
248
|
+
def skipped?
|
249
|
+
self.failure and Skip === self.failure
|
250
|
+
end
|
251
|
+
|
252
|
+
def time_it # :nodoc:
|
253
|
+
t0 = Time.now
|
254
|
+
|
255
|
+
yield
|
256
|
+
ensure
|
257
|
+
self.time = Time.now - t0
|
258
|
+
end
|
259
|
+
|
260
|
+
def to_s # :nodoc:
|
261
|
+
return location if passed? and not skipped?
|
262
|
+
|
263
|
+
failures.map { |failure|
|
264
|
+
"#{failure.result_label}:\n#{self.location}:\n#{failure.message}\n"
|
265
|
+
}.join "\n"
|
266
|
+
end
|
267
|
+
|
268
|
+
def with_info_handler # :nodoc:
|
269
|
+
supports_info_signal = Signal.list["INFO"]
|
270
|
+
|
271
|
+
t0 = Time.now
|
272
|
+
|
273
|
+
trap "INFO" do
|
274
|
+
warn ""
|
275
|
+
warn "Current: %s#%s %.2fs" % [self.class, self.name, Time.now - t0]
|
276
|
+
end if supports_info_signal
|
277
|
+
|
278
|
+
yield
|
279
|
+
ensure
|
280
|
+
trap "INFO", "DEFAULT" if supports_info_signal
|
281
|
+
end
|
282
|
+
|
283
|
+
include LifecycleHooks
|
284
|
+
include Guard
|
285
|
+
extend Guard
|
286
|
+
end # Test
|
287
|
+
end
|
data/lib/minitest/unit.rb
CHANGED
@@ -1,1415 +1,41 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
#
|
6
|
-
#
|
7
|
-
|
8
|
-
|
9
|
-
module
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
# Assertion base class
|
25
|
-
|
26
|
-
class Assertion < Exception; end
|
27
|
-
|
28
|
-
##
|
29
|
-
# Assertion raised when skipping a test
|
30
|
-
|
31
|
-
class Skip < Assertion; end
|
32
|
-
|
33
|
-
class << self
|
34
|
-
##
|
35
|
-
# Filter object for backtraces.
|
36
|
-
|
37
|
-
attr_accessor :backtrace_filter
|
38
|
-
end
|
39
|
-
|
40
|
-
class BacktraceFilter # :nodoc:
|
41
|
-
def filter bt
|
42
|
-
return ["No backtrace"] unless bt
|
43
|
-
|
44
|
-
new_bt = []
|
45
|
-
|
46
|
-
unless $DEBUG then
|
47
|
-
bt.each do |line|
|
48
|
-
break if line =~ /lib\/minitest/
|
49
|
-
new_bt << line
|
50
|
-
end
|
51
|
-
|
52
|
-
new_bt = bt.reject { |line| line =~ /lib\/minitest/ } if new_bt.empty?
|
53
|
-
new_bt = bt.dup if new_bt.empty?
|
54
|
-
else
|
55
|
-
new_bt = bt.dup
|
56
|
-
end
|
57
|
-
|
58
|
-
new_bt
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
self.backtrace_filter = BacktraceFilter.new
|
63
|
-
|
64
|
-
def self.filter_backtrace bt # :nodoc:
|
65
|
-
backtrace_filter.filter bt
|
66
|
-
end
|
67
|
-
|
68
|
-
##
|
69
|
-
# MiniTest Assertions. All assertion methods accept a +msg+ which is
|
70
|
-
# printed if the assertion fails.
|
71
|
-
|
72
|
-
module Assertions
|
73
|
-
UNDEFINED = Object.new # :nodoc:
|
74
|
-
|
75
|
-
def UNDEFINED.inspect # :nodoc:
|
76
|
-
"UNDEFINED" # again with the rdoc bugs... :(
|
77
|
-
end
|
78
|
-
|
79
|
-
##
|
80
|
-
# Returns the diff command to use in #diff. Tries to intelligently
|
81
|
-
# figure out what diff to use.
|
82
|
-
|
83
|
-
def self.diff
|
84
|
-
@diff = if (RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ &&
|
85
|
-
system("diff.exe", __FILE__, __FILE__)) then
|
86
|
-
"diff.exe -u"
|
87
|
-
elsif Minitest::Unit::Guard.maglev? then # HACK
|
88
|
-
"diff -u"
|
89
|
-
elsif system("gdiff", __FILE__, __FILE__)
|
90
|
-
"gdiff -u" # solaris and kin suck
|
91
|
-
elsif system("diff", __FILE__, __FILE__)
|
92
|
-
"diff -u"
|
93
|
-
else
|
94
|
-
nil
|
95
|
-
end unless defined? @diff
|
96
|
-
|
97
|
-
@diff
|
98
|
-
end
|
99
|
-
|
100
|
-
##
|
101
|
-
# Set the diff command to use in #diff.
|
102
|
-
|
103
|
-
def self.diff= o
|
104
|
-
@diff = o
|
105
|
-
end
|
106
|
-
|
107
|
-
##
|
108
|
-
# Returns a diff between +exp+ and +act+. If there is no known
|
109
|
-
# diff command or if it doesn't make sense to diff the output
|
110
|
-
# (single line, short output), then it simply returns a basic
|
111
|
-
# comparison between the two.
|
112
|
-
|
113
|
-
def diff exp, act
|
114
|
-
require "tempfile"
|
115
|
-
|
116
|
-
expect = mu_pp_for_diff exp
|
117
|
-
butwas = mu_pp_for_diff act
|
118
|
-
result = nil
|
119
|
-
|
120
|
-
need_to_diff =
|
121
|
-
MiniTest::Assertions.diff &&
|
122
|
-
(expect.include?("\n") ||
|
123
|
-
butwas.include?("\n") ||
|
124
|
-
expect.size > 30 ||
|
125
|
-
butwas.size > 30 ||
|
126
|
-
expect == butwas)
|
127
|
-
|
128
|
-
return "Expected: #{mu_pp exp}\n Actual: #{mu_pp act}" unless
|
129
|
-
need_to_diff
|
130
|
-
|
131
|
-
Tempfile.open("expect") do |a|
|
132
|
-
a.puts expect
|
133
|
-
a.flush
|
134
|
-
|
135
|
-
Tempfile.open("butwas") do |b|
|
136
|
-
b.puts butwas
|
137
|
-
b.flush
|
138
|
-
|
139
|
-
result = `#{MiniTest::Assertions.diff} #{a.path} #{b.path}`
|
140
|
-
result.sub!(/^\-\-\- .+/, "--- expected")
|
141
|
-
result.sub!(/^\+\+\+ .+/, "+++ actual")
|
142
|
-
|
143
|
-
if result.empty? then
|
144
|
-
klass = exp.class
|
145
|
-
result = [
|
146
|
-
"No visible difference in the #{klass}#inspect output.\n",
|
147
|
-
"You should look at the implementation of #== on ",
|
148
|
-
"#{klass} or its members.\n",
|
149
|
-
expect,
|
150
|
-
].join
|
151
|
-
end
|
152
|
-
end
|
153
|
-
end
|
154
|
-
|
155
|
-
result
|
156
|
-
end
|
157
|
-
|
158
|
-
##
|
159
|
-
# This returns a human-readable version of +obj+. By default
|
160
|
-
# #inspect is called. You can override this to use #pretty_print
|
161
|
-
# if you want.
|
162
|
-
|
163
|
-
def mu_pp obj
|
164
|
-
s = obj.inspect
|
165
|
-
s = s.encode Encoding.default_external if defined? Encoding
|
166
|
-
s
|
167
|
-
end
|
168
|
-
|
169
|
-
##
|
170
|
-
# This returns a diff-able human-readable version of +obj+. This
|
171
|
-
# differs from the regular mu_pp because it expands escaped
|
172
|
-
# newlines and makes hex-values generic (like object_ids). This
|
173
|
-
# uses mu_pp to do the first pass and then cleans it up.
|
174
|
-
|
175
|
-
def mu_pp_for_diff obj
|
176
|
-
mu_pp(obj).gsub(/\\n/, "\n").gsub(/:0x[a-fA-F0-9]{4,}/m, ':0xXXXXXX')
|
177
|
-
end
|
178
|
-
|
179
|
-
def _assertions= n # :nodoc:
|
180
|
-
@_assertions = n
|
181
|
-
end
|
182
|
-
|
183
|
-
def _assertions # :nodoc:
|
184
|
-
@_assertions ||= 0
|
185
|
-
end
|
186
|
-
|
187
|
-
##
|
188
|
-
# Fails unless +test+ is a true value.
|
189
|
-
|
190
|
-
def assert test, msg = nil
|
191
|
-
msg ||= "Failed assertion, no message given."
|
192
|
-
self._assertions += 1
|
193
|
-
unless test then
|
194
|
-
msg = msg.call if Proc === msg
|
195
|
-
raise MiniTest::Assertion, msg
|
196
|
-
end
|
197
|
-
true
|
198
|
-
end
|
199
|
-
|
200
|
-
##
|
201
|
-
# Fails unless +obj+ is empty.
|
202
|
-
|
203
|
-
def assert_empty obj, msg = nil
|
204
|
-
msg = message(msg) { "Expected #{mu_pp(obj)} to be empty" }
|
205
|
-
assert_respond_to obj, :empty?
|
206
|
-
assert obj.empty?, msg
|
207
|
-
end
|
208
|
-
|
209
|
-
##
|
210
|
-
# Fails unless <tt>exp == act</tt> printing the difference between
|
211
|
-
# the two, if possible.
|
212
|
-
#
|
213
|
-
# If there is no visible difference but the assertion fails, you
|
214
|
-
# should suspect that your #== is buggy, or your inspect output is
|
215
|
-
# missing crucial details.
|
216
|
-
#
|
217
|
-
# For floats use assert_in_delta.
|
218
|
-
#
|
219
|
-
# See also: MiniTest::Assertions.diff
|
220
|
-
|
221
|
-
def assert_equal exp, act, msg = nil
|
222
|
-
msg = message(msg, "") { diff exp, act }
|
223
|
-
assert exp == act, msg
|
224
|
-
end
|
225
|
-
|
226
|
-
##
|
227
|
-
# For comparing Floats. Fails unless +exp+ and +act+ are within +delta+
|
228
|
-
# of each other.
|
229
|
-
#
|
230
|
-
# assert_in_delta Math::PI, (22.0 / 7.0), 0.01
|
231
|
-
|
232
|
-
def assert_in_delta exp, act, delta = 0.001, msg = nil
|
233
|
-
n = (exp - act).abs
|
234
|
-
msg = message(msg) {
|
235
|
-
"Expected |#{exp} - #{act}| (#{n}) to be <= #{delta}"
|
236
|
-
}
|
237
|
-
assert delta >= n, msg
|
238
|
-
end
|
239
|
-
|
240
|
-
##
|
241
|
-
# For comparing Floats. Fails unless +exp+ and +act+ have a relative
|
242
|
-
# error less than +epsilon+.
|
243
|
-
|
244
|
-
def assert_in_epsilon a, b, epsilon = 0.001, msg = nil
|
245
|
-
assert_in_delta a, b, [a.abs, b.abs].min * epsilon, msg
|
246
|
-
end
|
247
|
-
|
248
|
-
##
|
249
|
-
# Fails unless +collection+ includes +obj+.
|
250
|
-
|
251
|
-
def assert_includes collection, obj, msg = nil
|
252
|
-
msg = message(msg) {
|
253
|
-
"Expected #{mu_pp(collection)} to include #{mu_pp(obj)}"
|
254
|
-
}
|
255
|
-
assert_respond_to collection, :include?
|
256
|
-
assert collection.include?(obj), msg
|
257
|
-
end
|
258
|
-
|
259
|
-
##
|
260
|
-
# Fails unless +obj+ is an instance of +cls+.
|
261
|
-
|
262
|
-
def assert_instance_of cls, obj, msg = nil
|
263
|
-
msg = message(msg) {
|
264
|
-
"Expected #{mu_pp(obj)} to be an instance of #{cls}, not #{obj.class}"
|
265
|
-
}
|
266
|
-
|
267
|
-
assert obj.instance_of?(cls), msg
|
268
|
-
end
|
269
|
-
|
270
|
-
##
|
271
|
-
# Fails unless +obj+ is a kind of +cls+.
|
272
|
-
|
273
|
-
def assert_kind_of cls, obj, msg = nil # TODO: merge with instance_of
|
274
|
-
msg = message(msg) {
|
275
|
-
"Expected #{mu_pp(obj)} to be a kind of #{cls}, not #{obj.class}" }
|
276
|
-
|
277
|
-
assert obj.kind_of?(cls), msg
|
278
|
-
end
|
279
|
-
|
280
|
-
##
|
281
|
-
# Fails unless +matcher+ <tt>=~</tt> +obj+.
|
282
|
-
|
283
|
-
def assert_match matcher, obj, msg = nil
|
284
|
-
msg = message(msg) { "Expected #{mu_pp matcher} to match #{mu_pp obj}" }
|
285
|
-
assert_respond_to matcher, :"=~"
|
286
|
-
matcher = Regexp.new Regexp.escape matcher if String === matcher
|
287
|
-
assert matcher =~ obj, msg
|
288
|
-
end
|
289
|
-
|
290
|
-
##
|
291
|
-
# Fails unless +obj+ is nil
|
292
|
-
|
293
|
-
def assert_nil obj, msg = nil
|
294
|
-
msg = message(msg) { "Expected #{mu_pp(obj)} to be nil" }
|
295
|
-
assert obj.nil?, msg
|
296
|
-
end
|
297
|
-
|
298
|
-
##
|
299
|
-
# For testing with binary operators.
|
300
|
-
#
|
301
|
-
# assert_operator 5, :<=, 4
|
302
|
-
|
303
|
-
def assert_operator o1, op, o2 = UNDEFINED, msg = nil
|
304
|
-
return assert_predicate o1, op, msg if UNDEFINED == o2
|
305
|
-
msg = message(msg) { "Expected #{mu_pp(o1)} to be #{op} #{mu_pp(o2)}" }
|
306
|
-
assert o1.__send__(op, o2), msg
|
307
|
-
end
|
308
|
-
|
309
|
-
##
|
310
|
-
# Fails if stdout or stderr do not output the expected results.
|
311
|
-
# Pass in nil if you don't care about that streams output. Pass in
|
312
|
-
# "" if you require it to be silent. Pass in a regexp if you want
|
313
|
-
# to pattern match.
|
314
|
-
#
|
315
|
-
# NOTE: this uses #capture_io, not #capture_subprocess_io.
|
316
|
-
#
|
317
|
-
# See also: #assert_silent
|
318
|
-
|
319
|
-
def assert_output stdout = nil, stderr = nil
|
320
|
-
out, err = capture_io do
|
321
|
-
yield
|
322
|
-
end
|
323
|
-
|
324
|
-
err_msg = Regexp === stderr ? :assert_match : :assert_equal if stderr
|
325
|
-
out_msg = Regexp === stdout ? :assert_match : :assert_equal if stdout
|
326
|
-
|
327
|
-
y = send err_msg, stderr, err, "In stderr" if err_msg
|
328
|
-
x = send out_msg, stdout, out, "In stdout" if out_msg
|
329
|
-
|
330
|
-
(!stdout || x) && (!stderr || y)
|
331
|
-
end
|
332
|
-
|
333
|
-
##
|
334
|
-
# For testing with predicates.
|
335
|
-
#
|
336
|
-
# assert_predicate str, :empty?
|
337
|
-
#
|
338
|
-
# This is really meant for specs and is front-ended by assert_operator:
|
339
|
-
#
|
340
|
-
# str.must_be :empty?
|
341
|
-
|
342
|
-
def assert_predicate o1, op, msg = nil
|
343
|
-
msg = message(msg) { "Expected #{mu_pp(o1)} to be #{op}" }
|
344
|
-
assert o1.__send__(op), msg
|
345
|
-
end
|
346
|
-
|
347
|
-
##
|
348
|
-
# Fails unless the block raises one of +exp+. Returns the
|
349
|
-
# exception matched so you can check the message, attributes, etc.
|
350
|
-
|
351
|
-
def assert_raises *exp
|
352
|
-
msg = "#{exp.pop}.\n" if String === exp.last
|
353
|
-
|
354
|
-
begin
|
355
|
-
yield
|
356
|
-
rescue MiniTest::Skip => e
|
357
|
-
return e if exp.include? MiniTest::Skip
|
358
|
-
raise e
|
359
|
-
rescue Exception => e
|
360
|
-
expected = exp.any? { |ex|
|
361
|
-
if ex.instance_of? Module then
|
362
|
-
e.kind_of? ex
|
363
|
-
else
|
364
|
-
e.instance_of? ex
|
365
|
-
end
|
366
|
-
}
|
367
|
-
|
368
|
-
assert expected, proc {
|
369
|
-
exception_details(e, "#{msg}#{mu_pp(exp)} exception expected, not")
|
370
|
-
}
|
371
|
-
|
372
|
-
return e
|
373
|
-
end
|
374
|
-
|
375
|
-
exp = exp.first if exp.size == 1
|
376
|
-
|
377
|
-
flunk "#{msg}#{mu_pp(exp)} expected but nothing was raised."
|
378
|
-
end
|
379
|
-
|
380
|
-
##
|
381
|
-
# Fails unless +obj+ responds to +meth+.
|
382
|
-
|
383
|
-
def assert_respond_to obj, meth, msg = nil
|
384
|
-
msg = message(msg) {
|
385
|
-
"Expected #{mu_pp(obj)} (#{obj.class}) to respond to ##{meth}"
|
386
|
-
}
|
387
|
-
assert obj.respond_to?(meth), msg
|
388
|
-
end
|
389
|
-
|
390
|
-
##
|
391
|
-
# Fails unless +exp+ and +act+ are #equal?
|
392
|
-
|
393
|
-
def assert_same exp, act, msg = nil
|
394
|
-
msg = message(msg) {
|
395
|
-
data = [mu_pp(act), act.object_id, mu_pp(exp), exp.object_id]
|
396
|
-
"Expected %s (oid=%d) to be the same as %s (oid=%d)" % data
|
397
|
-
}
|
398
|
-
assert exp.equal?(act), msg
|
399
|
-
end
|
400
|
-
|
401
|
-
##
|
402
|
-
# +send_ary+ is a receiver, message and arguments.
|
403
|
-
#
|
404
|
-
# Fails unless the call returns a true value
|
405
|
-
# TODO: I should prolly remove this from specs
|
406
|
-
|
407
|
-
def assert_send send_ary, m = nil
|
408
|
-
recv, msg, *args = send_ary
|
409
|
-
m = message(m) {
|
410
|
-
"Expected #{mu_pp(recv)}.#{msg}(*#{mu_pp(args)}) to return true" }
|
411
|
-
assert recv.__send__(msg, *args), m
|
412
|
-
end
|
413
|
-
|
414
|
-
##
|
415
|
-
# Fails if the block outputs anything to stderr or stdout.
|
416
|
-
#
|
417
|
-
# See also: #assert_output
|
418
|
-
|
419
|
-
def assert_silent
|
420
|
-
assert_output "", "" do
|
421
|
-
yield
|
422
|
-
end
|
423
|
-
end
|
424
|
-
|
425
|
-
##
|
426
|
-
# Fails unless the block throws +sym+
|
427
|
-
|
428
|
-
def assert_throws sym, msg = nil
|
429
|
-
default = "Expected #{mu_pp(sym)} to have been thrown"
|
430
|
-
caught = true
|
431
|
-
catch(sym) do
|
432
|
-
begin
|
433
|
-
yield
|
434
|
-
rescue ThreadError => e # wtf?!? 1.8 + threads == suck
|
435
|
-
default += ", not \:#{e.message[/uncaught throw \`(\w+?)\'/, 1]}"
|
436
|
-
rescue ArgumentError => e # 1.9 exception
|
437
|
-
default += ", not #{e.message.split(/ /).last}"
|
438
|
-
rescue NameError => e # 1.8 exception
|
439
|
-
default += ", not #{e.name.inspect}"
|
440
|
-
end
|
441
|
-
caught = false
|
442
|
-
end
|
443
|
-
|
444
|
-
assert caught, message(msg) { default }
|
445
|
-
end
|
446
|
-
|
447
|
-
##
|
448
|
-
# Captures $stdout and $stderr into strings:
|
449
|
-
#
|
450
|
-
# out, err = capture_io do
|
451
|
-
# puts "Some info"
|
452
|
-
# warn "You did a bad thing"
|
453
|
-
# end
|
454
|
-
#
|
455
|
-
# assert_match %r%info%, out
|
456
|
-
# assert_match %r%bad%, err
|
457
|
-
#
|
458
|
-
# NOTE: For efficiency, this method uses StringIO and does not
|
459
|
-
# capture IO for subprocesses. Use #capture_subprocess_io for
|
460
|
-
# that.
|
461
|
-
|
462
|
-
def capture_io
|
463
|
-
require 'stringio'
|
464
|
-
|
465
|
-
captured_stdout, captured_stderr = StringIO.new, StringIO.new
|
466
|
-
|
467
|
-
synchronize do
|
468
|
-
orig_stdout, orig_stderr = $stdout, $stderr
|
469
|
-
$stdout, $stderr = captured_stdout, captured_stderr
|
470
|
-
|
471
|
-
begin
|
472
|
-
yield
|
473
|
-
ensure
|
474
|
-
$stdout = orig_stdout
|
475
|
-
$stderr = orig_stderr
|
476
|
-
end
|
477
|
-
end
|
478
|
-
|
479
|
-
return captured_stdout.string, captured_stderr.string
|
480
|
-
end
|
481
|
-
|
482
|
-
##
|
483
|
-
# Captures $stdout and $stderr into strings, using Tempfile to
|
484
|
-
# ensure that subprocess IO is captured as well.
|
485
|
-
#
|
486
|
-
# out, err = capture_subprocess_io do
|
487
|
-
# system "echo Some info"
|
488
|
-
# system "echo You did a bad thing 1>&2"
|
489
|
-
# end
|
490
|
-
#
|
491
|
-
# assert_match %r%info%, out
|
492
|
-
# assert_match %r%bad%, err
|
493
|
-
#
|
494
|
-
# NOTE: This method is approximately 10x slower than #capture_io so
|
495
|
-
# only use it when you need to test the output of a subprocess.
|
496
|
-
|
497
|
-
def capture_subprocess_io
|
498
|
-
require 'tempfile'
|
499
|
-
|
500
|
-
captured_stdout, captured_stderr = Tempfile.new("out"), Tempfile.new("err")
|
501
|
-
|
502
|
-
synchronize do
|
503
|
-
orig_stdout, orig_stderr = $stdout.dup, $stderr.dup
|
504
|
-
$stdout.reopen captured_stdout
|
505
|
-
$stderr.reopen captured_stderr
|
506
|
-
|
507
|
-
begin
|
508
|
-
yield
|
509
|
-
|
510
|
-
$stdout.rewind
|
511
|
-
$stderr.rewind
|
512
|
-
|
513
|
-
[captured_stdout.read, captured_stderr.read]
|
514
|
-
ensure
|
515
|
-
captured_stdout.unlink
|
516
|
-
captured_stderr.unlink
|
517
|
-
$stdout.reopen orig_stdout
|
518
|
-
$stderr.reopen orig_stderr
|
519
|
-
end
|
1
|
+
# :stopdoc:
|
2
|
+
|
3
|
+
unless defined?(Minitest) then
|
4
|
+
# all of this crap is just to avoid circular requires and is only
|
5
|
+
# needed if a user requires "minitest/unit" directly instead of
|
6
|
+
# "minitest/autorun", so we also warn
|
7
|
+
from = caller.join("\n ")
|
8
|
+
warn "Warning: you should require 'minitest/autorun' instead.\nFrom #{from}"
|
9
|
+
module Minitest; end
|
10
|
+
MiniTest = Minitest # prevents minitest.rb from requiring back to us
|
11
|
+
require "minitest"
|
12
|
+
end
|
13
|
+
|
14
|
+
MiniTest = Minitest unless defined?(MiniTest)
|
15
|
+
|
16
|
+
module Minitest
|
17
|
+
class Unit
|
18
|
+
VERSION = Minitest::VERSION
|
19
|
+
class TestCase < Minitest::Test
|
20
|
+
def self.inherited klass # :nodoc:
|
21
|
+
from = caller.first
|
22
|
+
warn "MiniTest::Unit::TestCase is now Minitest::Test. From #{from}"
|
23
|
+
super
|
520
24
|
end
|
521
25
|
end
|
522
26
|
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
[
|
528
|
-
"#{msg}",
|
529
|
-
"Class: <#{e.class}>",
|
530
|
-
"Message: <#{e.message.inspect}>",
|
531
|
-
"---Backtrace---",
|
532
|
-
"#{MiniTest::filter_backtrace(e.backtrace).join("\n")}",
|
533
|
-
"---------------",
|
534
|
-
].join "\n"
|
535
|
-
end
|
536
|
-
|
537
|
-
##
|
538
|
-
# Fails with +msg+
|
539
|
-
|
540
|
-
def flunk msg = nil
|
541
|
-
msg ||= "Epic Fail!"
|
542
|
-
assert false, msg
|
543
|
-
end
|
544
|
-
|
545
|
-
##
|
546
|
-
# Returns a proc that will output +msg+ along with the default message.
|
547
|
-
|
548
|
-
def message msg = nil, ending = ".", &default
|
549
|
-
proc {
|
550
|
-
msg = msg.call.chomp(".") if Proc === msg
|
551
|
-
custom_message = "#{msg}.\n" unless msg.nil? or msg.to_s.empty?
|
552
|
-
"#{custom_message}#{default.call}#{ending}"
|
553
|
-
}
|
554
|
-
end
|
555
|
-
|
556
|
-
##
|
557
|
-
# used for counting assertions
|
558
|
-
|
559
|
-
def pass msg = nil
|
560
|
-
assert true
|
561
|
-
end
|
562
|
-
|
563
|
-
##
|
564
|
-
# Fails if +test+ is a true value
|
565
|
-
|
566
|
-
def refute test, msg = nil
|
567
|
-
msg ||= "Failed refutation, no message given"
|
568
|
-
not assert(! test, msg)
|
569
|
-
end
|
570
|
-
|
571
|
-
##
|
572
|
-
# Fails if +obj+ is empty.
|
573
|
-
|
574
|
-
def refute_empty obj, msg = nil
|
575
|
-
msg = message(msg) { "Expected #{mu_pp(obj)} to not be empty" }
|
576
|
-
assert_respond_to obj, :empty?
|
577
|
-
refute obj.empty?, msg
|
578
|
-
end
|
579
|
-
|
580
|
-
##
|
581
|
-
# Fails if <tt>exp == act</tt>.
|
582
|
-
#
|
583
|
-
# For floats use refute_in_delta.
|
584
|
-
|
585
|
-
def refute_equal exp, act, msg = nil
|
586
|
-
msg = message(msg) {
|
587
|
-
"Expected #{mu_pp(act)} to not be equal to #{mu_pp(exp)}"
|
588
|
-
}
|
589
|
-
refute exp == act, msg
|
590
|
-
end
|
591
|
-
|
592
|
-
##
|
593
|
-
# For comparing Floats. Fails if +exp+ is within +delta+ of +act+.
|
594
|
-
#
|
595
|
-
# refute_in_delta Math::PI, (22.0 / 7.0)
|
596
|
-
|
597
|
-
def refute_in_delta exp, act, delta = 0.001, msg = nil
|
598
|
-
n = (exp - act).abs
|
599
|
-
msg = message(msg) {
|
600
|
-
"Expected |#{exp} - #{act}| (#{n}) to not be <= #{delta}"
|
601
|
-
}
|
602
|
-
refute delta >= n, msg
|
603
|
-
end
|
604
|
-
|
605
|
-
##
|
606
|
-
# For comparing Floats. Fails if +exp+ and +act+ have a relative error
|
607
|
-
# less than +epsilon+.
|
608
|
-
|
609
|
-
def refute_in_epsilon a, b, epsilon = 0.001, msg = nil
|
610
|
-
refute_in_delta a, b, a * epsilon, msg
|
611
|
-
end
|
612
|
-
|
613
|
-
##
|
614
|
-
# Fails if +collection+ includes +obj+.
|
615
|
-
|
616
|
-
def refute_includes collection, obj, msg = nil
|
617
|
-
msg = message(msg) {
|
618
|
-
"Expected #{mu_pp(collection)} to not include #{mu_pp(obj)}"
|
619
|
-
}
|
620
|
-
assert_respond_to collection, :include?
|
621
|
-
refute collection.include?(obj), msg
|
622
|
-
end
|
623
|
-
|
624
|
-
##
|
625
|
-
# Fails if +obj+ is an instance of +cls+.
|
626
|
-
|
627
|
-
def refute_instance_of cls, obj, msg = nil
|
628
|
-
msg = message(msg) {
|
629
|
-
"Expected #{mu_pp(obj)} to not be an instance of #{cls}"
|
630
|
-
}
|
631
|
-
refute obj.instance_of?(cls), msg
|
632
|
-
end
|
633
|
-
|
634
|
-
##
|
635
|
-
# Fails if +obj+ is a kind of +cls+.
|
636
|
-
|
637
|
-
def refute_kind_of cls, obj, msg = nil # TODO: merge with instance_of
|
638
|
-
msg = message(msg) { "Expected #{mu_pp(obj)} to not be a kind of #{cls}" }
|
639
|
-
refute obj.kind_of?(cls), msg
|
640
|
-
end
|
641
|
-
|
642
|
-
##
|
643
|
-
# Fails if +matcher+ <tt>=~</tt> +obj+.
|
644
|
-
|
645
|
-
def refute_match matcher, obj, msg = nil
|
646
|
-
msg = message(msg) {"Expected #{mu_pp matcher} to not match #{mu_pp obj}"}
|
647
|
-
assert_respond_to matcher, :"=~"
|
648
|
-
matcher = Regexp.new Regexp.escape matcher if String === matcher
|
649
|
-
refute matcher =~ obj, msg
|
650
|
-
end
|
651
|
-
|
652
|
-
##
|
653
|
-
# Fails if +obj+ is nil.
|
654
|
-
|
655
|
-
def refute_nil obj, msg = nil
|
656
|
-
msg = message(msg) { "Expected #{mu_pp(obj)} to not be nil" }
|
657
|
-
refute obj.nil?, msg
|
658
|
-
end
|
659
|
-
|
660
|
-
##
|
661
|
-
# Fails if +o1+ is not +op+ +o2+. Eg:
|
662
|
-
#
|
663
|
-
# refute_operator 1, :>, 2 #=> pass
|
664
|
-
# refute_operator 1, :<, 2 #=> fail
|
665
|
-
|
666
|
-
def refute_operator o1, op, o2 = UNDEFINED, msg = nil
|
667
|
-
return refute_predicate o1, op, msg if UNDEFINED == o2
|
668
|
-
msg = message(msg) { "Expected #{mu_pp(o1)} to not be #{op} #{mu_pp(o2)}"}
|
669
|
-
refute o1.__send__(op, o2), msg
|
670
|
-
end
|
671
|
-
|
672
|
-
##
|
673
|
-
# For testing with predicates.
|
674
|
-
#
|
675
|
-
# refute_predicate str, :empty?
|
676
|
-
#
|
677
|
-
# This is really meant for specs and is front-ended by refute_operator:
|
678
|
-
#
|
679
|
-
# str.wont_be :empty?
|
680
|
-
|
681
|
-
def refute_predicate o1, op, msg = nil
|
682
|
-
msg = message(msg) { "Expected #{mu_pp(o1)} to not be #{op}" }
|
683
|
-
refute o1.__send__(op), msg
|
684
|
-
end
|
685
|
-
|
686
|
-
##
|
687
|
-
# Fails if +obj+ responds to the message +meth+.
|
688
|
-
|
689
|
-
def refute_respond_to obj, meth, msg = nil
|
690
|
-
msg = message(msg) { "Expected #{mu_pp(obj)} to not respond to #{meth}" }
|
691
|
-
|
692
|
-
refute obj.respond_to?(meth), msg
|
693
|
-
end
|
694
|
-
|
695
|
-
##
|
696
|
-
# Fails if +exp+ is the same (by object identity) as +act+.
|
697
|
-
|
698
|
-
def refute_same exp, act, msg = nil
|
699
|
-
msg = message(msg) {
|
700
|
-
data = [mu_pp(act), act.object_id, mu_pp(exp), exp.object_id]
|
701
|
-
"Expected %s (oid=%d) to not be the same as %s (oid=%d)" % data
|
702
|
-
}
|
703
|
-
refute exp.equal?(act), msg
|
704
|
-
end
|
705
|
-
|
706
|
-
##
|
707
|
-
# Skips the current test. Gets listed at the end of the run but
|
708
|
-
# doesn't cause a failure exit code.
|
709
|
-
|
710
|
-
def skip msg = nil, bt = caller
|
711
|
-
msg ||= "Skipped, no message given"
|
712
|
-
@skip = true
|
713
|
-
raise MiniTest::Skip, msg, bt
|
714
|
-
end
|
715
|
-
|
716
|
-
##
|
717
|
-
# Was this testcase skipped? Meant for #teardown.
|
718
|
-
|
719
|
-
def skipped?
|
720
|
-
defined?(@skip) and @skip
|
27
|
+
def self.autorun # :nodoc:
|
28
|
+
from = caller.first
|
29
|
+
warn "MiniTest::Unit.autorun is now Minitest.autorun. From #{from}"
|
30
|
+
Minitest.autorun
|
721
31
|
end
|
722
32
|
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
Minitest::Unit.runner.synchronize do
|
728
|
-
yield
|
729
|
-
end
|
33
|
+
def self.after_tests(&b)
|
34
|
+
from = caller.first
|
35
|
+
warn "MiniTest::Unit.after_tests is now Minitest.after_run. From #{from}"
|
36
|
+
Minitest.after_run(&b)
|
730
37
|
end
|
731
38
|
end
|
39
|
+
end
|
732
40
|
|
733
|
-
|
734
|
-
VERSION = "4.7.5" # :nodoc:
|
735
|
-
|
736
|
-
attr_accessor :report, :failures, :errors, :skips # :nodoc:
|
737
|
-
attr_accessor :assertion_count # :nodoc:
|
738
|
-
attr_writer :test_count # :nodoc:
|
739
|
-
attr_accessor :start_time # :nodoc:
|
740
|
-
attr_accessor :help # :nodoc:
|
741
|
-
attr_accessor :verbose # :nodoc:
|
742
|
-
attr_writer :options # :nodoc:
|
743
|
-
|
744
|
-
##
|
745
|
-
# :attr:
|
746
|
-
#
|
747
|
-
# if true, installs an "INFO" signal handler (only available to BSD and
|
748
|
-
# OS X users) which prints diagnostic information about the test run.
|
749
|
-
#
|
750
|
-
# This is auto-detected by default but may be overridden by custom
|
751
|
-
# runners.
|
752
|
-
|
753
|
-
attr_accessor :info_signal
|
754
|
-
|
755
|
-
##
|
756
|
-
# Lazy accessor for options.
|
757
|
-
|
758
|
-
def options
|
759
|
-
@options ||= {}
|
760
|
-
end
|
761
|
-
|
762
|
-
@@installed_at_exit ||= false
|
763
|
-
@@out = $stdout
|
764
|
-
@@after_tests = []
|
765
|
-
|
766
|
-
##
|
767
|
-
# A simple hook allowing you to run a block of code after _all_ of
|
768
|
-
# the tests are done. Eg:
|
769
|
-
#
|
770
|
-
# MiniTest::Unit.after_tests { p $debugging_info }
|
771
|
-
|
772
|
-
def self.after_tests &block
|
773
|
-
@@after_tests << block
|
774
|
-
end
|
775
|
-
|
776
|
-
##
|
777
|
-
# Registers MiniTest::Unit to run tests at process exit
|
778
|
-
|
779
|
-
def self.autorun
|
780
|
-
at_exit {
|
781
|
-
# don't run if there was a non-exit exception
|
782
|
-
next if $! and not $!.kind_of? SystemExit
|
783
|
-
|
784
|
-
# the order here is important. The at_exit handler must be
|
785
|
-
# installed before anyone else gets a chance to install their
|
786
|
-
# own, that way we can be assured that our exit will be last
|
787
|
-
# to run (at_exit stacks).
|
788
|
-
exit_code = nil
|
789
|
-
|
790
|
-
at_exit {
|
791
|
-
@@after_tests.reverse_each(&:call)
|
792
|
-
exit false if exit_code && exit_code != 0
|
793
|
-
}
|
794
|
-
|
795
|
-
exit_code = MiniTest::Unit.new.run ARGV
|
796
|
-
} unless @@installed_at_exit
|
797
|
-
@@installed_at_exit = true
|
798
|
-
end
|
799
|
-
|
800
|
-
##
|
801
|
-
# Returns the stream to use for output.
|
802
|
-
|
803
|
-
def self.output
|
804
|
-
@@out
|
805
|
-
end
|
806
|
-
|
807
|
-
##
|
808
|
-
# Sets MiniTest::Unit to write output to +stream+. $stdout is the default
|
809
|
-
# output
|
810
|
-
|
811
|
-
def self.output= stream
|
812
|
-
@@out = stream
|
813
|
-
end
|
814
|
-
|
815
|
-
##
|
816
|
-
# Tells MiniTest::Unit to delegate to +runner+, an instance of a
|
817
|
-
# MiniTest::Unit subclass, when MiniTest::Unit#run is called.
|
818
|
-
|
819
|
-
def self.runner= runner
|
820
|
-
@@runner = runner
|
821
|
-
end
|
822
|
-
|
823
|
-
##
|
824
|
-
# Returns the MiniTest::Unit subclass instance that will be used
|
825
|
-
# to run the tests. A MiniTest::Unit instance is the default
|
826
|
-
# runner.
|
827
|
-
|
828
|
-
def self.runner
|
829
|
-
@@runner ||= self.new
|
830
|
-
end
|
831
|
-
|
832
|
-
##
|
833
|
-
# Return all plugins' run methods (methods that start with "run_").
|
834
|
-
|
835
|
-
def self.plugins
|
836
|
-
@@plugins ||= (["run_tests"] +
|
837
|
-
public_instance_methods(false).
|
838
|
-
grep(/^run_/).map { |s| s.to_s }).uniq
|
839
|
-
end
|
840
|
-
|
841
|
-
##
|
842
|
-
# Return the IO for output.
|
843
|
-
|
844
|
-
def output
|
845
|
-
self.class.output
|
846
|
-
end
|
847
|
-
|
848
|
-
def puts *a # :nodoc:
|
849
|
-
output.puts(*a)
|
850
|
-
end
|
851
|
-
|
852
|
-
def print *a # :nodoc:
|
853
|
-
output.print(*a)
|
854
|
-
end
|
855
|
-
|
856
|
-
def test_count # :nodoc:
|
857
|
-
@test_count ||= 0
|
858
|
-
end
|
859
|
-
|
860
|
-
##
|
861
|
-
# Runner for a given +type+ (eg, test vs bench).
|
862
|
-
|
863
|
-
def _run_anything type
|
864
|
-
suites = TestCase.send "#{type}_suites"
|
865
|
-
return if suites.empty?
|
866
|
-
|
867
|
-
start = Time.now
|
868
|
-
|
869
|
-
puts
|
870
|
-
puts "# Running #{type}s:"
|
871
|
-
puts
|
872
|
-
|
873
|
-
@test_count, @assertion_count = 0, 0
|
874
|
-
sync = output.respond_to? :"sync=" # stupid emacs
|
875
|
-
old_sync, output.sync = output.sync, true if sync
|
876
|
-
|
877
|
-
results = _run_suites suites, type
|
878
|
-
|
879
|
-
@test_count = results.inject(0) { |sum, (tc, _)| sum + tc }
|
880
|
-
@assertion_count = results.inject(0) { |sum, (_, ac)| sum + ac }
|
881
|
-
|
882
|
-
output.sync = old_sync if sync
|
883
|
-
|
884
|
-
t = Time.now - start
|
885
|
-
|
886
|
-
puts
|
887
|
-
puts
|
888
|
-
puts "Finished #{type}s in %.6fs, %.4f tests/s, %.4f assertions/s." %
|
889
|
-
[t, test_count / t, assertion_count / t]
|
890
|
-
|
891
|
-
report.each_with_index do |msg, i|
|
892
|
-
puts "\n%3d) %s" % [i + 1, msg]
|
893
|
-
end
|
894
|
-
|
895
|
-
puts
|
896
|
-
|
897
|
-
status
|
898
|
-
end
|
899
|
-
|
900
|
-
##
|
901
|
-
# Runs all the +suites+ for a given +type+.
|
902
|
-
#
|
903
|
-
# NOTE: this method is redefined in parallel_each.rb, which is
|
904
|
-
# loaded if a test-suite calls parallelize_me!.
|
905
|
-
|
906
|
-
def _run_suites suites, type
|
907
|
-
suites.map { |suite| _run_suite suite, type }
|
908
|
-
end
|
909
|
-
|
910
|
-
##
|
911
|
-
# Run a single +suite+ for a given +type+.
|
912
|
-
|
913
|
-
def _run_suite suite, type
|
914
|
-
header = "#{type}_suite_header"
|
915
|
-
puts send(header, suite) if respond_to? header
|
916
|
-
|
917
|
-
filter = options[:filter] || '/./'
|
918
|
-
filter = Regexp.new $1 if filter =~ /\/(.*)\//
|
919
|
-
|
920
|
-
all_test_methods = suite.send "#{type}_methods"
|
921
|
-
|
922
|
-
filtered_test_methods = all_test_methods.find_all { |m|
|
923
|
-
filter === m || filter === "#{suite}##{m}"
|
924
|
-
}
|
925
|
-
|
926
|
-
assertions = filtered_test_methods.map { |method|
|
927
|
-
inst = suite.new method
|
928
|
-
inst._assertions = 0
|
929
|
-
|
930
|
-
print "#{suite}##{method} = " if @verbose
|
931
|
-
|
932
|
-
start_time = Time.now if @verbose
|
933
|
-
result = inst.run self
|
934
|
-
|
935
|
-
print "%.2f s = " % (Time.now - start_time) if @verbose
|
936
|
-
print result
|
937
|
-
puts if @verbose
|
938
|
-
|
939
|
-
inst._assertions
|
940
|
-
}
|
941
|
-
|
942
|
-
return assertions.size, assertions.inject(0) { |sum, n| sum + n }
|
943
|
-
end
|
944
|
-
|
945
|
-
##
|
946
|
-
# Record the result of a single test. Makes it very easy to gather
|
947
|
-
# information. Eg:
|
948
|
-
#
|
949
|
-
# class StatisticsRecorder < MiniTest::Unit
|
950
|
-
# def record suite, method, assertions, time, error
|
951
|
-
# # ... record the results somewhere ...
|
952
|
-
# end
|
953
|
-
# end
|
954
|
-
#
|
955
|
-
# MiniTest::Unit.runner = StatisticsRecorder.new
|
956
|
-
#
|
957
|
-
# NOTE: record might be sent more than once per test. It will be
|
958
|
-
# sent once with the results from the test itself. If there is a
|
959
|
-
# failure or error in teardown, it will be sent again with the
|
960
|
-
# error or failure.
|
961
|
-
|
962
|
-
def record suite, method, assertions, time, error
|
963
|
-
end
|
964
|
-
|
965
|
-
def location e # :nodoc:
|
966
|
-
last_before_assertion = ""
|
967
|
-
e.backtrace.reverse_each do |s|
|
968
|
-
break if s =~ /in .(assert|refute|flunk|pass|fail|raise|must|wont)/
|
969
|
-
last_before_assertion = s
|
970
|
-
end
|
971
|
-
last_before_assertion.sub(/:in .*$/, '')
|
972
|
-
end
|
973
|
-
|
974
|
-
##
|
975
|
-
# Writes status for failed test +meth+ in +klass+ which finished with
|
976
|
-
# exception +e+
|
977
|
-
|
978
|
-
def puke klass, meth, e
|
979
|
-
e = case e
|
980
|
-
when MiniTest::Skip then
|
981
|
-
@skips += 1
|
982
|
-
return "S" unless @verbose
|
983
|
-
"Skipped:\n#{klass}##{meth} [#{location e}]:\n#{e.message}\n"
|
984
|
-
when MiniTest::Assertion then
|
985
|
-
@failures += 1
|
986
|
-
"Failure:\n#{klass}##{meth} [#{location e}]:\n#{e.message}\n"
|
987
|
-
else
|
988
|
-
@errors += 1
|
989
|
-
bt = MiniTest::filter_backtrace(e.backtrace).join "\n "
|
990
|
-
"Error:\n#{klass}##{meth}:\n#{e.class}: #{e.message}\n #{bt}\n"
|
991
|
-
end
|
992
|
-
@report << e
|
993
|
-
e[0, 1]
|
994
|
-
end
|
995
|
-
|
996
|
-
def initialize # :nodoc:
|
997
|
-
@report = []
|
998
|
-
@errors = @failures = @skips = 0
|
999
|
-
@verbose = false
|
1000
|
-
@mutex = defined?(Mutex) ? Mutex.new : nil
|
1001
|
-
@info_signal = Signal.list['INFO']
|
1002
|
-
end
|
1003
|
-
|
1004
|
-
def synchronize # :nodoc:
|
1005
|
-
if @mutex then
|
1006
|
-
@mutex.synchronize { yield }
|
1007
|
-
else
|
1008
|
-
yield
|
1009
|
-
end
|
1010
|
-
end
|
1011
|
-
|
1012
|
-
def process_args args = [] # :nodoc:
|
1013
|
-
options = {}
|
1014
|
-
orig_args = args.dup
|
1015
|
-
|
1016
|
-
OptionParser.new do |opts|
|
1017
|
-
opts.banner = 'minitest options:'
|
1018
|
-
opts.version = MiniTest::Unit::VERSION
|
1019
|
-
|
1020
|
-
opts.on '-h', '--help', 'Display this help.' do
|
1021
|
-
puts opts
|
1022
|
-
exit
|
1023
|
-
end
|
1024
|
-
|
1025
|
-
opts.on '-s', '--seed SEED', Integer, "Sets random seed" do |m|
|
1026
|
-
options[:seed] = m.to_i
|
1027
|
-
end
|
1028
|
-
|
1029
|
-
opts.on '-v', '--verbose', "Verbose. Show progress processing files." do
|
1030
|
-
options[:verbose] = true
|
1031
|
-
end
|
1032
|
-
|
1033
|
-
opts.on '-n', '--name PATTERN', "Filter test names on pattern (e.g. /foo/)" do |a|
|
1034
|
-
options[:filter] = a
|
1035
|
-
end
|
1036
|
-
|
1037
|
-
opts.parse! args
|
1038
|
-
orig_args -= args
|
1039
|
-
end
|
1040
|
-
|
1041
|
-
unless options[:seed] then
|
1042
|
-
srand
|
1043
|
-
options[:seed] = srand % 0xFFFF
|
1044
|
-
orig_args << "--seed" << options[:seed].to_s
|
1045
|
-
end
|
1046
|
-
|
1047
|
-
srand options[:seed]
|
1048
|
-
|
1049
|
-
self.verbose = options[:verbose]
|
1050
|
-
@help = orig_args.map { |s| s =~ /[\s|&<>$()]/ ? s.inspect : s }.join " "
|
1051
|
-
|
1052
|
-
options
|
1053
|
-
end
|
1054
|
-
|
1055
|
-
##
|
1056
|
-
# Begins the full test run. Delegates to +runner+'s #_run method.
|
1057
|
-
|
1058
|
-
def run args = []
|
1059
|
-
self.class.runner._run(args)
|
1060
|
-
end
|
1061
|
-
|
1062
|
-
##
|
1063
|
-
# Top level driver, controls all output and filtering.
|
1064
|
-
|
1065
|
-
def _run args = []
|
1066
|
-
args = process_args args # ARGH!! blame test/unit process_args
|
1067
|
-
self.options.merge! args
|
1068
|
-
|
1069
|
-
puts "Run options: #{help}"
|
1070
|
-
|
1071
|
-
self.class.plugins.each do |plugin|
|
1072
|
-
send plugin
|
1073
|
-
break unless report.empty?
|
1074
|
-
end
|
1075
|
-
|
1076
|
-
return failures + errors if self.test_count > 0 # or return nil...
|
1077
|
-
rescue Interrupt
|
1078
|
-
abort 'Interrupted'
|
1079
|
-
end
|
1080
|
-
|
1081
|
-
##
|
1082
|
-
# Runs test suites matching +filter+.
|
1083
|
-
|
1084
|
-
def run_tests
|
1085
|
-
_run_anything :test
|
1086
|
-
end
|
1087
|
-
|
1088
|
-
##
|
1089
|
-
# Writes status to +io+
|
1090
|
-
|
1091
|
-
def status io = self.output
|
1092
|
-
format = "%d tests, %d assertions, %d failures, %d errors, %d skips"
|
1093
|
-
io.puts format % [test_count, assertion_count, failures, errors, skips]
|
1094
|
-
end
|
1095
|
-
|
1096
|
-
##
|
1097
|
-
# Provides a simple set of guards that you can use in your tests
|
1098
|
-
# to skip execution if it is not applicable. These methods are
|
1099
|
-
# mixed into TestCase as both instance and class methods so you
|
1100
|
-
# can use them inside or outside of the test methods.
|
1101
|
-
#
|
1102
|
-
# def test_something_for_mri
|
1103
|
-
# skip "bug 1234" if jruby?
|
1104
|
-
# # ...
|
1105
|
-
# end
|
1106
|
-
#
|
1107
|
-
# if windows? then
|
1108
|
-
# # ... lots of test methods ...
|
1109
|
-
# end
|
1110
|
-
|
1111
|
-
module Guard
|
1112
|
-
|
1113
|
-
##
|
1114
|
-
# Is this running on jruby?
|
1115
|
-
|
1116
|
-
def jruby? platform = RUBY_PLATFORM
|
1117
|
-
"java" == platform
|
1118
|
-
end
|
1119
|
-
|
1120
|
-
##
|
1121
|
-
# Is this running on mri?
|
1122
|
-
|
1123
|
-
def maglev? platform = defined?(RUBY_ENGINE) && RUBY_ENGINE
|
1124
|
-
"maglev" == platform
|
1125
|
-
end
|
1126
|
-
|
1127
|
-
module_function :maglev?
|
1128
|
-
|
1129
|
-
##
|
1130
|
-
# Is this running on mri?
|
1131
|
-
|
1132
|
-
def mri? platform = RUBY_DESCRIPTION
|
1133
|
-
/^ruby/ =~ platform
|
1134
|
-
end
|
1135
|
-
|
1136
|
-
##
|
1137
|
-
# Is this running on rubinius?
|
1138
|
-
|
1139
|
-
def rubinius? platform = defined?(RUBY_ENGINE) && RUBY_ENGINE
|
1140
|
-
"rbx" == platform
|
1141
|
-
end
|
1142
|
-
|
1143
|
-
##
|
1144
|
-
# Is this running on windows?
|
1145
|
-
|
1146
|
-
def windows? platform = RUBY_PLATFORM
|
1147
|
-
/mswin|mingw/ =~ platform
|
1148
|
-
end
|
1149
|
-
end
|
1150
|
-
|
1151
|
-
##
|
1152
|
-
# Provides before/after hooks for setup and teardown. These are
|
1153
|
-
# meant for library writers, NOT for regular test authors. See
|
1154
|
-
# #before_setup for an example.
|
1155
|
-
|
1156
|
-
module LifecycleHooks
|
1157
|
-
##
|
1158
|
-
# Runs before every test, after setup. This hook is meant for
|
1159
|
-
# libraries to extend minitest. It is not meant to be used by
|
1160
|
-
# test developers.
|
1161
|
-
#
|
1162
|
-
# See #before_setup for an example.
|
1163
|
-
|
1164
|
-
def after_setup; end
|
1165
|
-
|
1166
|
-
##
|
1167
|
-
# Runs before every test, before setup. This hook is meant for
|
1168
|
-
# libraries to extend minitest. It is not meant to be used by
|
1169
|
-
# test developers.
|
1170
|
-
#
|
1171
|
-
# As a simplistic example:
|
1172
|
-
#
|
1173
|
-
# module MyMinitestPlugin
|
1174
|
-
# def before_setup
|
1175
|
-
# super
|
1176
|
-
# # ... stuff to do before setup is run
|
1177
|
-
# end
|
1178
|
-
#
|
1179
|
-
# def after_setup
|
1180
|
-
# # ... stuff to do after setup is run
|
1181
|
-
# super
|
1182
|
-
# end
|
1183
|
-
#
|
1184
|
-
# def before_teardown
|
1185
|
-
# super
|
1186
|
-
# # ... stuff to do before teardown is run
|
1187
|
-
# end
|
1188
|
-
#
|
1189
|
-
# def after_teardown
|
1190
|
-
# # ... stuff to do after teardown is run
|
1191
|
-
# super
|
1192
|
-
# end
|
1193
|
-
# end
|
1194
|
-
#
|
1195
|
-
# class MiniTest::Unit::TestCase
|
1196
|
-
# include MyMinitestPlugin
|
1197
|
-
# end
|
1198
|
-
|
1199
|
-
def before_setup; end
|
1200
|
-
|
1201
|
-
##
|
1202
|
-
# Runs after every test, before teardown. This hook is meant for
|
1203
|
-
# libraries to extend minitest. It is not meant to be used by
|
1204
|
-
# test developers.
|
1205
|
-
#
|
1206
|
-
# See #before_setup for an example.
|
1207
|
-
|
1208
|
-
def before_teardown; end
|
1209
|
-
|
1210
|
-
##
|
1211
|
-
# Runs after every test, after teardown. This hook is meant for
|
1212
|
-
# libraries to extend minitest. It is not meant to be used by
|
1213
|
-
# test developers.
|
1214
|
-
#
|
1215
|
-
# See #before_setup for an example.
|
1216
|
-
|
1217
|
-
def after_teardown; end
|
1218
|
-
end
|
1219
|
-
|
1220
|
-
##
|
1221
|
-
# Subclass TestCase to create your own tests. Typically you'll want a
|
1222
|
-
# TestCase subclass per implementation class.
|
1223
|
-
#
|
1224
|
-
# See MiniTest::Assertions
|
1225
|
-
|
1226
|
-
class TestCase
|
1227
|
-
include LifecycleHooks
|
1228
|
-
include Guard
|
1229
|
-
extend Guard
|
1230
|
-
|
1231
|
-
attr_reader :__name__ # :nodoc:
|
1232
|
-
|
1233
|
-
PASSTHROUGH_EXCEPTIONS = [NoMemoryError, SignalException,
|
1234
|
-
Interrupt, SystemExit] # :nodoc:
|
1235
|
-
|
1236
|
-
##
|
1237
|
-
# Runs the tests reporting the status to +runner+
|
1238
|
-
|
1239
|
-
def run runner
|
1240
|
-
trap "INFO" do
|
1241
|
-
runner.report.each_with_index do |msg, i|
|
1242
|
-
warn "\n%3d) %s" % [i + 1, msg]
|
1243
|
-
end
|
1244
|
-
warn ''
|
1245
|
-
time = runner.start_time ? Time.now - runner.start_time : 0
|
1246
|
-
warn "Current Test: %s#%s %.2fs" % [self.class, self.__name__, time]
|
1247
|
-
runner.status $stderr
|
1248
|
-
end if runner.info_signal
|
1249
|
-
|
1250
|
-
start_time = Time.now
|
1251
|
-
|
1252
|
-
result = ""
|
1253
|
-
begin
|
1254
|
-
@passed = nil
|
1255
|
-
self.before_setup
|
1256
|
-
self.setup
|
1257
|
-
self.after_setup
|
1258
|
-
self.run_test self.__name__
|
1259
|
-
result = "." unless io?
|
1260
|
-
time = Time.now - start_time
|
1261
|
-
runner.record self.class, self.__name__, self._assertions, time, nil
|
1262
|
-
@passed = true
|
1263
|
-
rescue *PASSTHROUGH_EXCEPTIONS
|
1264
|
-
raise
|
1265
|
-
rescue Exception => e
|
1266
|
-
@passed = Skip === e
|
1267
|
-
time = Time.now - start_time
|
1268
|
-
runner.record self.class, self.__name__, self._assertions, time, e
|
1269
|
-
result = runner.puke self.class, self.__name__, e
|
1270
|
-
ensure
|
1271
|
-
%w{ before_teardown teardown after_teardown }.each do |hook|
|
1272
|
-
begin
|
1273
|
-
self.send hook
|
1274
|
-
rescue *PASSTHROUGH_EXCEPTIONS
|
1275
|
-
raise
|
1276
|
-
rescue Exception => e
|
1277
|
-
@passed = false
|
1278
|
-
runner.record self.class, self.__name__, self._assertions, time, e
|
1279
|
-
result = runner.puke self.class, self.__name__, e
|
1280
|
-
end
|
1281
|
-
end
|
1282
|
-
trap 'INFO', 'DEFAULT' if runner.info_signal
|
1283
|
-
end
|
1284
|
-
result
|
1285
|
-
end
|
1286
|
-
|
1287
|
-
alias :run_test :__send__
|
1288
|
-
|
1289
|
-
def initialize name # :nodoc:
|
1290
|
-
@__name__ = name
|
1291
|
-
@__io__ = nil
|
1292
|
-
@passed = nil
|
1293
|
-
@@current = self # FIX: make thread local
|
1294
|
-
end
|
1295
|
-
|
1296
|
-
def self.current # :nodoc:
|
1297
|
-
@@current # FIX: make thread local
|
1298
|
-
end
|
1299
|
-
|
1300
|
-
##
|
1301
|
-
# Return the output IO object
|
1302
|
-
|
1303
|
-
def io
|
1304
|
-
@__io__ = true
|
1305
|
-
MiniTest::Unit.output
|
1306
|
-
end
|
1307
|
-
|
1308
|
-
##
|
1309
|
-
# Have we hooked up the IO yet?
|
1310
|
-
|
1311
|
-
def io?
|
1312
|
-
@__io__
|
1313
|
-
end
|
1314
|
-
|
1315
|
-
def self.reset # :nodoc:
|
1316
|
-
@@test_suites = {}
|
1317
|
-
end
|
1318
|
-
|
1319
|
-
reset
|
1320
|
-
|
1321
|
-
##
|
1322
|
-
# Call this at the top of your tests when you absolutely
|
1323
|
-
# positively need to have ordered tests. In doing so, you're
|
1324
|
-
# admitting that you suck and your tests are weak.
|
1325
|
-
|
1326
|
-
def self.i_suck_and_my_tests_are_order_dependent!
|
1327
|
-
class << self
|
1328
|
-
undef_method :test_order if method_defined? :test_order
|
1329
|
-
define_method :test_order do :alpha end
|
1330
|
-
end
|
1331
|
-
end
|
1332
|
-
|
1333
|
-
##
|
1334
|
-
# Make diffs for this TestCase use #pretty_inspect so that diff
|
1335
|
-
# in assert_equal can be more details. NOTE: this is much slower
|
1336
|
-
# than the regular inspect but much more usable for complex
|
1337
|
-
# objects.
|
1338
|
-
|
1339
|
-
def self.make_my_diffs_pretty!
|
1340
|
-
require 'pp'
|
1341
|
-
|
1342
|
-
define_method :mu_pp do |o|
|
1343
|
-
o.pretty_inspect
|
1344
|
-
end
|
1345
|
-
end
|
1346
|
-
|
1347
|
-
##
|
1348
|
-
# Call this at the top of your tests when you want to run your
|
1349
|
-
# tests in parallel. In doing so, you're admitting that you rule
|
1350
|
-
# and your tests are awesome.
|
1351
|
-
|
1352
|
-
def self.parallelize_me!
|
1353
|
-
require "minitest/parallel_each"
|
1354
|
-
|
1355
|
-
class << self
|
1356
|
-
undef_method :test_order if method_defined? :test_order
|
1357
|
-
define_method :test_order do :parallel end
|
1358
|
-
end
|
1359
|
-
end
|
1360
|
-
|
1361
|
-
def self.inherited klass # :nodoc:
|
1362
|
-
@@test_suites[klass] = true
|
1363
|
-
super
|
1364
|
-
end
|
1365
|
-
|
1366
|
-
def self.test_order # :nodoc:
|
1367
|
-
:random
|
1368
|
-
end
|
1369
|
-
|
1370
|
-
def self.test_suites # :nodoc:
|
1371
|
-
@@test_suites.keys.sort_by { |ts| ts.name.to_s }
|
1372
|
-
end
|
1373
|
-
|
1374
|
-
def self.test_methods # :nodoc:
|
1375
|
-
methods = public_instance_methods(true).grep(/^test/).map { |m| m.to_s }
|
1376
|
-
|
1377
|
-
case self.test_order
|
1378
|
-
when :parallel
|
1379
|
-
max = methods.size
|
1380
|
-
ParallelEach.new methods.sort.sort_by { rand max }
|
1381
|
-
when :random then
|
1382
|
-
max = methods.size
|
1383
|
-
methods.sort.sort_by { rand max }
|
1384
|
-
when :alpha, :sorted then
|
1385
|
-
methods.sort
|
1386
|
-
else
|
1387
|
-
raise "Unknown test_order: #{self.test_order.inspect}"
|
1388
|
-
end
|
1389
|
-
end
|
1390
|
-
|
1391
|
-
##
|
1392
|
-
# Returns true if the test passed.
|
1393
|
-
|
1394
|
-
def passed?
|
1395
|
-
@passed
|
1396
|
-
end
|
1397
|
-
|
1398
|
-
##
|
1399
|
-
# Runs before every test. Use this to set up before each test
|
1400
|
-
# run.
|
1401
|
-
|
1402
|
-
def setup; end
|
1403
|
-
|
1404
|
-
##
|
1405
|
-
# Runs after every test. Use this to clean up after each test
|
1406
|
-
# run.
|
1407
|
-
|
1408
|
-
def teardown; end
|
1409
|
-
|
1410
|
-
include MiniTest::Assertions
|
1411
|
-
end # class TestCase
|
1412
|
-
end # class Unit
|
1413
|
-
end # module MiniTest
|
1414
|
-
|
1415
|
-
Minitest = MiniTest # :nodoc: because ugh... I typo this all the time
|
41
|
+
# :startdoc:
|