minitest 1.5.0 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
data.tar.gz.sig CHANGED
Binary file
data/.autotest CHANGED
@@ -3,6 +3,8 @@
3
3
  require 'autotest/restart'
4
4
 
5
5
  Autotest.add_hook :initialize do |at|
6
+ at.testlib = 'minitest/unit'
7
+
6
8
  at.extra_class_map["MiniSpec"] = "test/test_mini_spec.rb"
7
9
  at.extra_class_map["TestMiniTestTestCase"] = "test/test_mini_test.rb"
8
10
 
data/History.txt CHANGED
@@ -1,3 +1,23 @@
1
+ === 1.6.0 / 2010-03-27
2
+
3
+ * 10 minor enhancements:
4
+
5
+ * Added --seed argument so you can reproduce a random order for debugging.
6
+ * Added documentation for assertions
7
+ * Added more rdoc and tons of :nodoc:
8
+ * Added output to give you all the options you need to reproduce that run.
9
+ * Added proper argument parsing to minitest.
10
+ * Added unique serial # to spec names so order can be preserved (needs tests). (phrogz)
11
+ * Empty 'it' fails with default msg. (phrogz)
12
+ * Remove previous method on expect to remove 1.9 warnings
13
+ * Spec#it is now order-proof wrt subclasses/nested describes.
14
+ * assert_same error message now reports in decimal, eg: oid=123. (mattkent)
15
+
16
+ * 2 bug fixes:
17
+
18
+ * Fixed message on refute_same to be consistent with assert_same.
19
+ * Fixed method randomization to be stable for testing.
20
+
1
21
  === 1.5.0 / 2010-01-06
2
22
 
3
23
  * 4 minor enhancements:
data/lib/minitest/mock.rb CHANGED
@@ -10,6 +10,7 @@ module MiniTest
10
10
  def expect(name, retval, args=[])
11
11
  n, r, a = name, retval, args # for the closure below
12
12
  @expected_calls[name] = { :retval => retval, :args => args }
13
+ self.class.__send__ :remove_method, name if respond_to? name
13
14
  self.class.__send__(:define_method, name) { |*x|
14
15
  raise ArgumentError unless @expected_calls[n][:args].size == x.size
15
16
  @actual_calls[n] << { :retval => r, :args => x }
data/lib/minitest/spec.rb CHANGED
@@ -58,6 +58,14 @@ class Object
58
58
  end
59
59
 
60
60
  module Kernel
61
+ ##
62
+ # Describe a series of expectations for a given target +desc+.
63
+ #
64
+ # TODO: find good tutorial url.
65
+ #
66
+ # Defines a test class subclassing from either
67
+ # MiniTest::Unit::TestCase or from the surrounding describe's class.
68
+
61
69
  def describe desc, &block
62
70
  stack = MiniTest::Spec.describe_stack
63
71
  name = desc.to_s.split(/\W+/).map { |s| s.capitalize }.join + "Spec"
@@ -74,28 +82,36 @@ module Kernel
74
82
  private :describe
75
83
  end
76
84
 
85
+ class Module
86
+ def classes type = Object # :nodoc:
87
+ constants.map { |n| const_get n }.find_all { |c|
88
+ c.class == Class and type > c
89
+ } - [self]
90
+ end
91
+ end
92
+
77
93
  class MiniTest::Spec < MiniTest::Unit::TestCase
78
94
  @@describe_stack = [MiniTest::Spec]
79
- def self.describe_stack
95
+ def self.describe_stack # :nodoc:
80
96
  @@describe_stack
81
97
  end
82
98
 
83
- def self.current
99
+ def self.current # :nodoc:
84
100
  @@current_spec
85
101
  end
86
102
 
87
- def initialize name
103
+ def initialize name # :nodoc:
88
104
  super
89
105
  @@current_spec = self
90
106
  end
91
107
 
92
- def self.nuke_test_methods!
108
+ def self.nuke_test_methods! # :nodoc:
93
109
  self.public_instance_methods.grep(/^test_/).each do |name|
94
110
  self.send :undef_method, name
95
111
  end
96
112
  end
97
113
 
98
- def self.define_inheritable_method name, &block
114
+ def self.define_inheritable_method name, &block # :nodoc:
99
115
  super_method = self.superclass.instance_method name
100
116
 
101
117
  define_method name do
@@ -104,25 +120,167 @@ class MiniTest::Spec < MiniTest::Unit::TestCase
104
120
  end
105
121
  end
106
122
 
107
- def self.before(type = :each, &block)
108
- if type == :all
109
- warn "change before :all to before :each"
110
- type = :each
111
- end
123
+ ##
124
+ # Define a 'before' action. Inherits the way normal methods should.
125
+ #
126
+ # NOTE: +type+ is ignored and is only there to make porting easier.
127
+ #
128
+ # Equivalent to MiniTest::Unit::TestCase#setup.
129
+
130
+ def self.before type = :each, &block
112
131
  raise "unsupported before type: #{type}" unless type == :each
113
132
  define_inheritable_method :setup, &block
114
133
  end
115
134
 
116
- def self.after(type = :each, &block)
117
- if type == :all # REFACTOR
118
- warn "change before :all to before :each"
119
- type = :each
120
- end
135
+ ##
136
+ # Define an 'after' action. Inherits the way normal methods should.
137
+ #
138
+ # NOTE: +type+ is ignored and is only there to make porting easier.
139
+ #
140
+ # Equivalent to MiniTest::Unit::TestCase#teardown.
141
+
142
+ def self.after type = :each, &block
121
143
  raise "unsupported after type: #{type}" unless type == :each
122
144
  define_inheritable_method :teardown, &block
123
145
  end
124
146
 
147
+ ##
148
+ # Define an expectation with name +desc+. Name gets morphed to a
149
+ # proper test method name. For some freakish reason, people who
150
+ # write specs don't like class inheritence, so this goes way out of
151
+ # its way to make sure that expectations aren't inherited.
152
+ #
153
+ # Hint: If you _do_ want inheritence, use minitest/unit. You can mix
154
+ # and match between assertions and expectations as much as you want.
155
+
125
156
  def self.it desc, &block
126
- define_method "test_#{desc.gsub(/\W+/, '_').downcase}", &block
157
+ block ||= proc { skip "(no tests defined)" }
158
+
159
+ @specs ||= 0
160
+ @specs += 1
161
+
162
+ name = "test_%04d_%s" % [ @specs, desc.gsub(/\W+/, '_').downcase ]
163
+
164
+ define_method name, &block
165
+
166
+ classes(MiniTest::Spec).each do |mod|
167
+ mod.send :undef_method, name if mod.respond_to? name
168
+ end
127
169
  end
170
+
171
+ ##
172
+ # :method: must_be
173
+ # See MiniTest::Assertions#assert
174
+
175
+ ##
176
+ # :method: must_be_close_to
177
+ # See MiniTest::Assertions#assert_in_delta
178
+
179
+ ##
180
+ # :method: must_be_empty
181
+ # See MiniTest::Assertions#assert_empty
182
+
183
+ ##
184
+ # :method: must_be_instance_of
185
+ # See MiniTest::Assertions#assert_instance_of
186
+
187
+ ##
188
+ # :method: must_be_kind_of
189
+ # See MiniTest::Assertions#assert_kind_of
190
+
191
+ ##
192
+ # :method: must_be_nil
193
+ # See MiniTest::Assertions#assert_nil
194
+
195
+ ##
196
+ # :method: must_be_same_as
197
+ # See MiniTest::Assertions#assert_same
198
+
199
+ ##
200
+ # :method: must_be_within_delta
201
+ # See MiniTest::Assertions#assert_in_delta
202
+
203
+ ##
204
+ # :method: must_be_within_epsilon
205
+ # See MiniTest::Assertions#assert_in_epsilon
206
+
207
+ ##
208
+ # :method: must_equal
209
+ # See MiniTest::Assertions#assert_equal
210
+
211
+ ##
212
+ # :method: must_include
213
+ # See MiniTest::Assertions#assert_includes
214
+
215
+ ##
216
+ # :method: must_match
217
+ # See MiniTest::Assertions#assert_match
218
+
219
+ ##
220
+ # :method: must_raise
221
+ # See MiniTest::Assertions#assert_raises
222
+
223
+ ##
224
+ # :method: must_respond_to
225
+ # See MiniTest::Assertions#assert_respond_to
226
+
227
+ ##
228
+ # :method: must_send
229
+ # See MiniTest::Assertions#assert_send
230
+
231
+ ##
232
+ # :method: must_throw
233
+ # See MiniTest::Assertions#assert_throw
234
+
235
+ ##
236
+ # :method: wont_be
237
+ # See MiniTest::Assertions#refute
238
+
239
+ ##
240
+ # :method: wont_be_close_to
241
+ # See MiniTest::Assertions#refute_in_delta
242
+
243
+ ##
244
+ # :method: wont_be_empty
245
+ # See MiniTest::Assertions#refute_empty
246
+
247
+ ##
248
+ # :method: wont_be_instance_of
249
+ # See MiniTest::Assertions#refute_instance_of
250
+
251
+ ##
252
+ # :method: wont_be_kind_of
253
+ # See MiniTest::Assertions#refute_kind_of
254
+
255
+ ##
256
+ # :method: wont_be_nil
257
+ # See MiniTest::Assertions#refute_nil
258
+
259
+ ##
260
+ # :method: wont_be_same_as
261
+ # See MiniTest::Assertions#refute_same
262
+
263
+ ##
264
+ # :method: wont_be_within_delta
265
+ # See MiniTest::Assertions#refute_in_delta
266
+
267
+ ##
268
+ # :method: wont_be_within_epsilon
269
+ # See MiniTest::Assertions#refute_in_epsilon
270
+
271
+ ##
272
+ # :method: wont_equal
273
+ # See MiniTest::Assertions#refute_equal
274
+
275
+ ##
276
+ # :method: wont_include
277
+ # See MiniTest::Assertions#refute_includes
278
+
279
+ ##
280
+ # :method: wont_match
281
+ # See MiniTest::Assertions#refute_match
282
+
283
+ ##
284
+ # :method: wont_respond_to
285
+ # See MiniTest::Assertions#refute_respond_to
128
286
  end
data/lib/minitest/unit.rb CHANGED
@@ -1,11 +1,18 @@
1
+ require 'optparse'
2
+
1
3
  ##
2
- #
3
- # Totally minimal drop-in replacement for test-unit
4
- #
5
- # TODO: refute -> debunk, prove/rebut, show/deny... lots of possibilities
4
+ # Minimal (mostly drop-in) replacement for test-unit.
6
5
 
7
6
  module MiniTest
7
+
8
+ ##
9
+ # Assertion base class
10
+
8
11
  class Assertion < Exception; end
12
+
13
+ ##
14
+ # Assertion raised when skipping a test
15
+
9
16
  class Skip < Assertion; end
10
17
 
11
18
  file = if RUBY_VERSION =~ /^1\.9/ then # bt's expanded, but __FILE__ isn't :(
@@ -21,9 +28,9 @@ module MiniTest
21
28
  end
22
29
 
23
30
  # './lib' in project dir, or '/usr/local/blahblah' if installed
24
- MINI_DIR = File.dirname(File.dirname(file))
31
+ MINI_DIR = File.dirname(File.dirname(file)) # :nodoc:
25
32
 
26
- def self.filter_backtrace bt
33
+ def self.filter_backtrace bt # :nodoc:
27
34
  return ["No backtrace"] unless bt
28
35
 
29
36
  new_bt = []
@@ -37,21 +44,33 @@ module MiniTest
37
44
  new_bt
38
45
  end
39
46
 
47
+ ##
48
+ # MiniTest Assertions. All assertion methods accept a +msg+ which is
49
+ # printed if the assertion fails.
50
+
40
51
  module Assertions
41
- def mu_pp(obj)
52
+
53
+ ##
54
+ # mu_pp gives a human-readable version of +obj+. By default #inspect is
55
+ # called. You can override this to use #pretty_print if you want.
56
+
57
+ def mu_pp obj
42
58
  s = obj.inspect
43
59
  s = s.force_encoding(Encoding.default_external) if defined? Encoding
44
60
  s
45
61
  end
46
62
 
47
- def _assertions= n
63
+ def _assertions= n # :nodoc:
48
64
  @_assertions = n
49
65
  end
50
66
 
51
- def _assertions
67
+ def _assertions # :nodoc:
52
68
  @_assertions ||= 0
53
69
  end
54
70
 
71
+ ##
72
+ # Fails unless +test+ is a true value.
73
+
55
74
  def assert test, msg = nil
56
75
  msg ||= "Failed assertion, no message given."
57
76
  self._assertions += 1
@@ -62,32 +81,56 @@ module MiniTest
62
81
  true
63
82
  end
64
83
 
84
+ ##
85
+ # Fails unless the block returns a true value.
86
+
65
87
  def assert_block msg = nil
66
88
  msg = message(msg) { "Expected block to return true value" }
67
89
  assert yield, msg
68
90
  end
69
91
 
92
+ ##
93
+ # Fails unless +obj+ is empty.
94
+
70
95
  def assert_empty obj, msg = nil
71
96
  msg = message(msg) { "Expected #{obj.inspect} to be empty" }
72
97
  assert_respond_to obj, :empty?
73
98
  assert obj.empty?, msg
74
99
  end
75
100
 
101
+ ##
102
+ # Fails unless <tt>exp == act</tt>.
103
+ #
104
+ # For floats use assert_in_delta
105
+
76
106
  def assert_equal exp, act, msg = nil
77
107
  msg = message(msg) { "Expected #{mu_pp(exp)}, not #{mu_pp(act)}" }
78
108
  assert(exp == act, msg)
79
109
  end
80
110
 
111
+ ##
112
+ # For comparing Floats. Fails unless +exp+ and +act+ are within +delta+
113
+ # of each other.
114
+ #
115
+ # assert_in_delta Math::PI, (22.0 / 7.0), 0.01
116
+
81
117
  def assert_in_delta exp, act, delta = 0.001, msg = nil
82
118
  n = (exp - act).abs
83
119
  msg = message(msg) { "Expected #{exp} - #{act} (#{n}) to be < #{delta}" }
84
120
  assert delta >= n, msg
85
121
  end
86
122
 
123
+ ##
124
+ # For comparing Floats. Fails unless +exp+ and +act+ have a relative
125
+ # error less than +epsilon+.
126
+
87
127
  def assert_in_epsilon a, b, epsilon = 0.001, msg = nil
88
128
  assert_in_delta a, b, [a, b].min * epsilon, msg
89
129
  end
90
130
 
131
+ ##
132
+ # Fails unless +collection+ includes +obj+.
133
+
91
134
  def assert_includes collection, obj, msg = nil
92
135
  msg = message(msg) {
93
136
  "Expected #{mu_pp(collection)} to include #{mu_pp(obj)}"
@@ -96,6 +139,9 @@ module MiniTest
96
139
  assert collection.include?(obj), msg
97
140
  end
98
141
 
142
+ ##
143
+ # Fails unless +obj+ is an instace of +cls+.
144
+
99
145
  def assert_instance_of cls, obj, msg = nil
100
146
  msg = message(msg) {
101
147
  "Expected #{mu_pp(obj)} to be an instance of #{cls}, not #{obj.class}"
@@ -104,6 +150,9 @@ module MiniTest
104
150
  assert obj.instance_of?(cls), msg
105
151
  end
106
152
 
153
+ ##
154
+ # Fails unless +obj+ is a kind of +cls+.
155
+
107
156
  def assert_kind_of cls, obj, msg = nil # TODO: merge with instance_of
108
157
  msg = message(msg) {
109
158
  "Expected #{mu_pp(obj)} to be a kind of #{cls}, not #{obj.class}" }
@@ -111,23 +160,37 @@ module MiniTest
111
160
  assert obj.kind_of?(cls), msg
112
161
  end
113
162
 
163
+ ##
164
+ # Fails unless +exp+ is <tt>=~</tt> +act+.
165
+
114
166
  def assert_match exp, act, msg = nil
115
167
  msg = message(msg) { "Expected #{mu_pp(exp)} to match #{mu_pp(act)}" }
116
168
  assert_respond_to act, :"=~"
117
- exp = /#{Regexp.escape(exp)}/ if String === exp && String === act
169
+ exp = /#{Regexp.escape exp}/ if String === exp && String === act
118
170
  assert exp =~ act, msg
119
171
  end
120
172
 
173
+ ##
174
+ # Fails unless +obj+ is nil
175
+
121
176
  def assert_nil obj, msg = nil
122
177
  msg = message(msg) { "Expected #{mu_pp(obj)} to be nil" }
123
178
  assert obj.nil?, msg
124
179
  end
125
180
 
181
+ ##
182
+ # For testing equality operators and so-forth.
183
+ #
184
+ # assert_operator 5, :<=, 4
185
+
126
186
  def assert_operator o1, op, o2, msg = nil
127
187
  msg = message(msg) { "Expected #{mu_pp(o1)} to be #{op} #{mu_pp(o2)}" }
128
188
  assert o1.__send__(op, o2), msg
129
189
  end
130
190
 
191
+ ##
192
+ # Fails unless the block raises one of +exp+
193
+
131
194
  def assert_raises *exp
132
195
  msg = String === exp.last ? exp.pop : nil
133
196
  msg = msg.to_s + "\n" if msg
@@ -149,6 +212,9 @@ module MiniTest
149
212
  should_raise
150
213
  end
151
214
 
215
+ ##
216
+ # Fails unless +obj+ responds to +meth+.
217
+
152
218
  def assert_respond_to obj, meth, msg = nil
153
219
  msg = message(msg) {
154
220
  "Expected #{mu_pp(obj)} (#{obj.class}) to respond to ##{meth}"
@@ -156,14 +222,22 @@ module MiniTest
156
222
  assert obj.respond_to?(meth), msg
157
223
  end
158
224
 
225
+ ##
226
+ # Fails unless +exp+ and +act+ are #equal?
227
+
159
228
  def assert_same exp, act, msg = nil
160
229
  msg = message(msg) {
161
230
  data = [mu_pp(act), act.object_id, mu_pp(exp), exp.object_id]
162
- "Expected %s (0x%x) to be the same as %s (0x%x)" % data
231
+ "Expected %s (oid=%d) to be the same as %s (oid=%d)" % data
163
232
  }
164
233
  assert exp.equal?(act), msg
165
234
  end
166
235
 
236
+ ##
237
+ # +send_ary+ is a receiver, message and arguments.
238
+ #
239
+ # Fails unless the call returns a true value
240
+
167
241
  def assert_send send_ary, m = nil
168
242
  recv, msg, *args = send_ary
169
243
  m = message(m) {
@@ -171,6 +245,9 @@ module MiniTest
171
245
  assert recv.__send__(msg, *args), m
172
246
  end
173
247
 
248
+ ##
249
+ # Fails unless the block throws +sym+
250
+
174
251
  def assert_throws sym, msg = nil
175
252
  default = "Expected #{mu_pp(sym)} to have been thrown"
176
253
  caught = true
@@ -188,6 +265,15 @@ module MiniTest
188
265
  assert caught, message(msg) { default }
189
266
  end
190
267
 
268
+ ##
269
+ # Captures $stdout and $stderr into strings:
270
+ #
271
+ # out, err = capture_io do
272
+ # warn "You did a bad thing"
273
+ # end
274
+ #
275
+ # assert_match %r%bad%, err
276
+
191
277
  def capture_io
192
278
  require 'stringio'
193
279
 
@@ -203,15 +289,24 @@ module MiniTest
203
289
  $stderr = orig_stderr
204
290
  end
205
291
 
292
+ ##
293
+ # Returns details for exception +e+
294
+
206
295
  def exception_details e, msg
207
296
  "#{msg}\nClass: <#{e.class}>\nMessage: <#{e.message.inspect}>\n---Backtrace---\n#{MiniTest::filter_backtrace(e.backtrace).join("\n")}\n---------------"
208
297
  end
209
298
 
299
+ ##
300
+ # Fails with +msg+
301
+
210
302
  def flunk msg = nil
211
303
  msg ||= "Epic Fail!"
212
304
  assert false, msg
213
305
  end
214
306
 
307
+ ##
308
+ # Returns a proc that will output +msg+ along with the default message.
309
+
215
310
  def message msg = nil, &default
216
311
  proc {
217
312
  if msg then
@@ -225,22 +320,35 @@ module MiniTest
225
320
  }
226
321
  end
227
322
 
323
+ ##
228
324
  # used for counting assertions
325
+
229
326
  def pass msg = nil
230
327
  assert true
231
328
  end
232
329
 
330
+ ##
331
+ # Fails if +test+ is a true value
332
+
233
333
  def refute test, msg = nil
234
334
  msg ||= "Failed refutation, no message given"
235
335
  not assert(! test, msg)
236
336
  end
237
337
 
338
+ ##
339
+ # Fails if +obj+ is empty.
340
+
238
341
  def refute_empty obj, msg = nil
239
342
  msg = message(msg) { "Expected #{obj.inspect} to not be empty" }
240
343
  assert_respond_to obj, :empty?
241
344
  refute obj.empty?, msg
242
345
  end
243
346
 
347
+ ##
348
+ # Fails if <tt>exp == act</tt>.
349
+ #
350
+ # For floats use refute_in_delta.
351
+
244
352
  def refute_equal exp, act, msg = nil
245
353
  msg = message(msg) {
246
354
  "Expected #{mu_pp(act)} to not be equal to #{mu_pp(exp)}"
@@ -248,6 +356,11 @@ module MiniTest
248
356
  refute exp == act, msg
249
357
  end
250
358
 
359
+ ##
360
+ # For comparing Floats. Fails if +exp+ is within +delta+ of +act+
361
+ #
362
+ # refute_in_delta Math::PI, (22.0 / 7.0)
363
+
251
364
  def refute_in_delta exp, act, delta = 0.001, msg = nil
252
365
  n = (exp - act).abs
253
366
  msg = message(msg) {
@@ -256,10 +369,17 @@ module MiniTest
256
369
  refute delta > n, msg
257
370
  end
258
371
 
372
+ ##
373
+ # For comparing Floats. Fails if +exp+ and +act+ have a relative error
374
+ # less than +epsilon+.
375
+
259
376
  def refute_in_epsilon a, b, epsilon = 0.001, msg = nil
260
377
  refute_in_delta a, b, a * epsilon, msg
261
378
  end
262
379
 
380
+ ##
381
+ # Fails if +collection+ includes +obj+
382
+
263
383
  def refute_includes collection, obj, msg = nil
264
384
  msg = message(msg) {
265
385
  "Expected #{mu_pp(collection)} to not include #{mu_pp(obj)}"
@@ -268,6 +388,9 @@ module MiniTest
268
388
  refute collection.include?(obj), msg
269
389
  end
270
390
 
391
+ ##
392
+ # Fails if +obj+ is an instance of +cls+
393
+
271
394
  def refute_instance_of cls, obj, msg = nil
272
395
  msg = message(msg) {
273
396
  "Expected #{mu_pp(obj)} to not be an instance of #{cls}"
@@ -275,23 +398,38 @@ module MiniTest
275
398
  refute obj.instance_of?(cls), msg
276
399
  end
277
400
 
401
+ ##
402
+ # Fails if +obj+ is a kind of +cls+
403
+
278
404
  def refute_kind_of cls, obj, msg = nil # TODO: merge with instance_of
279
405
  msg = message(msg) { "Expected #{mu_pp(obj)} to not be a kind of #{cls}" }
280
406
  refute obj.kind_of?(cls), msg
281
407
  end
282
408
 
409
+ ##
410
+ # Fails if +exp+ <tt>=~</tt> +act+
411
+
283
412
  def refute_match exp, act, msg = nil
284
413
  msg = message(msg) { "Expected #{mu_pp(exp)} to not match #{mu_pp(act)}" }
285
414
  assert_respond_to act, :"=~"
286
- exp = /#{Regexp.escape(exp)}/ if String === exp && String === act
415
+ exp = (/#{Regexp.escape exp}/) if String === exp and String === act
287
416
  refute exp =~ act, msg
288
417
  end
289
418
 
419
+ ##
420
+ # Fails if +obj+ is nil.
421
+
290
422
  def refute_nil obj, msg = nil
291
423
  msg = message(msg) { "Expected #{mu_pp(obj)} to not be nil" }
292
424
  refute obj.nil?, msg
293
425
  end
294
426
 
427
+ ##
428
+ # Fails if +o1+ is not +op+ +o2+ nil. eg:
429
+ #
430
+ # refute_operator 1, :>, 2 #=> pass
431
+ # refute_operator 1, :<, 2 #=> fail
432
+
295
433
  def refute_operator o1, op, o2, msg = nil
296
434
  msg = message(msg) {
297
435
  "Expected #{mu_pp(o1)} to not be #{op} #{mu_pp(o2)}"
@@ -299,19 +437,30 @@ module MiniTest
299
437
  refute o1.__send__(op, o2), msg
300
438
  end
301
439
 
440
+ ##
441
+ # Fails if +obj+ responds to the message +meth+.
442
+
302
443
  def refute_respond_to obj, meth, msg = nil
303
444
  msg = message(msg) { "Expected #{mu_pp(obj)} to not respond to #{meth}" }
304
445
 
305
446
  refute obj.respond_to?(meth), msg
306
447
  end
307
448
 
449
+ ##
450
+ # Fails if +exp+ is the same (by object identity) as +act+.
451
+
308
452
  def refute_same exp, act, msg = nil
309
453
  msg = message(msg) {
310
- "Expected #{mu_pp(act)} to not be the same as #{mu_pp(exp)}"
454
+ data = [mu_pp(act), act.object_id, mu_pp(exp), exp.object_id]
455
+ "Expected %s (oid=%d) to not be the same as %s (oid=%d)" % data
311
456
  }
312
457
  refute exp.equal?(act), msg
313
458
  end
314
459
 
460
+ ##
461
+ # Skips the current test. Gets listed at the end of the run but
462
+ # doesn't cause a failure exit code.
463
+
315
464
  def skip msg = nil, bt = caller
316
465
  msg ||= "Skipped, no message given"
317
466
  raise MiniTest::Skip, msg, bt
@@ -319,15 +468,18 @@ module MiniTest
319
468
  end
320
469
 
321
470
  class Unit
322
- VERSION = "1.5.0"
471
+ VERSION = "1.6.0" # :nodoc:
323
472
 
324
- attr_accessor :report, :failures, :errors, :skips
325
- attr_accessor :test_count, :assertion_count
326
- attr_accessor :start_time
473
+ attr_accessor :report, :failures, :errors, :skips # :nodoc:
474
+ attr_accessor :test_count, :assertion_count # :nodoc:
475
+ attr_accessor :start_time # :nodoc:
327
476
 
328
477
  @@installed_at_exit ||= false
329
478
  @@out = $stdout
330
479
 
480
+ ##
481
+ # Registers MiniTest::Unit to run tests at process exit
482
+
331
483
  def self.autorun
332
484
  at_exit {
333
485
  next if $! # don't run if there was an exception
@@ -337,11 +489,15 @@ module MiniTest
337
489
  @@installed_at_exit = true
338
490
  end
339
491
 
492
+ ##
493
+ # Sets MiniTest::Unit to write output to +stream+. $stdout is the default
494
+ # output
495
+
340
496
  def self.output= stream
341
497
  @@out = stream
342
498
  end
343
499
 
344
- def location e
500
+ def location e # :nodoc:
345
501
  last_before_assertion = ""
346
502
  e.backtrace.reverse_each do |s|
347
503
  break if s =~ /in .(assert|refute|flunk|pass|fail|raise|must|wont)/
@@ -350,6 +506,10 @@ module MiniTest
350
506
  last_before_assertion.sub(/:in .*$/, '')
351
507
  end
352
508
 
509
+ ##
510
+ # Writes status for failed test +meth+ in +klass+ which finished with
511
+ # exception +e+
512
+
353
513
  def puke klass, meth, e
354
514
  e = case e
355
515
  when MiniTest::Skip then
@@ -367,25 +527,60 @@ module MiniTest
367
527
  e[0, 1]
368
528
  end
369
529
 
370
- def initialize
530
+ def initialize # :nodoc:
371
531
  @report = []
372
532
  @errors = @failures = @skips = 0
373
533
  @verbose = false
374
534
  end
375
535
 
536
+ def process_args args = []
537
+ options = {}
538
+
539
+ OptionParser.new do |opts|
540
+ opts.banner = 'minitest options:'
541
+ opts.version = MiniTest::Unit::VERSION
542
+
543
+ opts.on '-h', '--help', 'Display this help.' do
544
+ puts opts
545
+ exit
546
+ end
547
+
548
+ opts.on '-s', '--seed SEED', Integer, "Sets random seed" do |m|
549
+ options[:seed] = m.to_i
550
+ end
551
+
552
+ opts.on '-v', '--verbose', "Verbose. Show progress processing files." do
553
+ options[:verbose] = true
554
+ end
555
+
556
+ opts.on '-n', '--name PATTERN', "Filter test names on pattern." do |a|
557
+ options[:filter] = a
558
+ end
559
+
560
+ opts.parse args
561
+ end
562
+
563
+ options
564
+ end
565
+
376
566
  ##
377
567
  # Top level driver, controls all output and filtering.
378
568
 
379
569
  def run args = []
380
- @verbose = args.delete('-v')
570
+ options = process_args args
571
+
572
+ @verbose = options[:verbose]
573
+
574
+ filter = options[:filter] || '/./'
575
+ filter = Regexp.new $1 if filter and filter =~ /\/(.*)\//
576
+
577
+ seed = options[:seed]
578
+ unless seed then
579
+ srand
580
+ seed = srand % 0xFFFF
581
+ end
381
582
 
382
- filter = if args.first =~ /^(-n|--name)$/ then
383
- args.shift
384
- arg = args.shift
385
- arg =~ /\/(.*)\// ? Regexp.new($1) : arg
386
- else
387
- /./ # anything - ^test_ already filtered by #tests
388
- end
583
+ srand seed
389
584
 
390
585
  @@out.puts "Loaded suite #{$0.sub(/\.rb$/, '')}\nStarted"
391
586
 
@@ -403,16 +598,30 @@ module MiniTest
403
598
 
404
599
  status
405
600
 
601
+ @@out.puts
602
+
603
+ help = ["--seed", seed]
604
+ help.push "--verbose" if @verbose
605
+ help.push("--name", options[:filter].inspect) if options[:filter]
606
+
607
+ @@out.puts "Test run options: #{help.join(" ")}"
608
+
406
609
  return failures + errors if @test_count > 0 # or return nil...
407
610
  rescue Interrupt
408
611
  abort 'Interrupted'
409
612
  end
410
613
 
614
+ ##
615
+ # Writes status to +io+
616
+
411
617
  def status io = @@out
412
618
  format = "%d tests, %d assertions, %d failures, %d errors, %d skips"
413
619
  io.puts format % [test_count, assertion_count, failures, errors, skips]
414
620
  end
415
621
 
622
+ ##
623
+ # Runs test suites matching +filter+
624
+
416
625
  def run_test_suites filter = /./
417
626
  @test_count, @assertion_count = 0, 0
418
627
  old_sync, @@out.sync = @@out.sync, true if @@out.respond_to? :sync=
@@ -436,13 +645,20 @@ module MiniTest
436
645
  [@test_count, @assertion_count]
437
646
  end
438
647
 
648
+ ##
649
+ # Subclass TestCase to create your own tests. Typically you'll want a
650
+ # TestCase subclass per implementation class.
651
+
439
652
  class TestCase
440
- attr_reader :__name__
653
+ attr_reader :__name__ # :nodoc:
441
654
 
442
- PASSTHROUGH_EXCEPTIONS = [NoMemoryError, SignalException, Interrupt,
443
- SystemExit]
655
+ PASSTHROUGH_EXCEPTIONS = [NoMemoryError, SignalException,
656
+ Interrupt, SystemExit] # :nodoc:
444
657
 
445
- SUPPORTS_INFO_SIGNAL = Signal.list['INFO']
658
+ SUPPORTS_INFO_SIGNAL = Signal.list['INFO'] # :nodoc:
659
+
660
+ ##
661
+ # Runs the tests reporting the status to +runner+
446
662
 
447
663
  def run runner
448
664
  trap 'INFO' do
@@ -475,61 +691,79 @@ module MiniTest
475
691
  result
476
692
  end
477
693
 
478
- def initialize name
694
+ def initialize name # :nodoc:
479
695
  @__name__ = name
480
696
  @passed = nil
481
697
  end
482
698
 
483
- def self.reset
699
+ def self.reset # :nodoc:
484
700
  @@test_suites = {}
485
701
  end
486
702
 
487
703
  reset
488
704
 
489
- def self.inherited klass
705
+ def self.inherited klass # :nodoc:
490
706
  @@test_suites[klass] = true
491
707
  end
492
708
 
709
+ ##
710
+ # Defines test order and is subclassable. Defaults to :random
711
+ # but can be overridden to return :alpha if your tests are order
712
+ # dependent (read: weak).
713
+
493
714
  def self.test_order
494
715
  :random
495
716
  end
496
717
 
497
- def self.test_suites
718
+ def self.test_suites # :nodoc:
498
719
  @@test_suites.keys.sort_by { |ts| ts.name }
499
720
  end
500
721
 
501
- def self.test_methods
502
- methods = public_instance_methods(true).grep(/^test/).map { |m|
503
- m.to_s
504
- }.sort
722
+ def self.test_methods # :nodoc:
723
+ methods = public_instance_methods(true).grep(/^test/).map { |m| m.to_s }
505
724
 
506
- if self.test_order == :random then
725
+ case self.test_order
726
+ when :random then
507
727
  max = methods.size
508
- methods = methods.sort_by { rand(max) }
728
+ methods.sort.sort_by { rand(max) }
729
+ when :alpha, :sorted then
730
+ methods.sort
731
+ else
732
+ raise "Unknown test_order: #{self.test_order.inspect}"
509
733
  end
510
-
511
- methods
512
734
  end
513
735
 
514
- def setup; end
515
- def teardown; end
736
+ ##
737
+ # Returns true if the test passed.
516
738
 
517
739
  def passed?
518
740
  @passed
519
741
  end
520
742
 
743
+ ##
744
+ # Runs before every test. Use this to refactor test initialization.
745
+
746
+ def setup; end
747
+
748
+ ##
749
+ # Runs after every test. Use this to refactor test cleanup.
750
+
751
+ def teardown; end
752
+
521
753
  include MiniTest::Assertions
522
754
  end # class TestCase
523
755
  end # class Unit
524
756
  end # module MiniTest
525
757
 
526
758
  if $DEBUG then
527
- # this helps me ferret out porting issues
528
- module Test; end
529
- module Test::Unit; end
530
- class Test::Unit::TestCase
531
- def self.inherited x
532
- raise "You're running minitest and test/unit in the same process: #{x}"
759
+ module Test # :nodoc:
760
+ module Unit # :nodoc:
761
+ class TestCase # :nodoc:
762
+ def self.inherited x # :nodoc:
763
+ # this helps me ferret out porting issues
764
+ raise "Using minitest and test/unit in the same process: #{x}"
765
+ end
766
+ end
533
767
  end
534
768
  end
535
769
  end
@@ -11,6 +11,9 @@ describe MiniTest::Spec do
11
11
  self._assertions.must_equal @assertion_count
12
12
  end
13
13
 
14
+ # TODO: figure out how the hell to write a test for this
15
+ # it "will skip if there is no block"
16
+
14
17
  it "needs to have all methods named well" do
15
18
  @assertion_count = 2
16
19
 
@@ -201,7 +201,7 @@ class TestMiniTest < MiniTest::Unit::TestCase
201
201
 
202
202
  Object.const_set(:ATestCase, tc)
203
203
 
204
- @tu.run
204
+ @tu.run %w[-s 42]
205
205
 
206
206
  expected = "Loaded suite blah
207
207
  Started
@@ -213,6 +213,8 @@ test_failure(ATestCase) [FILE:LINE]:
213
213
  Failed assertion, no message given.
214
214
 
215
215
  2 tests, 2 assertions, 1 failures, 0 errors, 0 skips
216
+
217
+ Test run options: --seed 42
216
218
  "
217
219
  util_assert_report expected
218
220
  end
@@ -230,7 +232,7 @@ Failed assertion, no message given.
230
232
 
231
233
  Object.const_set(:ATestCase, tc)
232
234
 
233
- @tu.run
235
+ @tu.run %w[-s 42]
234
236
 
235
237
  expected = "Loaded suite blah
236
238
  Started
@@ -243,6 +245,8 @@ RuntimeError: unhandled exception
243
245
  FILE:LINE:in `test_error'
244
246
 
245
247
  2 tests, 1 assertions, 0 failures, 1 errors, 0 skips
248
+
249
+ Test run options: --seed 42
246
250
  "
247
251
  util_assert_report expected
248
252
  end
@@ -260,7 +264,7 @@ RuntimeError: unhandled exception
260
264
 
261
265
  Object.const_set(:ATestCase, tc)
262
266
 
263
- @tu.run
267
+ @tu.run %w[-s 42]
264
268
 
265
269
  expected = "Loaded suite blah
266
270
  Started
@@ -273,6 +277,8 @@ RuntimeError: unhandled exception
273
277
  FILE:LINE:in `teardown'
274
278
 
275
279
  1 tests, 1 assertions, 0 failures, 1 errors, 0 skips
280
+
281
+ Test run options: --seed 42
276
282
  "
277
283
  util_assert_report expected
278
284
  end
@@ -290,7 +296,7 @@ RuntimeError: unhandled exception
290
296
 
291
297
  Object.const_set(:ATestCase, tc)
292
298
 
293
- @tu.run
299
+ @tu.run %w[-s 42]
294
300
 
295
301
  expected = "Loaded suite blah
296
302
  Started
@@ -302,6 +308,8 @@ test_skip(ATestCase) [FILE:LINE]:
302
308
  not yet
303
309
 
304
310
  2 tests, 1 assertions, 0 failures, 0 errors, 1 skips
311
+
312
+ Test run options: --seed 42
305
313
  "
306
314
  util_assert_report expected
307
315
  end
@@ -313,6 +321,8 @@ Started
313
321
  Finished in 0.00
314
322
 
315
323
  1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
324
+
325
+ Test run options: --seed 42
316
326
  "
317
327
  output = @output.string.sub(/Finished in .*/, "Finished in 0.00")
318
328
  output.sub!(/Loaded suite .*/, 'Loaded suite blah')
@@ -334,9 +344,18 @@ Finished in 0.00
334
344
 
335
345
  Object.const_set(:ATestCase, tc)
336
346
 
337
- @tu.run(%w(-n /something/))
347
+ @tu.run %w[-n /something/ -s 42]
338
348
 
339
- util_assert_report
349
+ expected = "Loaded suite blah
350
+ Started
351
+ .
352
+ Finished in 0.00
353
+
354
+ 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
355
+
356
+ Test run options: --seed 42 --name \"/something/\"
357
+ "
358
+ util_assert_report expected
340
359
  end
341
360
 
342
361
  def test_run_passing
@@ -348,7 +367,7 @@ Finished in 0.00
348
367
 
349
368
  Object.const_set(:ATestCase, tc)
350
369
 
351
- @tu.run
370
+ @tu.run %w[-s 42]
352
371
 
353
372
  util_assert_report
354
373
  end
@@ -697,14 +716,14 @@ FILE:LINE:in `test_assert_raises_triggered_subclass'
697
716
  def test_assert_same_triggered
698
717
  @assertion_count = 2
699
718
 
700
- util_assert_triggered 'Expected 2 (0xXXX) to be the same as 1 (0xXXX).' do
719
+ util_assert_triggered 'Expected 2 (oid=N) to be the same as 1 (oid=N).' do
701
720
  @tc.assert_same 1, 2
702
721
  end
703
722
 
704
723
  s1 = "blah"
705
724
  s2 = "blah"
706
725
 
707
- util_assert_triggered 'Expected "blah" (0xXXX) to be the same as "blah" (0xXXX).' do
726
+ util_assert_triggered 'Expected "blah" (oid=N) to be the same as "blah" (oid=N).' do
708
727
  @tc.assert_same s1, s2
709
728
  end
710
729
  end
@@ -780,16 +799,11 @@ FILE:LINE:in `test_assert_raises_triggered_subclass'
780
799
  def test_test_methods_sorted
781
800
  @assertion_count = 0
782
801
 
783
- sample_test_case = Class.new(MiniTest::Unit::TestCase)
784
-
785
- class << sample_test_case
786
- def test_order; :sorted end
787
- end
788
-
789
- sample_test_case.instance_eval do
790
- define_method :test_test3 do assert "does not matter" end
791
- define_method :test_test2 do assert "does not matter" end
792
- define_method :test_test1 do assert "does not matter" end
802
+ sample_test_case = Class.new(MiniTest::Unit::TestCase) do
803
+ def self.test_order; :sorted end
804
+ def test_test3; assert "does not matter" end
805
+ def test_test2; assert "does not matter" end
806
+ def test_test1; assert "does not matter" end
793
807
  end
794
808
 
795
809
  expected = %w(test_test1 test_test2 test_test3)
@@ -799,27 +813,15 @@ FILE:LINE:in `test_assert_raises_triggered_subclass'
799
813
  def test_test_methods_random
800
814
  @assertion_count = 0
801
815
 
802
- sample_test_case = Class.new(MiniTest::Unit::TestCase)
803
-
804
- class << sample_test_case
805
- def test_order; :random end
816
+ sample_test_case = Class.new(MiniTest::Unit::TestCase) do
817
+ def test_test1; assert "does not matter" end
818
+ def test_test2; assert "does not matter" end
819
+ def test_test3; assert "does not matter" end
806
820
  end
807
821
 
808
- sample_test_case.instance_eval do
809
- define_method :test_test1 do assert "does not matter" end
810
- define_method :test_test2 do assert "does not matter" end
811
- define_method :test_test3 do assert "does not matter" end
812
- end
813
-
814
- srand 42
815
- expected = %w(test_test1 test_test2 test_test3)
816
- max = expected.size
817
- expected = expected.sort_by { rand(max) }
818
-
819
822
  srand 42
820
- result = sample_test_case.test_methods
821
-
822
- assert_equal expected, result
823
+ expected = %w(test_test2 test_test1 test_test3)
824
+ assert_equal expected, sample_test_case.test_methods
823
825
  end
824
826
 
825
827
  def test_refute
@@ -985,9 +987,8 @@ FILE:LINE:in `test_assert_raises_triggered_subclass'
985
987
  @tc.refute_same 1, 2
986
988
  end
987
989
 
988
- # TODO: "with id <id>" crap from assertions.rb
989
990
  def test_refute_same_triggered
990
- util_assert_triggered 'Expected 1 to not be the same as 1.' do
991
+ util_assert_triggered 'Expected 1 (oid=N) to not be the same as 1 (oid=N).' do
991
992
  @tc.refute_same 1, 1
992
993
  end
993
994
  end
@@ -1006,7 +1007,7 @@ FILE:LINE:in `test_assert_raises_triggered_subclass'
1006
1007
  end
1007
1008
 
1008
1009
  msg = e.message.sub(/(---Backtrace---).*/m, '\1')
1009
- msg.gsub!(/\(0x[0-9a-f]+\)/, '(0xXXX)')
1010
+ msg.gsub!(/\(oid=[-0-9]+\)/, '(oid=N)')
1010
1011
 
1011
1012
  assert_equal expected, msg
1012
1013
  end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minitest
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 6
8
+ - 0
9
+ version: 1.6.0
5
10
  platform: ruby
6
11
  authors:
7
12
  - Ryan Davis
@@ -30,49 +35,51 @@ cert_chain:
30
35
  FBHgymkyj/AOSqKRIpXPhjC6
31
36
  -----END CERTIFICATE-----
32
37
 
33
- date: 2010-01-06 00:00:00 -08:00
38
+ date: 2010-03-27 00:00:00 -07:00
34
39
  default_executable:
35
40
  dependencies:
36
41
  - !ruby/object:Gem::Dependency
37
42
  name: rubyforge
38
- type: :development
39
- version_requirement:
40
- version_requirements: !ruby/object:Gem::Requirement
43
+ prerelease: false
44
+ requirement: &id001 !ruby/object:Gem::Requirement
41
45
  requirements:
42
46
  - - ">="
43
47
  - !ruby/object:Gem::Version
48
+ segments:
49
+ - 2
50
+ - 0
51
+ - 3
44
52
  version: 2.0.3
45
- version:
46
- - !ruby/object:Gem::Dependency
47
- name: gemcutter
48
53
  type: :development
49
- version_requirement:
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: 0.2.1
55
- version:
54
+ version_requirements: *id001
56
55
  - !ruby/object:Gem::Dependency
57
56
  name: minitest
58
- type: :development
59
- version_requirement:
60
- version_requirements: !ruby/object:Gem::Requirement
57
+ prerelease: false
58
+ requirement: &id002 !ruby/object:Gem::Requirement
61
59
  requirements:
62
60
  - - ">="
63
61
  - !ruby/object:Gem::Version
64
- version: 1.4.2
65
- version:
62
+ segments:
63
+ - 1
64
+ - 5
65
+ - 0
66
+ version: 1.5.0
67
+ type: :development
68
+ version_requirements: *id002
66
69
  - !ruby/object:Gem::Dependency
67
70
  name: hoe
68
- type: :development
69
- version_requirement:
70
- version_requirements: !ruby/object:Gem::Requirement
71
+ prerelease: false
72
+ requirement: &id003 !ruby/object:Gem::Requirement
71
73
  requirements:
72
74
  - - ">="
73
75
  - !ruby/object:Gem::Version
74
- version: 2.5.0
75
- version:
76
+ segments:
77
+ - 2
78
+ - 6
79
+ - 0
80
+ version: 2.6.0
81
+ type: :development
82
+ version_requirements: *id003
76
83
  description: |-
77
84
  minitest/unit is a small and fast replacement for ruby's huge and slow
78
85
  test/unit. This is meant to be clean and easy to use both as a regular
@@ -122,18 +129,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
122
129
  requirements:
123
130
  - - ">="
124
131
  - !ruby/object:Gem::Version
132
+ segments:
133
+ - 0
125
134
  version: "0"
126
- version:
127
135
  required_rubygems_version: !ruby/object:Gem::Requirement
128
136
  requirements:
129
137
  - - ">="
130
138
  - !ruby/object:Gem::Version
139
+ segments:
140
+ - 0
131
141
  version: "0"
132
- version:
133
142
  requirements: []
134
143
 
135
144
  rubyforge_project: bfts
136
- rubygems_version: 1.3.5
145
+ rubygems_version: 1.3.6
137
146
  signing_key:
138
147
  specification_version: 3
139
148
  summary: minitest/unit is a small and fast replacement for ruby's huge and slow test/unit
metadata.gz.sig CHANGED
Binary file