miniunit 1.1.0 → 1.2.0

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,304 +1,21 @@
1
- #
2
- # Totally minimal (hopefully) drop-in replacement for test/unit
3
- #
1
+ require 'mini/test'
4
2
 
5
- # Minimal methods needed to run full miniunit + assert:
6
- #
7
- # Array: each, grep, join, size
8
- # Class: new, inherited
9
- # Enumerable: find
10
- # Exception: backtrace, message
11
- # Fixnum: +
12
- # Hash: keys
13
- # Kernel: at_exit
14
- # Module: ===, public_instance_methods
15
- # Object: __send__, class, inspect
16
- # String: sub, %
17
- # Regexp: =~
18
-
19
- # Only needed for specific assertions:
20
- #
21
- # Array.last
22
- # Enumerable.include?
23
- # Float: abs
24
- # Object: ==, inspect, equal?, kind_of?, nil?, respond_to?
25
- # Numeric: to_f, <, -
26
-
27
- $TESTING_MINIUNIT ||= false
28
-
29
- at_exit { exit MiniTest::Unit.autotest } unless $TESTING_MINIUNIT
30
-
31
- MINIUNIT_FILE_SEPARATORS = %r{[\\/:]}
32
- MINIUNIT_PREFIX = __FILE__.split(MINIUNIT_FILE_SEPARATORS)[0..-3]
33
- MINIUNIT_RB_FILE = /\.rb\Z/
34
-
35
- def filter_backtrace(backtrace, prefix=nil)
36
- return ["No backtrace"] unless(backtrace)
37
- return backtrace if $TESTING_MINIUNIT
38
- split_p = if(prefix)
39
- prefix.split(MINIUNIT_FILE_SEPARATORS)
40
- else
41
- MINIUNIT_PREFIX
42
- end
43
- match = proc do |e|
44
- split_e = e.split(MINIUNIT_FILE_SEPARATORS)[0, split_p.size]
45
- next false unless(split_e[0..-2] == split_p[0..-2])
46
- split_e[-1].sub(MINIUNIT_RB_FILE, '') == split_p[-1]
47
- end
48
- return backtrace unless(backtrace.detect(&match))
49
- found_prefix = false
50
- new_backtrace = backtrace.reverse.reject do |e|
51
- if(match[e])
52
- found_prefix = true
53
- true
54
- elsif(found_prefix)
55
- false
56
- else
57
- true
58
- end
59
- end.reverse
60
- new_backtrace = (new_backtrace.empty? ? backtrace : new_backtrace)
61
- new_backtrace = new_backtrace.reject(&match)
62
- new_backtrace.empty? ? backtrace : new_backtrace
63
- end
64
-
65
- module MiniTest
66
- class Assertion < Exception; end
67
-
68
- class Unit
69
- VERSION = "1.1.0"
70
-
71
- attr_reader :report
72
-
73
- @@out = $stdout
74
-
75
- def self.output= stream
76
- @@out = stream
77
- end
78
-
79
- def self.autotest
80
- self.new.autotest
81
- end
82
-
83
- def self.tests_from(klass)
84
- klass.public_instance_methods(true).grep(/^test/).sort
3
+ module Test
4
+ module Unit # was ::Mini::Test, but rails' horrid code forced my hand
5
+ if defined? TestCase then
6
+ warn "ARGH! someone defined Test::Unit::TestCase rather than requiring"
7
+ remove_const :TestCase
85
8
  end
86
9
 
87
- def self.location_of_failure(e)
88
- e.backtrace.find { |s|
89
- s !~ /in .(assert|flunk)/
90
- }.sub(/:in .*$/, '')
91
- end
92
-
93
- def puke(klass, meth, e)
94
- if MiniTest::Assertion === e then
95
- @results[:failures] += 1
96
- loc = self.class.location_of_failure(e)
97
- @report << "Failure:\n#{meth}(#{klass}) [#{loc}]:\n#{e.message}\n"
98
- 'F'
99
- else
100
- @results[:errors] += 1
101
- bt = filter_backtrace(e.backtrace).join("\n ")
102
- @report << "Error:\n#{meth}(#{klass}):\n#{e.class}: #{e.message}\n #{bt}\n"
103
- 'E'
104
- end
105
- end
106
-
107
- def initialize
108
- @report = []
109
- @results = {:errors => 0, :failures => 0}
110
- end
111
-
112
- def autotest
113
- @@out.puts "Loaded suite #{$0.sub(/\.rb$/, '')}\nStarted"
114
-
115
- timestamp_before_test_run = Time.now
116
- test_count, assertion_count = run_test_suites
117
-
118
- @@out.puts
119
- @@out.puts "Finished in #{'%.6f' % (Time.now - timestamp_before_test_run)} seconds."
120
-
121
- @report.each_with_index do |msg, i|
122
- @@out.puts "\n%3d) %s" % [i + 1, msg]
123
- end
124
-
125
- @@out.puts
126
- @@out.puts "%d tests, %d assertions, %d failures, %d errors" % [test_count, assertion_count, failures, errors]
127
-
128
- return failures + errors
129
- end
130
-
131
- def run_test_suites
132
- test_count, assertion_count = 0, 0
133
- TestCase.test_suites.each do |klass|
134
- inst = klass.new
135
- inst._assertions = 0
136
- tests = self.class.tests_from(klass)
137
- tests.each do |meth|
138
- result = '.'
139
- begin
140
- inst.setup
141
- inst.__send__ meth
142
- rescue Exception => e
143
- result = puke(klass, meth, e)
144
- ensure
145
- begin
146
- inst.teardown
147
- rescue Exception => e
148
- result = puke(klass, meth, e)
149
- end
150
- end
151
- @@out.print result
152
- end
153
- test_count += tests.size
154
- assertion_count += inst._assertions
155
- end
156
- [test_count, assertion_count]
157
- end
158
-
159
- def failures
160
- @results[:failures]
161
- end
162
-
163
- def errors
164
- @results[:errors]
165
- end
10
+ TestCase = ::Mini::Test::TestCase
11
+ AssertionFailedError = ::Mini::Assertion
166
12
 
167
13
  class TestCase
168
- def self.reset
169
- @@test_suites = {}
170
- end
171
-
172
- reset
173
-
174
- def self.inherited klass
175
- @@test_suites[klass] = true
176
- end
177
-
178
- def self.test_suites
179
- @@test_suites.keys.sort_by { |ts| ts.name }
180
- end
181
-
182
- attr_accessor :_assertions
183
- def setup; end
184
- def teardown; end
185
-
186
- def _increment_assertions
187
- @_assertions ||= 0
188
- @_assertions += 1
189
- end
190
-
191
- def assert test, msg = "failed assertion (no message given)"
192
- _increment_assertions
193
- raise MiniTest::Assertion, msg unless test
194
- end
195
-
196
- def assert_block msg = "assert_block failed."
197
- assert yield, msg
198
- end
199
-
200
- def assert_equal exp, act, msg = ""
201
- msg += '.' unless msg.empty?
202
- assert exp == act, "#{msg}\n<#{exp.inspect}> expected but was\n<#{act.inspect}>.".strip
203
- end
204
-
205
- def assert_in_delta exp, act, delta, msg = "Expected #{exp} to be within #{delta} of #{act}"
206
- assert delta.to_f > (exp.to_f - act.to_f).abs, msg
207
- end
208
-
209
- def assert_instance_of cls, obj, msg = "Expected #{obj.inspect} to be an instance of #{cls}"
210
- assert cls === obj, msg
211
- end
212
-
213
- def assert_kind_of cls, obj, msg = "Expected #{obj.inspect} to be a kind of #{cls}"
214
- assert obj.kind_of?(cls), msg
215
- end
216
-
217
- def assert_match exp, act, msg = "Expected #{act.inspect} to match #{exp.inspect}"
218
- assert act =~ exp, msg
219
- end
220
-
221
- def assert_nil obj, msg = "Expected #{obj.inspect} to be nil"
222
- assert obj.nil?, msg
223
- end
224
-
225
- def assert_no_match exp, act, msg = "Expected #{act.inspect} to not match #{exp.inspect}"
226
- assert act !~ exp, msg
227
- end
228
-
229
- def assert_not_equal exp, act, msg = "Expected #{act.inspect} to not be equal to #{exp.inspect}"
230
- assert exp != act, msg
231
- end
232
-
233
- def assert_not_nil obj, msg = "Expected #{obj.inspect} to not be nil"
234
- assert ! obj.nil?, msg
235
- end
236
-
237
- def assert_not_same exp, act, msg = "Expected #{act.inspect} to not be the same as #{exp.inspect}"
238
- assert ! exp.equal?(act), msg
239
- end
240
-
241
- def assert_nothing_raised
242
- _increment_assertions
243
- yield
244
- rescue => e
245
- raise MiniTest::Assertion, exception_details(e, "Exception raised:")
246
- end
247
-
248
- alias :assert_nothing_thrown :assert_nothing_raised
249
-
250
- def assert_operator o1, op, o2, msg = "<#{o1.inspect}> expected to be\n#{op.inspect}\n<#{o2.inspect}>."
251
- assert o1.__send__(op, o2), msg
252
- end
253
-
254
- def exception_details e, msg
255
- "#{msg}\nClass: <#{e.class}>\nMessage: <#{e.message.inspect}>\n---Backtrace---\n#{filter_backtrace(e.backtrace).join("\n")}\n---------------"
256
- end
257
-
258
- def assert_raises *exp
259
- msg = String === exp.last ? exp.pop : nil
260
- exp = exp.first if exp.size == 1
261
- begin
262
- yield
263
- raise MiniTest::Assertion, "<#{exp.inspect}> exception expected but none was thrown."
264
- rescue Exception => e
265
- assert((Array === exp ? exp.include?(e) : exp === e),
266
- exception_details(e,
267
- "<#{exp.inspect}> exception expected but was"))
268
-
269
- return e
270
- end
271
- end
272
- alias :assert_raise :assert_raises
273
-
274
- def assert_respond_to obj, meth, msg = "Expected #{obj.inspect} of type #{obj.class} to respond_to? #{meth.inspect}"
275
- assert obj.respond_to?(meth), msg
276
- end
277
-
278
- def assert_same exp, act, msg = "Expected #{act.inspect} to be the same as #{exp.inspect}."
279
- assert exp.equal?(act), msg
280
- end
281
-
282
- def assert_send send, msg = nil
283
- recv, meth, *args = send
284
- assert(recv.__send__(meth, *args),
285
- msg || "<#{recv.inspect}> expected to respond to\n<#{meth.inspect}> with a true value.")
286
- end
287
-
288
- def assert_throws sym, msg = "<#{sym.inspect}> should have been thrown."
289
- caught = true
290
- catch(sym) do
291
- yield rescue nil
292
- caught = false
293
- end
294
- assert caught, msg
295
- end
14
+ alias :method_name :name # so lame
15
+ end
16
+ end
17
+ end
296
18
 
297
- def flunk msg = "Flunked"
298
- assert false, msg
299
- end
300
- end # class TestCase
301
- end # class Unit
302
- end # module MiniTest
19
+ require 'test/unit/assertions' # brings in deprecated methods
303
20
 
304
- Test = MiniTest unless defined? Test
21
+ Mini::Test.autorun
@@ -0,0 +1,75 @@
1
+ require 'mini/test'
2
+ require 'test/unit'
3
+
4
+ module Test::Unit # patch up bastards that that extend improperly.
5
+ if defined? Assertions then
6
+ warn "ARGH! someone defined Test::Unit::Assertions rather than requiring"
7
+ CRAP_ASSERTIONS = Assertions
8
+ remove_const :Assertions
9
+
10
+ ObjectSpace.each_object(Module) do |offender|
11
+ offender.send :include, ::Mini::Assertions if offender < CRAP_ASSERTIONS
12
+ end rescue :fuck_you_then!
13
+
14
+ Test::Unit::TestCase.send :include, CRAP_ASSERTIONS
15
+ end
16
+
17
+ Assertions = ::Mini::Assertions
18
+
19
+ module Assertions
20
+ def self.included x
21
+ x.send :include, Test::Unit::CRAP_ASSERTIONS
22
+ end if defined? Test::Unit::CRAP_ASSERTIONS
23
+ end
24
+ end
25
+
26
+ class Module # define deprecation api
27
+ DEPS = Hash.new { |h,k| h[k] = {} }
28
+
29
+ def deprecation_warning old, new = nil, kaller = nil
30
+ kaller ||= caller[1]
31
+ unless DEPS[old][kaller] then
32
+ msg = "#{self}##{old} deprecated. "
33
+ msg += new ? "Use ##{new}" : "No replacement is provided"
34
+ msg += ". From #{kaller}."
35
+ warn msg
36
+ end
37
+ DEPS[old][kaller] = true
38
+ end
39
+
40
+ def deprecate old, new
41
+ class_eval <<-EOM
42
+ def #{old} *args, &block
43
+ cls, clr = self.class, caller.first
44
+ self.class.deprecation_warning #{old.inspect}, #{new.inspect}, clr
45
+ #{new}(*args, &block)
46
+ end
47
+ EOM
48
+ end
49
+ end
50
+
51
+ module Test::Unit
52
+ module Assertions # deprecations
53
+ deprecate :assert_nothing_thrown, :assert_nothing_raised # drop 2009-06-01
54
+ deprecate :assert_raise, :assert_raises # drop 2010-06-01
55
+ deprecate :assert_not_equal, :refute_equal # drop 2009-06-01
56
+ deprecate :assert_no_match, :refute_match # drop 2009-06-01
57
+ deprecate :assert_not_nil, :refute_nil # drop 2009-06-01
58
+ deprecate :assert_not_same, :refute_same # drop 2009-06-01
59
+
60
+ def assert_nothing_raised _ = :ignored # drop 2009-06-01
61
+ self.class.deprecation_warning :assert_nothing_raised
62
+ self._assertions += 1
63
+ yield
64
+ rescue => e
65
+ raise Mini::Assertion, exception_details(e, "Exception raised:")
66
+ end
67
+
68
+ def build_message(user_message, template_message, *args) # drop 2009-06-01
69
+ user_message ||= ''
70
+ user_message += ' ' unless user_message.empty?
71
+ msg = template_message.split(/<\?>/).zip(args.map { |o| o.inspect })
72
+ user_message + msg.join
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,8 @@
1
+ # rails currently needs this file and this one method.
2
+ module Test::Unit
3
+ class Error
4
+ def message
5
+ "you're a loser"
6
+ end
7
+ end
8
+ end
@@ -1,7 +1,2 @@
1
1
  # don't define anything, this is just so we don't get the real one
2
-
3
- $TEST_UNIT_TESTCASE_DEPRECATION ||= false
4
-
5
- warn "require 'test/unit/testcase' has been deprecated" unless $TEST_UNIT_TESTCASE_DEPRECATION
6
-
7
- $TEST_UNIT_TESTCASE_DEPRECATION = true
2
+ warn "require 'test/unit/testcase' has been deprecated"
@@ -0,0 +1,76 @@
1
+ require 'mini/mock'
2
+
3
+ Mini::Test.autorun
4
+
5
+ class TestMiniMock < Mini::Test::TestCase
6
+ def setup
7
+ @mock = Mini::Mock.new.expect(:foo, nil)
8
+ @mock.expect(:meaning_of_life, 42)
9
+ end
10
+
11
+ def test_should_create_stub_method
12
+ assert_nil @mock.foo
13
+ end
14
+
15
+ def test_should_allow_return_value_specification
16
+ assert_equal 42, @mock.meaning_of_life
17
+ end
18
+
19
+ def test_should_blow_up_if_not_called
20
+ @mock.foo
21
+
22
+ util_verify_bad
23
+ end
24
+
25
+ def test_should_not_blow_up_if_everything_called
26
+ @mock.foo
27
+ @mock.meaning_of_life
28
+
29
+ assert @mock.verify
30
+ end
31
+
32
+ def test_should_allow_expectations_to_be_added_after_creation
33
+ @mock.expect(:bar, true)
34
+ assert @mock.bar
35
+ end
36
+
37
+ def test_should_not_verify_if_new_expected_method_is_not_called
38
+ @mock.foo
39
+ @mock.meaning_of_life
40
+ @mock.expect(:bar, true)
41
+
42
+ util_verify_bad
43
+ end
44
+
45
+ def test_should_not_verify_if_unexpected_method_is_called
46
+ assert_raises NoMethodError do
47
+ @mock.unexpected
48
+ end
49
+ end
50
+
51
+ def test_should_blow_up_on_wrong_number_of_arguments
52
+ @mock.foo
53
+ @mock.meaning_of_life
54
+ @mock.expect(:sum, 3, [1, 2])
55
+
56
+ assert_raises ArgumentError do
57
+ @mock.sum
58
+ end
59
+ end
60
+
61
+ def test_should_blow_up_on_wrong_arguments
62
+ @mock.foo
63
+ @mock.meaning_of_life
64
+ @mock.expect(:sum, 3, [1, 2])
65
+
66
+ @mock.sum(2, 4)
67
+
68
+ util_verify_bad
69
+ end
70
+
71
+ def util_verify_bad
72
+ assert_raises MockExpectationError do
73
+ @mock.verify
74
+ end
75
+ end
76
+ end