glib2 0.90.8 → 0.90.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (123) hide show
  1. data/ChangeLog +44 -0
  2. data/Rakefile +1 -0
  3. data/ext/glib2/rbglib.h +1 -1
  4. data/ext/glib2/rbglib_iochannel.c +15 -6
  5. data/ext/glib2/rbgobj_signal.c +1 -1
  6. data/ext/glib2/rbgobject.h +1 -1
  7. data/lib/gnome2-raketask.rb +4 -2
  8. data/lib/mkmf-gnome2.rb +47 -23
  9. data/test/glib-test-init.rb +1 -3
  10. metadata +20 -121
  11. data/test-unit/COPYING +0 -56
  12. data/test-unit/GPL +0 -340
  13. data/test-unit/History.txt +0 -279
  14. data/test-unit/Manifest.txt +0 -110
  15. data/test-unit/PSFL +0 -271
  16. data/test-unit/README.txt +0 -75
  17. data/test-unit/Rakefile +0 -53
  18. data/test-unit/TODO +0 -5
  19. data/test-unit/bin/testrb +0 -5
  20. data/test-unit/html/bar.png +0 -0
  21. data/test-unit/html/bar.svg +0 -153
  22. data/test-unit/html/developer.png +0 -0
  23. data/test-unit/html/developer.svg +0 -469
  24. data/test-unit/html/famfamfam-logo.png +0 -0
  25. data/test-unit/html/favicon.ico +0 -0
  26. data/test-unit/html/favicon.png +0 -0
  27. data/test-unit/html/favicon.svg +0 -82
  28. data/test-unit/html/heading-mark.png +0 -0
  29. data/test-unit/html/heading-mark.svg +0 -393
  30. data/test-unit/html/index.html +0 -285
  31. data/test-unit/html/index.html.ja +0 -299
  32. data/test-unit/html/install.png +0 -0
  33. data/test-unit/html/install.svg +0 -636
  34. data/test-unit/html/jp.png +0 -0
  35. data/test-unit/html/kinotan-failure.png +0 -0
  36. data/test-unit/html/kinotan-pass.png +0 -0
  37. data/test-unit/html/logo.png +0 -0
  38. data/test-unit/html/logo.svg +0 -483
  39. data/test-unit/html/reference.png +0 -0
  40. data/test-unit/html/rubyforge.png +0 -0
  41. data/test-unit/html/tango-logo.png +0 -0
  42. data/test-unit/html/test-unit.css +0 -339
  43. data/test-unit/html/tutorial.png +0 -0
  44. data/test-unit/html/tutorial.svg +0 -559
  45. data/test-unit/html/us.png +0 -0
  46. data/test-unit/images/color-diff.png +0 -0
  47. data/test-unit/lib/test/unit.rb +0 -328
  48. data/test-unit/lib/test/unit/assertionfailederror.rb +0 -25
  49. data/test-unit/lib/test/unit/assertions.rb +0 -1792
  50. data/test-unit/lib/test/unit/attribute.rb +0 -125
  51. data/test-unit/lib/test/unit/autorunner.rb +0 -415
  52. data/test-unit/lib/test/unit/collector.rb +0 -38
  53. data/test-unit/lib/test/unit/collector/descendant.rb +0 -23
  54. data/test-unit/lib/test/unit/collector/dir.rb +0 -108
  55. data/test-unit/lib/test/unit/collector/load.rb +0 -187
  56. data/test-unit/lib/test/unit/collector/objectspace.rb +0 -34
  57. data/test-unit/lib/test/unit/collector/xml.rb +0 -250
  58. data/test-unit/lib/test/unit/color-scheme.rb +0 -106
  59. data/test-unit/lib/test/unit/color.rb +0 -96
  60. data/test-unit/lib/test/unit/diff.rb +0 -740
  61. data/test-unit/lib/test/unit/error.rb +0 -131
  62. data/test-unit/lib/test/unit/exceptionhandler.rb +0 -39
  63. data/test-unit/lib/test/unit/failure.rb +0 -136
  64. data/test-unit/lib/test/unit/fixture.rb +0 -185
  65. data/test-unit/lib/test/unit/notification.rb +0 -129
  66. data/test-unit/lib/test/unit/omission.rb +0 -191
  67. data/test-unit/lib/test/unit/pending.rb +0 -150
  68. data/test-unit/lib/test/unit/priority.rb +0 -180
  69. data/test-unit/lib/test/unit/runner/console.rb +0 -52
  70. data/test-unit/lib/test/unit/runner/emacs.rb +0 -8
  71. data/test-unit/lib/test/unit/runner/tap.rb +0 -8
  72. data/test-unit/lib/test/unit/runner/xml.rb +0 -15
  73. data/test-unit/lib/test/unit/testcase.rb +0 -515
  74. data/test-unit/lib/test/unit/testresult.rb +0 -125
  75. data/test-unit/lib/test/unit/testsuite.rb +0 -134
  76. data/test-unit/lib/test/unit/ui/console/outputlevel.rb +0 -14
  77. data/test-unit/lib/test/unit/ui/console/testrunner.rb +0 -467
  78. data/test-unit/lib/test/unit/ui/emacs/testrunner.rb +0 -63
  79. data/test-unit/lib/test/unit/ui/tap/testrunner.rb +0 -82
  80. data/test-unit/lib/test/unit/ui/testrunner.rb +0 -53
  81. data/test-unit/lib/test/unit/ui/testrunnermediator.rb +0 -86
  82. data/test-unit/lib/test/unit/ui/testrunnerutilities.rb +0 -41
  83. data/test-unit/lib/test/unit/ui/xml/testrunner.rb +0 -224
  84. data/test-unit/lib/test/unit/util/backtracefilter.rb +0 -42
  85. data/test-unit/lib/test/unit/util/method-owner-finder.rb +0 -28
  86. data/test-unit/lib/test/unit/util/observable.rb +0 -90
  87. data/test-unit/lib/test/unit/util/output.rb +0 -31
  88. data/test-unit/lib/test/unit/util/procwrapper.rb +0 -48
  89. data/test-unit/lib/test/unit/version.rb +0 -7
  90. data/test-unit/sample/adder.rb +0 -13
  91. data/test-unit/sample/subtracter.rb +0 -12
  92. data/test-unit/sample/test_adder.rb +0 -20
  93. data/test-unit/sample/test_subtracter.rb +0 -20
  94. data/test-unit/sample/test_user.rb +0 -23
  95. data/test-unit/test/collector/test-descendant.rb +0 -133
  96. data/test-unit/test/collector/test-load.rb +0 -442
  97. data/test-unit/test/collector/test_dir.rb +0 -406
  98. data/test-unit/test/collector/test_objectspace.rb +0 -100
  99. data/test-unit/test/run-test.rb +0 -22
  100. data/test-unit/test/test-assertions.rb +0 -1849
  101. data/test-unit/test/test-attribute.rb +0 -86
  102. data/test-unit/test/test-color-scheme.rb +0 -69
  103. data/test-unit/test/test-color.rb +0 -47
  104. data/test-unit/test/test-diff.rb +0 -518
  105. data/test-unit/test/test-emacs-runner.rb +0 -60
  106. data/test-unit/test/test-fixture.rb +0 -324
  107. data/test-unit/test/test-notification.rb +0 -33
  108. data/test-unit/test/test-omission.rb +0 -81
  109. data/test-unit/test/test-pending.rb +0 -70
  110. data/test-unit/test/test-priority.rb +0 -119
  111. data/test-unit/test/test-testcase.rb +0 -569
  112. data/test-unit/test/test_error.rb +0 -26
  113. data/test-unit/test/test_failure.rb +0 -33
  114. data/test-unit/test/test_testresult.rb +0 -113
  115. data/test-unit/test/test_testsuite.rb +0 -137
  116. data/test-unit/test/testunit-test-util.rb +0 -14
  117. data/test-unit/test/ui/test_tap.rb +0 -33
  118. data/test-unit/test/ui/test_testrunmediator.rb +0 -20
  119. data/test-unit/test/util/test-method-owner-finder.rb +0 -38
  120. data/test-unit/test/util/test-output.rb +0 -11
  121. data/test-unit/test/util/test_backtracefilter.rb +0 -41
  122. data/test-unit/test/util/test_observable.rb +0 -102
  123. data/test-unit/test/util/test_procwrapper.rb +0 -36
Binary file
@@ -1,328 +0,0 @@
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.
94
- #
95
- # Exception: lib/test/unit/diff.rb is copyright (c)
96
- # 2008-2010 Kouhei Sutou and 2001-2008 Python Software
97
- # Foundation. It is free software, and is distributed
98
- # under the Ruby license and/or the PSF license. See the
99
- # COPYING file and PSFL file.
100
- #
101
- # == Warranty
102
- #
103
- # This software is provided "as is" and without any express or
104
- # implied warranties, including, without limitation, the implied
105
- # warranties of merchantibility and fitness for a particular
106
- # purpose.
107
- #
108
- #
109
- # == Author
110
- #
111
- # Nathaniel Talbott.
112
- # Copyright (c) 2000-2003, Nathaniel Talbott
113
- #
114
- # ----
115
- #
116
- # = Usage
117
- #
118
- # The general idea behind unit testing is that you write a _test_
119
- # _method_ that makes certain _assertions_ about your code, working
120
- # against a _test_ _fixture_. A bunch of these _test_ _methods_ are
121
- # bundled up into a _test_ _suite_ and can be run any time the
122
- # developer wants. The results of a run are gathered in a _test_
123
- # _result_ and displayed to the user through some UI. So, lets break
124
- # this down and see how Test::Unit provides each of these necessary
125
- # pieces.
126
- #
127
- #
128
- # == Assertions
129
- #
130
- # These are the heart of the framework. Think of an assertion as a
131
- # statement of expected outcome, i.e. "I assert that x should be equal
132
- # to y". If, when the assertion is executed, it turns out to be
133
- # correct, nothing happens, and life is good. If, on the other hand,
134
- # your assertion turns out to be false, an error is propagated with
135
- # pertinent information so that you can go back and make your
136
- # assertion succeed, and, once again, life is good. For an explanation
137
- # of the current assertions, see Test::Unit::Assertions.
138
- #
139
- #
140
- # == Test Method & Test Fixture
141
- #
142
- # Obviously, these assertions have to be called within a context that
143
- # knows about them and can do something meaningful with their
144
- # pass/fail value. Also, it's handy to collect a bunch of related
145
- # tests, each test represented by a method, into a common test class
146
- # that knows how to run them. The tests will be in a separate class
147
- # from the code they're testing for a couple of reasons. First of all,
148
- # it allows your code to stay uncluttered with test code, making it
149
- # easier to maintain. Second, it allows the tests to be stripped out
150
- # for deployment, since they're really there for you, the developer,
151
- # and your users don't need them. Third, and most importantly, it
152
- # allows you to set up a common test fixture for your tests to run
153
- # against.
154
- #
155
- # What's a test fixture? Well, tests do not live in a vacuum; rather,
156
- # they're run against the code they are testing. Often, a collection
157
- # of tests will run against a common set of data, also called a
158
- # fixture. If they're all bundled into the same test class, they can
159
- # all share the setting up and tearing down of that data, eliminating
160
- # unnecessary duplication and making it much easier to add related
161
- # tests.
162
- #
163
- # Test::Unit::TestCase wraps up a collection of test methods together
164
- # and allows you to easily set up and tear down the same test fixture
165
- # for each test. This is done by overriding #setup and/or #teardown,
166
- # which will be called before and after each test method that is
167
- # run. The TestCase also knows how to collect the results of your
168
- # assertions into a Test::Unit::TestResult, which can then be reported
169
- # back to you... but I'm getting ahead of myself. To write a test,
170
- # follow these steps:
171
- #
172
- # * Make sure Test::Unit is in your library path.
173
- # * require 'test/unit' in your test script.
174
- # * Create a class that subclasses Test::Unit::TestCase.
175
- # * Add a method that begins with "test" to your class.
176
- # * Make assertions in your test method.
177
- # * Optionally define #setup and/or #teardown to set up and/or tear
178
- # down your common test fixture.
179
- # * You can now run your test as you would any other Ruby
180
- # script... try it and see!
181
- #
182
- # A really simple test might look like this (#setup and #teardown are
183
- # commented out to indicate that they are completely optional):
184
- #
185
- # require 'test/unit'
186
- #
187
- # class MyTest < Test::Unit::TestCase
188
- # # def setup
189
- # # end
190
- #
191
- # # def teardown
192
- # # end
193
- #
194
- # def test_fail
195
- # assert(false, 'Assertion was false.')
196
- # end
197
- # end
198
- #
199
- #
200
- # == Test Runners
201
- #
202
- # So, now you have this great test class, but you still
203
- # need a way to run it and view any failures that occur
204
- # during the run. There are some test runner; console test
205
- # runner, GTK+ test runner and so on. The console test
206
- # runner is automatically invoked for you if you require
207
- # 'test/unit' and simply run the file. To use another
208
- # runner simply set default test runner ID to
209
- # Test::Unit::AutoRunner:
210
- #
211
- # require 'test/unit'
212
- # Test::Unit::AutoRunner.default_runner = "gtk2"
213
- #
214
- # == Test Suite
215
- #
216
- # As more and more unit tests accumulate for a given project, it
217
- # becomes a real drag running them one at a time, and it also
218
- # introduces the potential to overlook a failing test because you
219
- # forget to run it. Suddenly it becomes very handy that the
220
- # TestRunners can take any object that returns a Test::Unit::TestSuite
221
- # in response to a suite method. The TestSuite can, in turn, contain
222
- # other TestSuites or individual tests (typically created by a
223
- # TestCase). In other words, you can easily wrap up a group of
224
- # TestCases and TestSuites.
225
- #
226
- # Test::Unit does a little bit more for you, by wrapping
227
- # these up automatically when you require
228
- # 'test/unit'. What does this mean? It means you could
229
- # write the above test case like this instead:
230
- #
231
- # require 'test/unit'
232
- # require 'test_myfirsttests'
233
- # require 'test_moretestsbyme'
234
- # require 'test_anothersetoftests'
235
- #
236
- # Test::Unit is smart enough to find all the test cases existing in
237
- # the ObjectSpace and wrap them up into a suite for you. It then runs
238
- # the dynamic suite using the console TestRunner.
239
- #
240
- #
241
- # == Configuration file
242
- #
243
- # Test::Unit reads 'test-unit.yml' in the current working
244
- # directory as Test::Unit's configuration file. It can
245
- # contain the following configurations:
246
- #
247
- # * color scheme definitions
248
- # * test runner to be used
249
- # * test runner options
250
- # * test collector to be used
251
- #
252
- # Except color scheme definitions, all of them are
253
- # specified by command line option.
254
- #
255
- # Here are sample color scheme definitions:
256
- #
257
- # color_schemes:
258
- # inverted:
259
- # success:
260
- # name: red
261
- # bold: true
262
- # failure:
263
- # name: green
264
- # bold: true
265
- # other_scheme:
266
- # ...
267
- #
268
- # Here are the syntax of color scheme definitions:
269
- #
270
- # color_schemes:
271
- # SCHEME_NAME:
272
- # EVENT_NAME:
273
- # name: COLOR_NAME
274
- # intensity: BOOLEAN
275
- # bold: BOOLEAN
276
- # italic: BOOLEAN
277
- # underline: BOOLEAN
278
- # ...
279
- # ...
280
- #
281
- # SCHEME_NAME:: the name of the color scheme
282
- # EVENT_NAME:: one of [success, failure, pending,
283
- # omission, notification, error]
284
- # COLOR_NAME:: one of [black, red, green, yellow, blue,
285
- # magenta, cyan, white]
286
- # BOOLEAN:: true or false
287
- #
288
- # You can use the above 'inverted' color scheme with the
289
- # following configuration:
290
- #
291
- # runner: console
292
- # console_options:
293
- # color_scheme: inverted
294
- # color_schemes:
295
- # inverted:
296
- # success:
297
- # name: red
298
- # bold: true
299
- # failure:
300
- # name: green
301
- # bold: true
302
- #
303
- # == Questions?
304
- #
305
- # I'd really like to get feedback from all levels of Ruby
306
- # practitioners about typos, grammatical errors, unclear statements,
307
- # missing points, etc., in this document (or any other).
308
- #
309
-
310
- module Unit
311
- # Set true when Test::Unit has run. If set to true Test::Unit
312
- # will not automatically run at exit.
313
- def self.run=(flag)
314
- @run = flag
315
- end
316
-
317
- # Already tests have run?
318
- def self.run?
319
- @run ||= false
320
- end
321
- end
322
- end
323
-
324
- at_exit do
325
- unless $! || Test::Unit.run?
326
- exit Test::Unit::AutoRunner.run
327
- end
328
- end
@@ -1,25 +0,0 @@
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
- attr_accessor :expected, :actual, :user_message
13
- attr_accessor :inspected_expected, :inspected_actual
14
- def initialize(message=nil, options=nil)
15
- options ||= {}
16
- @expected = options[:expected]
17
- @actual = options[:actual]
18
- @inspected_expected = options[:inspected_expected]
19
- @inspected_actual = options[:inspected_actual]
20
- @user_message = options[:user_message]
21
- super(message)
22
- end
23
- end
24
- end
25
- end
@@ -1,1792 +0,0 @@
1
- # Author:: Nathaniel Talbott.
2
- # Copyright:: Copyright (c) 2000-2003 Nathaniel Talbott. All rights reserved.
3
- # Copyright (c) 2009-2010 Kouhei Sutou. All rights reserved.
4
- # License:: Ruby license.
5
-
6
- require 'test/unit/assertionfailederror'
7
- require 'test/unit/util/backtracefilter'
8
- require 'test/unit/util/method-owner-finder'
9
- require 'test/unit/diff'
10
-
11
- module Test
12
- module Unit
13
-
14
- ##
15
- # Test::Unit::Assertions contains the standard Test::Unit assertions.
16
- # Assertions is included in Test::Unit::TestCase.
17
- #
18
- # To include it in your own code and use its functionality, you simply
19
- # need to rescue Test::Unit::AssertionFailedError. Additionally you may
20
- # override add_assertion to get notified whenever an assertion is made.
21
- #
22
- # Notes:
23
- # * The message to each assertion, if given, will be propagated with the
24
- # failure.
25
- # * It is easy to add your own assertions based on assert_block().
26
- #
27
- # = Example Custom Assertion
28
- #
29
- # def deny(boolean, message = nil)
30
- # message = build_message message, '<?> is not false or nil.', boolean
31
- # assert_block message do
32
- # not boolean
33
- # end
34
- # end
35
-
36
- module Assertions
37
-
38
- ##
39
- # The assertion upon which all other assertions are based. Passes if the
40
- # block yields true.
41
- #
42
- # Example:
43
- # assert_block "Couldn't do the thing" do
44
- # do_the_thing
45
- # end
46
-
47
- public
48
- def assert_block(message="assert_block failed.") # :yields:
49
- _wrap_assertion do
50
- if (! yield)
51
- raise AssertionFailedError.new(message.to_s)
52
- end
53
- end
54
- end
55
-
56
- ##
57
- # Asserts that +boolean+ is not false or nil.
58
- #
59
- # Example:
60
- # assert [1, 2].include?(5)
61
-
62
- public
63
- def assert(boolean, message=nil)
64
- _wrap_assertion do
65
- case message
66
- when nil, String, Proc
67
- else
68
- error_message = "assertion message must be String or Proc: "
69
- error_message << "<#{message.inspect}>(<#{message.class}>)"
70
- raise ArgumentError, error_message, filter_backtrace(caller)
71
- end
72
- assert_block("assert should not be called with a block.") do
73
- !block_given?
74
- end
75
- assert_block(build_message(message, "<?> is not true.", boolean)) do
76
- boolean
77
- end
78
- end
79
- end
80
-
81
- ##
82
- # Passes if +expected+ == +actual.
83
- #
84
- # Note that the ordering of arguments is important, since a helpful
85
- # error message is generated when this one fails that tells you the
86
- # values of expected and actual.
87
- #
88
- # Example:
89
- # assert_equal 'MY STRING', 'my string'.upcase
90
-
91
- public
92
- def assert_equal(expected, actual, message=nil)
93
- diff = AssertionMessage.delayed_diff(expected, actual)
94
- if expected.respond_to?(:encoding) and
95
- actual.respond_to?(:encoding) and
96
- expected.encoding != actual.encoding
97
- format = <<EOT
98
- <?>(?) expected but was
99
- <?>(?).?
100
- EOT
101
- full_message = build_message(message, format,
102
- expected, expected.encoding.name,
103
- actual, actual.encoding.name,
104
- diff)
105
- else
106
- full_message = build_message(message, <<EOT, expected, actual, diff)
107
- <?> expected but was
108
- <?>.?
109
- EOT
110
- end
111
- begin
112
- assert_block(full_message) { expected == actual }
113
- rescue AssertionFailedError => failure
114
- failure.expected = expected
115
- failure.actual = actual
116
- failure.inspected_expected = AssertionMessage.convert(expected)
117
- failure.inspected_actual = AssertionMessage.convert(actual)
118
- failure.user_message = message
119
- raise
120
- end
121
- end
122
-
123
- ##
124
- # Passes if the block raises one of the expected
125
- # exceptions. When an expected exception is an Exception
126
- # object, passes if expected_exception == actual_exception.
127
- #
128
- # Example:
129
- # assert_raise(RuntimeError, LoadError) do
130
- # raise 'Boom!!!'
131
- # end # -> pass
132
- #
133
- # assert_raise do
134
- # raise Exception, 'Any exception should be raised!!!'
135
- # end # -> pass
136
- #
137
- # assert_raise(RuntimeError.new("XXX")) {raise "XXX"} # -> pass
138
- # assert_raise(MyError.new("XXX")) {raise "XXX"} # -> fail
139
- # assert_raise(RuntimeError.new("ZZZ")) {raise "XXX"} # -> fail
140
- public
141
- def assert_raise(*args, &block)
142
- assert_expected_exception = Proc.new do |*_args|
143
- message, assert_exception_helper, actual_exception = _args
144
- expected = assert_exception_helper.expected_exceptions
145
- full_message = build_message(message,
146
- "<?> exception expected but was\n?",
147
- expected, actual_exception)
148
- assert_block(full_message) do
149
- expected == [] or assert_exception_helper.expected?(actual_exception)
150
- end
151
- end
152
- _assert_raise(assert_expected_exception, *args, &block)
153
- end
154
-
155
- ##
156
- # Alias of assert_raise.
157
- #
158
- # Will be deprecated in 1.9, and removed in 2.0.
159
-
160
- public
161
- def assert_raises(*args, &block)
162
- assert_raise(*args, &block)
163
- end
164
-
165
- ##
166
- # Passes if the block raises one of the given
167
- # exceptions or sub exceptions of the given exceptions.
168
- #
169
- # Example:
170
- # assert_raise_kind_of(SystemCallError) do
171
- # raise Errno::EACCES
172
- # end
173
- def assert_raise_kind_of(*args, &block)
174
- assert_expected_exception = Proc.new do |*_args|
175
- message, assert_exception_helper, actual_exception = _args
176
- expected = assert_exception_helper.expected_exceptions
177
- full_message = build_message(message,
178
- "<?> family exception expected " +
179
- "but was\n?",
180
- expected, actual_exception)
181
- assert_block(full_message) do
182
- assert_exception_helper.expected?(actual_exception, :kind_of?)
183
- end
184
- end
185
- _assert_raise(assert_expected_exception, *args, &block)
186
- end
187
-
188
-
189
- ##
190
- # Passes if +object+.instance_of?(+klass+). When +klass+ is
191
- # an array of classes, it passes if any class
192
- # satisfies +object.instance_of?(class).
193
- #
194
- # Example:
195
- # assert_instance_of(String, 'foo') # -> pass
196
- # assert_instance_of([Fixnum, NilClass], 100) # -> pass
197
- # assert_instance_of([Numeric, NilClass], 100) # -> fail
198
-
199
- public
200
- def assert_instance_of(klass, object, message="")
201
- _wrap_assertion do
202
- klasses = nil
203
- klasses = klass if klass.is_a?(Array)
204
- assert_block("The first parameter to assert_instance_of should be " +
205
- "a Class or an Array of Class.") do
206
- if klasses
207
- klasses.all? {|k| k.is_a?(Class)}
208
- else
209
- klass.is_a?(Class)
210
- end
211
- end
212
- klass_message = AssertionMessage.maybe_container(klass) do |value|
213
- "<#{value}>"
214
- end
215
- full_message = build_message(message, <<EOT, object, klass_message, object.class)
216
- <?> expected to be an instance of
217
- ? but was
218
- <?>.
219
- EOT
220
- assert_block(full_message) do
221
- if klasses
222
- klasses.any? {|k| object.instance_of?(k)}
223
- else
224
- object.instance_of?(klass)
225
- end
226
- end
227
- end
228
- end
229
-
230
- ##
231
- # Passes if +object+ is nil.
232
- #
233
- # Example:
234
- # assert_nil [1, 2].uniq!
235
-
236
- public
237
- def assert_nil(object, message="")
238
- full_message = build_message(message, <<EOT, object)
239
- <?> expected to be nil.
240
- EOT
241
- assert_block(full_message) { object.nil? }
242
- end
243
-
244
- ##
245
- # Passes if +object+.kind_of?(+klass+). When +klass+ is
246
- # an array of classes or modules, it passes if any
247
- # class or module satisfies +object.kind_of?(class_or_module).
248
- #
249
- # Example:
250
- # assert_kind_of(Object, 'foo') # -> pass
251
- # assert_kind_of([Fixnum, NilClass], 100) # -> pass
252
- # assert_kind_of([Fixnum, NilClass], "string") # -> fail
253
-
254
- public
255
- def assert_kind_of(klass, object, message="")
256
- _wrap_assertion do
257
- klasses = nil
258
- klasses = klass if klass.is_a?(Array)
259
- assert_block("The first parameter to assert_kind_of should be " +
260
- "a kind_of Module or an Array of a kind_of Module.") do
261
- if klasses
262
- klasses.all? {|k| k.kind_of?(Module)}
263
- else
264
- klass.kind_of?(Module)
265
- end
266
- end
267
- klass_message = AssertionMessage.maybe_container(klass) do |value|
268
- "<#{value}>"
269
- end
270
- full_message = build_message(message,
271
- "<?> expected to be kind_of\\?\n" +
272
- "? but was\n" +
273
- "<?>.",
274
- object,
275
- klass_message,
276
- object.class)
277
- assert_block(full_message) do
278
- if klasses
279
- klasses.any? {|k| object.kind_of?(k)}
280
- else
281
- object.kind_of?(klass)
282
- end
283
- end
284
- end
285
- end
286
-
287
- ##
288
- # Passes if +object+ .respond_to? +method+
289
- #
290
- # Example:
291
- # assert_respond_to 'bugbear', :slice
292
-
293
- public
294
- def assert_respond_to(object, method, message="")
295
- _wrap_assertion do
296
- full_message = build_message(message,
297
- "<?>.kind_of\\?(Symbol) or\n" +
298
- "<?>.respond_to\\?(:to_str) expected",
299
- method, method)
300
- assert_block(full_message) do
301
- method.kind_of?(Symbol) or method.respond_to?(:to_str)
302
- end
303
- full_message = build_message(message,
304
- "<?>.respond_to\\?(?) expected\n" +
305
- "(Class: <?>)",
306
- object, method, object.class)
307
- assert_block(full_message) {object.respond_to?(method)}
308
- end
309
- end
310
-
311
- ##
312
- # Passes if +object+ does not .respond_to? +method+.
313
- #
314
- # Example:
315
- # assert_not_respond_to('bugbear', :nonexistence) # -> pass
316
- # assert_not_respond_to('bugbear', :size) # -> fail
317
-
318
- public
319
- def assert_not_respond_to(object, method, message="")
320
- _wrap_assertion do
321
- full_message = build_message(message,
322
- "<?>.kind_of\\?(Symbol) or\n" +
323
- "<?>.respond_to\\?(:to_str) expected",
324
- method, method)
325
- assert_block(full_message) do
326
- method.kind_of?(Symbol) or method.respond_to?(:to_str)
327
- end
328
- full_message = build_message(message,
329
- "!<?>.respond_to\\?(?) expected\n" +
330
- "(Class: <?>)",
331
- object, method, object.class)
332
- assert_block(full_message) {!object.respond_to?(method)}
333
- end
334
- end
335
-
336
- ##
337
- # Passes if +string+ =~ +pattern+.
338
- #
339
- # Example:
340
- # assert_match(/\d+/, 'five, 6, seven')
341
-
342
- public
343
- def assert_match(pattern, string, message="")
344
- _wrap_assertion do
345
- pattern = case(pattern)
346
- when String
347
- Regexp.new(Regexp.escape(pattern))
348
- else
349
- pattern
350
- end
351
- full_message = build_message(message, "<?> expected to be =~\n<?>.", string, pattern)
352
- assert_block(full_message) { string =~ pattern }
353
- end
354
- end
355
-
356
- ##
357
- # Passes if +actual+ .equal? +expected+ (i.e. they are the same
358
- # instance).
359
- #
360
- # Example:
361
- # o = Object.new
362
- # assert_same o, o
363
-
364
- public
365
- def assert_same(expected, actual, message="")
366
- full_message = build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__)
367
- <?>
368
- with id <?> expected to be equal\\? to
369
- <?>
370
- with id <?>.
371
- EOT
372
- assert_block(full_message) { actual.equal?(expected) }
373
- end
374
-
375
- ##
376
- # Compares the +object1+ with +object2+ using +operator+.
377
- #
378
- # Passes if object1.__send__(operator, object2) is true.
379
- #
380
- # Example:
381
- # assert_operator 5, :>=, 4
382
-
383
- public
384
- def assert_operator(object1, operator, object2, message="")
385
- _wrap_assertion do
386
- full_message = build_message(nil, "<?>\ngiven as the operator for #assert_operator must be a Symbol or #respond_to\\?(:to_str).", operator)
387
- assert_block(full_message){operator.kind_of?(Symbol) || operator.respond_to?(:to_str)}
388
- full_message = build_message(message, <<EOT, object1, AssertionMessage.literal(operator), object2)
389
- <?> expected to be
390
- ?
391
- <?>.
392
- EOT
393
- assert_block(full_message) { object1.__send__(operator, object2) }
394
- end
395
- end
396
-
397
- ##
398
- # Passes if block does not raise an exception.
399
- #
400
- # Example:
401
- # assert_nothing_raised do
402
- # [1, 2].uniq
403
- # end
404
-
405
- public
406
- def assert_nothing_raised(*args)
407
- _wrap_assertion do
408
- if args.last.is_a?(String)
409
- message = args.pop
410
- else
411
- message = ""
412
- end
413
-
414
- assert_exception_helper = AssertExceptionHelper.new(self, args)
415
- begin
416
- yield
417
- rescue Exception => e
418
- if ((args.empty? && !e.instance_of?(AssertionFailedError)) ||
419
- assert_exception_helper.expected?(e))
420
- failure_message = build_message(message, "Exception raised:\n?", e)
421
- assert_block(failure_message) {false}
422
- else
423
- raise
424
- end
425
- end
426
- nil
427
- end
428
- end
429
-
430
- ##
431
- # Flunk always fails.
432
- #
433
- # Example:
434
- # flunk 'Not done testing yet.'
435
-
436
- public
437
- def flunk(message="Flunked")
438
- assert_block(build_message(message)){false}
439
- end
440
-
441
- ##
442
- # Passes if ! +actual+ .equal? +expected+
443
- #
444
- # Example:
445
- # assert_not_same Object.new, Object.new
446
-
447
- public
448
- def assert_not_same(expected, actual, message="")
449
- full_message = build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__)
450
- <?>
451
- with id <?> expected to not be equal\\? to
452
- <?>
453
- with id <?>.
454
- EOT
455
- assert_block(full_message) { !actual.equal?(expected) }
456
- end
457
-
458
- ##
459
- # Passes if +expected+ != +actual+
460
- #
461
- # Example:
462
- # assert_not_equal 'some string', 5
463
-
464
- public
465
- def assert_not_equal(expected, actual, message="")
466
- full_message = build_message(message, "<?> expected to be != to\n<?>.", expected, actual)
467
- assert_block(full_message) { expected != actual }
468
- end
469
-
470
- ##
471
- # Passes if ! +object+ .nil?
472
- #
473
- # Example:
474
- # assert_not_nil '1 two 3'.sub!(/two/, '2')
475
-
476
- public
477
- def assert_not_nil(object, message="")
478
- full_message = build_message(message, "<?> expected to not be nil.", object)
479
- assert_block(full_message){!object.nil?}
480
- end
481
-
482
- ##
483
- # Passes if +regexp+ !~ +string+
484
- #
485
- # Example:
486
- # assert_not_match(/two/, 'one 2 three') # -> pass
487
- # assert_not_match(/three/, 'one 2 three') # -> fail
488
-
489
- public
490
- def assert_not_match(regexp, string, message="")
491
- _wrap_assertion do
492
- assert_instance_of(Regexp, regexp,
493
- "<REGEXP> in assert_not_match(<REGEXP>, ...) " +
494
- "should be a Regexp.")
495
- full_message = build_message(message,
496
- "<?> expected to not match\n<?>.",
497
- regexp, string)
498
- assert_block(full_message) { regexp !~ string }
499
- end
500
- end
501
-
502
- ##
503
- # Deprecated. Use #assert_not_match instead.
504
- #
505
- # Passes if +regexp+ !~ +string+
506
- #
507
- # Example:
508
- # assert_no_match(/two/, 'one 2 three') # -> pass
509
- # assert_no_match(/three/, 'one 2 three') # -> fail
510
-
511
- public
512
- def assert_no_match(regexp, string, message="")
513
- _wrap_assertion do
514
- assert_instance_of(Regexp, regexp,
515
- "The first argument to assert_no_match " +
516
- "should be a Regexp.")
517
- assert_not_match(regexp, string, message)
518
- end
519
- end
520
-
521
- UncaughtThrow = {
522
- NameError => /^uncaught throw \`(.+)\'$/, #`
523
- ArgumentError => /^uncaught throw (.+)$/,
524
- ThreadError => /^uncaught throw \`(.+)\' in thread / #`
525
- }
526
-
527
- ##
528
- # Passes if the block throws +expected_object+
529
- #
530
- # Example:
531
- # assert_throw(:done) do
532
- # throw(:done)
533
- # end
534
-
535
- public
536
- def assert_throw(expected_object, message="", &proc)
537
- _wrap_assertion do
538
- begin
539
- catch([]) {}
540
- rescue TypeError
541
- assert_instance_of(Symbol, expected_object,
542
- "assert_throws expects the symbol that should be thrown for its first argument")
543
- end
544
- assert_block("Should have passed a block to assert_throw.") do
545
- block_given?
546
- end
547
- caught = true
548
- begin
549
- catch(expected_object) do
550
- proc.call
551
- caught = false
552
- end
553
- full_message = build_message(message,
554
- "<?> should have been thrown.",
555
- expected_object)
556
- assert_block(full_message) {caught}
557
- rescue NameError, ArgumentError, ThreadError => error
558
- raise unless UncaughtThrow[error.class] =~ error.message
559
- tag = $1
560
- tag = tag[1..-1].intern if tag[0, 1] == ":"
561
- full_message = build_message(message,
562
- "<?> expected to be thrown but\n" +
563
- "<?> was thrown.",
564
- expected_object, tag)
565
- flunk(full_message)
566
- end
567
- end
568
- end
569
-
570
- ##
571
- # Alias of assert_throw.
572
- #
573
- # Will be deprecated in 1.9, and removed in 2.0.
574
- def assert_throws(*args, &block)
575
- assert_throw(*args, &block)
576
- end
577
-
578
- ##
579
- # Passes if block does not throw anything.
580
- #
581
- # Example:
582
- # assert_nothing_thrown do
583
- # [1, 2].uniq
584
- # end
585
-
586
- public
587
- def assert_nothing_thrown(message="", &proc)
588
- _wrap_assertion do
589
- assert(block_given?, "Should have passed a block to assert_nothing_thrown")
590
- begin
591
- proc.call
592
- rescue NameError, ArgumentError, ThreadError => error
593
- raise unless UncaughtThrow[error.class] =~ error.message
594
- tag = $1
595
- tag = tag[1..-1].intern if tag[0, 1] == ":"
596
- full_message = build_message(message,
597
- "<?> was thrown when nothing was expected",
598
- tag)
599
- flunk(full_message)
600
- end
601
- assert(true, "Expected nothing to be thrown")
602
- end
603
- end
604
-
605
- ##
606
- # Passes if +expected_float+ and +actual_float+ are equal
607
- # within +delta+ tolerance.
608
- #
609
- # Example:
610
- # assert_in_delta 0.05, (50000.0 / 10**6), 0.00001
611
-
612
- public
613
- def assert_in_delta(expected_float, actual_float, delta=0.001, message="")
614
- _wrap_assertion do
615
- _assert_in_delta_validate_arguments(expected_float,
616
- actual_float,
617
- delta)
618
- full_message = _assert_in_delta_message(expected_float,
619
- actual_float,
620
- delta,
621
- message)
622
- assert_block(full_message) do
623
- (expected_float.to_f - actual_float.to_f).abs <= delta.to_f
624
- end
625
- end
626
- end
627
-
628
- ##
629
- # Passes if +expected_float+ and +actual_float+ are
630
- # not equal within +delta+ tolerance.
631
- #
632
- # Example:
633
- # assert_not_in_delta(0.05, (50000.0 / 10**6), 0.00002) # -> pass
634
- # assert_not_in_delta(0.05, (50000.0 / 10**6), 0.00001) # -> fail
635
-
636
- public
637
- def assert_not_in_delta(expected_float, actual_float, delta=0.001, message="")
638
- _wrap_assertion do
639
- _assert_in_delta_validate_arguments(expected_float,
640
- actual_float,
641
- delta)
642
- full_message = _assert_in_delta_message(expected_float,
643
- actual_float,
644
- delta,
645
- message,
646
- :negative_assertion => true)
647
- assert_block(full_message) do
648
- (expected_float.to_f - actual_float.to_f).abs > delta.to_f
649
- end
650
- end
651
- end
652
-
653
- # :stopdoc:
654
- private
655
- def _assert_in_delta_validate_arguments(expected_float,
656
- actual_float,
657
- delta)
658
- {
659
- expected_float => "first float",
660
- actual_float => "second float",
661
- delta => "delta"
662
- }.each do |float, name|
663
- assert_respond_to(float, :to_f,
664
- "The arguments must respond to to_f; " +
665
- "the #{name} did not")
666
- end
667
- delta = delta.to_f
668
- assert_operator(delta, :>=, 0.0, "The delta should not be negative")
669
- end
670
-
671
- def _assert_in_delta_message(expected_float, actual_float, delta,
672
- message, options={})
673
- if options[:negative_assertion]
674
- format = <<-EOT
675
- <?> -/+ <?> expected to not include
676
- <?>.
677
- EOT
678
- else
679
- format = <<-EOT
680
- <?> -/+ <?> expected to include
681
- <?>.
682
- EOT
683
- end
684
- arguments = [expected_float, delta, actual_float]
685
- normalized_expected = expected_float.to_f
686
- normalized_actual = actual_float.to_f
687
- normalized_delta = delta.to_f
688
- relation_format = nil
689
- relation_arguments = nil
690
- if normalized_actual < normalized_expected - normalized_delta
691
- relation_format = "<<?> < <?>-<?>[?] <= <?>+<?>[?]>"
692
- relation_arguments = [actual_float,
693
- expected_float, delta,
694
- normalized_expected - normalized_delta,
695
- expected_float, delta,
696
- normalized_expected + normalized_delta]
697
- elsif normalized_actual <= normalized_expected + normalized_delta
698
- relation_format = "<<?>-<?>[?] <= <?> <= <?>+<?>[?]>"
699
- relation_arguments = [expected_float, delta,
700
- normalized_expected - normalized_delta,
701
- actual_float,
702
- expected_float, delta,
703
- normalized_expected + normalized_delta]
704
- else
705
- relation_format = "<<?>-<?>[?] <= <?>+<?>[?] < <?>>"
706
- relation_arguments = [expected_float, delta,
707
- normalized_expected - normalized_delta,
708
- expected_float, delta,
709
- normalized_expected + normalized_delta,
710
- actual_float]
711
- end
712
-
713
- if relation_format
714
- format << <<-EOT
715
-
716
- Relation:
717
- #{relation_format}
718
- EOT
719
- arguments.concat(relation_arguments)
720
- end
721
-
722
- build_message(message, format, *arguments)
723
- end
724
-
725
- public
726
- # :startdoc:
727
-
728
- ##
729
- # Passes if +expected_float+ and +actual_float+ are equal
730
- # within +epsilon+ relative error of +expected_float+.
731
- #
732
- # Example:
733
- # assert_in_epsilon(10000.0, 9900.0, 0.1) # -> pass
734
- # assert_in_epsilon(10000.0, 9899.0, 0.1) # -> fail
735
-
736
- public
737
- def assert_in_epsilon(expected_float, actual_float, epsilon=0.001,
738
- message="")
739
- _wrap_assertion do
740
- _assert_in_epsilon_validate_arguments(expected_float,
741
- actual_float,
742
- epsilon)
743
- full_message = _assert_in_epsilon_message(expected_float,
744
- actual_float,
745
- epsilon,
746
- message)
747
- assert_block(full_message) do
748
- normalized_expected_float = expected_float.to_f
749
- delta = normalized_expected_float * epsilon.to_f
750
- (normalized_expected_float - actual_float.to_f).abs <= delta
751
- end
752
- end
753
- end
754
-
755
- ##
756
- # Passes if +expected_float+ and +actual_float+ are
757
- # not equal within +epsilon+ relative error of
758
- # +expected_float+.
759
- #
760
- # Example:
761
- # assert_not_in_epsilon(10000.0, 9900.0, 0.1) # -> fail
762
- # assert_not_in_epsilon(10000.0, 9899.0, 0.1) # -> pass
763
-
764
- public
765
- def assert_not_in_epsilon(expected_float, actual_float, epsilon=0.001,
766
- message="")
767
- _wrap_assertion do
768
- _assert_in_epsilon_validate_arguments(expected_float,
769
- actual_float,
770
- epsilon)
771
- full_message = _assert_in_epsilon_message(expected_float,
772
- actual_float,
773
- epsilon,
774
- message,
775
- :negative_assertion => true)
776
- assert_block(full_message) do
777
- normalized_expected_float = expected_float.to_f
778
- delta = normalized_expected_float * epsilon.to_f
779
- (normalized_expected_float - actual_float.to_f).abs > delta
780
- end
781
- end
782
- end
783
-
784
- # :stopdoc:
785
- private
786
- def _assert_in_epsilon_validate_arguments(expected_float,
787
- actual_float,
788
- epsilon)
789
- {
790
- expected_float => "first float",
791
- actual_float => "second float",
792
- epsilon => "epsilon"
793
- }.each do |float, name|
794
- assert_respond_to(float, :to_f,
795
- "The arguments must respond to to_f; " +
796
- "the #{name} did not")
797
- end
798
- epsilon = epsilon.to_f
799
- assert_operator(epsilon, :>=, 0.0, "The epsilon should not be negative")
800
- end
801
-
802
- def _assert_in_epsilon_message(expected_float, actual_float, epsilon,
803
- message, options={})
804
- normalized_expected = expected_float.to_f
805
- normalized_actual = actual_float.to_f
806
- normalized_epsilon = epsilon.to_f
807
- delta = normalized_expected * normalized_epsilon
808
-
809
- if options[:negative_assertion]
810
- format = <<-EOT
811
- <?> -/+ (<?> * <?>)[?] expected to not include
812
- <?>.
813
- EOT
814
- else
815
- format = <<-EOT
816
- <?> -/+ (<?> * <?>)[?] expected to include
817
- <?>.
818
- EOT
819
- end
820
- arguments = [expected_float, expected_float, epsilon, delta,
821
- actual_float]
822
-
823
- relation_format = nil
824
- relation_arguments = nil
825
- if normalized_actual < normalized_expected - delta
826
- relation_format = "<<?> < <?>-(<?>*<?>)[?] <= <?>+(<?>*<?>)[?]>"
827
- relation_arguments = [actual_float,
828
- expected_float, expected_float, epsilon,
829
- normalized_expected - delta,
830
- expected_float, expected_float, epsilon,
831
- normalized_expected + delta]
832
- elsif normalized_actual <= normalized_expected + delta
833
- relation_format = "<<?>-(<?>*<?>)[?] <= <?> <= <?>+(<?>*<?>)[?]>"
834
- relation_arguments = [expected_float, expected_float, epsilon,
835
- normalized_expected - delta,
836
- actual_float,
837
- expected_float, expected_float, epsilon,
838
- normalized_expected + delta]
839
- else
840
- relation_format = "<<?>-(<?>*<?>)[?] <= <?>+(<?>*<?>)[?] < <?>>"
841
- relation_arguments = [expected_float, expected_float, epsilon,
842
- normalized_expected - delta,
843
- expected_float, expected_float, epsilon,
844
- normalized_expected + delta,
845
- actual_float]
846
- end
847
-
848
- if relation_format
849
- format << <<-EOT
850
-
851
- Relation:
852
- #{relation_format}
853
- EOT
854
- arguments.concat(relation_arguments)
855
- end
856
-
857
- build_message(message, format, *arguments)
858
- end
859
-
860
- public
861
- # :startdoc:
862
-
863
- ##
864
- # Passes if the method send returns a true value.
865
- #
866
- # +send_array+ is composed of:
867
- # * A receiver
868
- # * A method
869
- # * Arguments to the method
870
- #
871
- # Example:
872
- # assert_send([[1, 2], :member?, 1]) # -> pass
873
- # assert_send([[1, 2], :member?, 4]) # -> fail
874
-
875
- public
876
- def assert_send(send_array, message=nil)
877
- _wrap_assertion do
878
- assert_instance_of(Array, send_array,
879
- "assert_send requires an array " +
880
- "of send information")
881
- assert_operator(send_array.size, :>=, 2,
882
- "assert_send requires at least a receiver " +
883
- "and a message name")
884
- format = <<EOT
885
- <?> expected to respond to
886
- <?(*?)> with a true value but was
887
- <?>.
888
- EOT
889
- receiver, message_name, *arguments = send_array
890
- result = nil
891
- full_message =
892
- build_message(message,
893
- format,
894
- receiver,
895
- AssertionMessage.literal(message_name.to_s),
896
- arguments,
897
- AssertionMessage.delayed_literal {result})
898
- assert_block(full_message) do
899
- result = receiver.__send__(message_name, *arguments)
900
- result
901
- end
902
- end
903
- end
904
-
905
- ##
906
- # Passes if the method send doesn't return a true value.
907
- #
908
- # +send_array+ is composed of:
909
- # * A receiver
910
- # * A method
911
- # * Arguments to the method
912
- #
913
- # Example:
914
- # assert_not_send([[1, 2], :member?, 1]) # -> fail
915
- # assert_not_send([[1, 2], :member?, 4]) # -> pass
916
- def assert_not_send(send_array, message=nil)
917
- _wrap_assertion do
918
- assert_instance_of(Array, send_array,
919
- "assert_not_send requires an array " +
920
- "of send information")
921
- assert_operator(send_array.size, :>=, 2,
922
- "assert_not_send requires at least a receiver " +
923
- "and a message name")
924
- format = <<EOT
925
- <?> expected to respond to
926
- <?(*?)> with not a true value but was
927
- <?>.
928
- EOT
929
- receiver, message_name, *arguments = send_array
930
- result = nil
931
- full_message =
932
- build_message(message,
933
- format,
934
- receiver,
935
- AssertionMessage.literal(message_name.to_s),
936
- arguments,
937
- AssertionMessage.delayed_literal {result})
938
- assert_block(full_message) do
939
- result = receiver.__send__(message_name, *arguments)
940
- not result
941
- end
942
- end
943
- end
944
-
945
- ##
946
- # Passes if +actual+ is a boolean value.
947
- #
948
- # Example:
949
- # assert_boolean(true) # -> pass
950
- # assert_boolean(nil) # -> fail
951
- def assert_boolean(actual, message=nil)
952
- _wrap_assertion do
953
- assert_block(build_message(message,
954
- "<true> or <false> expected but was\n<?>",
955
- actual)) do
956
- [true, false].include?(actual)
957
- end
958
- end
959
- end
960
-
961
- ##
962
- # Passes if +actual+ is true.
963
- #
964
- # Example:
965
- # assert_true(true) # -> pass
966
- # assert_true(:true) # -> fail
967
- def assert_true(actual, message=nil)
968
- _wrap_assertion do
969
- assert_block(build_message(message,
970
- "<true> expected but was\n<?>",
971
- actual)) do
972
- actual == true
973
- end
974
- end
975
- end
976
-
977
- ##
978
- # Passes if +actual+ is false.
979
- #
980
- # Example:
981
- # assert_false(false) # -> pass
982
- # assert_false(nil) # -> fail
983
- def assert_false(actual, message=nil)
984
- _wrap_assertion do
985
- assert_block(build_message(message,
986
- "<false> expected but was\n<?>",
987
- actual)) do
988
- actual == false
989
- end
990
- end
991
- end
992
-
993
- ##
994
- # Passes if expression "+expected+ +operator+
995
- # +actual+" is true.
996
- #
997
- # Example:
998
- # assert_compare(1, "<", 10) # -> pass
999
- # assert_compare(1, ">=", 10) # -> fail
1000
- def assert_compare(expected, operator, actual, message=nil)
1001
- _wrap_assertion do
1002
- assert_send([["<", "<=", ">", ">="], :include?, operator.to_s])
1003
- case operator.to_s
1004
- when "<"
1005
- operator_description = "less than"
1006
- when "<="
1007
- operator_description = "less than or equal to"
1008
- when ">"
1009
- operator_description = "greater than"
1010
- when ">="
1011
- operator_description = "greater than or equal to"
1012
- end
1013
- template = <<-EOT
1014
- <?> #{operator} <?> should be true
1015
- <?> expected #{operator_description}
1016
- <?>.
1017
- EOT
1018
- full_message = build_message(message, template,
1019
- expected, actual,
1020
- expected, actual)
1021
- assert_block(full_message) do
1022
- expected.send(operator, actual)
1023
- end
1024
- end
1025
- end
1026
-
1027
- ##
1028
- # Passes if assertion is failed in block.
1029
- #
1030
- # Example:
1031
- # assert_fail_assertion {assert_equal("A", "B")} # -> pass
1032
- # assert_fail_assertion {assert_equal("A", "A")} # -> fail
1033
- def assert_fail_assertion(message=nil)
1034
- _wrap_assertion do
1035
- full_message = build_message(message,
1036
- "Failed assertion was expected.")
1037
- assert_block(full_message) do
1038
- begin
1039
- yield
1040
- false
1041
- rescue AssertionFailedError
1042
- true
1043
- end
1044
- end
1045
- end
1046
- end
1047
-
1048
- ##
1049
- # Passes if an exception is raised in block and its
1050
- # message is +expected+.
1051
- #
1052
- # Example:
1053
- # assert_raise_message("exception") {raise "exception"} # -> pass
1054
- # assert_raise_message(/exc/i) {raise "exception"} # -> pass
1055
- # assert_raise_message("exception") {raise "EXCEPTION"} # -> fail
1056
- # assert_raise_message("exception") {} # -> fail
1057
- def assert_raise_message(expected, message=nil)
1058
- _wrap_assertion do
1059
- full_message = build_message(message,
1060
- "<?> exception message expected " +
1061
- "but none was thrown.",
1062
- expected)
1063
- exception = nil
1064
- assert_block(full_message) do
1065
- begin
1066
- yield
1067
- false
1068
- rescue Exception => exception
1069
- true
1070
- end
1071
- end
1072
-
1073
- actual = exception.message
1074
- diff = AssertionMessage.delayed_diff(expected, actual)
1075
- full_message =
1076
- build_message(message,
1077
- "<?> exception message expected but was\n" +
1078
- "<?>.?", expected, actual, diff)
1079
- assert_block(full_message) do
1080
- if expected.is_a?(Regexp)
1081
- expected =~ actual
1082
- else
1083
- expected == actual
1084
- end
1085
- end
1086
- end
1087
- end
1088
-
1089
- ##
1090
- # Passes if +object+.const_defined?(+constant_name+)
1091
- #
1092
- # Example:
1093
- # assert_const_defined(Test, :Unit) # -> pass
1094
- # assert_const_defined(Object, :Nonexistent) # -> fail
1095
- def assert_const_defined(object, constant_name, message=nil)
1096
- _wrap_assertion do
1097
- full_message = build_message(message,
1098
- "<?>.const_defined\\?(<?>) expected.",
1099
- object, constant_name)
1100
- assert_block(full_message) do
1101
- object.const_defined?(constant_name)
1102
- end
1103
- end
1104
- end
1105
-
1106
- ##
1107
- # Passes if !+object+.const_defined?(+constant_name+)
1108
- #
1109
- # Example:
1110
- # assert_not_const_defined(Object, :Nonexistent) # -> pass
1111
- # assert_not_const_defined(Test, :Unit) # -> fail
1112
- def assert_not_const_defined(object, constant_name, message=nil)
1113
- _wrap_assertion do
1114
- full_message = build_message(message,
1115
- "!<?>.const_defined\\?(<?>) expected.",
1116
- object, constant_name)
1117
- assert_block(full_message) do
1118
- !object.const_defined?(constant_name)
1119
- end
1120
- end
1121
- end
1122
-
1123
- ##
1124
- # Passes if +object+.+predicate+ is _true_.
1125
- #
1126
- # Example:
1127
- # assert_predicate([], :empty?) # -> pass
1128
- # assert_predicate([1], :empty?) # -> fail
1129
- def assert_predicate(object, predicate, message=nil)
1130
- _wrap_assertion do
1131
- assert_respond_to(object, predicate, message)
1132
- actual = object.send(predicate)
1133
- full_message = build_message(message,
1134
- "<?>.? is true value expected but was\n" +
1135
- "<?>",
1136
- object,
1137
- AssertionMessage.literal(predicate),
1138
- actual)
1139
- assert_block(full_message) do
1140
- actual
1141
- end
1142
- end
1143
- end
1144
-
1145
- ##
1146
- # Passes if +object+.+predicate+ is not _true_.
1147
- #
1148
- # Example:
1149
- # assert_not_predicate([1], :empty?) # -> pass
1150
- # assert_not_predicate([], :empty?) # -> fail
1151
- def assert_not_predicate(object, predicate, message=nil)
1152
- _wrap_assertion do
1153
- assert_respond_to(object, predicate, message)
1154
- actual = object.send(predicate)
1155
- full_message = build_message(message,
1156
- "<?>.? is false value expected but was\n" +
1157
- "<?>",
1158
- object,
1159
- AssertionMessage.literal(predicate),
1160
- actual)
1161
- assert_block(full_message) do
1162
- not actual
1163
- end
1164
- end
1165
- end
1166
-
1167
- ##
1168
- # Passes if +object+#+alias_name+ is an alias method of
1169
- # +object+#+original_name+.
1170
- #
1171
- # Example:
1172
- # assert_alias_method([], :length, :size) # -> pass
1173
- # assert_alias_method([], :size, :length) # -> pass
1174
- # assert_alias_method([], :each, :size) # -> fail
1175
- def assert_alias_method(object, alias_name, original_name, message=nil)
1176
- _wrap_assertion do
1177
- find_method_failure_message = Proc.new do |method_name|
1178
- build_message(message,
1179
- "<?>.? doesn't exist\n" +
1180
- "(Class: <?>)",
1181
- object,
1182
- AssertionMessage.literal(method_name),
1183
- object.class)
1184
- end
1185
-
1186
- alias_method = original_method = nil
1187
- assert_block(find_method_failure_message.call(alias_name)) do
1188
- begin
1189
- alias_method = object.method(alias_name)
1190
- true
1191
- rescue NameError
1192
- false
1193
- end
1194
- end
1195
- assert_block(find_method_failure_message.call(original_name)) do
1196
- begin
1197
- original_method = object.method(original_name)
1198
- true
1199
- rescue NameError
1200
- false
1201
- end
1202
- end
1203
-
1204
- full_message = build_message(message,
1205
- "<?> is alias of\n" +
1206
- "<?> expected",
1207
- alias_method,
1208
- original_method)
1209
- assert_block(full_message) do
1210
- alias_method == original_method
1211
- end
1212
- end
1213
- end
1214
-
1215
- ##
1216
- # Passes if +path+ exists.
1217
- #
1218
- # Example:
1219
- # assert_path_exist("/tmp") # -> pass
1220
- # assert_path_exist("/bin/sh") # -> pass
1221
- # assert_path_exist("/nonexistent") # -> fail
1222
- def assert_path_exist(path, message=nil)
1223
- _wrap_assertion do
1224
- failure_message = build_message(message,
1225
- "<?> expected to exist",
1226
- path)
1227
- assert_block(failure_message) do
1228
- File.exist?(path)
1229
- end
1230
- end
1231
- end
1232
-
1233
- ##
1234
- # Passes if +path+ doesn't exist.
1235
- #
1236
- # Example:
1237
- # assert_path_not_exist("/nonexistent") # -> pass
1238
- # assert_path_not_exist("/tmp") # -> fail
1239
- # assert_path_not_exist("/bin/sh") # -> fail
1240
- def assert_path_not_exist(path, message=nil)
1241
- _wrap_assertion do
1242
- failure_message = build_message(message,
1243
- "<?> expected to not exist",
1244
- path)
1245
- assert_block(failure_message) do
1246
- not File.exist?(path)
1247
- end
1248
- end
1249
- end
1250
-
1251
- ##
1252
- # Passes if +collection+ includes +object+.
1253
- #
1254
- # Example:
1255
- # assert_include([1, 10], 1) # -> pass
1256
- # assert_include(1..10, 5) # -> pass
1257
- # assert_include([1, 10], 5) # -> fail
1258
- # assert_include(1..10, 20) # -> fail
1259
- def assert_include(collection, object, message=nil)
1260
- _wrap_assertion do
1261
- assert_respond_to(collection, :include?,
1262
- "The collection must respond to :include?.")
1263
- full_message = build_message(message,
1264
- "<?> expected to include\n<?>.",
1265
- collection,
1266
- object)
1267
- assert_block(full_message) do
1268
- collection.include?(object)
1269
- end
1270
- end
1271
- end
1272
-
1273
- ##
1274
- # Passes if +collection+ doesn't include +object+.
1275
- #
1276
- # Example:
1277
- # assert_not_include([1, 10], 5) # -> pass
1278
- # assert_not_include(1..10, 20) # -> pass
1279
- # assert_not_include([1, 10], 1) # -> fail
1280
- # assert_not_include(1..10, 5) # -> fail
1281
- def assert_not_include(collection, object, message=nil)
1282
- _wrap_assertion do
1283
- assert_respond_to(collection, :include?,
1284
- "The collection must respond to :include?.")
1285
- full_message = build_message(message,
1286
- "<?> expected to not include\n<?>.",
1287
- collection,
1288
- object)
1289
- assert_block(full_message) do
1290
- not collection.include?(object)
1291
- end
1292
- end
1293
- end
1294
-
1295
- ##
1296
- # Passes if +object+ is empty.
1297
- #
1298
- # Example:
1299
- # assert_empty("") # -> pass
1300
- # assert_empty([]) # -> pass
1301
- # assert_empty({}) # -> pass
1302
- # assert_empty(" ") # -> fail
1303
- # assert_empty([nil]) # -> fail
1304
- # assert_empty({1 => 2}) # -> fail
1305
- def assert_empty(object, message=nil)
1306
- _wrap_assertion do
1307
- assert_respond_to(object, :empty?,
1308
- "The object must respond to :empty?.")
1309
- full_message = build_message(message,
1310
- "<?> expected to be empty.",
1311
- object)
1312
- assert_block(full_message) do
1313
- object.empty?
1314
- end
1315
- end
1316
- end
1317
-
1318
- ##
1319
- # Passes if +object+ is not empty.
1320
- #
1321
- # Example:
1322
- # assert_not_empty(" ") # -> pass
1323
- # assert_not_empty([nil]) # -> pass
1324
- # assert_not_empty({1 => 2}) # -> pass
1325
- # assert_not_empty("") # -> fail
1326
- # assert_not_empty([]) # -> fail
1327
- # assert_not_empty({}) # -> fail
1328
- def assert_not_empty(object, message=nil)
1329
- _wrap_assertion do
1330
- assert_respond_to(object, :empty?,
1331
- "The object must respond to :empty?.")
1332
- full_message = build_message(message,
1333
- "<?> expected to not be empty.",
1334
- object)
1335
- assert_block(full_message) do
1336
- not object.empty?
1337
- end
1338
- end
1339
- end
1340
-
1341
- ##
1342
- # Builds a failure message. +head+ is added before the +template+ and
1343
- # +arguments+ replaces the '?'s positionally in the template.
1344
-
1345
- public
1346
- def build_message(head, template=nil, *arguments)
1347
- template &&= template.chomp
1348
- return AssertionMessage.new(head, template, arguments)
1349
- end
1350
-
1351
- private
1352
- def _wrap_assertion(&block)
1353
- @_assertion_wrapped ||= false
1354
- if @_assertion_wrapped
1355
- block.call
1356
- else
1357
- @_assertion_wrapped = true
1358
- begin
1359
- add_assertion
1360
- block.call
1361
- ensure
1362
- @_assertion_wrapped = false
1363
- end
1364
- end
1365
- end
1366
-
1367
- ##
1368
- # Called whenever an assertion is made. Define this in classes that
1369
- # include Test::Unit::Assertions to record assertion counts.
1370
-
1371
- private
1372
- def add_assertion
1373
- end
1374
-
1375
- ##
1376
- # Select whether or not to use the pretty-printer. If this option is set
1377
- # to false before any assertions are made, pp.rb will not be required.
1378
-
1379
- public
1380
- def self.use_pp=(value)
1381
- AssertionMessage.use_pp = value
1382
- end
1383
-
1384
- # :stopdoc:
1385
- private
1386
- def _assert_raise(assert_expected_exception, *args, &block)
1387
- _wrap_assertion do
1388
- if args.last.is_a?(String)
1389
- message = args.pop
1390
- else
1391
- message = ""
1392
- end
1393
-
1394
- assert_exception_helper = AssertExceptionHelper.new(self, args)
1395
- expected = assert_exception_helper.expected_exceptions
1396
- actual_exception = nil
1397
- full_message = build_message(message,
1398
- "<?> exception expected " +
1399
- "but none was thrown.",
1400
- expected)
1401
- assert_block(full_message) do
1402
- begin
1403
- yield
1404
- false
1405
- rescue Exception => actual_exception
1406
- true
1407
- end
1408
- end
1409
- assert_expected_exception.call(message, assert_exception_helper,
1410
- actual_exception)
1411
- actual_exception
1412
- end
1413
- end
1414
-
1415
- class AssertionMessage
1416
- @use_pp = true
1417
- class << self
1418
- attr_accessor :use_pp
1419
-
1420
- def literal(value)
1421
- Literal.new(value)
1422
- end
1423
-
1424
- def delayed_literal(&block)
1425
- DelayedLiteral.new(block)
1426
- end
1427
-
1428
- def maybe_container(value, &formatter)
1429
- MaybeContainer.new(value, &formatter)
1430
- end
1431
-
1432
- MAX_DIFF_TARGET_STRING_SIZE = 1000
1433
- def max_diff_target_string_size
1434
- size = ENV["TEST_UNIT_MAX_DIFF_TARGET_STRING_SIZE"]
1435
- if size
1436
- begin
1437
- size = Integer(size)
1438
- rescue ArgumentError
1439
- size = nil
1440
- end
1441
- end
1442
- size || MAX_DIFF_TARGET_STRING_SIZE
1443
- end
1444
-
1445
- def diff_target_string?(string)
1446
- if string.respond_to?(:bytesize)
1447
- string.bytesize < max_diff_target_string_size
1448
- else
1449
- string.size < max_diff_target_string_size
1450
- end
1451
- end
1452
-
1453
- def ensure_diffable_string(string)
1454
- if string.respond_to?(:encoding) and
1455
- !string.encoding.ascii_compatible?
1456
- string = string.dup.force_encoding("ASCII-8BIT")
1457
- end
1458
- string
1459
- end
1460
-
1461
- def prepare_for_diff(from, to)
1462
- if !from.is_a?(String) or !to.is_a?(String)
1463
- from = convert(from)
1464
- to = convert(to)
1465
- end
1466
-
1467
- if diff_target_string?(from) and diff_target_string?(to)
1468
- from = ensure_diffable_string(from)
1469
- to = ensure_diffable_string(to)
1470
- [from, to]
1471
- else
1472
- [nil, nil]
1473
- end
1474
- end
1475
-
1476
- def delayed_diff(from, to)
1477
- delayed_literal do
1478
- from, to = prepare_for_diff(from, to)
1479
-
1480
- diff = "" if from.nil? or to.nil?
1481
- diff ||= Diff.readable(from, to)
1482
- if /^[-+]/ !~ diff
1483
- diff = ""
1484
- elsif /^[ ?]/ =~ diff or /(?:.*\n){2,}/ =~ diff
1485
- diff = "\n\ndiff:\n#{diff}"
1486
- else
1487
- diff = ""
1488
- end
1489
-
1490
- if Diff.need_fold?(diff)
1491
- folded_diff = Diff.folded_readable(from, to)
1492
- diff << "\n\nfolded diff:\n#{folded_diff}"
1493
- end
1494
-
1495
- diff
1496
- end
1497
- end
1498
-
1499
- def convert(object)
1500
- case object
1501
- when Exception
1502
- <<EOM.chop
1503
- Class: <#{convert(object.class)}>
1504
- Message: <#{convert(object.message)}>
1505
- ---Backtrace---
1506
- #{Util::BacktraceFilter.filter_backtrace(object.backtrace).join("\n")}
1507
- ---------------
1508
- EOM
1509
- else
1510
- if use_pp
1511
- begin
1512
- require 'pp' unless defined?(PP)
1513
- if HashInspector.target?(object)
1514
- pp_target = HashInspector.new(object)
1515
- else
1516
- pp_target = object
1517
- end
1518
- begin
1519
- return PP.pp(pp_target, '').chomp
1520
- rescue NameError
1521
- end
1522
- rescue LoadError
1523
- self.use_pp = false
1524
- end
1525
- end
1526
- object.inspect
1527
- end
1528
- end
1529
- end
1530
-
1531
- class HashInspector
1532
- class << self
1533
- def target?(object)
1534
- object.is_a?(Hash) or object == ENV
1535
- end
1536
-
1537
- def normalize(object)
1538
- if target?(object)
1539
- new(object)
1540
- else
1541
- object
1542
- end
1543
- end
1544
- end
1545
-
1546
- def initialize(hash)
1547
- @hash = hash
1548
- end
1549
-
1550
- def inspect
1551
- @hash.inspect
1552
- end
1553
-
1554
- def pretty_print(q)
1555
- q.group(1, '{', '}') do
1556
- q.seplist(self, nil, :each_pair) do |k, v|
1557
- q.group do
1558
- q.pp(k)
1559
- q.text('=>')
1560
- q.group(1) do
1561
- q.breakable('')
1562
- q.pp(v)
1563
- end
1564
- end
1565
- end
1566
- end
1567
- end
1568
-
1569
- def pretty_print_cycle(q)
1570
- q.text(@hash.empty? ? '{}' : '{...}')
1571
- end
1572
-
1573
- def each_pair
1574
- keys = @hash.keys
1575
- begin
1576
- keys = keys.sort # FIXME: more cleverly
1577
- rescue ArgumentError
1578
- end
1579
- keys.each do |key|
1580
- yield(self.class.normalize(key),
1581
- self.class.normalize(@hash[key]))
1582
- end
1583
- end
1584
- end
1585
-
1586
- class Literal
1587
- def initialize(value)
1588
- @value = value
1589
- end
1590
-
1591
- def inspect
1592
- @value.to_s
1593
- end
1594
- end
1595
-
1596
- class DelayedLiteral
1597
- def initialize(value)
1598
- @value = value
1599
- end
1600
-
1601
- def inspect
1602
- @value.call.to_s
1603
- end
1604
- end
1605
-
1606
- class MaybeContainer
1607
- def initialize(value, &formatter)
1608
- @value = value
1609
- @formatter = formatter
1610
- end
1611
-
1612
- def inspect
1613
- if @value.is_a?(Array)
1614
- values = @value.collect do |value|
1615
- @formatter.call(AssertionMessage.convert(value))
1616
- end
1617
- "[#{values.join(', ')}]"
1618
- else
1619
- @formatter.call(AssertionMessage.convert(@value))
1620
- end
1621
- end
1622
- end
1623
-
1624
- class Template
1625
- def self.create(string)
1626
- parts = (string ? string.scan(/(?=[^\\])\?|(?:\\\?|[^\?])+/m) : [])
1627
- self.new(parts)
1628
- end
1629
-
1630
- attr_reader :count
1631
-
1632
- def initialize(parts)
1633
- @parts = parts
1634
- @count = parts.find_all{|e| e == '?'}.size
1635
- end
1636
-
1637
- def result(parameters)
1638
- raise "The number of parameters does not match the number of substitutions." if(parameters.size != count)
1639
- params = parameters.dup
1640
- @parts.collect{|e| e == '?' ? params.shift : e.gsub(/\\\?/m, '?')}.join('')
1641
- end
1642
- end
1643
-
1644
- include Util::BacktraceFilter
1645
-
1646
- def initialize(head, template_string, parameters)
1647
- @head = head
1648
- @template_string = template_string
1649
- @parameters = parameters
1650
- end
1651
-
1652
- def convert(object)
1653
- self.class.convert(object)
1654
- end
1655
-
1656
- def template
1657
- @template ||= Template.create(@template_string)
1658
- end
1659
-
1660
- def add_period(string)
1661
- (string =~ /\.\Z/ ? string : string + '.')
1662
- end
1663
-
1664
- def to_s
1665
- message_parts = []
1666
- if (@head)
1667
- head = @head.to_s
1668
- unless(head.empty?)
1669
- message_parts << add_period(head)
1670
- end
1671
- end
1672
- tail = template.result(@parameters.collect{|e| convert(e)})
1673
- message_parts << tail unless(tail.empty?)
1674
- message_parts.join("\n")
1675
- end
1676
- end
1677
-
1678
- class AssertExceptionHelper
1679
- class WrappedException
1680
- def initialize(exception)
1681
- @exception = exception
1682
- end
1683
-
1684
- def inspect
1685
- if default_inspect?
1686
- "#{@exception.class.inspect}(#{@exception.message.inspect})"
1687
- else
1688
- @exception.inspect
1689
- end
1690
- end
1691
-
1692
- def method_missing(name, *args, &block)
1693
- @exception.send(name, *args, &block)
1694
- end
1695
-
1696
- private
1697
- def default_inspect?
1698
- inspect_method = @exception.method(:inspect)
1699
- if inspect_method.respond_to?(:owner) and
1700
- inspect_method.owner == Exception
1701
- true
1702
- else
1703
- default_inspect_method = Exception.instance_method(:inspect)
1704
- default_inspect_method.bind(@exception).call == @exception.inspect
1705
- end
1706
- end
1707
- end
1708
-
1709
- def initialize(test_case, expected_exceptions)
1710
- @test_case = test_case
1711
- @expected_exceptions = expected_exceptions
1712
- @expected_classes, @expected_modules, @expected_objects =
1713
- split_expected_exceptions(expected_exceptions)
1714
- end
1715
-
1716
- def expected_exceptions
1717
- exceptions = @expected_exceptions.collect do |exception|
1718
- if exception.is_a?(Exception)
1719
- WrappedException.new(exception)
1720
- else
1721
- exception
1722
- end
1723
- end
1724
- if exceptions.size == 1
1725
- exceptions[0]
1726
- else
1727
- exceptions
1728
- end
1729
- end
1730
-
1731
- def expected?(actual_exception, equality=nil)
1732
- equality ||= :instance_of?
1733
- expected_class?(actual_exception, equality) or
1734
- expected_module?(actual_exception) or
1735
- expected_object?(actual_exception)
1736
- end
1737
-
1738
- private
1739
- def split_expected_exceptions(expected_exceptions)
1740
- exception_modules = []
1741
- exception_objects = []
1742
- exception_classes = []
1743
- expected_exceptions.each do |exception_type|
1744
- if exception_type.instance_of?(Module)
1745
- exception_modules << exception_type
1746
- elsif exception_type.is_a?(Exception)
1747
- exception_objects << exception_type
1748
- else
1749
- @test_case.send(:assert,
1750
- Exception >= exception_type,
1751
- "Should expect a class of exception, " +
1752
- "#{exception_type}")
1753
- exception_classes << exception_type
1754
- end
1755
- end
1756
- [exception_classes, exception_modules, exception_objects]
1757
- end
1758
-
1759
- def expected_class?(actual_exception, equality)
1760
- @expected_classes.any? do |expected_class|
1761
- actual_exception.send(equality, expected_class)
1762
- end
1763
- end
1764
-
1765
- def expected_module?(actual_exception)
1766
- @expected_modules.any? do |expected_module|
1767
- actual_exception.is_a?(expected_module)
1768
- end
1769
- end
1770
-
1771
- def expected_object?(actual_exception)
1772
- @expected_objects.any? do |expected_object|
1773
- expected_object == actual_exception or
1774
- fallback_exception_object_equal(expected_object, actual_exception)
1775
- end
1776
- end
1777
-
1778
- def fallback_exception_object_equal(expected_object, actual_exception)
1779
- owner = Util::MethodOwnerFinder.find(expected_object, :==)
1780
- if owner == Kernel or owner == Exception
1781
- expected_object.class == actual_exception.class and
1782
- expected_object.message == actual_exception.message
1783
- else
1784
- false
1785
- end
1786
- end
1787
- end
1788
-
1789
- # :startdoc:
1790
- end
1791
- end
1792
- end