activegroonga 1.0.4 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +55 -0
- data/Rakefile +45 -165
- data/{license/LGPL → doc/text/lgpl.txt} +0 -0
- data/doc/text/news.textile +60 -0
- data/doc/text/tutorial.textile +3 -0
- data/lib/active_groonga/base.rb +50 -2
- data/lib/active_groonga/railtie.rb +6 -5
- data/lib/active_groonga/railties/groonga.rake +8 -4
- data/lib/active_groonga/result_set.rb +133 -8
- data/lib/active_groonga/version.rb +1 -1
- data/test/active-groonga-test-utils.rb +6 -0
- data/test/run-test.rb +11 -24
- data/test/test-base.rb +10 -4
- data/test/test-result-set.rb +121 -9
- metadata +90 -138
- data/AUTHORS +0 -1
- data/NEWS.ja.rdoc +0 -46
- data/NEWS.rdoc +0 -46
- data/README.ja.rdoc +0 -52
- data/README.rdoc +0 -52
- data/test-unit-notify/Rakefile +0 -47
- data/test-unit-notify/lib/test/unit/notify.rb +0 -104
- data/test-unit/COPYING +0 -56
- data/test-unit/GPL +0 -340
- data/test-unit/PSFL +0 -271
- data/test-unit/Rakefile +0 -53
- data/test-unit/TODO +0 -5
- data/test-unit/bin/testrb +0 -5
- data/test-unit/html/bar.svg +0 -153
- data/test-unit/html/developer.svg +0 -469
- data/test-unit/html/favicon.ico +0 -0
- data/test-unit/html/favicon.svg +0 -82
- data/test-unit/html/heading-mark.svg +0 -393
- data/test-unit/html/index.html +0 -247
- data/test-unit/html/index.html.ja +0 -270
- data/test-unit/html/install.svg +0 -636
- data/test-unit/html/logo.svg +0 -483
- data/test-unit/html/test-unit.css +0 -339
- data/test-unit/html/tutorial.svg +0 -559
- data/test-unit/lib/test/unit.rb +0 -328
- data/test-unit/lib/test/unit/assertionfailederror.rb +0 -25
- data/test-unit/lib/test/unit/assertions.rb +0 -1334
- data/test-unit/lib/test/unit/attribute.rb +0 -125
- data/test-unit/lib/test/unit/autorunner.rb +0 -363
- data/test-unit/lib/test/unit/collector.rb +0 -36
- data/test-unit/lib/test/unit/collector/descendant.rb +0 -23
- data/test-unit/lib/test/unit/collector/dir.rb +0 -108
- data/test-unit/lib/test/unit/collector/load.rb +0 -144
- data/test-unit/lib/test/unit/collector/objectspace.rb +0 -34
- data/test-unit/lib/test/unit/color-scheme.rb +0 -106
- data/test-unit/lib/test/unit/color.rb +0 -96
- data/test-unit/lib/test/unit/diff.rb +0 -740
- data/test-unit/lib/test/unit/error.rb +0 -130
- data/test-unit/lib/test/unit/exceptionhandler.rb +0 -39
- data/test-unit/lib/test/unit/failure.rb +0 -136
- data/test-unit/lib/test/unit/fixture.rb +0 -176
- data/test-unit/lib/test/unit/notification.rb +0 -129
- data/test-unit/lib/test/unit/omission.rb +0 -191
- data/test-unit/lib/test/unit/pending.rb +0 -150
- data/test-unit/lib/test/unit/priority.rb +0 -180
- data/test-unit/lib/test/unit/runner/console.rb +0 -52
- data/test-unit/lib/test/unit/runner/emacs.rb +0 -8
- data/test-unit/lib/test/unit/runner/tap.rb +0 -8
- data/test-unit/lib/test/unit/testcase.rb +0 -483
- data/test-unit/lib/test/unit/testresult.rb +0 -121
- data/test-unit/lib/test/unit/testsuite.rb +0 -110
- data/test-unit/lib/test/unit/ui/console/outputlevel.rb +0 -14
- data/test-unit/lib/test/unit/ui/console/testrunner.rb +0 -430
- data/test-unit/lib/test/unit/ui/emacs/testrunner.rb +0 -63
- data/test-unit/lib/test/unit/ui/tap/testrunner.rb +0 -82
- data/test-unit/lib/test/unit/ui/testrunner.rb +0 -53
- data/test-unit/lib/test/unit/ui/testrunnermediator.rb +0 -77
- data/test-unit/lib/test/unit/ui/testrunnerutilities.rb +0 -41
- data/test-unit/lib/test/unit/util/backtracefilter.rb +0 -42
- data/test-unit/lib/test/unit/util/method-owner-finder.rb +0 -28
- data/test-unit/lib/test/unit/util/observable.rb +0 -90
- data/test-unit/lib/test/unit/util/output.rb +0 -31
- data/test-unit/lib/test/unit/util/procwrapper.rb +0 -48
- data/test-unit/lib/test/unit/version.rb +0 -7
- data/test-unit/sample/adder.rb +0 -13
- data/test-unit/sample/subtracter.rb +0 -12
- data/test-unit/sample/test_adder.rb +0 -20
- data/test-unit/sample/test_subtracter.rb +0 -20
- data/test-unit/sample/test_user.rb +0 -23
- data/test-unit/test/collector/test-descendant.rb +0 -133
- data/test-unit/test/collector/test-load.rb +0 -442
- data/test-unit/test/collector/test_dir.rb +0 -406
- data/test-unit/test/collector/test_objectspace.rb +0 -100
- data/test-unit/test/run-test.rb +0 -15
- data/test-unit/test/test-attribute.rb +0 -86
- data/test-unit/test/test-color-scheme.rb +0 -69
- data/test-unit/test/test-color.rb +0 -47
- data/test-unit/test/test-diff.rb +0 -518
- data/test-unit/test/test-emacs-runner.rb +0 -60
- data/test-unit/test/test-fixture.rb +0 -287
- data/test-unit/test/test-notification.rb +0 -33
- data/test-unit/test/test-omission.rb +0 -81
- data/test-unit/test/test-pending.rb +0 -70
- data/test-unit/test/test-priority.rb +0 -119
- data/test-unit/test/test-testcase.rb +0 -544
- data/test-unit/test/test_assertions.rb +0 -1197
- data/test-unit/test/test_error.rb +0 -26
- data/test-unit/test/test_failure.rb +0 -33
- data/test-unit/test/test_testresult.rb +0 -113
- data/test-unit/test/test_testsuite.rb +0 -129
- data/test-unit/test/testunit-test-util.rb +0 -14
- data/test-unit/test/ui/test_tap.rb +0 -33
- data/test-unit/test/ui/test_testrunmediator.rb +0 -20
- data/test-unit/test/util/test-method-owner-finder.rb +0 -38
- data/test-unit/test/util/test-output.rb +0 -11
- data/test-unit/test/util/test_backtracefilter.rb +0 -41
- data/test-unit/test/util/test_observable.rb +0 -102
- data/test-unit/test/util/test_procwrapper.rb +0 -36
data/test-unit/lib/test/unit.rb
DELETED
@@ -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,1334 +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
|
-
assert_block("assert should not be called with a block.") { !block_given? }
|
66
|
-
assert_block(build_message(message, "<?> is not true.", boolean)) { boolean }
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
##
|
71
|
-
# Passes if +expected+ == +actual.
|
72
|
-
#
|
73
|
-
# Note that the ordering of arguments is important, since a helpful
|
74
|
-
# error message is generated when this one fails that tells you the
|
75
|
-
# values of expected and actual.
|
76
|
-
#
|
77
|
-
# Example:
|
78
|
-
# assert_equal 'MY STRING', 'my string'.upcase
|
79
|
-
|
80
|
-
public
|
81
|
-
def assert_equal(expected, actual, message=nil)
|
82
|
-
diff = AssertionMessage.delayed_diff(expected, actual)
|
83
|
-
if expected.respond_to?(:encoding) and
|
84
|
-
actual.respond_to?(:encoding) and
|
85
|
-
expected.encoding != actual.encoding
|
86
|
-
format = <<EOT
|
87
|
-
<?>(?) expected but was
|
88
|
-
<?>(?).?
|
89
|
-
EOT
|
90
|
-
full_message = build_message(message, format,
|
91
|
-
expected, expected.encoding.name,
|
92
|
-
actual, actual.encoding.name,
|
93
|
-
diff)
|
94
|
-
else
|
95
|
-
full_message = build_message(message, <<EOT, expected, actual, diff)
|
96
|
-
<?> expected but was
|
97
|
-
<?>.?
|
98
|
-
EOT
|
99
|
-
end
|
100
|
-
begin
|
101
|
-
assert_block(full_message) { expected == actual }
|
102
|
-
rescue AssertionFailedError => failure
|
103
|
-
failure.expected = expected
|
104
|
-
failure.actual = actual
|
105
|
-
failure.inspected_expected = AssertionMessage.convert(expected)
|
106
|
-
failure.inspected_actual = AssertionMessage.convert(actual)
|
107
|
-
failure.user_message = message
|
108
|
-
raise
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
##
|
113
|
-
# Passes if the block raises one of the expected
|
114
|
-
# exceptions. When an expected exception is an Exception
|
115
|
-
# object, passes if expected_exception == actual_exception.
|
116
|
-
#
|
117
|
-
# Example:
|
118
|
-
# assert_raise(RuntimeError, LoadError) do
|
119
|
-
# raise 'Boom!!!'
|
120
|
-
# end # -> pass
|
121
|
-
#
|
122
|
-
# assert_raise do
|
123
|
-
# raise Exception, 'Any exception should be raised!!!'
|
124
|
-
# end # -> pass
|
125
|
-
#
|
126
|
-
# assert_raise(RuntimeError.new("XXX")) {raise "XXX"} # -> pass
|
127
|
-
# assert_raise(MyError.new("XXX")) {raise "XXX"} # -> fail
|
128
|
-
# assert_raise(RuntimeError.new("ZZZ")) {raise "XXX"} # -> fail
|
129
|
-
public
|
130
|
-
def assert_raise(*args, &block)
|
131
|
-
assert_expected_exception = Proc.new do |*_args|
|
132
|
-
message, assert_exception_helper, actual_exception = _args
|
133
|
-
expected = assert_exception_helper.expected_exceptions
|
134
|
-
full_message = build_message(message,
|
135
|
-
"<?> exception expected but was\n?",
|
136
|
-
expected, actual_exception)
|
137
|
-
assert_block(full_message) do
|
138
|
-
expected == [] or assert_exception_helper.expected?(actual_exception)
|
139
|
-
end
|
140
|
-
end
|
141
|
-
_assert_raise(assert_expected_exception, *args, &block)
|
142
|
-
end
|
143
|
-
|
144
|
-
##
|
145
|
-
# Alias of assert_raise.
|
146
|
-
#
|
147
|
-
# Will be deprecated in 1.9, and removed in 2.0.
|
148
|
-
|
149
|
-
public
|
150
|
-
def assert_raises(*args, &block)
|
151
|
-
assert_raise(*args, &block)
|
152
|
-
end
|
153
|
-
|
154
|
-
##
|
155
|
-
# Passes if the block raises one of the given
|
156
|
-
# exceptions or sub exceptions of the given exceptions.
|
157
|
-
#
|
158
|
-
# Example:
|
159
|
-
# assert_raise_kind_of(SystemCallError) do
|
160
|
-
# raise Errno::EACCES
|
161
|
-
# end
|
162
|
-
def assert_raise_kind_of(*args, &block)
|
163
|
-
assert_expected_exception = Proc.new do |*_args|
|
164
|
-
message, assert_exception_helper, actual_exception = _args
|
165
|
-
expected = assert_exception_helper.expected_exceptions
|
166
|
-
full_message = build_message(message,
|
167
|
-
"<?> family exception expected " +
|
168
|
-
"but was\n?",
|
169
|
-
expected, actual_exception)
|
170
|
-
assert_block(full_message) do
|
171
|
-
assert_exception_helper.expected?(actual_exception, :kind_of?)
|
172
|
-
end
|
173
|
-
end
|
174
|
-
_assert_raise(assert_expected_exception, *args, &block)
|
175
|
-
end
|
176
|
-
|
177
|
-
|
178
|
-
##
|
179
|
-
# Passes if +object+.instance_of?(+klass+). When +klass+ is
|
180
|
-
# an array of classes, it passes if any class
|
181
|
-
# satisfies +object.instance_of?(class).
|
182
|
-
#
|
183
|
-
# Example:
|
184
|
-
# assert_instance_of(String, 'foo') # -> pass
|
185
|
-
# assert_instance_of([Fixnum, NilClass], 100) # -> pass
|
186
|
-
# assert_instance_of([Numeric, NilClass], 100) # -> fail
|
187
|
-
|
188
|
-
public
|
189
|
-
def assert_instance_of(klass, object, message="")
|
190
|
-
_wrap_assertion do
|
191
|
-
klasses = nil
|
192
|
-
klasses = klass if klass.is_a?(Array)
|
193
|
-
assert_block("The first parameter to assert_instance_of should be " +
|
194
|
-
"a Class or an Array of Class.") do
|
195
|
-
if klasses
|
196
|
-
klasses.all? {|k| k.is_a?(Class)}
|
197
|
-
else
|
198
|
-
klass.is_a?(Class)
|
199
|
-
end
|
200
|
-
end
|
201
|
-
klass_message = AssertionMessage.maybe_container(klass) do |value|
|
202
|
-
"<#{value}>"
|
203
|
-
end
|
204
|
-
full_message = build_message(message, <<EOT, object, klass_message, object.class)
|
205
|
-
<?> expected to be an instance of
|
206
|
-
? but was
|
207
|
-
<?>.
|
208
|
-
EOT
|
209
|
-
assert_block(full_message) do
|
210
|
-
if klasses
|
211
|
-
klasses.any? {|k| object.instance_of?(k)}
|
212
|
-
else
|
213
|
-
object.instance_of?(klass)
|
214
|
-
end
|
215
|
-
end
|
216
|
-
end
|
217
|
-
end
|
218
|
-
|
219
|
-
##
|
220
|
-
# Passes if +object+ is nil.
|
221
|
-
#
|
222
|
-
# Example:
|
223
|
-
# assert_nil [1, 2].uniq!
|
224
|
-
|
225
|
-
public
|
226
|
-
def assert_nil(object, message="")
|
227
|
-
full_message = build_message(message, <<EOT, object)
|
228
|
-
<?> expected to be nil.
|
229
|
-
EOT
|
230
|
-
assert_block(full_message) { object.nil? }
|
231
|
-
end
|
232
|
-
|
233
|
-
##
|
234
|
-
# Passes if +object+.kind_of?(+klass+). When +klass+ is
|
235
|
-
# an array of classes or modules, it passes if any
|
236
|
-
# class or module satisfies +object.kind_of?(class_or_module).
|
237
|
-
#
|
238
|
-
# Example:
|
239
|
-
# assert_kind_of(Object, 'foo') # -> pass
|
240
|
-
# assert_kind_of([Fixnum, NilClass], 100) # -> pass
|
241
|
-
# assert_kind_of([Fixnum, NilClass], "string") # -> fail
|
242
|
-
|
243
|
-
public
|
244
|
-
def assert_kind_of(klass, object, message="")
|
245
|
-
_wrap_assertion do
|
246
|
-
klasses = nil
|
247
|
-
klasses = klass if klass.is_a?(Array)
|
248
|
-
assert_block("The first parameter to assert_kind_of should be " +
|
249
|
-
"a kind_of Module or an Array of a kind_of Module.") do
|
250
|
-
if klasses
|
251
|
-
klasses.all? {|k| k.kind_of?(Module)}
|
252
|
-
else
|
253
|
-
klass.kind_of?(Module)
|
254
|
-
end
|
255
|
-
end
|
256
|
-
klass_message = AssertionMessage.maybe_container(klass) do |value|
|
257
|
-
"<#{value}>"
|
258
|
-
end
|
259
|
-
full_message = build_message(message,
|
260
|
-
"<?> expected to be kind_of\\?\n" +
|
261
|
-
"? but was\n" +
|
262
|
-
"<?>.",
|
263
|
-
object,
|
264
|
-
klass_message,
|
265
|
-
object.class)
|
266
|
-
assert_block(full_message) do
|
267
|
-
if klasses
|
268
|
-
klasses.any? {|k| object.kind_of?(k)}
|
269
|
-
else
|
270
|
-
object.kind_of?(klass)
|
271
|
-
end
|
272
|
-
end
|
273
|
-
end
|
274
|
-
end
|
275
|
-
|
276
|
-
##
|
277
|
-
# Passes if +object+ .respond_to? +method+
|
278
|
-
#
|
279
|
-
# Example:
|
280
|
-
# assert_respond_to 'bugbear', :slice
|
281
|
-
|
282
|
-
public
|
283
|
-
def assert_respond_to(object, method, message="")
|
284
|
-
_wrap_assertion do
|
285
|
-
full_message = build_message(message,
|
286
|
-
"<?>.kind_of\\?(Symbol) or\n" +
|
287
|
-
"<?>.respond_to\\?(:to_str) expected",
|
288
|
-
method, method)
|
289
|
-
assert_block(full_message) do
|
290
|
-
method.kind_of?(Symbol) or method.respond_to?(:to_str)
|
291
|
-
end
|
292
|
-
full_message = build_message(message,
|
293
|
-
"<?>.respond_to\\?(?) expected\n" +
|
294
|
-
"(Class: <?>)",
|
295
|
-
object, method, object.class)
|
296
|
-
assert_block(full_message) {object.respond_to?(method)}
|
297
|
-
end
|
298
|
-
end
|
299
|
-
|
300
|
-
##
|
301
|
-
# Passes if +string+ =~ +pattern+.
|
302
|
-
#
|
303
|
-
# Example:
|
304
|
-
# assert_match(/\d+/, 'five, 6, seven')
|
305
|
-
|
306
|
-
public
|
307
|
-
def assert_match(pattern, string, message="")
|
308
|
-
_wrap_assertion do
|
309
|
-
pattern = case(pattern)
|
310
|
-
when String
|
311
|
-
Regexp.new(Regexp.escape(pattern))
|
312
|
-
else
|
313
|
-
pattern
|
314
|
-
end
|
315
|
-
full_message = build_message(message, "<?> expected to be =~\n<?>.", string, pattern)
|
316
|
-
assert_block(full_message) { string =~ pattern }
|
317
|
-
end
|
318
|
-
end
|
319
|
-
|
320
|
-
##
|
321
|
-
# Passes if +actual+ .equal? +expected+ (i.e. they are the same
|
322
|
-
# instance).
|
323
|
-
#
|
324
|
-
# Example:
|
325
|
-
# o = Object.new
|
326
|
-
# assert_same o, o
|
327
|
-
|
328
|
-
public
|
329
|
-
def assert_same(expected, actual, message="")
|
330
|
-
full_message = build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__)
|
331
|
-
<?>
|
332
|
-
with id <?> expected to be equal\\? to
|
333
|
-
<?>
|
334
|
-
with id <?>.
|
335
|
-
EOT
|
336
|
-
assert_block(full_message) { actual.equal?(expected) }
|
337
|
-
end
|
338
|
-
|
339
|
-
##
|
340
|
-
# Compares the +object1+ with +object2+ using +operator+.
|
341
|
-
#
|
342
|
-
# Passes if object1.__send__(operator, object2) is true.
|
343
|
-
#
|
344
|
-
# Example:
|
345
|
-
# assert_operator 5, :>=, 4
|
346
|
-
|
347
|
-
public
|
348
|
-
def assert_operator(object1, operator, object2, message="")
|
349
|
-
_wrap_assertion do
|
350
|
-
full_message = build_message(nil, "<?>\ngiven as the operator for #assert_operator must be a Symbol or #respond_to\\?(:to_str).", operator)
|
351
|
-
assert_block(full_message){operator.kind_of?(Symbol) || operator.respond_to?(:to_str)}
|
352
|
-
full_message = build_message(message, <<EOT, object1, AssertionMessage.literal(operator), object2)
|
353
|
-
<?> expected to be
|
354
|
-
?
|
355
|
-
<?>.
|
356
|
-
EOT
|
357
|
-
assert_block(full_message) { object1.__send__(operator, object2) }
|
358
|
-
end
|
359
|
-
end
|
360
|
-
|
361
|
-
##
|
362
|
-
# Passes if block does not raise an exception.
|
363
|
-
#
|
364
|
-
# Example:
|
365
|
-
# assert_nothing_raised do
|
366
|
-
# [1, 2].uniq
|
367
|
-
# end
|
368
|
-
|
369
|
-
public
|
370
|
-
def assert_nothing_raised(*args)
|
371
|
-
_wrap_assertion do
|
372
|
-
if args.last.is_a?(String)
|
373
|
-
message = args.pop
|
374
|
-
else
|
375
|
-
message = ""
|
376
|
-
end
|
377
|
-
|
378
|
-
assert_exception_helper = AssertExceptionHelper.new(self, args)
|
379
|
-
begin
|
380
|
-
yield
|
381
|
-
rescue Exception => e
|
382
|
-
if ((args.empty? && !e.instance_of?(AssertionFailedError)) ||
|
383
|
-
assert_exception_helper.expected?(e))
|
384
|
-
failure_message = build_message(message, "Exception raised:\n?", e)
|
385
|
-
assert_block(failure_message) {false}
|
386
|
-
else
|
387
|
-
raise
|
388
|
-
end
|
389
|
-
end
|
390
|
-
nil
|
391
|
-
end
|
392
|
-
end
|
393
|
-
|
394
|
-
##
|
395
|
-
# Flunk always fails.
|
396
|
-
#
|
397
|
-
# Example:
|
398
|
-
# flunk 'Not done testing yet.'
|
399
|
-
|
400
|
-
public
|
401
|
-
def flunk(message="Flunked")
|
402
|
-
assert_block(build_message(message)){false}
|
403
|
-
end
|
404
|
-
|
405
|
-
##
|
406
|
-
# Passes if ! +actual+ .equal? +expected+
|
407
|
-
#
|
408
|
-
# Example:
|
409
|
-
# assert_not_same Object.new, Object.new
|
410
|
-
|
411
|
-
public
|
412
|
-
def assert_not_same(expected, actual, message="")
|
413
|
-
full_message = build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__)
|
414
|
-
<?>
|
415
|
-
with id <?> expected to not be equal\\? to
|
416
|
-
<?>
|
417
|
-
with id <?>.
|
418
|
-
EOT
|
419
|
-
assert_block(full_message) { !actual.equal?(expected) }
|
420
|
-
end
|
421
|
-
|
422
|
-
##
|
423
|
-
# Passes if +expected+ != +actual+
|
424
|
-
#
|
425
|
-
# Example:
|
426
|
-
# assert_not_equal 'some string', 5
|
427
|
-
|
428
|
-
public
|
429
|
-
def assert_not_equal(expected, actual, message="")
|
430
|
-
full_message = build_message(message, "<?> expected to be != to\n<?>.", expected, actual)
|
431
|
-
assert_block(full_message) { expected != actual }
|
432
|
-
end
|
433
|
-
|
434
|
-
##
|
435
|
-
# Passes if ! +object+ .nil?
|
436
|
-
#
|
437
|
-
# Example:
|
438
|
-
# assert_not_nil '1 two 3'.sub!(/two/, '2')
|
439
|
-
|
440
|
-
public
|
441
|
-
def assert_not_nil(object, message="")
|
442
|
-
full_message = build_message(message, "<?> expected to not be nil.", object)
|
443
|
-
assert_block(full_message){!object.nil?}
|
444
|
-
end
|
445
|
-
|
446
|
-
##
|
447
|
-
# Passes if +regexp+ !~ +string+
|
448
|
-
#
|
449
|
-
# Example:
|
450
|
-
# assert_no_match(/two/, 'one 2 three')
|
451
|
-
|
452
|
-
public
|
453
|
-
def assert_no_match(regexp, string, message="")
|
454
|
-
_wrap_assertion do
|
455
|
-
assert_instance_of(Regexp, regexp, "The first argument to assert_no_match should be a Regexp.")
|
456
|
-
full_message = build_message(message, "<?> expected to not match\n<?>.", regexp, string)
|
457
|
-
assert_block(full_message) { regexp !~ string }
|
458
|
-
end
|
459
|
-
end
|
460
|
-
|
461
|
-
UncaughtThrow = {
|
462
|
-
NameError => /^uncaught throw \`(.+)\'$/,
|
463
|
-
ArgumentError => /^uncaught throw (.+)$/,
|
464
|
-
ThreadError => /^uncaught throw \`(.+)\' in thread /
|
465
|
-
} #`
|
466
|
-
|
467
|
-
##
|
468
|
-
# Passes if the block throws +expected_object+
|
469
|
-
#
|
470
|
-
# Example:
|
471
|
-
# assert_throw(:done) do
|
472
|
-
# throw(:done)
|
473
|
-
# end
|
474
|
-
|
475
|
-
public
|
476
|
-
def assert_throw(expected_object, message="", &proc)
|
477
|
-
_wrap_assertion do
|
478
|
-
begin
|
479
|
-
catch([]) {}
|
480
|
-
rescue TypeError
|
481
|
-
assert_instance_of(Symbol, expected_object,
|
482
|
-
"assert_throws expects the symbol that should be thrown for its first argument")
|
483
|
-
end
|
484
|
-
assert_block("Should have passed a block to assert_throw.") do
|
485
|
-
block_given?
|
486
|
-
end
|
487
|
-
caught = true
|
488
|
-
begin
|
489
|
-
catch(expected_object) do
|
490
|
-
proc.call
|
491
|
-
caught = false
|
492
|
-
end
|
493
|
-
full_message = build_message(message,
|
494
|
-
"<?> should have been thrown.",
|
495
|
-
expected_object)
|
496
|
-
assert_block(full_message) {caught}
|
497
|
-
rescue NameError, ArgumentError, ThreadError => error
|
498
|
-
raise unless UncaughtThrow[error.class] =~ error.message
|
499
|
-
tag = $1
|
500
|
-
tag = tag[1..-1].intern if tag[0, 1] == ":"
|
501
|
-
full_message = build_message(message,
|
502
|
-
"<?> expected to be thrown but\n" +
|
503
|
-
"<?> was thrown.",
|
504
|
-
expected_object, tag)
|
505
|
-
flunk(full_message)
|
506
|
-
end
|
507
|
-
end
|
508
|
-
end
|
509
|
-
|
510
|
-
##
|
511
|
-
# Alias of assert_throw.
|
512
|
-
#
|
513
|
-
# Will be deprecated in 1.9, and removed in 2.0.
|
514
|
-
def assert_throws(*args, &block)
|
515
|
-
assert_throw(*args, &block)
|
516
|
-
end
|
517
|
-
|
518
|
-
##
|
519
|
-
# Passes if block does not throw anything.
|
520
|
-
#
|
521
|
-
# Example:
|
522
|
-
# assert_nothing_thrown do
|
523
|
-
# [1, 2].uniq
|
524
|
-
# end
|
525
|
-
|
526
|
-
public
|
527
|
-
def assert_nothing_thrown(message="", &proc)
|
528
|
-
_wrap_assertion do
|
529
|
-
assert(block_given?, "Should have passed a block to assert_nothing_thrown")
|
530
|
-
begin
|
531
|
-
proc.call
|
532
|
-
rescue NameError, ArgumentError, ThreadError => error
|
533
|
-
raise unless UncaughtThrow[error.class] =~ error.message
|
534
|
-
tag = $1
|
535
|
-
tag = tag[1..-1].intern if tag[0, 1] == ":"
|
536
|
-
full_message = build_message(message,
|
537
|
-
"<?> was thrown when nothing was expected",
|
538
|
-
tag)
|
539
|
-
flunk(full_message)
|
540
|
-
end
|
541
|
-
assert(true, "Expected nothing to be thrown")
|
542
|
-
end
|
543
|
-
end
|
544
|
-
|
545
|
-
##
|
546
|
-
# Passes if +expected_float+ and +actual_float+ are equal
|
547
|
-
# within +delta+ tolerance.
|
548
|
-
#
|
549
|
-
# Example:
|
550
|
-
# assert_in_delta 0.05, (50000.0 / 10**6), 0.00001
|
551
|
-
|
552
|
-
public
|
553
|
-
def assert_in_delta(expected_float, actual_float, delta, message="")
|
554
|
-
_wrap_assertion do
|
555
|
-
_assert_in_delta_validate_arguments(expected_float,
|
556
|
-
actual_float,
|
557
|
-
delta)
|
558
|
-
full_message = _assert_in_delta_message(expected_float,
|
559
|
-
actual_float,
|
560
|
-
delta,
|
561
|
-
message)
|
562
|
-
assert_block(full_message) do
|
563
|
-
(expected_float.to_f - actual_float.to_f).abs <= delta.to_f
|
564
|
-
end
|
565
|
-
end
|
566
|
-
end
|
567
|
-
|
568
|
-
# :stopdoc:
|
569
|
-
private
|
570
|
-
def _assert_in_delta_validate_arguments(expected_float,
|
571
|
-
actual_float,
|
572
|
-
delta)
|
573
|
-
{
|
574
|
-
expected_float => "first float",
|
575
|
-
actual_float => "second float",
|
576
|
-
delta => "delta"
|
577
|
-
}.each do |float, name|
|
578
|
-
assert_respond_to(float, :to_f,
|
579
|
-
"The arguments must respond to to_f; " +
|
580
|
-
"the #{name} did not")
|
581
|
-
end
|
582
|
-
assert_operator(delta, :>=, 0.0, "The delta should not be negative")
|
583
|
-
end
|
584
|
-
|
585
|
-
def _assert_in_delta_message(expected_float, actual_float, delta,
|
586
|
-
message)
|
587
|
-
format = <<-EOT
|
588
|
-
<?> expected but was
|
589
|
-
<?> (tolerance <?>).
|
590
|
-
EOT
|
591
|
-
arguments = [expected_float, actual_float, delta]
|
592
|
-
normalized_expected = expected_float.to_f
|
593
|
-
normalized_actual = actual_float.to_f
|
594
|
-
normalized_delta = delta.to_f
|
595
|
-
relation_format = nil
|
596
|
-
relation_arguments = nil
|
597
|
-
if normalized_actual < normalized_expected - normalized_delta
|
598
|
-
relation_format = "<<?> < <?>-<?>(?) <= <?>+<?>(?)>"
|
599
|
-
relation_arguments = [actual_float,
|
600
|
-
expected_float, delta, expected_float - delta,
|
601
|
-
expected_float, delta, expected_float + delta]
|
602
|
-
elsif normalized_expected - normalized_delta < normalized_actual
|
603
|
-
relation_format = "<<?>-<?>(?) <= <?>+<?>(?) < <?>>"
|
604
|
-
relation_arguments = [expected_float, delta, expected_float - delta,
|
605
|
-
expected_float, delta, expected_float + delta,
|
606
|
-
actual_float]
|
607
|
-
end
|
608
|
-
|
609
|
-
if relation_format
|
610
|
-
format << <<-EOT
|
611
|
-
|
612
|
-
Relation:
|
613
|
-
#{relation_format}
|
614
|
-
EOT
|
615
|
-
arguments.concat(relation_arguments)
|
616
|
-
end
|
617
|
-
|
618
|
-
build_message(message, format, *arguments)
|
619
|
-
end
|
620
|
-
public
|
621
|
-
# :startdoc:
|
622
|
-
|
623
|
-
##
|
624
|
-
# Passes if the method send returns a true value.
|
625
|
-
#
|
626
|
-
# +send_array+ is composed of:
|
627
|
-
# * A receiver
|
628
|
-
# * A method
|
629
|
-
# * Arguments to the method
|
630
|
-
#
|
631
|
-
# Example:
|
632
|
-
# assert_send [[1, 2], :include?, 4]
|
633
|
-
|
634
|
-
public
|
635
|
-
def assert_send(send_array, message="")
|
636
|
-
_wrap_assertion do
|
637
|
-
assert_instance_of(Array, send_array, "assert_send requires an array of send information")
|
638
|
-
assert(send_array.size >= 2, "assert_send requires at least a receiver and a message name")
|
639
|
-
full_message = build_message(message, <<EOT, send_array[0], AssertionMessage.literal(send_array[1].to_s), send_array[2..-1])
|
640
|
-
<?> expected to respond to
|
641
|
-
<?(?)> with a true value.
|
642
|
-
EOT
|
643
|
-
assert_block(full_message) { send_array[0].__send__(send_array[1], *send_array[2..-1]) }
|
644
|
-
end
|
645
|
-
end
|
646
|
-
|
647
|
-
##
|
648
|
-
# Passes if +actual+ is a boolean value.
|
649
|
-
#
|
650
|
-
# Example:
|
651
|
-
# assert_boolean(true) # -> pass
|
652
|
-
# assert_boolean(nil) # -> fail
|
653
|
-
def assert_boolean(actual, message=nil)
|
654
|
-
_wrap_assertion do
|
655
|
-
assert_block(build_message(message,
|
656
|
-
"<true> or <false> expected but was\n<?>",
|
657
|
-
actual)) do
|
658
|
-
[true, false].include?(actual)
|
659
|
-
end
|
660
|
-
end
|
661
|
-
end
|
662
|
-
|
663
|
-
##
|
664
|
-
# Passes if +actual+ is true.
|
665
|
-
#
|
666
|
-
# Example:
|
667
|
-
# assert_true(true) # -> pass
|
668
|
-
# assert_true(:true) # -> fail
|
669
|
-
def assert_true(actual, message=nil)
|
670
|
-
_wrap_assertion do
|
671
|
-
assert_block(build_message(message,
|
672
|
-
"<true> expected but was\n<?>",
|
673
|
-
actual)) do
|
674
|
-
actual == true
|
675
|
-
end
|
676
|
-
end
|
677
|
-
end
|
678
|
-
|
679
|
-
##
|
680
|
-
# Passes if +actual+ is false.
|
681
|
-
#
|
682
|
-
# Example:
|
683
|
-
# assert_false(false) # -> pass
|
684
|
-
# assert_false(nil) # -> fail
|
685
|
-
def assert_false(actual, message=nil)
|
686
|
-
_wrap_assertion do
|
687
|
-
assert_block(build_message(message,
|
688
|
-
"<false> expected but was\n<?>",
|
689
|
-
actual)) do
|
690
|
-
actual == false
|
691
|
-
end
|
692
|
-
end
|
693
|
-
end
|
694
|
-
|
695
|
-
##
|
696
|
-
# Passes if expression "+expected+ +operator+
|
697
|
-
# +actual+" is true.
|
698
|
-
#
|
699
|
-
# Example:
|
700
|
-
# assert_compare(1, "<", 10) # -> pass
|
701
|
-
# assert_compare(1, ">=", 10) # -> fail
|
702
|
-
def assert_compare(expected, operator, actual, message=nil)
|
703
|
-
_wrap_assertion do
|
704
|
-
assert_send([["<", "<=", ">", ">="], :include?, operator.to_s])
|
705
|
-
case operator.to_s
|
706
|
-
when "<"
|
707
|
-
operator_description = "less than"
|
708
|
-
when "<="
|
709
|
-
operator_description = "less than or equal to"
|
710
|
-
when ">"
|
711
|
-
operator_description = "greater than"
|
712
|
-
when ">="
|
713
|
-
operator_description = "greater than or equal to"
|
714
|
-
end
|
715
|
-
template = <<-EOT
|
716
|
-
<?> #{operator} <?> should be true
|
717
|
-
<?> expected #{operator_description}
|
718
|
-
<?>.
|
719
|
-
EOT
|
720
|
-
full_message = build_message(message, template,
|
721
|
-
expected, actual,
|
722
|
-
expected, actual)
|
723
|
-
assert_block(full_message) do
|
724
|
-
expected.send(operator, actual)
|
725
|
-
end
|
726
|
-
end
|
727
|
-
end
|
728
|
-
|
729
|
-
##
|
730
|
-
# Passes if assertion is failed in block.
|
731
|
-
#
|
732
|
-
# Example:
|
733
|
-
# assert_fail_assertion {assert_equal("A", "B")} # -> pass
|
734
|
-
# assert_fail_assertion {assert_equal("A", "A")} # -> fail
|
735
|
-
def assert_fail_assertion(message=nil)
|
736
|
-
_wrap_assertion do
|
737
|
-
full_message = build_message(message,
|
738
|
-
"Failed assertion was expected.")
|
739
|
-
assert_block(full_message) do
|
740
|
-
begin
|
741
|
-
yield
|
742
|
-
false
|
743
|
-
rescue AssertionFailedError
|
744
|
-
true
|
745
|
-
end
|
746
|
-
end
|
747
|
-
end
|
748
|
-
end
|
749
|
-
|
750
|
-
##
|
751
|
-
# Passes if an exception is raised in block and its
|
752
|
-
# message is +expected+.
|
753
|
-
#
|
754
|
-
# Example:
|
755
|
-
# assert_raise_message("exception") {raise "exception"} # -> pass
|
756
|
-
# assert_raise_message(/exc/i) {raise "exception"} # -> pass
|
757
|
-
# assert_raise_message("exception") {raise "EXCEPTION"} # -> fail
|
758
|
-
# assert_raise_message("exception") {} # -> fail
|
759
|
-
def assert_raise_message(expected, message=nil)
|
760
|
-
_wrap_assertion do
|
761
|
-
full_message = build_message(message,
|
762
|
-
"<?> exception message expected " +
|
763
|
-
"but none was thrown.",
|
764
|
-
expected)
|
765
|
-
exception = nil
|
766
|
-
assert_block(full_message) do
|
767
|
-
begin
|
768
|
-
yield
|
769
|
-
false
|
770
|
-
rescue Exception => exception
|
771
|
-
true
|
772
|
-
end
|
773
|
-
end
|
774
|
-
|
775
|
-
actual = exception.message
|
776
|
-
diff = AssertionMessage.delayed_diff(expected, actual)
|
777
|
-
full_message =
|
778
|
-
build_message(message,
|
779
|
-
"<?> exception message expected but was\n" +
|
780
|
-
"<?>.?", expected, actual, diff)
|
781
|
-
assert_block(full_message) do
|
782
|
-
if expected.is_a?(Regexp)
|
783
|
-
expected =~ actual
|
784
|
-
else
|
785
|
-
expected == actual
|
786
|
-
end
|
787
|
-
end
|
788
|
-
end
|
789
|
-
end
|
790
|
-
|
791
|
-
##
|
792
|
-
# Passes if +object+.const_defined?(+constant_name+)
|
793
|
-
#
|
794
|
-
# Example:
|
795
|
-
# assert_const_defined(Test, :Unit) # -> pass
|
796
|
-
# assert_const_defined(Object, :Nonexistent) # -> fail
|
797
|
-
def assert_const_defined(object, constant_name, message=nil)
|
798
|
-
_wrap_assertion do
|
799
|
-
full_message = build_message(message,
|
800
|
-
"<?>.const_defined\\?(<?>) expected.",
|
801
|
-
object, constant_name)
|
802
|
-
assert_block(full_message) do
|
803
|
-
object.const_defined?(constant_name)
|
804
|
-
end
|
805
|
-
end
|
806
|
-
end
|
807
|
-
|
808
|
-
##
|
809
|
-
# Passes if !+object+.const_defined?(+constant_name+)
|
810
|
-
#
|
811
|
-
# Example:
|
812
|
-
# assert_not_const_defined(Object, :Nonexistent) # -> pass
|
813
|
-
# assert_not_const_defined(Test, :Unit) # -> fail
|
814
|
-
def assert_not_const_defined(object, constant_name, message=nil)
|
815
|
-
_wrap_assertion do
|
816
|
-
full_message = build_message(message,
|
817
|
-
"!<?>.const_defined\\?(<?>) expected.",
|
818
|
-
object, constant_name)
|
819
|
-
assert_block(full_message) do
|
820
|
-
!object.const_defined?(constant_name)
|
821
|
-
end
|
822
|
-
end
|
823
|
-
end
|
824
|
-
|
825
|
-
##
|
826
|
-
# Passes if +object+.+predicate+
|
827
|
-
#
|
828
|
-
# Example:
|
829
|
-
# assert_predicate([], :empty?) # -> pass
|
830
|
-
# assert_predicate([1], :empty?) # -> fail
|
831
|
-
def assert_predicate(object, predicate, message=nil)
|
832
|
-
_wrap_assertion do
|
833
|
-
assert_respond_to(object, predicate, message)
|
834
|
-
actual = object.send(predicate)
|
835
|
-
full_message = build_message(message,
|
836
|
-
"<?>.? is true value expected but was\n" +
|
837
|
-
"<?>",
|
838
|
-
object,
|
839
|
-
AssertionMessage.literal(predicate),
|
840
|
-
actual)
|
841
|
-
assert_block(full_message) do
|
842
|
-
actual
|
843
|
-
end
|
844
|
-
end
|
845
|
-
end
|
846
|
-
|
847
|
-
##
|
848
|
-
# Passes if +object+.+predicate+
|
849
|
-
#
|
850
|
-
# Example:
|
851
|
-
# assert_not_predicate([1], :empty?) # -> pass
|
852
|
-
# assert_not_predicate([], :empty?) # -> fail
|
853
|
-
def assert_not_predicate(object, predicate, message=nil)
|
854
|
-
_wrap_assertion do
|
855
|
-
assert_respond_to(object, predicate, message)
|
856
|
-
actual = object.send(predicate)
|
857
|
-
full_message = build_message(message,
|
858
|
-
"<?>.? is false value expected but was\n" +
|
859
|
-
"<?>",
|
860
|
-
object,
|
861
|
-
AssertionMessage.literal(predicate),
|
862
|
-
actual)
|
863
|
-
assert_block(full_message) do
|
864
|
-
not actual
|
865
|
-
end
|
866
|
-
end
|
867
|
-
end
|
868
|
-
|
869
|
-
##
|
870
|
-
# Passes if +object+#+alias_name+ is an alias method of
|
871
|
-
# +object+#+original_name+.
|
872
|
-
#
|
873
|
-
# Example:
|
874
|
-
# assert_alias_method([], :length, :size) # -> pass
|
875
|
-
# assert_alias_method([], :size, :length) # -> pass
|
876
|
-
# assert_alias_method([], :each, :size) # -> fail
|
877
|
-
def assert_alias_method(object, alias_name, original_name, message=nil)
|
878
|
-
_wrap_assertion do
|
879
|
-
find_method_failure_message = Proc.new do |method_name|
|
880
|
-
build_message(message,
|
881
|
-
"<?>.? doesn't exist\n" +
|
882
|
-
"(Class: <?>)",
|
883
|
-
object,
|
884
|
-
AssertionMessage.literal(method_name),
|
885
|
-
object.class)
|
886
|
-
end
|
887
|
-
|
888
|
-
alias_method = original_method = nil
|
889
|
-
assert_block(find_method_failure_message.call(alias_name)) do
|
890
|
-
begin
|
891
|
-
alias_method = object.method(alias_name)
|
892
|
-
true
|
893
|
-
rescue NameError
|
894
|
-
false
|
895
|
-
end
|
896
|
-
end
|
897
|
-
assert_block(find_method_failure_message.call(original_name)) do
|
898
|
-
begin
|
899
|
-
original_method = object.method(original_name)
|
900
|
-
true
|
901
|
-
rescue NameError
|
902
|
-
false
|
903
|
-
end
|
904
|
-
end
|
905
|
-
|
906
|
-
full_message = build_message(message,
|
907
|
-
"<?> is alias of\n" +
|
908
|
-
"<?> expected",
|
909
|
-
alias_method,
|
910
|
-
original_method)
|
911
|
-
assert_block(full_message) do
|
912
|
-
alias_method == original_method
|
913
|
-
end
|
914
|
-
end
|
915
|
-
end
|
916
|
-
|
917
|
-
##
|
918
|
-
# Passes if +path+ exists.
|
919
|
-
#
|
920
|
-
# Example:
|
921
|
-
# assert_path_exist("/tmp") # -> pass
|
922
|
-
# assert_path_exist("/bin/sh") # -> pass
|
923
|
-
# assert_path_exist("/nonexistent") # -> fail
|
924
|
-
def assert_path_exist(path, message=nil)
|
925
|
-
_wrap_assertion do
|
926
|
-
failure_message = build_message(message,
|
927
|
-
"<?> expected to exist",
|
928
|
-
path)
|
929
|
-
assert_block(failure_message) do
|
930
|
-
File.exist?(path)
|
931
|
-
end
|
932
|
-
end
|
933
|
-
end
|
934
|
-
|
935
|
-
##
|
936
|
-
# Passes if +path+ doesn't exist.
|
937
|
-
#
|
938
|
-
# Example:
|
939
|
-
# assert_path_not_exist("/nonexistent") # -> pass
|
940
|
-
# assert_path_not_exist("/tmp") # -> fail
|
941
|
-
# assert_path_not_exist("/bin/sh") # -> fail
|
942
|
-
def assert_path_not_exist(path, message=nil)
|
943
|
-
_wrap_assertion do
|
944
|
-
failure_message = build_message(message,
|
945
|
-
"<?> expected to not exist",
|
946
|
-
path)
|
947
|
-
assert_block(failure_message) do
|
948
|
-
not File.exist?(path)
|
949
|
-
end
|
950
|
-
end
|
951
|
-
end
|
952
|
-
|
953
|
-
##
|
954
|
-
# Builds a failure message. +head+ is added before the +template+ and
|
955
|
-
# +arguments+ replaces the '?'s positionally in the template.
|
956
|
-
|
957
|
-
public
|
958
|
-
def build_message(head, template=nil, *arguments)
|
959
|
-
template &&= template.chomp
|
960
|
-
return AssertionMessage.new(head, template, arguments)
|
961
|
-
end
|
962
|
-
|
963
|
-
private
|
964
|
-
def _wrap_assertion
|
965
|
-
@_assertion_wrapped ||= false
|
966
|
-
unless (@_assertion_wrapped)
|
967
|
-
@_assertion_wrapped = true
|
968
|
-
begin
|
969
|
-
add_assertion
|
970
|
-
return yield
|
971
|
-
ensure
|
972
|
-
@_assertion_wrapped = false
|
973
|
-
end
|
974
|
-
else
|
975
|
-
return yield
|
976
|
-
end
|
977
|
-
end
|
978
|
-
|
979
|
-
##
|
980
|
-
# Called whenever an assertion is made. Define this in classes that
|
981
|
-
# include Test::Unit::Assertions to record assertion counts.
|
982
|
-
|
983
|
-
private
|
984
|
-
def add_assertion
|
985
|
-
end
|
986
|
-
|
987
|
-
##
|
988
|
-
# Select whether or not to use the pretty-printer. If this option is set
|
989
|
-
# to false before any assertions are made, pp.rb will not be required.
|
990
|
-
|
991
|
-
public
|
992
|
-
def self.use_pp=(value)
|
993
|
-
AssertionMessage.use_pp = value
|
994
|
-
end
|
995
|
-
|
996
|
-
# :stopdoc:
|
997
|
-
private
|
998
|
-
def _assert_raise(assert_expected_exception, *args, &block)
|
999
|
-
_wrap_assertion do
|
1000
|
-
if args.last.is_a?(String)
|
1001
|
-
message = args.pop
|
1002
|
-
else
|
1003
|
-
message = ""
|
1004
|
-
end
|
1005
|
-
|
1006
|
-
assert_exception_helper = AssertExceptionHelper.new(self, args)
|
1007
|
-
expected = assert_exception_helper.expected_exceptions
|
1008
|
-
actual_exception = nil
|
1009
|
-
full_message = build_message(message,
|
1010
|
-
"<?> exception expected " +
|
1011
|
-
"but none was thrown.",
|
1012
|
-
expected)
|
1013
|
-
assert_block(full_message) do
|
1014
|
-
begin
|
1015
|
-
yield
|
1016
|
-
false
|
1017
|
-
rescue Exception => actual_exception
|
1018
|
-
true
|
1019
|
-
end
|
1020
|
-
end
|
1021
|
-
assert_expected_exception.call(message, assert_exception_helper,
|
1022
|
-
actual_exception)
|
1023
|
-
actual_exception
|
1024
|
-
end
|
1025
|
-
end
|
1026
|
-
|
1027
|
-
class AssertionMessage
|
1028
|
-
@use_pp = true
|
1029
|
-
class << self
|
1030
|
-
attr_accessor :use_pp
|
1031
|
-
|
1032
|
-
def literal(value)
|
1033
|
-
Literal.new(value)
|
1034
|
-
end
|
1035
|
-
|
1036
|
-
def delayed_literal(&block)
|
1037
|
-
DelayedLiteral.new(block)
|
1038
|
-
end
|
1039
|
-
|
1040
|
-
def maybe_container(value, &formatter)
|
1041
|
-
MaybeContainer.new(value, &formatter)
|
1042
|
-
end
|
1043
|
-
|
1044
|
-
MAX_DIFF_TARGET_STRING_SIZE = 1000
|
1045
|
-
def max_diff_target_string_size
|
1046
|
-
size = ENV["TEST_UNIT_MAX_DIFF_TARGET_STRING_SIZE"]
|
1047
|
-
if size
|
1048
|
-
begin
|
1049
|
-
size = Integer(size)
|
1050
|
-
rescue ArgumentError
|
1051
|
-
size = nil
|
1052
|
-
end
|
1053
|
-
end
|
1054
|
-
size || MAX_DIFF_TARGET_STRING_SIZE
|
1055
|
-
end
|
1056
|
-
|
1057
|
-
def diff_target_string?(string)
|
1058
|
-
if string.respond_to?(:bytesize)
|
1059
|
-
string.bytesize < max_diff_target_string_size
|
1060
|
-
else
|
1061
|
-
string.size < max_diff_target_string_size
|
1062
|
-
end
|
1063
|
-
end
|
1064
|
-
|
1065
|
-
def prepare_for_diff(from, to)
|
1066
|
-
if !from.is_a?(String) or !to.is_a?(String)
|
1067
|
-
from = convert(from)
|
1068
|
-
to = convert(to)
|
1069
|
-
end
|
1070
|
-
|
1071
|
-
if diff_target_string?(from) and diff_target_string?(to)
|
1072
|
-
[from, to]
|
1073
|
-
else
|
1074
|
-
[nil, nil]
|
1075
|
-
end
|
1076
|
-
end
|
1077
|
-
|
1078
|
-
def delayed_diff(from, to)
|
1079
|
-
delayed_literal do
|
1080
|
-
from, to = prepare_for_diff(from, to)
|
1081
|
-
|
1082
|
-
diff = "" if from.nil? or to.nil?
|
1083
|
-
diff ||= Diff.readable(from, to)
|
1084
|
-
if /^[-+]/ !~ diff
|
1085
|
-
diff = ""
|
1086
|
-
elsif /^[ ?]/ =~ diff or /(?:.*\n){2,}/ =~ diff
|
1087
|
-
diff = "\n\ndiff:\n#{diff}"
|
1088
|
-
else
|
1089
|
-
diff = ""
|
1090
|
-
end
|
1091
|
-
|
1092
|
-
if Diff.need_fold?(diff)
|
1093
|
-
folded_diff = Diff.folded_readable(from, to)
|
1094
|
-
diff << "\n\nfolded diff:\n#{folded_diff}"
|
1095
|
-
end
|
1096
|
-
|
1097
|
-
diff
|
1098
|
-
end
|
1099
|
-
end
|
1100
|
-
|
1101
|
-
def convert(object)
|
1102
|
-
case object
|
1103
|
-
when Exception
|
1104
|
-
<<EOM.chop
|
1105
|
-
Class: <#{convert(object.class)}>
|
1106
|
-
Message: <#{convert(object.message)}>
|
1107
|
-
---Backtrace---
|
1108
|
-
#{Util::BacktraceFilter.filter_backtrace(object.backtrace).join("\n")}
|
1109
|
-
---------------
|
1110
|
-
EOM
|
1111
|
-
else
|
1112
|
-
if use_pp
|
1113
|
-
begin
|
1114
|
-
require 'pp' unless defined?(PP)
|
1115
|
-
begin
|
1116
|
-
return PP.pp(object, '').chomp
|
1117
|
-
rescue NameError
|
1118
|
-
end
|
1119
|
-
rescue LoadError
|
1120
|
-
self.use_pp = false
|
1121
|
-
end
|
1122
|
-
end
|
1123
|
-
object.inspect
|
1124
|
-
end
|
1125
|
-
end
|
1126
|
-
end
|
1127
|
-
|
1128
|
-
class Literal
|
1129
|
-
def initialize(value)
|
1130
|
-
@value = value
|
1131
|
-
end
|
1132
|
-
|
1133
|
-
def inspect
|
1134
|
-
@value.to_s
|
1135
|
-
end
|
1136
|
-
end
|
1137
|
-
|
1138
|
-
class DelayedLiteral
|
1139
|
-
def initialize(value)
|
1140
|
-
@value = value
|
1141
|
-
end
|
1142
|
-
|
1143
|
-
def inspect
|
1144
|
-
@value.call.to_s
|
1145
|
-
end
|
1146
|
-
end
|
1147
|
-
|
1148
|
-
class MaybeContainer
|
1149
|
-
def initialize(value, &formatter)
|
1150
|
-
@value = value
|
1151
|
-
@formatter = formatter
|
1152
|
-
end
|
1153
|
-
|
1154
|
-
def inspect
|
1155
|
-
if @value.is_a?(Array)
|
1156
|
-
values = @value.collect do |value|
|
1157
|
-
@formatter.call(AssertionMessage.convert(value))
|
1158
|
-
end
|
1159
|
-
"[#{values.join(', ')}]"
|
1160
|
-
else
|
1161
|
-
@formatter.call(AssertionMessage.convert(@value))
|
1162
|
-
end
|
1163
|
-
end
|
1164
|
-
end
|
1165
|
-
|
1166
|
-
class Template
|
1167
|
-
def self.create(string)
|
1168
|
-
parts = (string ? string.scan(/(?=[^\\])\?|(?:\\\?|[^\?])+/m) : [])
|
1169
|
-
self.new(parts)
|
1170
|
-
end
|
1171
|
-
|
1172
|
-
attr_reader :count
|
1173
|
-
|
1174
|
-
def initialize(parts)
|
1175
|
-
@parts = parts
|
1176
|
-
@count = parts.find_all{|e| e == '?'}.size
|
1177
|
-
end
|
1178
|
-
|
1179
|
-
def result(parameters)
|
1180
|
-
raise "The number of parameters does not match the number of substitutions." if(parameters.size != count)
|
1181
|
-
params = parameters.dup
|
1182
|
-
@parts.collect{|e| e == '?' ? params.shift : e.gsub(/\\\?/m, '?')}.join('')
|
1183
|
-
end
|
1184
|
-
end
|
1185
|
-
|
1186
|
-
include Util::BacktraceFilter
|
1187
|
-
|
1188
|
-
def initialize(head, template_string, parameters)
|
1189
|
-
@head = head
|
1190
|
-
@template_string = template_string
|
1191
|
-
@parameters = parameters
|
1192
|
-
end
|
1193
|
-
|
1194
|
-
def convert(object)
|
1195
|
-
self.class.convert(object)
|
1196
|
-
end
|
1197
|
-
|
1198
|
-
def template
|
1199
|
-
@template ||= Template.create(@template_string)
|
1200
|
-
end
|
1201
|
-
|
1202
|
-
def add_period(string)
|
1203
|
-
(string =~ /\.\Z/ ? string : string + '.')
|
1204
|
-
end
|
1205
|
-
|
1206
|
-
def to_s
|
1207
|
-
message_parts = []
|
1208
|
-
if (@head)
|
1209
|
-
head = @head.to_s
|
1210
|
-
unless(head.empty?)
|
1211
|
-
message_parts << add_period(head)
|
1212
|
-
end
|
1213
|
-
end
|
1214
|
-
tail = template.result(@parameters.collect{|e| convert(e)})
|
1215
|
-
message_parts << tail unless(tail.empty?)
|
1216
|
-
message_parts.join("\n")
|
1217
|
-
end
|
1218
|
-
end
|
1219
|
-
|
1220
|
-
class AssertExceptionHelper
|
1221
|
-
class WrappedException
|
1222
|
-
def initialize(exception)
|
1223
|
-
@exception = exception
|
1224
|
-
end
|
1225
|
-
|
1226
|
-
def inspect
|
1227
|
-
if default_inspect?
|
1228
|
-
"#{@exception.class.inspect}(#{@exception.message.inspect})"
|
1229
|
-
else
|
1230
|
-
@exception.inspect
|
1231
|
-
end
|
1232
|
-
end
|
1233
|
-
|
1234
|
-
def method_missing(name, *args, &block)
|
1235
|
-
@exception.send(name, *args, &block)
|
1236
|
-
end
|
1237
|
-
|
1238
|
-
private
|
1239
|
-
def default_inspect?
|
1240
|
-
inspect_method = @exception.method(:inspect)
|
1241
|
-
if inspect_method.respond_to?(:owner) and
|
1242
|
-
inspect_method.owner == Exception
|
1243
|
-
true
|
1244
|
-
else
|
1245
|
-
default_inspect_method = Exception.instance_method(:inspect)
|
1246
|
-
default_inspect_method.bind(@exception).call == @exception.inspect
|
1247
|
-
end
|
1248
|
-
end
|
1249
|
-
end
|
1250
|
-
|
1251
|
-
def initialize(test_case, expected_exceptions)
|
1252
|
-
@test_case = test_case
|
1253
|
-
@expected_exceptions = expected_exceptions
|
1254
|
-
@expected_classes, @expected_modules, @expected_objects =
|
1255
|
-
split_expected_exceptions(expected_exceptions)
|
1256
|
-
end
|
1257
|
-
|
1258
|
-
def expected_exceptions
|
1259
|
-
exceptions = @expected_exceptions.collect do |exception|
|
1260
|
-
if exception.is_a?(Exception)
|
1261
|
-
WrappedException.new(exception)
|
1262
|
-
else
|
1263
|
-
exception
|
1264
|
-
end
|
1265
|
-
end
|
1266
|
-
if exceptions.size == 1
|
1267
|
-
exceptions[0]
|
1268
|
-
else
|
1269
|
-
exceptions
|
1270
|
-
end
|
1271
|
-
end
|
1272
|
-
|
1273
|
-
def expected?(actual_exception, equality=nil)
|
1274
|
-
equality ||= :instance_of?
|
1275
|
-
expected_class?(actual_exception, equality) or
|
1276
|
-
expected_module?(actual_exception) or
|
1277
|
-
expected_object?(actual_exception)
|
1278
|
-
end
|
1279
|
-
|
1280
|
-
private
|
1281
|
-
def split_expected_exceptions(expected_exceptions)
|
1282
|
-
exception_modules = []
|
1283
|
-
exception_objects = []
|
1284
|
-
exception_classes = []
|
1285
|
-
expected_exceptions.each do |exception_type|
|
1286
|
-
if exception_type.instance_of?(Module)
|
1287
|
-
exception_modules << exception_type
|
1288
|
-
elsif exception_type.is_a?(Exception)
|
1289
|
-
exception_objects << exception_type
|
1290
|
-
else
|
1291
|
-
@test_case.send(:assert,
|
1292
|
-
Exception >= exception_type,
|
1293
|
-
"Should expect a class of exception, " +
|
1294
|
-
"#{exception_type}")
|
1295
|
-
exception_classes << exception_type
|
1296
|
-
end
|
1297
|
-
end
|
1298
|
-
[exception_classes, exception_modules, exception_objects]
|
1299
|
-
end
|
1300
|
-
|
1301
|
-
def expected_class?(actual_exception, equality)
|
1302
|
-
@expected_classes.any? do |expected_class|
|
1303
|
-
actual_exception.send(equality, expected_class)
|
1304
|
-
end
|
1305
|
-
end
|
1306
|
-
|
1307
|
-
def expected_module?(actual_exception)
|
1308
|
-
@expected_modules.any? do |expected_module|
|
1309
|
-
actual_exception.is_a?(expected_module)
|
1310
|
-
end
|
1311
|
-
end
|
1312
|
-
|
1313
|
-
def expected_object?(actual_exception)
|
1314
|
-
@expected_objects.any? do |expected_object|
|
1315
|
-
expected_object == actual_exception or
|
1316
|
-
fallback_exception_object_equal(expected_object, actual_exception)
|
1317
|
-
end
|
1318
|
-
end
|
1319
|
-
|
1320
|
-
def fallback_exception_object_equal(expected_object, actual_exception)
|
1321
|
-
owner = Util::MethodOwnerFinder.find(expected_object, :==)
|
1322
|
-
if owner == Kernel or owner == Exception
|
1323
|
-
expected_object.class == actual_exception.class and
|
1324
|
-
expected_object.message == actual_exception.message
|
1325
|
-
else
|
1326
|
-
false
|
1327
|
-
end
|
1328
|
-
end
|
1329
|
-
end
|
1330
|
-
|
1331
|
-
# :startdoc:
|
1332
|
-
end
|
1333
|
-
end
|
1334
|
-
end
|