groonga 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (148) hide show
  1. data/AUTHORS +1 -0
  2. data/NEWS.ja.rdoc +5 -0
  3. data/NEWS.rdoc +5 -0
  4. data/README.ja.rdoc +53 -0
  5. data/README.rdoc +54 -0
  6. data/Rakefile +209 -0
  7. data/TUTORIAL.ja.rdoc +160 -0
  8. data/benchmark/small-many-items.rb +175 -0
  9. data/example/bookmark.rb +38 -0
  10. data/ext/.gitignore +2 -0
  11. data/ext/rb-grn-accessor.c +52 -0
  12. data/ext/rb-grn-array-cursor.c +28 -0
  13. data/ext/rb-grn-array.c +168 -0
  14. data/ext/rb-grn-column.c +273 -0
  15. data/ext/rb-grn-context.c +333 -0
  16. data/ext/rb-grn-database.c +128 -0
  17. data/ext/rb-grn-encoding.c +163 -0
  18. data/ext/rb-grn-exception.c +1014 -0
  19. data/ext/rb-grn-hash-cursor.c +30 -0
  20. data/ext/rb-grn-hash.c +40 -0
  21. data/ext/rb-grn-logger.c +277 -0
  22. data/ext/rb-grn-object.c +985 -0
  23. data/ext/rb-grn-patricia-trie-cursor.c +30 -0
  24. data/ext/rb-grn-patricia-trie.c +40 -0
  25. data/ext/rb-grn-procedure.c +52 -0
  26. data/ext/rb-grn-query.c +207 -0
  27. data/ext/rb-grn-record.c +33 -0
  28. data/ext/rb-grn-snippet.c +274 -0
  29. data/ext/rb-grn-table-cursor-key-support.c +55 -0
  30. data/ext/rb-grn-table-cursor.c +294 -0
  31. data/ext/rb-grn-table-key-support.c +299 -0
  32. data/ext/rb-grn-table.c +706 -0
  33. data/ext/rb-grn-type.c +114 -0
  34. data/ext/rb-grn-utils.c +578 -0
  35. data/ext/rb-grn.h +346 -0
  36. data/ext/rb-groonga.c +98 -0
  37. data/extconf.rb +171 -0
  38. data/html/bar.svg +153 -0
  39. data/html/developer.html +121 -0
  40. data/html/developer.svg +469 -0
  41. data/html/download.svg +253 -0
  42. data/html/footer.html.erb +28 -0
  43. data/html/head.html.erb +4 -0
  44. data/html/header.html.erb +17 -0
  45. data/html/index.html +153 -0
  46. data/html/install.svg +636 -0
  47. data/html/logo.xcf +0 -0
  48. data/html/ranguba.css +248 -0
  49. data/html/tutorial.svg +559 -0
  50. data/lib/groonga.rb +50 -0
  51. data/lib/groonga/record.rb +98 -0
  52. data/license/GPL +340 -0
  53. data/license/LGPL +504 -0
  54. data/license/RUBY +59 -0
  55. data/pkg-config.rb +328 -0
  56. data/test-unit/Rakefile +35 -0
  57. data/test-unit/TODO +5 -0
  58. data/test-unit/bin/testrb +5 -0
  59. data/test-unit/html/classic.html +15 -0
  60. data/test-unit/html/index.html +25 -0
  61. data/test-unit/html/index.html.ja +27 -0
  62. data/test-unit/lib/test/unit.rb +342 -0
  63. data/test-unit/lib/test/unit/assertionfailederror.rb +14 -0
  64. data/test-unit/lib/test/unit/assertions.rb +1149 -0
  65. data/test-unit/lib/test/unit/attribute.rb +125 -0
  66. data/test-unit/lib/test/unit/autorunner.rb +306 -0
  67. data/test-unit/lib/test/unit/collector.rb +43 -0
  68. data/test-unit/lib/test/unit/collector/descendant.rb +23 -0
  69. data/test-unit/lib/test/unit/collector/dir.rb +108 -0
  70. data/test-unit/lib/test/unit/collector/load.rb +135 -0
  71. data/test-unit/lib/test/unit/collector/objectspace.rb +34 -0
  72. data/test-unit/lib/test/unit/color-scheme.rb +86 -0
  73. data/test-unit/lib/test/unit/color.rb +96 -0
  74. data/test-unit/lib/test/unit/diff.rb +538 -0
  75. data/test-unit/lib/test/unit/error.rb +124 -0
  76. data/test-unit/lib/test/unit/exceptionhandler.rb +39 -0
  77. data/test-unit/lib/test/unit/failure.rb +110 -0
  78. data/test-unit/lib/test/unit/fixture.rb +176 -0
  79. data/test-unit/lib/test/unit/notification.rb +125 -0
  80. data/test-unit/lib/test/unit/omission.rb +143 -0
  81. data/test-unit/lib/test/unit/pending.rb +146 -0
  82. data/test-unit/lib/test/unit/priority.rb +161 -0
  83. data/test-unit/lib/test/unit/runner/console.rb +52 -0
  84. data/test-unit/lib/test/unit/runner/emacs.rb +8 -0
  85. data/test-unit/lib/test/unit/testcase.rb +360 -0
  86. data/test-unit/lib/test/unit/testresult.rb +89 -0
  87. data/test-unit/lib/test/unit/testsuite.rb +110 -0
  88. data/test-unit/lib/test/unit/ui/console/outputlevel.rb +14 -0
  89. data/test-unit/lib/test/unit/ui/console/testrunner.rb +220 -0
  90. data/test-unit/lib/test/unit/ui/emacs/testrunner.rb +49 -0
  91. data/test-unit/lib/test/unit/ui/testrunner.rb +20 -0
  92. data/test-unit/lib/test/unit/ui/testrunnermediator.rb +77 -0
  93. data/test-unit/lib/test/unit/ui/testrunnerutilities.rb +41 -0
  94. data/test-unit/lib/test/unit/util/backtracefilter.rb +41 -0
  95. data/test-unit/lib/test/unit/util/method-owner-finder.rb +28 -0
  96. data/test-unit/lib/test/unit/util/observable.rb +90 -0
  97. data/test-unit/lib/test/unit/util/procwrapper.rb +48 -0
  98. data/test-unit/lib/test/unit/version.rb +7 -0
  99. data/test-unit/sample/adder.rb +13 -0
  100. data/test-unit/sample/subtracter.rb +12 -0
  101. data/test-unit/sample/tc_adder.rb +18 -0
  102. data/test-unit/sample/tc_subtracter.rb +18 -0
  103. data/test-unit/sample/test_user.rb +22 -0
  104. data/test-unit/sample/ts_examples.rb +7 -0
  105. data/test-unit/test/collector/test-descendant.rb +135 -0
  106. data/test-unit/test/collector/test-load.rb +333 -0
  107. data/test-unit/test/collector/test_dir.rb +406 -0
  108. data/test-unit/test/collector/test_objectspace.rb +98 -0
  109. data/test-unit/test/run-test.rb +13 -0
  110. data/test-unit/test/test-attribute.rb +86 -0
  111. data/test-unit/test/test-color-scheme.rb +56 -0
  112. data/test-unit/test/test-color.rb +47 -0
  113. data/test-unit/test/test-diff.rb +477 -0
  114. data/test-unit/test/test-emacs-runner.rb +60 -0
  115. data/test-unit/test/test-fixture.rb +287 -0
  116. data/test-unit/test/test-notification.rb +33 -0
  117. data/test-unit/test/test-omission.rb +81 -0
  118. data/test-unit/test/test-pending.rb +70 -0
  119. data/test-unit/test/test-priority.rb +119 -0
  120. data/test-unit/test/test_assertions.rb +1082 -0
  121. data/test-unit/test/test_error.rb +26 -0
  122. data/test-unit/test/test_failure.rb +33 -0
  123. data/test-unit/test/test_testcase.rb +478 -0
  124. data/test-unit/test/test_testresult.rb +113 -0
  125. data/test-unit/test/test_testsuite.rb +129 -0
  126. data/test-unit/test/testunit-test-util.rb +14 -0
  127. data/test-unit/test/ui/test_testrunmediator.rb +20 -0
  128. data/test-unit/test/util/test-method-owner-finder.rb +38 -0
  129. data/test-unit/test/util/test_backtracefilter.rb +41 -0
  130. data/test-unit/test/util/test_observable.rb +102 -0
  131. data/test-unit/test/util/test_procwrapper.rb +36 -0
  132. data/test/.gitignore +1 -0
  133. data/test/groonga-test-utils.rb +90 -0
  134. data/test/run-test.rb +54 -0
  135. data/test/test-column.rb +190 -0
  136. data/test/test-context.rb +90 -0
  137. data/test/test-database.rb +62 -0
  138. data/test/test-encoding.rb +33 -0
  139. data/test/test-exception.rb +85 -0
  140. data/test/test-procedure.rb +35 -0
  141. data/test/test-query.rb +22 -0
  142. data/test/test-record.rb +188 -0
  143. data/test/test-snippet.rb +121 -0
  144. data/test/test-table-cursor.rb +51 -0
  145. data/test/test-table.rb +447 -0
  146. data/test/test-type.rb +52 -0
  147. data/test/test-version.rb +31 -0
  148. metadata +213 -0
@@ -0,0 +1,25 @@
1
+ <?xml version="1.0" encoding="utf-8" ?>
2
+ <!DOCTYPE html
3
+ PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
+ <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
6
+ <head>
7
+ <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
8
+ <title>Test::Unit - a Unit Testing Framework for Ruby</title>
9
+ </head>
10
+ <body>
11
+ <h1>Test::Unit - a Unit Testing Framework for Ruby</h1>
12
+ <p>[<a href="index.html.ja">Japanese</a>]</p>
13
+ <ul>
14
+ <li><a href="http://rubyforge.org/projects/test-unit/">Project Page</a></li>
15
+ <li>
16
+ <a href="test-unit/">RDoc</a>
17
+ <!--(<a href="ja/test-unit/">ja</a>)-->
18
+ </li>
19
+ <li>
20
+ <a href="classic.html">Classic</a>
21
+ <!--(<a href="ja/classic.html">ja</a>)-->
22
+ </li>
23
+ </ul>
24
+ </body>
25
+ </html>
@@ -0,0 +1,27 @@
1
+ <?xml version="1.0" encoding="utf-8" ?>
2
+ <!DOCTYPE html
3
+ PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
+ <html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">
6
+ <head>
7
+ <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
8
+ <title>Test::Unit - Ruby用単体テストフレームワーク</title>
9
+ </head>
10
+ <body>
11
+ <h1>Test::Unit - Ruby用単体テストフレームワーク</h1>
12
+ <p>[<a href="./">English</a>]</p>
13
+ <ul>
14
+ <li>
15
+ <a href="http://rubyforge.org/projects/test-unit/">プロジェクトページ</a>
16
+ </li>
17
+ <li>
18
+ <!--<a href="ja/test-unit/">RDoc</a>-->
19
+ RDoc(<a href="test-unit/">en</a>)
20
+ </li>
21
+ <li>
22
+ <!--<a href="classic.html.html">Classic</a>-->
23
+ Classic(<a href="classic.html">en</a>)
24
+ </li>
25
+ </ul>
26
+ </body>
27
+ </html>
@@ -0,0 +1,342 @@
1
+ require 'test/unit/testcase'
2
+ require 'test/unit/autorunner'
3
+
4
+ module Test # :nodoc:
5
+ #
6
+ # = Test::Unit - Ruby Unit Testing Framework
7
+ #
8
+ # == Introduction
9
+ #
10
+ # Unit testing is making waves all over the place, largely due to the
11
+ # fact that it is a core practice of XP. While XP is great, unit testing
12
+ # has been around for a long time and has always been a good idea. One
13
+ # of the keys to good unit testing, though, is not just writing tests,
14
+ # but having tests. What's the difference? Well, if you just _write_ a
15
+ # test and throw it away, you have no guarantee that something won't
16
+ # change later which breaks your code. If, on the other hand, you _have_
17
+ # tests (obviously you have to write them first), and run them as often
18
+ # as possible, you slowly build up a wall of things that cannot break
19
+ # without you immediately knowing about it. This is when unit testing
20
+ # hits its peak usefulness.
21
+ #
22
+ # Enter Test::Unit, a framework for unit testing in Ruby, helping you to
23
+ # design, debug and evaluate your code by making it easy to write and
24
+ # have tests for it.
25
+ #
26
+ #
27
+ # == Notes
28
+ #
29
+ # Test::Unit has grown out of and superceded Lapidary.
30
+ #
31
+ #
32
+ # == Feedback
33
+ #
34
+ # I like (and do my best to practice) XP, so I value early releases,
35
+ # user feedback, and clean, simple, expressive code. There is always
36
+ # room for improvement in everything I do, and Test::Unit is no
37
+ # exception. Please, let me know what you think of Test::Unit as it
38
+ # stands, and what you'd like to see expanded/changed/improved/etc. If
39
+ # you find a bug, let me know ASAP; one good way to let me know what the
40
+ # bug is is to submit a new test that catches it :-) Also, I'd love to
41
+ # hear about any successes you have with Test::Unit, and any
42
+ # documentation you might add will be greatly appreciated. My contact
43
+ # info is below.
44
+ #
45
+ #
46
+ # == Contact Information
47
+ #
48
+ # A lot of discussion happens about Ruby in general on the ruby-talk
49
+ # mailing list (http://www.ruby-lang.org/en/ml.html), and you can ask
50
+ # any questions you might have there. I monitor the list, as do many
51
+ # other helpful Rubyists, and you're sure to get a quick answer. Of
52
+ # course, you're also welcome to email me (Nathaniel Talbott) directly
53
+ # at mailto:testunit@talbott.ws, and I'll do my best to help you out.
54
+ #
55
+ #
56
+ # == Credits
57
+ #
58
+ # I'd like to thank...
59
+ #
60
+ # Matz, for a great language!
61
+ #
62
+ # Masaki Suketa, for his work on RubyUnit, which filled a vital need in
63
+ # the Ruby world for a very long time. I'm also grateful for his help in
64
+ # polishing Test::Unit and getting the RubyUnit compatibility layer
65
+ # right. His graciousness in allowing Test::Unit to supercede RubyUnit
66
+ # continues to be a challenge to me to be more willing to defer my own
67
+ # rights.
68
+ #
69
+ # Ken McKinlay, for his interest and work on unit testing, and for his
70
+ # willingness to dialog about it. He was also a great help in pointing
71
+ # out some of the holes in the RubyUnit compatibility layer.
72
+ #
73
+ # Dave Thomas, for the original idea that led to the extremely simple
74
+ # "require 'test/unit'", plus his code to improve it even more by
75
+ # allowing the selection of tests from the command-line. Also, without
76
+ # RDoc, the documentation for Test::Unit would stink a lot more than it
77
+ # does now.
78
+ #
79
+ # Everyone who's helped out with bug reports, feature ideas,
80
+ # encouragement to continue, etc. It's a real privilege to be a part of
81
+ # the Ruby community.
82
+ #
83
+ # The guys at RoleModel Software, for putting up with me repeating, "But
84
+ # this would be so much easier in Ruby!" whenever we're coding in Java.
85
+ #
86
+ # My Creator, for giving me life, and giving it more abundantly.
87
+ #
88
+ #
89
+ # == License
90
+ #
91
+ # Test::Unit is copyright (c) 2000-2003 Nathaniel Talbott. It is free
92
+ # software, and is distributed under the Ruby license. See the COPYING
93
+ # file in the standard Ruby distribution for details.
94
+ #
95
+ #
96
+ # == Warranty
97
+ #
98
+ # This software is provided "as is" and without any express or
99
+ # implied warranties, including, without limitation, the implied
100
+ # warranties of merchantibility and fitness for a particular
101
+ # purpose.
102
+ #
103
+ #
104
+ # == Author
105
+ #
106
+ # Nathaniel Talbott.
107
+ # Copyright (c) 2000-2003, Nathaniel Talbott
108
+ #
109
+ # ----
110
+ #
111
+ # = Usage
112
+ #
113
+ # The general idea behind unit testing is that you write a _test_
114
+ # _method_ that makes certain _assertions_ about your code, working
115
+ # against a _test_ _fixture_. A bunch of these _test_ _methods_ are
116
+ # bundled up into a _test_ _suite_ and can be run any time the
117
+ # developer wants. The results of a run are gathered in a _test_
118
+ # _result_ and displayed to the user through some UI. So, lets break
119
+ # this down and see how Test::Unit provides each of these necessary
120
+ # pieces.
121
+ #
122
+ #
123
+ # == Assertions
124
+ #
125
+ # These are the heart of the framework. Think of an assertion as a
126
+ # statement of expected outcome, i.e. "I assert that x should be equal
127
+ # to y". If, when the assertion is executed, it turns out to be
128
+ # correct, nothing happens, and life is good. If, on the other hand,
129
+ # your assertion turns out to be false, an error is propagated with
130
+ # pertinent information so that you can go back and make your
131
+ # assertion succeed, and, once again, life is good. For an explanation
132
+ # of the current assertions, see Test::Unit::Assertions.
133
+ #
134
+ #
135
+ # == Test Method & Test Fixture
136
+ #
137
+ # Obviously, these assertions have to be called within a context that
138
+ # knows about them and can do something meaningful with their
139
+ # pass/fail value. Also, it's handy to collect a bunch of related
140
+ # tests, each test represented by a method, into a common test class
141
+ # that knows how to run them. The tests will be in a separate class
142
+ # from the code they're testing for a couple of reasons. First of all,
143
+ # it allows your code to stay uncluttered with test code, making it
144
+ # easier to maintain. Second, it allows the tests to be stripped out
145
+ # for deployment, since they're really there for you, the developer,
146
+ # and your users don't need them. Third, and most importantly, it
147
+ # allows you to set up a common test fixture for your tests to run
148
+ # against.
149
+ #
150
+ # What's a test fixture? Well, tests do not live in a vacuum; rather,
151
+ # they're run against the code they are testing. Often, a collection
152
+ # of tests will run against a common set of data, also called a
153
+ # fixture. If they're all bundled into the same test class, they can
154
+ # all share the setting up and tearing down of that data, eliminating
155
+ # unnecessary duplication and making it much easier to add related
156
+ # tests.
157
+ #
158
+ # Test::Unit::TestCase wraps up a collection of test methods together
159
+ # and allows you to easily set up and tear down the same test fixture
160
+ # for each test. This is done by overriding #setup and/or #teardown,
161
+ # which will be called before and after each test method that is
162
+ # run. The TestCase also knows how to collect the results of your
163
+ # assertions into a Test::Unit::TestResult, which can then be reported
164
+ # back to you... but I'm getting ahead of myself. To write a test,
165
+ # follow these steps:
166
+ #
167
+ # * Make sure Test::Unit is in your library path.
168
+ # * require 'test/unit' in your test script.
169
+ # * Create a class that subclasses Test::Unit::TestCase.
170
+ # * Add a method that begins with "test" to your class.
171
+ # * Make assertions in your test method.
172
+ # * Optionally define #setup and/or #teardown to set up and/or tear
173
+ # down your common test fixture.
174
+ # * You can now run your test as you would any other Ruby
175
+ # script... try it and see!
176
+ #
177
+ # A really simple test might look like this (#setup and #teardown are
178
+ # commented out to indicate that they are completely optional):
179
+ #
180
+ # require 'test/unit'
181
+ #
182
+ # class TC_MyTest < Test::Unit::TestCase
183
+ # # def setup
184
+ # # end
185
+ #
186
+ # # def teardown
187
+ # # end
188
+ #
189
+ # def test_fail
190
+ # assert(false, 'Assertion was false.')
191
+ # end
192
+ # end
193
+ #
194
+ #
195
+ # == Test Runners
196
+ #
197
+ # So, now you have this great test class, but you still need a way to
198
+ # run it and view any failures that occur during the run. This is
199
+ # where Test::Unit::UI::Console::TestRunner (and others, such as
200
+ # Test::Unit::UI::GTK::TestRunner) comes into play. The console test
201
+ # runner is automatically invoked for you if you require 'test/unit'
202
+ # and simply run the file. To use another runner, or to manually
203
+ # invoke a runner, simply call its run class method and pass in an
204
+ # object that responds to the suite message with a
205
+ # Test::Unit::TestSuite. This can be as simple as passing in your
206
+ # TestCase class (which has a class suite method). It might look
207
+ # something like this:
208
+ #
209
+ # require 'test/unit/ui/console/testrunner'
210
+ # Test::Unit::UI::Console::TestRunner.run(TC_MyTest)
211
+ #
212
+ #
213
+ # == Test Suite
214
+ #
215
+ # As more and more unit tests accumulate for a given project, it
216
+ # becomes a real drag running them one at a time, and it also
217
+ # introduces the potential to overlook a failing test because you
218
+ # forget to run it. Suddenly it becomes very handy that the
219
+ # TestRunners can take any object that returns a Test::Unit::TestSuite
220
+ # in response to a suite method. The TestSuite can, in turn, contain
221
+ # other TestSuites or individual tests (typically created by a
222
+ # TestCase). In other words, you can easily wrap up a group of
223
+ # TestCases and TestSuites like this:
224
+ #
225
+ # require 'test/unit/testsuite'
226
+ # require 'tc_myfirsttests'
227
+ # require 'tc_moretestsbyme'
228
+ # require 'ts_anothersetoftests'
229
+ #
230
+ # class TS_MyTests
231
+ # def self.suite
232
+ # suite = Test::Unit::TestSuite.new
233
+ # suite << TC_MyFirstTests.suite
234
+ # suite << TC_MoreTestsByMe.suite
235
+ # suite << TS_AnotherSetOfTests.suite
236
+ # return suite
237
+ # end
238
+ # end
239
+ # Test::Unit::UI::Console::TestRunner.run(TS_MyTests)
240
+ #
241
+ # Now, this is a bit cumbersome, so Test::Unit does a little bit more
242
+ # for you, by wrapping these up automatically when you require
243
+ # 'test/unit'. What does this mean? It means you could write the above
244
+ # test case like this instead:
245
+ #
246
+ # require 'test/unit'
247
+ # require 'tc_myfirsttests'
248
+ # require 'tc_moretestsbyme'
249
+ # require 'ts_anothersetoftests'
250
+ #
251
+ # Test::Unit is smart enough to find all the test cases existing in
252
+ # the ObjectSpace and wrap them up into a suite for you. It then runs
253
+ # the dynamic suite using the console TestRunner.
254
+ #
255
+ #
256
+ # == Configuration file
257
+ #
258
+ # Test::Unit reads 'test-unit.yml' in the current working
259
+ # directory as Test::Unit's configuration file. It can
260
+ # contain the following configurations:
261
+ #
262
+ # * color scheme definitions
263
+ # * test runner to be used
264
+ # * test runner options
265
+ # * test collector to be used
266
+ #
267
+ # Except color scheme definitions, all of them are
268
+ # specified by command line option.
269
+ #
270
+ # Here are sample color scheme definitions:
271
+ #
272
+ # color_schemes:
273
+ # inverted:
274
+ # success:
275
+ # name: red
276
+ # bold: true
277
+ # failure:
278
+ # name: green
279
+ # bold: true
280
+ # other_scheme:
281
+ # ...
282
+ #
283
+ # Here are the syntax of color scheme definitions:
284
+ #
285
+ # color_schemes:
286
+ # SCHEME_NAME:
287
+ # EVENT_NAME:
288
+ # name: COLOR_NAME
289
+ # intensity: BOOLEAN
290
+ # bold: BOOLEAN
291
+ # italic: BOOLEAN
292
+ # underline: BOOLEAN
293
+ # ...
294
+ # ...
295
+ #
296
+ # SCHEME_NAME:: the name of the color scheme
297
+ # EVENT_NAME:: one of [success, failure, pending,
298
+ # omission, notification, error]
299
+ # COLOR_NAME:: one of [black, red, green, yellow, blue,
300
+ # magenta, cyan, white]
301
+ # BOOLEAN:: true or false
302
+ #
303
+ # You can use the above 'inverted' color scheme with the
304
+ # following configuration:
305
+ #
306
+ # runner: console
307
+ # console_options:
308
+ # color_scheme: inverted
309
+ # color_schemes:
310
+ # inverted:
311
+ # success:
312
+ # name: red
313
+ # bold: true
314
+ # failure:
315
+ # name: green
316
+ # bold: true
317
+ #
318
+ # == Questions?
319
+ #
320
+ # I'd really like to get feedback from all levels of Ruby
321
+ # practitioners about typos, grammatical errors, unclear statements,
322
+ # missing points, etc., in this document (or any other).
323
+ #
324
+
325
+ module Unit
326
+ # If set to false Test::Unit will not automatically run at exit.
327
+ def self.run=(flag)
328
+ @run = flag
329
+ end
330
+
331
+ # Automatically run tests at exit?
332
+ def self.run?
333
+ @run ||= false
334
+ end
335
+ end
336
+ end
337
+
338
+ at_exit do
339
+ unless $! || Test::Unit.run?
340
+ exit Test::Unit::AutoRunner.run
341
+ end
342
+ end
@@ -0,0 +1,14 @@
1
+ #--
2
+ #
3
+ # Author:: Nathaniel Talbott.
4
+ # Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved.
5
+ # License:: Ruby license.
6
+
7
+ module Test
8
+ module Unit
9
+
10
+ # Thrown by Test::Unit::Assertions when an assertion fails.
11
+ class AssertionFailedError < StandardError
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,1149 @@
1
+ # Author:: Nathaniel Talbott.
2
+ # Copyright:: Copyright (c) 2000-2003 Nathaniel Talbott. All rights reserved.
3
+ # Copyright (c) 2009 Kouhei Sutou.
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
+ full_message = build_message(message, <<EOT, expected, actual, diff)
84
+ <?> expected but was
85
+ <?>.?
86
+ EOT
87
+ assert_block(full_message) { expected == actual }
88
+ end
89
+
90
+ ##
91
+ # Passes if the block raises one of the expected
92
+ # exceptions. When an expected exception is an Exception
93
+ # object, passes if expected_exception == actual_exception.
94
+ #
95
+ # Example:
96
+ # assert_raise(RuntimeError, LoadError) do
97
+ # raise 'Boom!!!'
98
+ # end # -> pass
99
+ #
100
+ # assert_raise do
101
+ # raise Exception, 'Any exception should be raised!!!'
102
+ # end # -> pass
103
+ #
104
+ # assert_raise(RuntimeError.new("XXX")) {raise "XXX"} # -> pass
105
+ # assert_raise(MyError.new("XXX")) {raise "XXX"} # -> fail
106
+ # assert_raise(RuntimeError.new("ZZZ")) {raise "XXX"} # -> fail
107
+ public
108
+ def assert_raise(*args, &block)
109
+ assert_expected_exception = Proc.new do |*_args|
110
+ message, assert_exception_helper, actual_exception = _args
111
+ expected = assert_exception_helper.expected_exceptions
112
+ full_message = build_message(message,
113
+ "<?> exception expected but was\n?",
114
+ expected, actual_exception)
115
+ assert_block(full_message) do
116
+ expected == [] or assert_exception_helper.expected?(actual_exception)
117
+ end
118
+ end
119
+ _assert_raise(assert_expected_exception, *args, &block)
120
+ end
121
+
122
+ ##
123
+ # Alias of assert_raise.
124
+ #
125
+ # Will be deprecated in 1.9, and removed in 2.0.
126
+
127
+ public
128
+ def assert_raises(*args, &block)
129
+ assert_raise(*args, &block)
130
+ end
131
+
132
+ ##
133
+ # Passes if the block raises one of the given
134
+ # exceptions or sub exceptions of the given exceptions.
135
+ #
136
+ # Example:
137
+ # assert_raise_kind_of(SystemCallError) do
138
+ # raise Errno::EACCES
139
+ # end
140
+ def assert_raise_kind_of(*args, &block)
141
+ assert_expected_exception = Proc.new do |*_args|
142
+ message, assert_exception_helper, actual_exception = _args
143
+ expected = assert_exception_helper.expected_exceptions
144
+ full_message = build_message(message,
145
+ "<?> family exception expected " +
146
+ "but was\n?",
147
+ expected, actual_exception)
148
+ assert_block(full_message) do
149
+ assert_exception_helper.expected?(actual_exception, :kind_of?)
150
+ end
151
+ end
152
+ _assert_raise(assert_expected_exception, *args, &block)
153
+ end
154
+
155
+
156
+ ##
157
+ # Passes if +object+.instance_of?(+klass+). When +klass+ is
158
+ # an array of classes, it passes if any class
159
+ # satisfies +object.instance_of?(class).
160
+ #
161
+ # Example:
162
+ # assert_instance_of(String, 'foo') # -> pass
163
+ # assert_instance_of([Fixnum, NilClass], 100) # -> pass
164
+ # assert_instance_of([Numeric, NilClass], 100) # -> fail
165
+
166
+ public
167
+ def assert_instance_of(klass, object, message="")
168
+ _wrap_assertion do
169
+ klasses = nil
170
+ klasses = klass if klass.is_a?(Array)
171
+ assert_block("The first parameter to assert_instance_of should be " +
172
+ "a Class or an Array of Class.") do
173
+ if klasses
174
+ klasses.all? {|k| k.is_a?(Class)}
175
+ else
176
+ klass.is_a?(Class)
177
+ end
178
+ end
179
+ klass_message = AssertionMessage.maybe_container(klass) do |value|
180
+ "<#{value}>"
181
+ end
182
+ full_message = build_message(message, <<EOT, object, klass_message, object.class)
183
+ <?> expected to be an instance of
184
+ ? but was
185
+ <?>.
186
+ EOT
187
+ assert_block(full_message) do
188
+ if klasses
189
+ klasses.any? {|k| object.instance_of?(k)}
190
+ else
191
+ object.instance_of?(klass)
192
+ end
193
+ end
194
+ end
195
+ end
196
+
197
+ ##
198
+ # Passes if +object+ is nil.
199
+ #
200
+ # Example:
201
+ # assert_nil [1, 2].uniq!
202
+
203
+ public
204
+ def assert_nil(object, message="")
205
+ full_message = build_message(message, <<EOT, object)
206
+ <?> expected to be nil.
207
+ EOT
208
+ assert_block(full_message) { object.nil? }
209
+ end
210
+
211
+ ##
212
+ # Passes if +object+.kind_of?(+klass+). When +klass+ is
213
+ # an array of classes or modules, it passes if any
214
+ # class or module satisfies +object.kind_of?(class_or_module).
215
+ #
216
+ # Example:
217
+ # assert_kind_of(Object, 'foo') # -> pass
218
+ # assert_kind_of([Fixnum, NilClass], 100) # -> pass
219
+ # assert_kind_of([Fixnum, NilClass], "string") # -> fail
220
+
221
+ public
222
+ def assert_kind_of(klass, object, message="")
223
+ _wrap_assertion do
224
+ klasses = nil
225
+ klasses = klass if klass.is_a?(Array)
226
+ assert_block("The first parameter to assert_kind_of should be " +
227
+ "a kind_of Module or an Array of a kind_of Module.") do
228
+ if klasses
229
+ klasses.all? {|k| k.kind_of?(Module)}
230
+ else
231
+ klass.kind_of?(Module)
232
+ end
233
+ end
234
+ klass_message = AssertionMessage.maybe_container(klass) do |value|
235
+ "<#{value}>"
236
+ end
237
+ full_message = build_message(message,
238
+ "<?> expected to be kind_of\\?\n" +
239
+ "? but was\n" +
240
+ "<?>.",
241
+ object,
242
+ klass_message,
243
+ object.class)
244
+ assert_block(full_message) do
245
+ if klasses
246
+ klasses.any? {|k| object.kind_of?(k)}
247
+ else
248
+ object.kind_of?(klass)
249
+ end
250
+ end
251
+ end
252
+ end
253
+
254
+ ##
255
+ # Passes if +object+ .respond_to? +method+
256
+ #
257
+ # Example:
258
+ # assert_respond_to 'bugbear', :slice
259
+
260
+ public
261
+ def assert_respond_to(object, method, message="")
262
+ _wrap_assertion do
263
+ full_message = build_message(message,
264
+ "<?>.kind_of\\?(Symbol) or\n" +
265
+ "<?>.respond_to\\?(:to_str) expected",
266
+ method, method)
267
+ assert_block(full_message) do
268
+ method.kind_of?(Symbol) or method.respond_to?(:to_str)
269
+ end
270
+ full_message = build_message(message,
271
+ "<?>.respond_to\\?(?) expected\n" +
272
+ "(Class: <?>)",
273
+ object, method, object.class)
274
+ assert_block(full_message) {object.respond_to?(method)}
275
+ end
276
+ end
277
+
278
+ ##
279
+ # Passes if +string+ =~ +pattern+.
280
+ #
281
+ # Example:
282
+ # assert_match(/\d+/, 'five, 6, seven')
283
+
284
+ public
285
+ def assert_match(pattern, string, message="")
286
+ _wrap_assertion do
287
+ pattern = case(pattern)
288
+ when String
289
+ Regexp.new(Regexp.escape(pattern))
290
+ else
291
+ pattern
292
+ end
293
+ full_message = build_message(message, "<?> expected to be =~\n<?>.", string, pattern)
294
+ assert_block(full_message) { string =~ pattern }
295
+ end
296
+ end
297
+
298
+ ##
299
+ # Passes if +actual+ .equal? +expected+ (i.e. they are the same
300
+ # instance).
301
+ #
302
+ # Example:
303
+ # o = Object.new
304
+ # assert_same o, o
305
+
306
+ public
307
+ def assert_same(expected, actual, message="")
308
+ full_message = build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__)
309
+ <?>
310
+ with id <?> expected to be equal\\? to
311
+ <?>
312
+ with id <?>.
313
+ EOT
314
+ assert_block(full_message) { actual.equal?(expected) }
315
+ end
316
+
317
+ ##
318
+ # Compares the +object1+ with +object2+ using +operator+.
319
+ #
320
+ # Passes if object1.__send__(operator, object2) is true.
321
+ #
322
+ # Example:
323
+ # assert_operator 5, :>=, 4
324
+
325
+ public
326
+ def assert_operator(object1, operator, object2, message="")
327
+ _wrap_assertion do
328
+ full_message = build_message(nil, "<?>\ngiven as the operator for #assert_operator must be a Symbol or #respond_to\\?(:to_str).", operator)
329
+ assert_block(full_message){operator.kind_of?(Symbol) || operator.respond_to?(:to_str)}
330
+ full_message = build_message(message, <<EOT, object1, AssertionMessage.literal(operator), object2)
331
+ <?> expected to be
332
+ ?
333
+ <?>.
334
+ EOT
335
+ assert_block(full_message) { object1.__send__(operator, object2) }
336
+ end
337
+ end
338
+
339
+ ##
340
+ # Passes if block does not raise an exception.
341
+ #
342
+ # Example:
343
+ # assert_nothing_raised do
344
+ # [1, 2].uniq
345
+ # end
346
+
347
+ public
348
+ def assert_nothing_raised(*args)
349
+ _wrap_assertion do
350
+ if args.last.is_a?(String)
351
+ message = args.pop
352
+ else
353
+ message = ""
354
+ end
355
+
356
+ assert_exception_helper = AssertExceptionHelper.new(self, args)
357
+ begin
358
+ yield
359
+ rescue Exception => e
360
+ if ((args.empty? && !e.instance_of?(AssertionFailedError)) ||
361
+ assert_exception_helper.expected?(e))
362
+ failure_message = build_message(message, "Exception raised:\n?", e)
363
+ assert_block(failure_message) {false}
364
+ else
365
+ raise
366
+ end
367
+ end
368
+ nil
369
+ end
370
+ end
371
+
372
+ ##
373
+ # Flunk always fails.
374
+ #
375
+ # Example:
376
+ # flunk 'Not done testing yet.'
377
+
378
+ public
379
+ def flunk(message="Flunked")
380
+ assert_block(build_message(message)){false}
381
+ end
382
+
383
+ ##
384
+ # Passes if ! +actual+ .equal? +expected+
385
+ #
386
+ # Example:
387
+ # assert_not_same Object.new, Object.new
388
+
389
+ public
390
+ def assert_not_same(expected, actual, message="")
391
+ full_message = build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__)
392
+ <?>
393
+ with id <?> expected to not be equal\\? to
394
+ <?>
395
+ with id <?>.
396
+ EOT
397
+ assert_block(full_message) { !actual.equal?(expected) }
398
+ end
399
+
400
+ ##
401
+ # Passes if +expected+ != +actual+
402
+ #
403
+ # Example:
404
+ # assert_not_equal 'some string', 5
405
+
406
+ public
407
+ def assert_not_equal(expected, actual, message="")
408
+ full_message = build_message(message, "<?> expected to be != to\n<?>.", expected, actual)
409
+ assert_block(full_message) { expected != actual }
410
+ end
411
+
412
+ ##
413
+ # Passes if ! +object+ .nil?
414
+ #
415
+ # Example:
416
+ # assert_not_nil '1 two 3'.sub!(/two/, '2')
417
+
418
+ public
419
+ def assert_not_nil(object, message="")
420
+ full_message = build_message(message, "<?> expected to not be nil.", object)
421
+ assert_block(full_message){!object.nil?}
422
+ end
423
+
424
+ ##
425
+ # Passes if +regexp+ !~ +string+
426
+ #
427
+ # Example:
428
+ # assert_no_match(/two/, 'one 2 three')
429
+
430
+ public
431
+ def assert_no_match(regexp, string, message="")
432
+ _wrap_assertion do
433
+ assert_instance_of(Regexp, regexp, "The first argument to assert_no_match should be a Regexp.")
434
+ full_message = build_message(message, "<?> expected to not match\n<?>.", regexp, string)
435
+ assert_block(full_message) { regexp !~ string }
436
+ end
437
+ end
438
+
439
+ UncaughtThrow = {
440
+ NameError => /^uncaught throw \`(.+)\'$/,
441
+ ArgumentError => /^uncaught throw (.+)$/,
442
+ ThreadError => /^uncaught throw \`(.+)\' in thread /
443
+ } #`
444
+
445
+ ##
446
+ # Passes if the block throws +expected_object+
447
+ #
448
+ # Example:
449
+ # assert_throw(:done) do
450
+ # throw(:done)
451
+ # end
452
+
453
+ public
454
+ def assert_throw(expected_object, message="", &proc)
455
+ _wrap_assertion do
456
+ begin
457
+ catch([]) {}
458
+ rescue TypeError
459
+ assert_instance_of(Symbol, expected_object,
460
+ "assert_throws expects the symbol that should be thrown for its first argument")
461
+ end
462
+ assert_block("Should have passed a block to assert_throw.") do
463
+ block_given?
464
+ end
465
+ caught = true
466
+ begin
467
+ catch(expected_object) do
468
+ proc.call
469
+ caught = false
470
+ end
471
+ full_message = build_message(message,
472
+ "<?> should have been thrown.",
473
+ expected_object)
474
+ assert_block(full_message) {caught}
475
+ rescue NameError, ArgumentError, ThreadError => error
476
+ raise unless UncaughtThrow[error.class] =~ error.message
477
+ tag = $1
478
+ tag = tag[1..-1].intern if tag[0, 1] == ":"
479
+ full_message = build_message(message,
480
+ "<?> expected to be thrown but\n" +
481
+ "<?> was thrown.",
482
+ expected_object, tag)
483
+ flunk(full_message)
484
+ end
485
+ end
486
+ end
487
+
488
+ ##
489
+ # Alias of assert_throw.
490
+ #
491
+ # Will be deprecated in 1.9, and removed in 2.0.
492
+ def assert_throws(*args, &block)
493
+ assert_throw(*args, &block)
494
+ end
495
+
496
+ ##
497
+ # Passes if block does not throw anything.
498
+ #
499
+ # Example:
500
+ # assert_nothing_thrown do
501
+ # [1, 2].uniq
502
+ # end
503
+
504
+ public
505
+ def assert_nothing_thrown(message="", &proc)
506
+ _wrap_assertion do
507
+ assert(block_given?, "Should have passed a block to assert_nothing_thrown")
508
+ begin
509
+ proc.call
510
+ rescue NameError, ArgumentError, ThreadError => error
511
+ raise unless UncaughtThrow[error.class] =~ error.message
512
+ tag = $1
513
+ tag = tag[1..-1].intern if tag[0, 1] == ":"
514
+ full_message = build_message(message,
515
+ "<?> was thrown when nothing was expected",
516
+ tag)
517
+ flunk(full_message)
518
+ end
519
+ assert(true, "Expected nothing to be thrown")
520
+ end
521
+ end
522
+
523
+ ##
524
+ # Passes if +expected_float+ and +actual_float+ are equal
525
+ # within +delta+ tolerance.
526
+ #
527
+ # Example:
528
+ # assert_in_delta 0.05, (50000.0 / 10**6), 0.00001
529
+
530
+ public
531
+ def assert_in_delta(expected_float, actual_float, delta, message="")
532
+ _wrap_assertion do
533
+ {expected_float => "first float", actual_float => "second float", delta => "delta"}.each do |float, name|
534
+ assert_respond_to(float, :to_f, "The arguments must respond to to_f; the #{name} did not")
535
+ end
536
+ assert_operator(delta, :>=, 0.0, "The delta should not be negative")
537
+ full_message = build_message(message, <<EOT, expected_float, actual_float, delta)
538
+ <?> and
539
+ <?> expected to be within
540
+ <?> of each other.
541
+ EOT
542
+ assert_block(full_message) { (expected_float.to_f - actual_float.to_f).abs <= delta.to_f }
543
+ end
544
+ end
545
+
546
+ ##
547
+ # Passes if the method send returns a true value.
548
+ #
549
+ # +send_array+ is composed of:
550
+ # * A receiver
551
+ # * A method
552
+ # * Arguments to the method
553
+ #
554
+ # Example:
555
+ # assert_send [[1, 2], :include?, 4]
556
+
557
+ public
558
+ def assert_send(send_array, message="")
559
+ _wrap_assertion do
560
+ assert_instance_of(Array, send_array, "assert_send requires an array of send information")
561
+ assert(send_array.size >= 2, "assert_send requires at least a receiver and a message name")
562
+ full_message = build_message(message, <<EOT, send_array[0], AssertionMessage.literal(send_array[1].to_s), send_array[2..-1])
563
+ <?> expected to respond to
564
+ <?(?)> with a true value.
565
+ EOT
566
+ assert_block(full_message) { send_array[0].__send__(send_array[1], *send_array[2..-1]) }
567
+ end
568
+ end
569
+
570
+ ##
571
+ # Passes if +actual+ is a boolean value.
572
+ #
573
+ # Example:
574
+ # assert_boolean(true) # -> pass
575
+ # assert_boolean(nil) # -> fail
576
+ def assert_boolean(actual, message=nil)
577
+ _wrap_assertion do
578
+ assert_block(build_message(message,
579
+ "<true> or <false> expected but was\n<?>",
580
+ actual)) do
581
+ [true, false].include?(actual)
582
+ end
583
+ end
584
+ end
585
+
586
+ ##
587
+ # Passes if +actual+ is true.
588
+ #
589
+ # Example:
590
+ # assert_true(true) # -> pass
591
+ # assert_true(:true) # -> fail
592
+ def assert_true(actual, message=nil)
593
+ _wrap_assertion do
594
+ assert_block(build_message(message,
595
+ "<true> expected but was\n<?>",
596
+ actual)) do
597
+ actual == true
598
+ end
599
+ end
600
+ end
601
+
602
+ ##
603
+ # Passes if +actual+ is false.
604
+ #
605
+ # Example:
606
+ # assert_false(false) # -> pass
607
+ # assert_false(nil) # -> fail
608
+ def assert_false(actual, message=nil)
609
+ _wrap_assertion do
610
+ assert_block(build_message(message,
611
+ "<false> expected but was\n<?>",
612
+ actual)) do
613
+ actual == false
614
+ end
615
+ end
616
+ end
617
+
618
+ ##
619
+ # Passes if expression "+expected+ +operator+
620
+ # +actual+" is true.
621
+ #
622
+ # Example:
623
+ # assert_compare(1, "<", 10) # -> pass
624
+ # assert_compare(1, ">=", 10) # -> fail
625
+ def assert_compare(expected, operator, actual, message=nil)
626
+ _wrap_assertion do
627
+ assert_send([["<", "<=", ">", ">="], :include?, operator.to_s])
628
+ case operator.to_s
629
+ when "<"
630
+ operator_description = "less than"
631
+ when "<="
632
+ operator_description = "less than or equal to"
633
+ when ">"
634
+ operator_description = "greater than"
635
+ when ">="
636
+ operator_description = "greater than or equal to"
637
+ end
638
+ template = <<-EOT
639
+ <?> #{operator} <?> should be true
640
+ <?> expected #{operator_description}
641
+ <?>.
642
+ EOT
643
+ full_message = build_message(message, template,
644
+ expected, actual,
645
+ expected, actual)
646
+ assert_block(full_message) do
647
+ expected.send(operator, actual)
648
+ end
649
+ end
650
+ end
651
+
652
+ ##
653
+ # Passes if assertion is failed in block.
654
+ #
655
+ # Example:
656
+ # assert_fail_assertion {assert_equal("A", "B")} # -> pass
657
+ # assert_fail_assertion {assert_equal("A", "A")} # -> fail
658
+ def assert_fail_assertion(message=nil)
659
+ _wrap_assertion do
660
+ full_message = build_message(message,
661
+ "Failed assertion was expected.")
662
+ assert_block(full_message) do
663
+ begin
664
+ yield
665
+ false
666
+ rescue AssertionFailedError
667
+ true
668
+ end
669
+ end
670
+ end
671
+ end
672
+
673
+ ##
674
+ # Passes if an exception is raised in block and its
675
+ # message is +expected+.
676
+ #
677
+ # Example:
678
+ # assert_raise_message("exception") {raise "exception"} # -> pass
679
+ # assert_raise_message(/exc/i) {raise "exception"} # -> pass
680
+ # assert_raise_message("exception") {raise "EXCEPTION"} # -> fail
681
+ # assert_raise_message("exception") {} # -> fail
682
+ def assert_raise_message(expected, message=nil)
683
+ _wrap_assertion do
684
+ full_message = build_message(message,
685
+ "<?> exception message expected " +
686
+ "but none was thrown.",
687
+ expected)
688
+ exception = nil
689
+ assert_block(full_message) do
690
+ begin
691
+ yield
692
+ false
693
+ rescue Exception => exception
694
+ true
695
+ end
696
+ end
697
+
698
+ actual = exception.message
699
+ diff = AssertionMessage.delayed_diff(expected, actual)
700
+ full_message =
701
+ build_message(message,
702
+ "<?> exception message expected but was\n" +
703
+ "<?>.?", expected, actual, diff)
704
+ assert_block(full_message) do
705
+ if expected.is_a?(Regexp)
706
+ expected =~ actual
707
+ else
708
+ expected == actual
709
+ end
710
+ end
711
+ end
712
+ end
713
+
714
+ ##
715
+ # Passes if +object+.const_defined?(+constant_name+)
716
+ #
717
+ # Example:
718
+ # assert_const_defined(Test, :Unit) # -> pass
719
+ # assert_const_defined(Object, :Nonexistent) # -> fail
720
+ def assert_const_defined(object, constant_name, message=nil)
721
+ _wrap_assertion do
722
+ full_message = build_message(message,
723
+ "<?>.const_defined\\?(<?>) expected.",
724
+ object, constant_name)
725
+ assert_block(full_message) do
726
+ object.const_defined?(constant_name)
727
+ end
728
+ end
729
+ end
730
+
731
+ ##
732
+ # Passes if !+object+.const_defined?(+constant_name+)
733
+ #
734
+ # Example:
735
+ # assert_not_const_defined(Object, :Nonexistent) # -> pass
736
+ # assert_not_const_defined(Test, :Unit) # -> fail
737
+ def assert_not_const_defined(object, constant_name, message=nil)
738
+ _wrap_assertion do
739
+ full_message = build_message(message,
740
+ "!<?>.const_defined\\?(<?>) expected.",
741
+ object, constant_name)
742
+ assert_block(full_message) do
743
+ !object.const_defined?(constant_name)
744
+ end
745
+ end
746
+ end
747
+
748
+ ##
749
+ # Passes if +object+.+predicate+
750
+ #
751
+ # Example:
752
+ # assert_predicate([], :empty?) # -> pass
753
+ # assert_predicate([1], :empty?) # -> fail
754
+ def assert_predicate(object, predicate, message=nil)
755
+ _wrap_assertion do
756
+ assert_respond_to(object, predicate, message)
757
+ actual = object.send(predicate)
758
+ full_message = build_message(message,
759
+ "<?>.? is true value expected but was\n" +
760
+ "<?>",
761
+ object,
762
+ AssertionMessage.literal(predicate),
763
+ actual)
764
+ assert_block(full_message) do
765
+ actual
766
+ end
767
+ end
768
+ end
769
+
770
+ ##
771
+ # Passes if +object+.+predicate+
772
+ #
773
+ # Example:
774
+ # assert_not_predicate([1], :empty?) # -> pass
775
+ # assert_not_predicate([], :empty?) # -> fail
776
+ def assert_not_predicate(object, predicate, message=nil)
777
+ _wrap_assertion do
778
+ assert_respond_to(object, predicate, message)
779
+ actual = object.send(predicate)
780
+ full_message = build_message(message,
781
+ "<?>.? is false value expected but was\n" +
782
+ "<?>",
783
+ object,
784
+ AssertionMessage.literal(predicate),
785
+ actual)
786
+ assert_block(full_message) do
787
+ not actual
788
+ end
789
+ end
790
+ end
791
+
792
+ ##
793
+ # Builds a failure message. +head+ is added before the +template+ and
794
+ # +arguments+ replaces the '?'s positionally in the template.
795
+
796
+ public
797
+ def build_message(head, template=nil, *arguments)
798
+ template &&= template.chomp
799
+ return AssertionMessage.new(head, template, arguments)
800
+ end
801
+
802
+ private
803
+ def _wrap_assertion
804
+ @_assertion_wrapped ||= false
805
+ unless (@_assertion_wrapped)
806
+ @_assertion_wrapped = true
807
+ begin
808
+ add_assertion
809
+ return yield
810
+ ensure
811
+ @_assertion_wrapped = false
812
+ end
813
+ else
814
+ return yield
815
+ end
816
+ end
817
+
818
+ ##
819
+ # Called whenever an assertion is made. Define this in classes that
820
+ # include Test::Unit::Assertions to record assertion counts.
821
+
822
+ private
823
+ def add_assertion
824
+ end
825
+
826
+ ##
827
+ # Select whether or not to use the pretty-printer. If this option is set
828
+ # to false before any assertions are made, pp.rb will not be required.
829
+
830
+ public
831
+ def self.use_pp=(value)
832
+ AssertionMessage.use_pp = value
833
+ end
834
+
835
+ # :stopdoc:
836
+ private
837
+ def _assert_raise(assert_expected_exception, *args, &block)
838
+ _wrap_assertion do
839
+ if args.last.is_a?(String)
840
+ message = args.pop
841
+ else
842
+ message = ""
843
+ end
844
+
845
+ assert_exception_helper = AssertExceptionHelper.new(self, args)
846
+ expected = assert_exception_helper.expected_exceptions
847
+ actual_exception = nil
848
+ full_message = build_message(message,
849
+ "<?> exception expected " +
850
+ "but none was thrown.",
851
+ expected)
852
+ assert_block(full_message) do
853
+ begin
854
+ yield
855
+ false
856
+ rescue Exception => actual_exception
857
+ true
858
+ end
859
+ end
860
+ assert_expected_exception.call(message, assert_exception_helper,
861
+ actual_exception)
862
+ actual_exception
863
+ end
864
+ end
865
+
866
+ class AssertionMessage
867
+ @use_pp = true
868
+ class << self
869
+ attr_accessor :use_pp
870
+
871
+ def literal(value)
872
+ Literal.new(value)
873
+ end
874
+
875
+ def delayed_literal(&block)
876
+ DelayedLiteral.new(block)
877
+ end
878
+
879
+ def maybe_container(value, &formatter)
880
+ MaybeContainer.new(value, &formatter)
881
+ end
882
+
883
+ MAX_DIFF_TARGET_STRING_SIZE = 300
884
+ def diff_target_string?(string)
885
+ if string.respond_to?(:bytesize)
886
+ string.bytesize < MAX_DIFF_TARGET_STRING_SIZE
887
+ else
888
+ string.size < MAX_DIFF_TARGET_STRING_SIZE
889
+ end
890
+ end
891
+
892
+ def delayed_diff(from, to)
893
+ delayed_literal do
894
+ if !from.is_a?(String) or !to.is_a?(String)
895
+ from = convert(from)
896
+ to = convert(to)
897
+ end
898
+
899
+ diff = nil
900
+ diff = "" if !diff_target_string?(from) or !diff_target_string?(to)
901
+ diff ||= Diff.readable(from, to)
902
+ if /^[-+]/ !~ diff
903
+ diff = ""
904
+ elsif /^[ ?]/ =~ diff or /(?:.*\n){2,}/ =~ diff
905
+ diff = "\n\ndiff:\n#{diff}"
906
+ else
907
+ diff = ""
908
+ end
909
+
910
+ if Diff.need_fold?(diff)
911
+ folded_diff = Diff.folded_readable(from, to)
912
+ diff << "\n\nfolded diff:\n#{folded_diff}"
913
+ end
914
+
915
+ diff
916
+ end
917
+ end
918
+
919
+ def convert(object)
920
+ case object
921
+ when Exception
922
+ <<EOM.chop
923
+ Class: <#{convert(object.class)}>
924
+ Message: <#{convert(object.message)}>
925
+ ---Backtrace---
926
+ #{Util::BacktraceFilter.filter_backtrace(object.backtrace).join("\n")}
927
+ ---------------
928
+ EOM
929
+ else
930
+ if use_pp
931
+ begin
932
+ require 'pp' unless defined?(PP)
933
+ return PP.pp(object, '').chomp
934
+ rescue LoadError
935
+ self.use_pp = false
936
+ end
937
+ end
938
+ object.inspect
939
+ end
940
+ end
941
+ end
942
+
943
+ class Literal
944
+ def initialize(value)
945
+ @value = value
946
+ end
947
+
948
+ def inspect
949
+ @value.to_s
950
+ end
951
+ end
952
+
953
+ class DelayedLiteral
954
+ def initialize(value)
955
+ @value = value
956
+ end
957
+
958
+ def inspect
959
+ @value.call.to_s
960
+ end
961
+ end
962
+
963
+ class MaybeContainer
964
+ def initialize(value, &formatter)
965
+ @value = value
966
+ @formatter = formatter
967
+ end
968
+
969
+ def inspect
970
+ if @value.is_a?(Array)
971
+ values = @value.collect do |value|
972
+ @formatter.call(AssertionMessage.convert(value))
973
+ end
974
+ "[#{values.join(', ')}]"
975
+ else
976
+ @formatter.call(AssertionMessage.convert(@value))
977
+ end
978
+ end
979
+ end
980
+
981
+ class Template
982
+ def self.create(string)
983
+ parts = (string ? string.scan(/(?=[^\\])\?|(?:\\\?|[^\?])+/m) : [])
984
+ self.new(parts)
985
+ end
986
+
987
+ attr_reader :count
988
+
989
+ def initialize(parts)
990
+ @parts = parts
991
+ @count = parts.find_all{|e| e == '?'}.size
992
+ end
993
+
994
+ def result(parameters)
995
+ raise "The number of parameters does not match the number of substitutions." if(parameters.size != count)
996
+ params = parameters.dup
997
+ @parts.collect{|e| e == '?' ? params.shift : e.gsub(/\\\?/m, '?')}.join('')
998
+ end
999
+ end
1000
+
1001
+ include Util::BacktraceFilter
1002
+
1003
+ def initialize(head, template_string, parameters)
1004
+ @head = head
1005
+ @template_string = template_string
1006
+ @parameters = parameters
1007
+ end
1008
+
1009
+ def convert(object)
1010
+ self.class.convert(object)
1011
+ end
1012
+
1013
+ def template
1014
+ @template ||= Template.create(@template_string)
1015
+ end
1016
+
1017
+ def add_period(string)
1018
+ (string =~ /\.\Z/ ? string : string + '.')
1019
+ end
1020
+
1021
+ def to_s
1022
+ message_parts = []
1023
+ if (@head)
1024
+ head = @head.to_s
1025
+ unless(head.empty?)
1026
+ message_parts << add_period(head)
1027
+ end
1028
+ end
1029
+ tail = template.result(@parameters.collect{|e| convert(e)})
1030
+ message_parts << tail unless(tail.empty?)
1031
+ message_parts.join("\n")
1032
+ end
1033
+ end
1034
+
1035
+ class AssertExceptionHelper
1036
+ class WrappedException
1037
+ def initialize(exception)
1038
+ @exception = exception
1039
+ end
1040
+
1041
+ def inspect
1042
+ if default_inspect?
1043
+ "#{@exception.class.inspect}(#{@exception.message.inspect})"
1044
+ else
1045
+ @exception.inspect
1046
+ end
1047
+ end
1048
+
1049
+ def method_missing(name, *args, &block)
1050
+ @exception.send(name, *args, &block)
1051
+ end
1052
+
1053
+ private
1054
+ def default_inspect?
1055
+ inspect_method = @exception.method(:inspect)
1056
+ if inspect_method.respond_to?(:owner) and
1057
+ inspect_method.owner == Exception
1058
+ true
1059
+ else
1060
+ default_inspect_method = Exception.instance_method(:inspect)
1061
+ default_inspect_method.bind(@exception).call == @exception.inspect
1062
+ end
1063
+ end
1064
+ end
1065
+
1066
+ def initialize(test_case, expected_exceptions)
1067
+ @test_case = test_case
1068
+ @expected_exceptions = expected_exceptions
1069
+ @expected_classes, @expected_modules, @expected_objects =
1070
+ split_expected_exceptions(expected_exceptions)
1071
+ end
1072
+
1073
+ def expected_exceptions
1074
+ exceptions = @expected_exceptions.collect do |exception|
1075
+ if exception.is_a?(Exception)
1076
+ WrappedException.new(exception)
1077
+ else
1078
+ exception
1079
+ end
1080
+ end
1081
+ if exceptions.size == 1
1082
+ exceptions[0]
1083
+ else
1084
+ exceptions
1085
+ end
1086
+ end
1087
+
1088
+ def expected?(actual_exception, equality=nil)
1089
+ equality ||= :instance_of?
1090
+ expected_class?(actual_exception, equality) or
1091
+ expected_module?(actual_exception) or
1092
+ expected_object?(actual_exception)
1093
+ end
1094
+
1095
+ private
1096
+ def split_expected_exceptions(expected_exceptions)
1097
+ exception_modules = []
1098
+ exception_objects = []
1099
+ exception_classes = []
1100
+ expected_exceptions.each do |exception_type|
1101
+ if exception_type.instance_of?(Module)
1102
+ exception_modules << exception_type
1103
+ elsif exception_type.is_a?(Exception)
1104
+ exception_objects << exception_type
1105
+ else
1106
+ @test_case.send(:assert,
1107
+ Exception >= exception_type,
1108
+ "Should expect a class of exception, " +
1109
+ "#{exception_type}")
1110
+ exception_classes << exception_type
1111
+ end
1112
+ end
1113
+ [exception_classes, exception_modules, exception_objects]
1114
+ end
1115
+
1116
+ def expected_class?(actual_exception, equality)
1117
+ @expected_classes.any? do |expected_class|
1118
+ actual_exception.send(equality, expected_class)
1119
+ end
1120
+ end
1121
+
1122
+ def expected_module?(actual_exception)
1123
+ @expected_modules.any? do |expected_module|
1124
+ actual_exception.is_a?(expected_module)
1125
+ end
1126
+ end
1127
+
1128
+ def expected_object?(actual_exception)
1129
+ @expected_objects.any? do |expected_object|
1130
+ expected_object == actual_exception or
1131
+ fallback_exception_object_equal(expected_object, actual_exception)
1132
+ end
1133
+ end
1134
+
1135
+ def fallback_exception_object_equal(expected_object, actual_exception)
1136
+ owner = Util::MethodOwnerFinder.find(expected_object, :==)
1137
+ if owner == Kernel or owner == Exception
1138
+ expected_object.class == actual_exception.class and
1139
+ expected_object.message == actual_exception.message
1140
+ else
1141
+ false
1142
+ end
1143
+ end
1144
+ end
1145
+
1146
+ # :startdoc:
1147
+ end
1148
+ end
1149
+ end