test-unit 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/History.txt +5 -0
  2. data/Manifest.txt +48 -0
  3. data/README.txt +27 -0
  4. data/Rakefile +15 -0
  5. data/bin/testrb +5 -0
  6. data/lib/test/unit.rb +280 -0
  7. data/lib/test/unit/assertionfailederror.rb +14 -0
  8. data/lib/test/unit/assertions.rb +622 -0
  9. data/lib/test/unit/autorunner.rb +220 -0
  10. data/lib/test/unit/collector.rb +43 -0
  11. data/lib/test/unit/collector/dir.rb +108 -0
  12. data/lib/test/unit/collector/objectspace.rb +34 -0
  13. data/lib/test/unit/error.rb +56 -0
  14. data/lib/test/unit/failure.rb +51 -0
  15. data/lib/test/unit/testcase.rb +160 -0
  16. data/lib/test/unit/testresult.rb +80 -0
  17. data/lib/test/unit/testsuite.rb +76 -0
  18. data/lib/test/unit/ui/console/testrunner.rb +127 -0
  19. data/lib/test/unit/ui/fox/testrunner.rb +268 -0
  20. data/lib/test/unit/ui/gtk/testrunner.rb +416 -0
  21. data/lib/test/unit/ui/gtk2/testrunner.rb +465 -0
  22. data/lib/test/unit/ui/testrunnermediator.rb +68 -0
  23. data/lib/test/unit/ui/testrunnerutilities.rb +46 -0
  24. data/lib/test/unit/ui/tk/testrunner.rb +260 -0
  25. data/lib/test/unit/util/backtracefilter.rb +40 -0
  26. data/lib/test/unit/util/observable.rb +90 -0
  27. data/lib/test/unit/util/procwrapper.rb +48 -0
  28. data/lib/test/unit/version.rb +7 -0
  29. data/sample/adder.rb +13 -0
  30. data/sample/subtracter.rb +12 -0
  31. data/sample/tc_adder.rb +18 -0
  32. data/sample/tc_subtracter.rb +18 -0
  33. data/sample/ts_examples.rb +7 -0
  34. data/test/collector/test_dir.rb +406 -0
  35. data/test/collector/test_objectspace.rb +98 -0
  36. data/test/runit/test_assert.rb +402 -0
  37. data/test/runit/test_testcase.rb +91 -0
  38. data/test/runit/test_testresult.rb +144 -0
  39. data/test/runit/test_testsuite.rb +49 -0
  40. data/test/test_assertions.rb +528 -0
  41. data/test/test_error.rb +26 -0
  42. data/test/test_failure.rb +33 -0
  43. data/test/test_testcase.rb +275 -0
  44. data/test/test_testresult.rb +104 -0
  45. data/test/test_testsuite.rb +129 -0
  46. data/test/util/test_backtracefilter.rb +41 -0
  47. data/test/util/test_observable.rb +102 -0
  48. data/test/util/test_procwrapper.rb +36 -0
  49. metadata +128 -0
@@ -0,0 +1,5 @@
1
+ === 1.2.3 / 2008-02-25
2
+
3
+ * 1 major enhancement
4
+
5
+ * Birthday (as a gem)!
@@ -0,0 +1,48 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ bin/testrb
6
+ lib/test/unit.rb
7
+ lib/test/unit/assertionfailederror.rb
8
+ lib/test/unit/assertions.rb
9
+ lib/test/unit/autorunner.rb
10
+ lib/test/unit/collector.rb
11
+ lib/test/unit/collector/dir.rb
12
+ lib/test/unit/collector/objectspace.rb
13
+ lib/test/unit/error.rb
14
+ lib/test/unit/failure.rb
15
+ lib/test/unit/testcase.rb
16
+ lib/test/unit/testresult.rb
17
+ lib/test/unit/testsuite.rb
18
+ lib/test/unit/ui/console/testrunner.rb
19
+ lib/test/unit/ui/fox/testrunner.rb
20
+ lib/test/unit/ui/gtk/testrunner.rb
21
+ lib/test/unit/ui/gtk2/testrunner.rb
22
+ lib/test/unit/ui/testrunnermediator.rb
23
+ lib/test/unit/ui/testrunnerutilities.rb
24
+ lib/test/unit/ui/tk/testrunner.rb
25
+ lib/test/unit/util/backtracefilter.rb
26
+ lib/test/unit/util/observable.rb
27
+ lib/test/unit/util/procwrapper.rb
28
+ lib/test/unit/version.rb
29
+ sample/adder.rb
30
+ sample/subtracter.rb
31
+ sample/tc_adder.rb
32
+ sample/tc_subtracter.rb
33
+ sample/ts_examples.rb
34
+ test/collector/test_dir.rb
35
+ test/collector/test_objectspace.rb
36
+ test/runit/test_assert.rb
37
+ test/runit/test_testcase.rb
38
+ test/runit/test_testresult.rb
39
+ test/runit/test_testsuite.rb
40
+ test/test_assertions.rb
41
+ test/test_error.rb
42
+ test/test_failure.rb
43
+ test/test_testcase.rb
44
+ test/test_testresult.rb
45
+ test/test_testsuite.rb
46
+ test/util/test_backtracefilter.rb
47
+ test/util/test_observable.rb
48
+ test/util/test_procwrapper.rb
@@ -0,0 +1,27 @@
1
+ = Test::Unit (Classic)
2
+
3
+ * http://rubyforge.org/projects/test-unit/
4
+
5
+ == DESCRIPTION:
6
+
7
+ Test::Unit (Classic) - Nathaniel Talbott's originial test-unit,
8
+ externalized from the ruby project as a gem (for tool developers).
9
+
10
+ == FEATURES/PROBLEMS:
11
+
12
+ * This is the original Test::Unit, taken straight from the ruby
13
+ distribution. It is being distributed as a gem to allow tool
14
+ builders to use it as a stand-alone package. (The test framework in
15
+ ruby is going to radically change very soon).
16
+ * DO NOT INSTALL THIS GEM unless you're active state or some other
17
+ development tool provider! You don't need it.
18
+
19
+ == INSTALL:
20
+
21
+ * sudo gem install test-unit
22
+
23
+ == LICENSE:
24
+
25
+ (The Ruby License)
26
+
27
+ This software is distributed under the same terms as ruby.
@@ -0,0 +1,15 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/test/unit/version.rb'
6
+
7
+ Hoe.new('test-unit', Test::Unit::VERSION) do |p|
8
+ p.developer('Kouhei Sutou', 'kou@cozmixng.org')
9
+ p.developer('Ryan Davis', 'ryand-ruby@zenspider.com')
10
+
11
+ # Ex-Parrot:
12
+ # p.developer('Nathaniel Talbott', 'nathaniel@talbott.ws')
13
+ end
14
+
15
+ # vim: syntax=Ruby
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ require 'test/unit'
3
+ (r = Test::Unit::AutoRunner.new(true)).process_args(ARGV) or
4
+ abort r.options.banner + " tests..."
5
+ exit r.run
@@ -0,0 +1,280 @@
1
+ require 'test/unit/testcase'
2
+ require 'test/unit/autorunner'
3
+
4
+ module Test # :nodoc:
5
+ #
6
+ # = Test::Unit - Ruby Unit Testing Framework
7
+ #
8
+ # == Introduction
9
+ #
10
+ # Unit testing is making waves all over the place, largely due to the
11
+ # fact that it is a core practice of XP. While XP is great, unit testing
12
+ # has been around for a long time and has always been a good idea. One
13
+ # of the keys to good unit testing, though, is not just writing tests,
14
+ # but having tests. What's the difference? Well, if you just _write_ a
15
+ # test and throw it away, you have no guarantee that something won't
16
+ # change later which breaks your code. If, on the other hand, you _have_
17
+ # tests (obviously you have to write them first), and run them as often
18
+ # as possible, you slowly build up a wall of things that cannot break
19
+ # without you immediately knowing about it. This is when unit testing
20
+ # hits its peak usefulness.
21
+ #
22
+ # Enter Test::Unit, a framework for unit testing in Ruby, helping you to
23
+ # design, debug and evaluate your code by making it easy to write and
24
+ # have tests for it.
25
+ #
26
+ #
27
+ # == Notes
28
+ #
29
+ # Test::Unit has grown out of and superceded Lapidary.
30
+ #
31
+ #
32
+ # == Feedback
33
+ #
34
+ # I like (and do my best to practice) XP, so I value early releases,
35
+ # user feedback, and clean, simple, expressive code. There is always
36
+ # room for improvement in everything I do, and Test::Unit is no
37
+ # exception. Please, let me know what you think of Test::Unit as it
38
+ # stands, and what you'd like to see expanded/changed/improved/etc. If
39
+ # you find a bug, let me know ASAP; one good way to let me know what the
40
+ # bug is is to submit a new test that catches it :-) Also, I'd love to
41
+ # hear about any successes you have with Test::Unit, and any
42
+ # documentation you might add will be greatly appreciated. My contact
43
+ # info is below.
44
+ #
45
+ #
46
+ # == Contact Information
47
+ #
48
+ # A lot of discussion happens about Ruby in general on the ruby-talk
49
+ # mailing list (http://www.ruby-lang.org/en/ml.html), and you can ask
50
+ # any questions you might have there. I monitor the list, as do many
51
+ # other helpful Rubyists, and you're sure to get a quick answer. Of
52
+ # course, you're also welcome to email me (Nathaniel Talbott) directly
53
+ # at mailto:testunit@talbott.ws, and I'll do my best to help you out.
54
+ #
55
+ #
56
+ # == Credits
57
+ #
58
+ # I'd like to thank...
59
+ #
60
+ # Matz, for a great language!
61
+ #
62
+ # Masaki Suketa, for his work on RubyUnit, which filled a vital need in
63
+ # the Ruby world for a very long time. I'm also grateful for his help in
64
+ # polishing Test::Unit and getting the RubyUnit compatibility layer
65
+ # right. His graciousness in allowing Test::Unit to supercede RubyUnit
66
+ # continues to be a challenge to me to be more willing to defer my own
67
+ # rights.
68
+ #
69
+ # Ken McKinlay, for his interest and work on unit testing, and for his
70
+ # willingness to dialog about it. He was also a great help in pointing
71
+ # out some of the holes in the RubyUnit compatibility layer.
72
+ #
73
+ # Dave Thomas, for the original idea that led to the extremely simple
74
+ # "require 'test/unit'", plus his code to improve it even more by
75
+ # allowing the selection of tests from the command-line. Also, without
76
+ # RDoc, the documentation for Test::Unit would stink a lot more than it
77
+ # does now.
78
+ #
79
+ # Everyone who's helped out with bug reports, feature ideas,
80
+ # encouragement to continue, etc. It's a real privilege to be a part of
81
+ # the Ruby community.
82
+ #
83
+ # The guys at RoleModel Software, for putting up with me repeating, "But
84
+ # this would be so much easier in Ruby!" whenever we're coding in Java.
85
+ #
86
+ # My Creator, for giving me life, and giving it more abundantly.
87
+ #
88
+ #
89
+ # == License
90
+ #
91
+ # Test::Unit is copyright (c) 2000-2003 Nathaniel Talbott. It is free
92
+ # software, and is distributed under the Ruby license. See the COPYING
93
+ # file in the standard Ruby distribution for details.
94
+ #
95
+ #
96
+ # == Warranty
97
+ #
98
+ # This software is provided "as is" and without any express or
99
+ # implied warranties, including, without limitation, the implied
100
+ # warranties of merchantibility and fitness for a particular
101
+ # purpose.
102
+ #
103
+ #
104
+ # == Author
105
+ #
106
+ # Nathaniel Talbott.
107
+ # Copyright (c) 2000-2003, Nathaniel Talbott
108
+ #
109
+ # ----
110
+ #
111
+ # = Usage
112
+ #
113
+ # The general idea behind unit testing is that you write a _test_
114
+ # _method_ that makes certain _assertions_ about your code, working
115
+ # against a _test_ _fixture_. A bunch of these _test_ _methods_ are
116
+ # bundled up into a _test_ _suite_ and can be run any time the
117
+ # developer wants. The results of a run are gathered in a _test_
118
+ # _result_ and displayed to the user through some UI. So, lets break
119
+ # this down and see how Test::Unit provides each of these necessary
120
+ # pieces.
121
+ #
122
+ #
123
+ # == Assertions
124
+ #
125
+ # These are the heart of the framework. Think of an assertion as a
126
+ # statement of expected outcome, i.e. "I assert that x should be equal
127
+ # to y". If, when the assertion is executed, it turns out to be
128
+ # correct, nothing happens, and life is good. If, on the other hand,
129
+ # your assertion turns out to be false, an error is propagated with
130
+ # pertinent information so that you can go back and make your
131
+ # assertion succeed, and, once again, life is good. For an explanation
132
+ # of the current assertions, see Test::Unit::Assertions.
133
+ #
134
+ #
135
+ # == Test Method & Test Fixture
136
+ #
137
+ # Obviously, these assertions have to be called within a context that
138
+ # knows about them and can do something meaningful with their
139
+ # pass/fail value. Also, it's handy to collect a bunch of related
140
+ # tests, each test represented by a method, into a common test class
141
+ # that knows how to run them. The tests will be in a separate class
142
+ # from the code they're testing for a couple of reasons. First of all,
143
+ # it allows your code to stay uncluttered with test code, making it
144
+ # easier to maintain. Second, it allows the tests to be stripped out
145
+ # for deployment, since they're really there for you, the developer,
146
+ # and your users don't need them. Third, and most importantly, it
147
+ # allows you to set up a common test fixture for your tests to run
148
+ # against.
149
+ #
150
+ # What's a test fixture? Well, tests do not live in a vacuum; rather,
151
+ # they're run against the code they are testing. Often, a collection
152
+ # of tests will run against a common set of data, also called a
153
+ # fixture. If they're all bundled into the same test class, they can
154
+ # all share the setting up and tearing down of that data, eliminating
155
+ # unnecessary duplication and making it much easier to add related
156
+ # tests.
157
+ #
158
+ # Test::Unit::TestCase wraps up a collection of test methods together
159
+ # and allows you to easily set up and tear down the same test fixture
160
+ # for each test. This is done by overriding #setup and/or #teardown,
161
+ # which will be called before and after each test method that is
162
+ # run. The TestCase also knows how to collect the results of your
163
+ # assertions into a Test::Unit::TestResult, which can then be reported
164
+ # back to you... but I'm getting ahead of myself. To write a test,
165
+ # follow these steps:
166
+ #
167
+ # * Make sure Test::Unit is in your library path.
168
+ # * require 'test/unit' in your test script.
169
+ # * Create a class that subclasses Test::Unit::TestCase.
170
+ # * Add a method that begins with "test" to your class.
171
+ # * Make assertions in your test method.
172
+ # * Optionally define #setup and/or #teardown to set up and/or tear
173
+ # down your common test fixture.
174
+ # * You can now run your test as you would any other Ruby
175
+ # script... try it and see!
176
+ #
177
+ # A really simple test might look like this (#setup and #teardown are
178
+ # commented out to indicate that they are completely optional):
179
+ #
180
+ # require 'test/unit'
181
+ #
182
+ # class TC_MyTest < Test::Unit::TestCase
183
+ # # def setup
184
+ # # end
185
+ #
186
+ # # def teardown
187
+ # # end
188
+ #
189
+ # def test_fail
190
+ # assert(false, 'Assertion was false.')
191
+ # end
192
+ # end
193
+ #
194
+ #
195
+ # == Test Runners
196
+ #
197
+ # So, now you have this great test class, but you still need a way to
198
+ # run it and view any failures that occur during the run. This is
199
+ # where Test::Unit::UI::Console::TestRunner (and others, such as
200
+ # Test::Unit::UI::GTK::TestRunner) comes into play. The console test
201
+ # runner is automatically invoked for you if you require 'test/unit'
202
+ # and simply run the file. To use another runner, or to manually
203
+ # invoke a runner, simply call its run class method and pass in an
204
+ # object that responds to the suite message with a
205
+ # Test::Unit::TestSuite. This can be as simple as passing in your
206
+ # TestCase class (which has a class suite method). It might look
207
+ # something like this:
208
+ #
209
+ # require 'test/unit/ui/console/testrunner'
210
+ # Test::Unit::UI::Console::TestRunner.run(TC_MyTest)
211
+ #
212
+ #
213
+ # == Test Suite
214
+ #
215
+ # As more and more unit tests accumulate for a given project, it
216
+ # becomes a real drag running them one at a time, and it also
217
+ # introduces the potential to overlook a failing test because you
218
+ # forget to run it. Suddenly it becomes very handy that the
219
+ # TestRunners can take any object that returns a Test::Unit::TestSuite
220
+ # in response to a suite method. The TestSuite can, in turn, contain
221
+ # other TestSuites or individual tests (typically created by a
222
+ # TestCase). In other words, you can easily wrap up a group of
223
+ # TestCases and TestSuites like this:
224
+ #
225
+ # require 'test/unit/testsuite'
226
+ # require 'tc_myfirsttests'
227
+ # require 'tc_moretestsbyme'
228
+ # require 'ts_anothersetoftests'
229
+ #
230
+ # class TS_MyTests
231
+ # def self.suite
232
+ # suite = Test::Unit::TestSuite.new
233
+ # suite << TC_MyFirstTests.suite
234
+ # suite << TC_MoreTestsByMe.suite
235
+ # suite << TS_AnotherSetOfTests.suite
236
+ # return suite
237
+ # end
238
+ # end
239
+ # Test::Unit::UI::Console::TestRunner.run(TS_MyTests)
240
+ #
241
+ # Now, this is a bit cumbersome, so Test::Unit does a little bit more
242
+ # for you, by wrapping these up automatically when you require
243
+ # 'test/unit'. What does this mean? It means you could write the above
244
+ # test case like this instead:
245
+ #
246
+ # require 'test/unit'
247
+ # require 'tc_myfirsttests'
248
+ # require 'tc_moretestsbyme'
249
+ # require 'ts_anothersetoftests'
250
+ #
251
+ # Test::Unit is smart enough to find all the test cases existing in
252
+ # the ObjectSpace and wrap them up into a suite for you. It then runs
253
+ # the dynamic suite using the console TestRunner.
254
+ #
255
+ #
256
+ # == Questions?
257
+ #
258
+ # I'd really like to get feedback from all levels of Ruby
259
+ # practitioners about typos, grammatical errors, unclear statements,
260
+ # missing points, etc., in this document (or any other).
261
+ #
262
+
263
+ module Unit
264
+ # If set to false Test::Unit will not automatically run at exit.
265
+ def self.run=(flag)
266
+ @run = flag
267
+ end
268
+
269
+ # Automatically run tests at exit?
270
+ def self.run?
271
+ @run ||= false
272
+ end
273
+ end
274
+ end
275
+
276
+ at_exit do
277
+ unless $! || Test::Unit.run?
278
+ exit Test::Unit::AutoRunner.run
279
+ end
280
+ end
@@ -0,0 +1,14 @@
1
+ #--
2
+ #
3
+ # Author:: Nathaniel Talbott.
4
+ # Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved.
5
+ # License:: Ruby license.
6
+
7
+ module Test
8
+ module Unit
9
+
10
+ # Thrown by Test::Unit::Assertions when an assertion fails.
11
+ class AssertionFailedError < StandardError
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,622 @@
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'
7
+
8
+ module Test
9
+ module Unit
10
+
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
32
+
33
+ module Assertions
34
+
35
+ ##
36
+ # The assertion upon which all other assertions are based. Passes if the
37
+ # block yields true.
38
+ #
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
51
+ end
52
+
53
+ ##
54
+ # Asserts that +boolean+ is not false or nil.
55
+ #
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
65
+ end
66
+
67
+ ##
68
+ # Passes if +expected+ == +actual.
69
+ #
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.
73
+ #
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 }
84
+ end
85
+
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
92
+ end
93
+ end
94
+
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
+ end
100
+
101
+ ##
102
+ # Passes if the block raises one of the given exceptions.
103
+ #
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
116
+ 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
128
+ 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
+ end
133
+ end
134
+
135
+ ##
136
+ # Alias of assert_raise.
137
+ #
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.
166
+ #
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+
177
+ #
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)}
187
+ end
188
+ end
189
+
190
+ ##
191
+ # Passes if +object+ .respond_to? +method+
192
+ #
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+.
215
+ #
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))
225
+ else
226
+ pattern
227
+ end
228
+ full_message = build_message(message, "<?> expected to be =~\n<?>.", string, pattern)
229
+ assert_block(full_message) { string =~ pattern }
230
+ end
231
+ end
232
+
233
+ ##
234
+ # Passes if +actual+ .equal? +expected+ (i.e. they are the same
235
+ # instance).
236
+ #
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) }
250
+ end
251
+
252
+ ##
253
+ # Compares the +object1+ with +object2+ using +operator+.
254
+ #
255
+ # Passes if object1.__send__(operator, object2) is true.
256
+ #
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
272
+ end
273
+
274
+ ##
275
+ # Passes if block does not raise an exception.
276
+ #
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.
307
+ #
308
+ # Example:
309
+ # flunk 'Not done testing yet.'
310
+
311
+ public
312
+ def flunk(message="Flunked")
313
+ assert_block(build_message(message)){false}
314
+ end
315
+
316
+ ##
317
+ # Passes if ! +actual+ .equal? +expected+
318
+ #
319
+ # Example:
320
+ # assert_not_same Object.new, Object.new
321
+
322
+ public
323
+ def assert_not_same(expected, actual, message="")
324
+ full_message = build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__)
325
+ <?>
326
+ with id <?> expected to not be equal\\? to
327
+ <?>
328
+ with id <?>.
329
+ EOT
330
+ assert_block(full_message) { !actual.equal?(expected) }
331
+ end
332
+
333
+ ##
334
+ # Passes if +expected+ != +actual+
335
+ #
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?
347
+ #
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+
359
+ #
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 }
369
+ end
370
+ end
371
+
372
+ UncaughtThrow = {NameError => /^uncaught throw \`(.+)\'$/,
373
+ ThreadError => /^uncaught throw \`(.+)\' in thread /} #`
374
+
375
+ ##
376
+ # Passes if the block throws +expected_symbol+
377
+ #
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.
408
+ #
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.
434
+ #
435
+ # 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")
443
+ 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
452
+ end
453
+
454
+ ##
455
+ # Passes if the method send returns a true value.
456
+ #
457
+ # +send_array+ is composed of:
458
+ # * A receiver
459
+ # * A method
460
+ # * Arguments to the method
461
+ #
462
+ # 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
498
+ end
499
+ else
500
+ return yield
501
+ end
502
+ 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
+
508
+ private
509
+ def add_assertion
510
+ end
511
+
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
519
+ 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
+
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
593
+ 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")
615
+ end
616
+ end
617
+
618
+ # :startdoc:
619
+
620
+ end
621
+ end
622
+ end