minitest 4.7.5 → 5.0.7

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.
@@ -0,0 +1,287 @@
1
+ require "minitest" unless defined? Minitest::Runnable
2
+
3
+ module Minitest
4
+ ##
5
+ # Subclass Test to create your own tests. Typically you'll want a
6
+ # Test subclass per implementation class.
7
+ #
8
+ # See Minitest::Assertions
9
+
10
+ class Test < Runnable
11
+ require "minitest/assertions"
12
+ include Minitest::Assertions
13
+
14
+ PASSTHROUGH_EXCEPTIONS = [NoMemoryError, SignalException, # :nodoc:
15
+ Interrupt, SystemExit]
16
+
17
+ ##
18
+ # Call this at the top of your tests when you absolutely
19
+ # positively need to have ordered tests. In doing so, you're
20
+ # admitting that you suck and your tests are weak.
21
+
22
+ def self.i_suck_and_my_tests_are_order_dependent!
23
+ class << self
24
+ undef_method :test_order if method_defined? :test_order
25
+ define_method :test_order do :alpha end
26
+ end
27
+ end
28
+
29
+ ##
30
+ # Make diffs for this Test use #pretty_inspect so that diff
31
+ # in assert_equal can have more details. NOTE: this is much slower
32
+ # than the regular inspect but much more usable for complex
33
+ # objects.
34
+
35
+ def self.make_my_diffs_pretty!
36
+ require "pp"
37
+
38
+ define_method :mu_pp do |o|
39
+ o.pretty_inspect
40
+ end
41
+ end
42
+
43
+ ##
44
+ # Call this at the top of your tests when you want to run your
45
+ # tests in parallel. In doing so, you're admitting that you rule
46
+ # and your tests are awesome.
47
+
48
+ def self.parallelize_me!
49
+ require "minitest/parallel_each"
50
+
51
+ class << self
52
+ undef_method :test_order if method_defined? :test_order
53
+ define_method :test_order do :parallel end
54
+ end
55
+ end
56
+
57
+ ##
58
+ # Returns all instance methods starting with "test_". Based on
59
+ # #test_order, the methods are either sorted, randomized
60
+ # (default), or run in parallel.
61
+
62
+ def self.runnable_methods
63
+ methods = methods_matching(/^test_/)
64
+
65
+ case self.test_order
66
+ when :parallel
67
+ max = methods.size
68
+ ParallelEach.new methods.sort.sort_by { rand max }
69
+ when :random then
70
+ max = methods.size
71
+ methods.sort.sort_by { rand max }
72
+ when :alpha, :sorted then
73
+ methods.sort
74
+ else
75
+ raise "Unknown test_order: #{self.test_order.inspect}"
76
+ end
77
+ end
78
+
79
+ ##
80
+ # Defines the order to run tests (:random by default). Override
81
+ # this or use a convenience method to change it for your tests.
82
+
83
+ def self.test_order
84
+ :random
85
+ end
86
+
87
+ ##
88
+ # The time it took to run this test.
89
+
90
+ attr_accessor :time
91
+
92
+ def marshal_dump # :nodoc:
93
+ super << self.time
94
+ end
95
+
96
+ def marshal_load ary # :nodoc:
97
+ self.time = ary.pop
98
+ super
99
+ end
100
+
101
+ ##
102
+ # Runs a single test with setup/teardown hooks.
103
+
104
+ def run
105
+ with_info_handler do
106
+ time_it do
107
+ capture_exceptions do
108
+ before_setup; setup; after_setup
109
+
110
+ self.send self.name
111
+ end
112
+
113
+ %w{ before_teardown teardown after_teardown }.each do |hook|
114
+ capture_exceptions do
115
+ self.send hook
116
+ end
117
+ end
118
+ end
119
+ end
120
+
121
+ self # per contract
122
+ end
123
+
124
+ ##
125
+ # Provides before/after hooks for setup and teardown. These are
126
+ # meant for library writers, NOT for regular test authors. See
127
+ # #before_setup for an example.
128
+
129
+ module LifecycleHooks
130
+
131
+ ##
132
+ # Runs before every test, before setup. This hook is meant for
133
+ # libraries to extend minitest. It is not meant to be used by
134
+ # test developers.
135
+ #
136
+ # As a simplistic example:
137
+ #
138
+ # module MyMinitestPlugin
139
+ # def before_setup
140
+ # super
141
+ # # ... stuff to do before setup is run
142
+ # end
143
+ #
144
+ # def after_setup
145
+ # # ... stuff to do after setup is run
146
+ # super
147
+ # end
148
+ #
149
+ # def before_teardown
150
+ # super
151
+ # # ... stuff to do before teardown is run
152
+ # end
153
+ #
154
+ # def after_teardown
155
+ # # ... stuff to do after teardown is run
156
+ # super
157
+ # end
158
+ # end
159
+ #
160
+ # class MiniTest::Test
161
+ # include MyMinitestPlugin
162
+ # end
163
+
164
+ def before_setup; end
165
+
166
+ ##
167
+ # Runs before every test. Use this to set up before each test
168
+ # run.
169
+
170
+ def setup; end
171
+
172
+ ##
173
+ # Runs before every test, after setup. This hook is meant for
174
+ # libraries to extend minitest. It is not meant to be used by
175
+ # test developers.
176
+ #
177
+ # See #before_setup for an example.
178
+
179
+ def after_setup; end
180
+
181
+ ##
182
+ # Runs after every test, before teardown. This hook is meant for
183
+ # libraries to extend minitest. It is not meant to be used by
184
+ # test developers.
185
+ #
186
+ # See #before_setup for an example.
187
+
188
+ def before_teardown; end
189
+
190
+ ##
191
+ # Runs after every test. Use this to clean up after each test
192
+ # run.
193
+
194
+ def teardown; end
195
+
196
+ ##
197
+ # Runs after every test, after teardown. This hook is meant for
198
+ # libraries to extend minitest. It is not meant to be used by
199
+ # test developers.
200
+ #
201
+ # See #before_setup for an example.
202
+
203
+ def after_teardown; end
204
+ end # LifecycleHooks
205
+
206
+ def capture_exceptions # :nodoc:
207
+ begin
208
+ yield
209
+ rescue *PASSTHROUGH_EXCEPTIONS
210
+ raise
211
+ rescue Assertion => e
212
+ self.failures << e
213
+ rescue Exception => e
214
+ self.failures << UnexpectedError.new(e)
215
+ end
216
+ end
217
+
218
+ ##
219
+ # Did this run error?
220
+
221
+ def error?
222
+ self.failures.any? { |f| UnexpectedError === f }
223
+ end
224
+
225
+ ##
226
+ # The location identifier of this test.
227
+
228
+ def location
229
+ loc = " [#{self.failure.location}]" unless passed? or error?
230
+ "#{self.class}##{self.name}#{loc}"
231
+ end
232
+
233
+ ##
234
+ # Did this run pass?
235
+ #
236
+ # Note: skipped runs are not considered passing, but they don't
237
+ # cause the process to exit non-zero.
238
+
239
+ def passed?
240
+ not self.failure
241
+ end
242
+
243
+ ##
244
+ # Returns ".", "F", or "E" based on the result of the run.
245
+
246
+ def result_code
247
+ self.failure and self.failure.result_code or "."
248
+ end
249
+
250
+ ##
251
+ # Was this run skipped?
252
+
253
+ def skipped?
254
+ self.failure and Skip === self.failure
255
+ end
256
+
257
+ def time_it # :nodoc:
258
+ t0 = Time.now
259
+
260
+ yield
261
+ ensure
262
+ self.time = Time.now - t0
263
+ end
264
+
265
+ def to_s # :nodoc:
266
+ return location if passed? and not skipped?
267
+
268
+ failures.map { |failure|
269
+ "#{failure.result_label}:\n#{self.location}:\n#{failure.message}\n"
270
+ }.join "\n"
271
+ end
272
+
273
+ def with_info_handler &block # :nodoc:
274
+ t0 = Time.now
275
+
276
+ handler = lambda do
277
+ warn "\nCurrent: %s#%s %.2fs" % [self.class, self.name, Time.now - t0]
278
+ end
279
+
280
+ self.class.on_signal "INFO", handler, &block
281
+ end
282
+
283
+ include LifecycleHooks
284
+ include Guard
285
+ extend Guard
286
+ end # Test
287
+ end