rubysl-test-unit 1.0.1 → 2.0.1

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