rroonga 0.9.2-mswin32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (202) hide show
  1. data/AUTHORS +5 -0
  2. data/NEWS.ja.rdoc +114 -0
  3. data/NEWS.rdoc +116 -0
  4. data/README.ja.rdoc +65 -0
  5. data/README.rdoc +66 -0
  6. data/Rakefile +217 -0
  7. data/benchmark/common.rb +49 -0
  8. data/benchmark/read-write-many-small-items.rb +144 -0
  9. data/benchmark/write-many-small-items.rb +135 -0
  10. data/example/bookmark.rb +161 -0
  11. data/example/index-html.rb +89 -0
  12. data/example/search/config.ru +211 -0
  13. data/example/search/public/css/groonga.css +122 -0
  14. data/ext/.gitignore +2 -0
  15. data/ext/libruby-groonga.a +0 -0
  16. data/ext/rb-grn-accessor.c +52 -0
  17. data/ext/rb-grn-array-cursor.c +36 -0
  18. data/ext/rb-grn-array.c +210 -0
  19. data/ext/rb-grn-column.c +573 -0
  20. data/ext/rb-grn-context.c +655 -0
  21. data/ext/rb-grn-database.c +472 -0
  22. data/ext/rb-grn-encoding-support.c +64 -0
  23. data/ext/rb-grn-encoding.c +257 -0
  24. data/ext/rb-grn-exception.c +1110 -0
  25. data/ext/rb-grn-expression-builder.c +75 -0
  26. data/ext/rb-grn-expression.c +735 -0
  27. data/ext/rb-grn-fix-size-column.c +166 -0
  28. data/ext/rb-grn-hash-cursor.c +38 -0
  29. data/ext/rb-grn-hash.c +294 -0
  30. data/ext/rb-grn-index-column.c +488 -0
  31. data/ext/rb-grn-logger.c +504 -0
  32. data/ext/rb-grn-object.c +1369 -0
  33. data/ext/rb-grn-operation.c +198 -0
  34. data/ext/rb-grn-patricia-trie-cursor.c +39 -0
  35. data/ext/rb-grn-patricia-trie.c +488 -0
  36. data/ext/rb-grn-procedure.c +52 -0
  37. data/ext/rb-grn-query.c +260 -0
  38. data/ext/rb-grn-record.c +40 -0
  39. data/ext/rb-grn-snippet.c +334 -0
  40. data/ext/rb-grn-table-cursor-key-support.c +69 -0
  41. data/ext/rb-grn-table-cursor.c +247 -0
  42. data/ext/rb-grn-table-key-support.c +731 -0
  43. data/ext/rb-grn-table.c +2141 -0
  44. data/ext/rb-grn-type.c +181 -0
  45. data/ext/rb-grn-utils.c +769 -0
  46. data/ext/rb-grn-variable-size-column.c +36 -0
  47. data/ext/rb-grn-variable.c +108 -0
  48. data/ext/rb-grn-view-accessor.c +53 -0
  49. data/ext/rb-grn-view-cursor.c +35 -0
  50. data/ext/rb-grn-view-record.c +41 -0
  51. data/ext/rb-grn-view.c +421 -0
  52. data/ext/rb-grn.h +700 -0
  53. data/ext/rb-groonga.c +117 -0
  54. data/extconf.rb +266 -0
  55. data/html/bar.svg +153 -0
  56. data/html/developer.html +117 -0
  57. data/html/developer.svg +469 -0
  58. data/html/download.svg +253 -0
  59. data/html/favicon.ico +0 -0
  60. data/html/favicon.xcf +0 -0
  61. data/html/footer.html.erb +28 -0
  62. data/html/head.html.erb +4 -0
  63. data/html/header.html.erb +17 -0
  64. data/html/index.html +147 -0
  65. data/html/install.svg +636 -0
  66. data/html/logo.xcf +0 -0
  67. data/html/ranguba.css +250 -0
  68. data/html/tutorial.svg +559 -0
  69. data/lib/groonga.rb +90 -0
  70. data/lib/groonga/context.rb +184 -0
  71. data/lib/groonga/expression-builder.rb +285 -0
  72. data/lib/groonga/patricia-trie.rb +53 -0
  73. data/lib/groonga/record.rb +311 -0
  74. data/lib/groonga/schema.rb +1191 -0
  75. data/lib/groonga/view-record.rb +56 -0
  76. data/license/GPL +340 -0
  77. data/license/LGPL +504 -0
  78. data/license/RUBY +59 -0
  79. data/misc/grnop2ruby.rb +49 -0
  80. data/pkg-config.rb +333 -0
  81. data/test-unit/Rakefile +40 -0
  82. data/test-unit/TODO +5 -0
  83. data/test-unit/bin/testrb +5 -0
  84. data/test-unit/html/classic.html +15 -0
  85. data/test-unit/html/index.html +25 -0
  86. data/test-unit/html/index.html.ja +27 -0
  87. data/test-unit/lib/test/unit.rb +323 -0
  88. data/test-unit/lib/test/unit/assertionfailederror.rb +25 -0
  89. data/test-unit/lib/test/unit/assertions.rb +1230 -0
  90. data/test-unit/lib/test/unit/attribute.rb +125 -0
  91. data/test-unit/lib/test/unit/autorunner.rb +360 -0
  92. data/test-unit/lib/test/unit/collector.rb +36 -0
  93. data/test-unit/lib/test/unit/collector/descendant.rb +23 -0
  94. data/test-unit/lib/test/unit/collector/dir.rb +108 -0
  95. data/test-unit/lib/test/unit/collector/load.rb +136 -0
  96. data/test-unit/lib/test/unit/collector/objectspace.rb +34 -0
  97. data/test-unit/lib/test/unit/color-scheme.rb +102 -0
  98. data/test-unit/lib/test/unit/color.rb +96 -0
  99. data/test-unit/lib/test/unit/diff.rb +724 -0
  100. data/test-unit/lib/test/unit/error.rb +130 -0
  101. data/test-unit/lib/test/unit/exceptionhandler.rb +39 -0
  102. data/test-unit/lib/test/unit/failure.rb +136 -0
  103. data/test-unit/lib/test/unit/fixture.rb +176 -0
  104. data/test-unit/lib/test/unit/notification.rb +129 -0
  105. data/test-unit/lib/test/unit/omission.rb +191 -0
  106. data/test-unit/lib/test/unit/pending.rb +150 -0
  107. data/test-unit/lib/test/unit/priority.rb +180 -0
  108. data/test-unit/lib/test/unit/runner/console.rb +52 -0
  109. data/test-unit/lib/test/unit/runner/emacs.rb +8 -0
  110. data/test-unit/lib/test/unit/runner/tap.rb +8 -0
  111. data/test-unit/lib/test/unit/testcase.rb +476 -0
  112. data/test-unit/lib/test/unit/testresult.rb +89 -0
  113. data/test-unit/lib/test/unit/testsuite.rb +110 -0
  114. data/test-unit/lib/test/unit/ui/console/outputlevel.rb +14 -0
  115. data/test-unit/lib/test/unit/ui/console/testrunner.rb +466 -0
  116. data/test-unit/lib/test/unit/ui/emacs/testrunner.rb +63 -0
  117. data/test-unit/lib/test/unit/ui/tap/testrunner.rb +92 -0
  118. data/test-unit/lib/test/unit/ui/testrunner.rb +28 -0
  119. data/test-unit/lib/test/unit/ui/testrunnermediator.rb +77 -0
  120. data/test-unit/lib/test/unit/ui/testrunnerutilities.rb +41 -0
  121. data/test-unit/lib/test/unit/util/backtracefilter.rb +41 -0
  122. data/test-unit/lib/test/unit/util/method-owner-finder.rb +28 -0
  123. data/test-unit/lib/test/unit/util/observable.rb +90 -0
  124. data/test-unit/lib/test/unit/util/procwrapper.rb +48 -0
  125. data/test-unit/lib/test/unit/version.rb +7 -0
  126. data/test-unit/sample/adder.rb +13 -0
  127. data/test-unit/sample/subtracter.rb +12 -0
  128. data/test-unit/sample/test_adder.rb +20 -0
  129. data/test-unit/sample/test_subtracter.rb +20 -0
  130. data/test-unit/sample/test_user.rb +23 -0
  131. data/test-unit/test/collector/test-descendant.rb +133 -0
  132. data/test-unit/test/collector/test-load.rb +348 -0
  133. data/test-unit/test/collector/test_dir.rb +406 -0
  134. data/test-unit/test/collector/test_objectspace.rb +100 -0
  135. data/test-unit/test/run-test.rb +15 -0
  136. data/test-unit/test/test-attribute.rb +86 -0
  137. data/test-unit/test/test-color-scheme.rb +67 -0
  138. data/test-unit/test/test-color.rb +47 -0
  139. data/test-unit/test/test-diff.rb +518 -0
  140. data/test-unit/test/test-emacs-runner.rb +60 -0
  141. data/test-unit/test/test-fixture.rb +287 -0
  142. data/test-unit/test/test-notification.rb +33 -0
  143. data/test-unit/test/test-omission.rb +81 -0
  144. data/test-unit/test/test-pending.rb +70 -0
  145. data/test-unit/test/test-priority.rb +119 -0
  146. data/test-unit/test/test-testcase.rb +544 -0
  147. data/test-unit/test/test_assertions.rb +1151 -0
  148. data/test-unit/test/test_error.rb +26 -0
  149. data/test-unit/test/test_failure.rb +33 -0
  150. data/test-unit/test/test_testresult.rb +113 -0
  151. data/test-unit/test/test_testsuite.rb +129 -0
  152. data/test-unit/test/testunit-test-util.rb +14 -0
  153. data/test-unit/test/ui/test_testrunmediator.rb +20 -0
  154. data/test-unit/test/util/test-method-owner-finder.rb +38 -0
  155. data/test-unit/test/util/test_backtracefilter.rb +41 -0
  156. data/test-unit/test/util/test_observable.rb +102 -0
  157. data/test-unit/test/util/test_procwrapper.rb +36 -0
  158. data/test/.gitignore +1 -0
  159. data/test/groonga-test-utils.rb +133 -0
  160. data/test/run-test.rb +58 -0
  161. data/test/test-array.rb +97 -0
  162. data/test/test-column.rb +316 -0
  163. data/test/test-context-select.rb +93 -0
  164. data/test/test-context.rb +73 -0
  165. data/test/test-database.rb +113 -0
  166. data/test/test-encoding.rb +33 -0
  167. data/test/test-exception.rb +93 -0
  168. data/test/test-expression-builder.rb +156 -0
  169. data/test/test-expression.rb +134 -0
  170. data/test/test-fix-size-column.rb +65 -0
  171. data/test/test-gqtp.rb +72 -0
  172. data/test/test-hash.rb +312 -0
  173. data/test/test-index-column.rb +81 -0
  174. data/test/test-logger.rb +37 -0
  175. data/test/test-patricia-trie.rb +189 -0
  176. data/test/test-procedure.rb +37 -0
  177. data/test/test-query.rb +22 -0
  178. data/test/test-record.rb +279 -0
  179. data/test/test-remote.rb +54 -0
  180. data/test/test-schema-view.rb +90 -0
  181. data/test/test-schema.rb +459 -0
  182. data/test/test-snippet.rb +130 -0
  183. data/test/test-table-cursor.rb +153 -0
  184. data/test/test-table-offset-and-limit.rb +102 -0
  185. data/test/test-table-select-normalize.rb +53 -0
  186. data/test/test-table-select.rb +150 -0
  187. data/test/test-table.rb +690 -0
  188. data/test/test-type.rb +71 -0
  189. data/test/test-variable-size-column.rb +98 -0
  190. data/test/test-variable.rb +28 -0
  191. data/test/test-vector-column.rb +76 -0
  192. data/test/test-version.rb +31 -0
  193. data/test/test-view.rb +72 -0
  194. data/text/TUTORIAL.ja.rdoc +392 -0
  195. data/text/expression.rdoc +284 -0
  196. data/vendor/local/bin/grntest.exe +0 -0
  197. data/vendor/local/bin/groonga.exe +0 -0
  198. data/vendor/local/bin/libgroonga.dll +0 -0
  199. data/vendor/local/bin/libmecab.dll +0 -0
  200. data/vendor/local/include/groonga.h +2285 -0
  201. data/vendor/local/lib/libgroonga.lib +0 -0
  202. metadata +280 -0
data/test-unit/TODO ADDED
@@ -0,0 +1,5 @@
1
+ * mock.
2
+ * data-driven test.
3
+ * port ruby trunk's assert_equal's intelligent inspection
4
+ when inspected expected and actual are same content
5
+ string.
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ require 'test/unit'
3
+ (r = Test::Unit::AutoRunner.new(true)).process_args(ARGV) or
4
+ abort r.options.banner + " tests..."
5
+ exit r.run
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2
+ <html>
3
+ <head>
4
+ <title>Test::Unit (Classic)</title>
5
+ </head>
6
+ <body bgcolor="red">
7
+ <p style="text-align: center">
8
+ <img height="161" width="308" src="test-unit-classic.png">
9
+ <br>
10
+ <br>
11
+ <br>
12
+ Read the <a href="classic/test-unit/">rdoc</a>
13
+ </p>
14
+ </body>
15
+ </html>
@@ -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,323 @@
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 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
198
+ # need a way to run it and view any failures that occur
199
+ # during the run. There are some test runner; console test
200
+ # runner, GTK+ test runner and so on. The console test
201
+ # runner is automatically invoked for you if you require
202
+ # 'test/unit' and simply run the file. To use another
203
+ # runner simply set default test runner ID to
204
+ # Test::Unit::AutoRunner:
205
+ #
206
+ # require 'test/unit'
207
+ # Test::Unit::AutoRunner.default_runner = "gtk2"
208
+ #
209
+ # == Test Suite
210
+ #
211
+ # As more and more unit tests accumulate for a given project, it
212
+ # becomes a real drag running them one at a time, and it also
213
+ # introduces the potential to overlook a failing test because you
214
+ # forget to run it. Suddenly it becomes very handy that the
215
+ # TestRunners can take any object that returns a Test::Unit::TestSuite
216
+ # in response to a suite method. The TestSuite can, in turn, contain
217
+ # other TestSuites or individual tests (typically created by a
218
+ # TestCase). In other words, you can easily wrap up a group of
219
+ # TestCases and TestSuites.
220
+ #
221
+ # Test::Unit does a little bit more for you, by wrapping
222
+ # these up automatically when you require
223
+ # 'test/unit'. What does this mean? It means you could
224
+ # write the above test case like this instead:
225
+ #
226
+ # require 'test/unit'
227
+ # require 'test_myfirsttests'
228
+ # require 'test_moretestsbyme'
229
+ # require 'test_anothersetoftests'
230
+ #
231
+ # Test::Unit is smart enough to find all the test cases existing in
232
+ # the ObjectSpace and wrap them up into a suite for you. It then runs
233
+ # the dynamic suite using the console TestRunner.
234
+ #
235
+ #
236
+ # == Configuration file
237
+ #
238
+ # Test::Unit reads 'test-unit.yml' in the current working
239
+ # directory as Test::Unit's configuration file. It can
240
+ # contain the following configurations:
241
+ #
242
+ # * color scheme definitions
243
+ # * test runner to be used
244
+ # * test runner options
245
+ # * test collector to be used
246
+ #
247
+ # Except color scheme definitions, all of them are
248
+ # specified by command line option.
249
+ #
250
+ # Here are sample color scheme definitions:
251
+ #
252
+ # color_schemes:
253
+ # inverted:
254
+ # success:
255
+ # name: red
256
+ # bold: true
257
+ # failure:
258
+ # name: green
259
+ # bold: true
260
+ # other_scheme:
261
+ # ...
262
+ #
263
+ # Here are the syntax of color scheme definitions:
264
+ #
265
+ # color_schemes:
266
+ # SCHEME_NAME:
267
+ # EVENT_NAME:
268
+ # name: COLOR_NAME
269
+ # intensity: BOOLEAN
270
+ # bold: BOOLEAN
271
+ # italic: BOOLEAN
272
+ # underline: BOOLEAN
273
+ # ...
274
+ # ...
275
+ #
276
+ # SCHEME_NAME:: the name of the color scheme
277
+ # EVENT_NAME:: one of [success, failure, pending,
278
+ # omission, notification, error]
279
+ # COLOR_NAME:: one of [black, red, green, yellow, blue,
280
+ # magenta, cyan, white]
281
+ # BOOLEAN:: true or false
282
+ #
283
+ # You can use the above 'inverted' color scheme with the
284
+ # following configuration:
285
+ #
286
+ # runner: console
287
+ # console_options:
288
+ # color_scheme: inverted
289
+ # color_schemes:
290
+ # inverted:
291
+ # success:
292
+ # name: red
293
+ # bold: true
294
+ # failure:
295
+ # name: green
296
+ # bold: true
297
+ #
298
+ # == Questions?
299
+ #
300
+ # I'd really like to get feedback from all levels of Ruby
301
+ # practitioners about typos, grammatical errors, unclear statements,
302
+ # missing points, etc., in this document (or any other).
303
+ #
304
+
305
+ module Unit
306
+ # Set true when Test::Unit has run. If set to true Test::Unit
307
+ # will not automatically run at exit.
308
+ def self.run=(flag)
309
+ @run = flag
310
+ end
311
+
312
+ # Already tests have run?
313
+ def self.run?
314
+ @run ||= false
315
+ end
316
+ end
317
+ end
318
+
319
+ at_exit do
320
+ unless $! || Test::Unit.run?
321
+ exit Test::Unit::AutoRunner.run
322
+ end
323
+ end
@@ -0,0 +1,25 @@
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
@@ -0,0 +1,1230 @@
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
+ begin
88
+ assert_block(full_message) { expected == actual }
89
+ rescue AssertionFailedError => failure
90
+ failure.expected = expected
91
+ failure.actual = actual
92
+ failure.inspected_expected = AssertionMessage.convert(expected)
93
+ failure.inspected_actual = AssertionMessage.convert(actual)
94
+ failure.user_message = message
95
+ raise
96
+ end
97
+ end
98
+
99
+ ##
100
+ # Passes if the block raises one of the expected
101
+ # exceptions. When an expected exception is an Exception
102
+ # object, passes if expected_exception == actual_exception.
103
+ #
104
+ # Example:
105
+ # assert_raise(RuntimeError, LoadError) do
106
+ # raise 'Boom!!!'
107
+ # end # -> pass
108
+ #
109
+ # assert_raise do
110
+ # raise Exception, 'Any exception should be raised!!!'
111
+ # end # -> pass
112
+ #
113
+ # assert_raise(RuntimeError.new("XXX")) {raise "XXX"} # -> pass
114
+ # assert_raise(MyError.new("XXX")) {raise "XXX"} # -> fail
115
+ # assert_raise(RuntimeError.new("ZZZ")) {raise "XXX"} # -> fail
116
+ public
117
+ def assert_raise(*args, &block)
118
+ assert_expected_exception = Proc.new do |*_args|
119
+ message, assert_exception_helper, actual_exception = _args
120
+ expected = assert_exception_helper.expected_exceptions
121
+ full_message = build_message(message,
122
+ "<?> exception expected but was\n?",
123
+ expected, actual_exception)
124
+ assert_block(full_message) do
125
+ expected == [] or assert_exception_helper.expected?(actual_exception)
126
+ end
127
+ end
128
+ _assert_raise(assert_expected_exception, *args, &block)
129
+ end
130
+
131
+ ##
132
+ # Alias of assert_raise.
133
+ #
134
+ # Will be deprecated in 1.9, and removed in 2.0.
135
+
136
+ public
137
+ def assert_raises(*args, &block)
138
+ assert_raise(*args, &block)
139
+ end
140
+
141
+ ##
142
+ # Passes if the block raises one of the given
143
+ # exceptions or sub exceptions of the given exceptions.
144
+ #
145
+ # Example:
146
+ # assert_raise_kind_of(SystemCallError) do
147
+ # raise Errno::EACCES
148
+ # end
149
+ def assert_raise_kind_of(*args, &block)
150
+ assert_expected_exception = Proc.new do |*_args|
151
+ message, assert_exception_helper, actual_exception = _args
152
+ expected = assert_exception_helper.expected_exceptions
153
+ full_message = build_message(message,
154
+ "<?> family exception expected " +
155
+ "but was\n?",
156
+ expected, actual_exception)
157
+ assert_block(full_message) do
158
+ assert_exception_helper.expected?(actual_exception, :kind_of?)
159
+ end
160
+ end
161
+ _assert_raise(assert_expected_exception, *args, &block)
162
+ end
163
+
164
+
165
+ ##
166
+ # Passes if +object+.instance_of?(+klass+). When +klass+ is
167
+ # an array of classes, it passes if any class
168
+ # satisfies +object.instance_of?(class).
169
+ #
170
+ # Example:
171
+ # assert_instance_of(String, 'foo') # -> pass
172
+ # assert_instance_of([Fixnum, NilClass], 100) # -> pass
173
+ # assert_instance_of([Numeric, NilClass], 100) # -> fail
174
+
175
+ public
176
+ def assert_instance_of(klass, object, message="")
177
+ _wrap_assertion do
178
+ klasses = nil
179
+ klasses = klass if klass.is_a?(Array)
180
+ assert_block("The first parameter to assert_instance_of should be " +
181
+ "a Class or an Array of Class.") do
182
+ if klasses
183
+ klasses.all? {|k| k.is_a?(Class)}
184
+ else
185
+ klass.is_a?(Class)
186
+ end
187
+ end
188
+ klass_message = AssertionMessage.maybe_container(klass) do |value|
189
+ "<#{value}>"
190
+ end
191
+ full_message = build_message(message, <<EOT, object, klass_message, object.class)
192
+ <?> expected to be an instance of
193
+ ? but was
194
+ <?>.
195
+ EOT
196
+ assert_block(full_message) do
197
+ if klasses
198
+ klasses.any? {|k| object.instance_of?(k)}
199
+ else
200
+ object.instance_of?(klass)
201
+ end
202
+ end
203
+ end
204
+ end
205
+
206
+ ##
207
+ # Passes if +object+ is nil.
208
+ #
209
+ # Example:
210
+ # assert_nil [1, 2].uniq!
211
+
212
+ public
213
+ def assert_nil(object, message="")
214
+ full_message = build_message(message, <<EOT, object)
215
+ <?> expected to be nil.
216
+ EOT
217
+ assert_block(full_message) { object.nil? }
218
+ end
219
+
220
+ ##
221
+ # Passes if +object+.kind_of?(+klass+). When +klass+ is
222
+ # an array of classes or modules, it passes if any
223
+ # class or module satisfies +object.kind_of?(class_or_module).
224
+ #
225
+ # Example:
226
+ # assert_kind_of(Object, 'foo') # -> pass
227
+ # assert_kind_of([Fixnum, NilClass], 100) # -> pass
228
+ # assert_kind_of([Fixnum, NilClass], "string") # -> fail
229
+
230
+ public
231
+ def assert_kind_of(klass, object, message="")
232
+ _wrap_assertion do
233
+ klasses = nil
234
+ klasses = klass if klass.is_a?(Array)
235
+ assert_block("The first parameter to assert_kind_of should be " +
236
+ "a kind_of Module or an Array of a kind_of Module.") do
237
+ if klasses
238
+ klasses.all? {|k| k.kind_of?(Module)}
239
+ else
240
+ klass.kind_of?(Module)
241
+ end
242
+ end
243
+ klass_message = AssertionMessage.maybe_container(klass) do |value|
244
+ "<#{value}>"
245
+ end
246
+ full_message = build_message(message,
247
+ "<?> expected to be kind_of\\?\n" +
248
+ "? but was\n" +
249
+ "<?>.",
250
+ object,
251
+ klass_message,
252
+ object.class)
253
+ assert_block(full_message) do
254
+ if klasses
255
+ klasses.any? {|k| object.kind_of?(k)}
256
+ else
257
+ object.kind_of?(klass)
258
+ end
259
+ end
260
+ end
261
+ end
262
+
263
+ ##
264
+ # Passes if +object+ .respond_to? +method+
265
+ #
266
+ # Example:
267
+ # assert_respond_to 'bugbear', :slice
268
+
269
+ public
270
+ def assert_respond_to(object, method, message="")
271
+ _wrap_assertion do
272
+ full_message = build_message(message,
273
+ "<?>.kind_of\\?(Symbol) or\n" +
274
+ "<?>.respond_to\\?(:to_str) expected",
275
+ method, method)
276
+ assert_block(full_message) do
277
+ method.kind_of?(Symbol) or method.respond_to?(:to_str)
278
+ end
279
+ full_message = build_message(message,
280
+ "<?>.respond_to\\?(?) expected\n" +
281
+ "(Class: <?>)",
282
+ object, method, object.class)
283
+ assert_block(full_message) {object.respond_to?(method)}
284
+ end
285
+ end
286
+
287
+ ##
288
+ # Passes if +string+ =~ +pattern+.
289
+ #
290
+ # Example:
291
+ # assert_match(/\d+/, 'five, 6, seven')
292
+
293
+ public
294
+ def assert_match(pattern, string, message="")
295
+ _wrap_assertion do
296
+ pattern = case(pattern)
297
+ when String
298
+ Regexp.new(Regexp.escape(pattern))
299
+ else
300
+ pattern
301
+ end
302
+ full_message = build_message(message, "<?> expected to be =~\n<?>.", string, pattern)
303
+ assert_block(full_message) { string =~ pattern }
304
+ end
305
+ end
306
+
307
+ ##
308
+ # Passes if +actual+ .equal? +expected+ (i.e. they are the same
309
+ # instance).
310
+ #
311
+ # Example:
312
+ # o = Object.new
313
+ # assert_same o, o
314
+
315
+ public
316
+ def assert_same(expected, actual, message="")
317
+ full_message = build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__)
318
+ <?>
319
+ with id <?> expected to be equal\\? to
320
+ <?>
321
+ with id <?>.
322
+ EOT
323
+ assert_block(full_message) { actual.equal?(expected) }
324
+ end
325
+
326
+ ##
327
+ # Compares the +object1+ with +object2+ using +operator+.
328
+ #
329
+ # Passes if object1.__send__(operator, object2) is true.
330
+ #
331
+ # Example:
332
+ # assert_operator 5, :>=, 4
333
+
334
+ public
335
+ def assert_operator(object1, operator, object2, message="")
336
+ _wrap_assertion do
337
+ full_message = build_message(nil, "<?>\ngiven as the operator for #assert_operator must be a Symbol or #respond_to\\?(:to_str).", operator)
338
+ assert_block(full_message){operator.kind_of?(Symbol) || operator.respond_to?(:to_str)}
339
+ full_message = build_message(message, <<EOT, object1, AssertionMessage.literal(operator), object2)
340
+ <?> expected to be
341
+ ?
342
+ <?>.
343
+ EOT
344
+ assert_block(full_message) { object1.__send__(operator, object2) }
345
+ end
346
+ end
347
+
348
+ ##
349
+ # Passes if block does not raise an exception.
350
+ #
351
+ # Example:
352
+ # assert_nothing_raised do
353
+ # [1, 2].uniq
354
+ # end
355
+
356
+ public
357
+ def assert_nothing_raised(*args)
358
+ _wrap_assertion do
359
+ if args.last.is_a?(String)
360
+ message = args.pop
361
+ else
362
+ message = ""
363
+ end
364
+
365
+ assert_exception_helper = AssertExceptionHelper.new(self, args)
366
+ begin
367
+ yield
368
+ rescue Exception => e
369
+ if ((args.empty? && !e.instance_of?(AssertionFailedError)) ||
370
+ assert_exception_helper.expected?(e))
371
+ failure_message = build_message(message, "Exception raised:\n?", e)
372
+ assert_block(failure_message) {false}
373
+ else
374
+ raise
375
+ end
376
+ end
377
+ nil
378
+ end
379
+ end
380
+
381
+ ##
382
+ # Flunk always fails.
383
+ #
384
+ # Example:
385
+ # flunk 'Not done testing yet.'
386
+
387
+ public
388
+ def flunk(message="Flunked")
389
+ assert_block(build_message(message)){false}
390
+ end
391
+
392
+ ##
393
+ # Passes if ! +actual+ .equal? +expected+
394
+ #
395
+ # Example:
396
+ # assert_not_same Object.new, Object.new
397
+
398
+ public
399
+ def assert_not_same(expected, actual, message="")
400
+ full_message = build_message(message, <<EOT, expected, expected.__id__, actual, actual.__id__)
401
+ <?>
402
+ with id <?> expected to not be equal\\? to
403
+ <?>
404
+ with id <?>.
405
+ EOT
406
+ assert_block(full_message) { !actual.equal?(expected) }
407
+ end
408
+
409
+ ##
410
+ # Passes if +expected+ != +actual+
411
+ #
412
+ # Example:
413
+ # assert_not_equal 'some string', 5
414
+
415
+ public
416
+ def assert_not_equal(expected, actual, message="")
417
+ full_message = build_message(message, "<?> expected to be != to\n<?>.", expected, actual)
418
+ assert_block(full_message) { expected != actual }
419
+ end
420
+
421
+ ##
422
+ # Passes if ! +object+ .nil?
423
+ #
424
+ # Example:
425
+ # assert_not_nil '1 two 3'.sub!(/two/, '2')
426
+
427
+ public
428
+ def assert_not_nil(object, message="")
429
+ full_message = build_message(message, "<?> expected to not be nil.", object)
430
+ assert_block(full_message){!object.nil?}
431
+ end
432
+
433
+ ##
434
+ # Passes if +regexp+ !~ +string+
435
+ #
436
+ # Example:
437
+ # assert_no_match(/two/, 'one 2 three')
438
+
439
+ public
440
+ def assert_no_match(regexp, string, message="")
441
+ _wrap_assertion do
442
+ assert_instance_of(Regexp, regexp, "The first argument to assert_no_match should be a Regexp.")
443
+ full_message = build_message(message, "<?> expected to not match\n<?>.", regexp, string)
444
+ assert_block(full_message) { regexp !~ string }
445
+ end
446
+ end
447
+
448
+ UncaughtThrow = {
449
+ NameError => /^uncaught throw \`(.+)\'$/,
450
+ ArgumentError => /^uncaught throw (.+)$/,
451
+ ThreadError => /^uncaught throw \`(.+)\' in thread /
452
+ } #`
453
+
454
+ ##
455
+ # Passes if the block throws +expected_object+
456
+ #
457
+ # Example:
458
+ # assert_throw(:done) do
459
+ # throw(:done)
460
+ # end
461
+
462
+ public
463
+ def assert_throw(expected_object, message="", &proc)
464
+ _wrap_assertion do
465
+ begin
466
+ catch([]) {}
467
+ rescue TypeError
468
+ assert_instance_of(Symbol, expected_object,
469
+ "assert_throws expects the symbol that should be thrown for its first argument")
470
+ end
471
+ assert_block("Should have passed a block to assert_throw.") do
472
+ block_given?
473
+ end
474
+ caught = true
475
+ begin
476
+ catch(expected_object) do
477
+ proc.call
478
+ caught = false
479
+ end
480
+ full_message = build_message(message,
481
+ "<?> should have been thrown.",
482
+ expected_object)
483
+ assert_block(full_message) {caught}
484
+ rescue NameError, ArgumentError, ThreadError => error
485
+ raise unless UncaughtThrow[error.class] =~ error.message
486
+ tag = $1
487
+ tag = tag[1..-1].intern if tag[0, 1] == ":"
488
+ full_message = build_message(message,
489
+ "<?> expected to be thrown but\n" +
490
+ "<?> was thrown.",
491
+ expected_object, tag)
492
+ flunk(full_message)
493
+ end
494
+ end
495
+ end
496
+
497
+ ##
498
+ # Alias of assert_throw.
499
+ #
500
+ # Will be deprecated in 1.9, and removed in 2.0.
501
+ def assert_throws(*args, &block)
502
+ assert_throw(*args, &block)
503
+ end
504
+
505
+ ##
506
+ # Passes if block does not throw anything.
507
+ #
508
+ # Example:
509
+ # assert_nothing_thrown do
510
+ # [1, 2].uniq
511
+ # end
512
+
513
+ public
514
+ def assert_nothing_thrown(message="", &proc)
515
+ _wrap_assertion do
516
+ assert(block_given?, "Should have passed a block to assert_nothing_thrown")
517
+ begin
518
+ proc.call
519
+ rescue NameError, ArgumentError, ThreadError => error
520
+ raise unless UncaughtThrow[error.class] =~ error.message
521
+ tag = $1
522
+ tag = tag[1..-1].intern if tag[0, 1] == ":"
523
+ full_message = build_message(message,
524
+ "<?> was thrown when nothing was expected",
525
+ tag)
526
+ flunk(full_message)
527
+ end
528
+ assert(true, "Expected nothing to be thrown")
529
+ end
530
+ end
531
+
532
+ ##
533
+ # Passes if +expected_float+ and +actual_float+ are equal
534
+ # within +delta+ tolerance.
535
+ #
536
+ # Example:
537
+ # assert_in_delta 0.05, (50000.0 / 10**6), 0.00001
538
+
539
+ public
540
+ def assert_in_delta(expected_float, actual_float, delta, message="")
541
+ _wrap_assertion do
542
+ {expected_float => "first float", actual_float => "second float", delta => "delta"}.each do |float, name|
543
+ assert_respond_to(float, :to_f, "The arguments must respond to to_f; the #{name} did not")
544
+ end
545
+ assert_operator(delta, :>=, 0.0, "The delta should not be negative")
546
+ full_message = build_message(message, <<EOT, expected_float, actual_float, delta)
547
+ <?> and
548
+ <?> expected to be within
549
+ <?> of each other.
550
+ EOT
551
+ assert_block(full_message) { (expected_float.to_f - actual_float.to_f).abs <= delta.to_f }
552
+ end
553
+ end
554
+
555
+ ##
556
+ # Passes if the method send returns a true value.
557
+ #
558
+ # +send_array+ is composed of:
559
+ # * A receiver
560
+ # * A method
561
+ # * Arguments to the method
562
+ #
563
+ # Example:
564
+ # assert_send [[1, 2], :include?, 4]
565
+
566
+ public
567
+ def assert_send(send_array, message="")
568
+ _wrap_assertion do
569
+ assert_instance_of(Array, send_array, "assert_send requires an array of send information")
570
+ assert(send_array.size >= 2, "assert_send requires at least a receiver and a message name")
571
+ full_message = build_message(message, <<EOT, send_array[0], AssertionMessage.literal(send_array[1].to_s), send_array[2..-1])
572
+ <?> expected to respond to
573
+ <?(?)> with a true value.
574
+ EOT
575
+ assert_block(full_message) { send_array[0].__send__(send_array[1], *send_array[2..-1]) }
576
+ end
577
+ end
578
+
579
+ ##
580
+ # Passes if +actual+ is a boolean value.
581
+ #
582
+ # Example:
583
+ # assert_boolean(true) # -> pass
584
+ # assert_boolean(nil) # -> fail
585
+ def assert_boolean(actual, message=nil)
586
+ _wrap_assertion do
587
+ assert_block(build_message(message,
588
+ "<true> or <false> expected but was\n<?>",
589
+ actual)) do
590
+ [true, false].include?(actual)
591
+ end
592
+ end
593
+ end
594
+
595
+ ##
596
+ # Passes if +actual+ is true.
597
+ #
598
+ # Example:
599
+ # assert_true(true) # -> pass
600
+ # assert_true(:true) # -> fail
601
+ def assert_true(actual, message=nil)
602
+ _wrap_assertion do
603
+ assert_block(build_message(message,
604
+ "<true> expected but was\n<?>",
605
+ actual)) do
606
+ actual == true
607
+ end
608
+ end
609
+ end
610
+
611
+ ##
612
+ # Passes if +actual+ is false.
613
+ #
614
+ # Example:
615
+ # assert_false(false) # -> pass
616
+ # assert_false(nil) # -> fail
617
+ def assert_false(actual, message=nil)
618
+ _wrap_assertion do
619
+ assert_block(build_message(message,
620
+ "<false> expected but was\n<?>",
621
+ actual)) do
622
+ actual == false
623
+ end
624
+ end
625
+ end
626
+
627
+ ##
628
+ # Passes if expression "+expected+ +operator+
629
+ # +actual+" is true.
630
+ #
631
+ # Example:
632
+ # assert_compare(1, "<", 10) # -> pass
633
+ # assert_compare(1, ">=", 10) # -> fail
634
+ def assert_compare(expected, operator, actual, message=nil)
635
+ _wrap_assertion do
636
+ assert_send([["<", "<=", ">", ">="], :include?, operator.to_s])
637
+ case operator.to_s
638
+ when "<"
639
+ operator_description = "less than"
640
+ when "<="
641
+ operator_description = "less than or equal to"
642
+ when ">"
643
+ operator_description = "greater than"
644
+ when ">="
645
+ operator_description = "greater than or equal to"
646
+ end
647
+ template = <<-EOT
648
+ <?> #{operator} <?> should be true
649
+ <?> expected #{operator_description}
650
+ <?>.
651
+ EOT
652
+ full_message = build_message(message, template,
653
+ expected, actual,
654
+ expected, actual)
655
+ assert_block(full_message) do
656
+ expected.send(operator, actual)
657
+ end
658
+ end
659
+ end
660
+
661
+ ##
662
+ # Passes if assertion is failed in block.
663
+ #
664
+ # Example:
665
+ # assert_fail_assertion {assert_equal("A", "B")} # -> pass
666
+ # assert_fail_assertion {assert_equal("A", "A")} # -> fail
667
+ def assert_fail_assertion(message=nil)
668
+ _wrap_assertion do
669
+ full_message = build_message(message,
670
+ "Failed assertion was expected.")
671
+ assert_block(full_message) do
672
+ begin
673
+ yield
674
+ false
675
+ rescue AssertionFailedError
676
+ true
677
+ end
678
+ end
679
+ end
680
+ end
681
+
682
+ ##
683
+ # Passes if an exception is raised in block and its
684
+ # message is +expected+.
685
+ #
686
+ # Example:
687
+ # assert_raise_message("exception") {raise "exception"} # -> pass
688
+ # assert_raise_message(/exc/i) {raise "exception"} # -> pass
689
+ # assert_raise_message("exception") {raise "EXCEPTION"} # -> fail
690
+ # assert_raise_message("exception") {} # -> fail
691
+ def assert_raise_message(expected, message=nil)
692
+ _wrap_assertion do
693
+ full_message = build_message(message,
694
+ "<?> exception message expected " +
695
+ "but none was thrown.",
696
+ expected)
697
+ exception = nil
698
+ assert_block(full_message) do
699
+ begin
700
+ yield
701
+ false
702
+ rescue Exception => exception
703
+ true
704
+ end
705
+ end
706
+
707
+ actual = exception.message
708
+ diff = AssertionMessage.delayed_diff(expected, actual)
709
+ full_message =
710
+ build_message(message,
711
+ "<?> exception message expected but was\n" +
712
+ "<?>.?", expected, actual, diff)
713
+ assert_block(full_message) do
714
+ if expected.is_a?(Regexp)
715
+ expected =~ actual
716
+ else
717
+ expected == actual
718
+ end
719
+ end
720
+ end
721
+ end
722
+
723
+ ##
724
+ # Passes if +object+.const_defined?(+constant_name+)
725
+ #
726
+ # Example:
727
+ # assert_const_defined(Test, :Unit) # -> pass
728
+ # assert_const_defined(Object, :Nonexistent) # -> fail
729
+ def assert_const_defined(object, constant_name, message=nil)
730
+ _wrap_assertion do
731
+ full_message = build_message(message,
732
+ "<?>.const_defined\\?(<?>) expected.",
733
+ object, constant_name)
734
+ assert_block(full_message) do
735
+ object.const_defined?(constant_name)
736
+ end
737
+ end
738
+ end
739
+
740
+ ##
741
+ # Passes if !+object+.const_defined?(+constant_name+)
742
+ #
743
+ # Example:
744
+ # assert_not_const_defined(Object, :Nonexistent) # -> pass
745
+ # assert_not_const_defined(Test, :Unit) # -> fail
746
+ def assert_not_const_defined(object, constant_name, message=nil)
747
+ _wrap_assertion do
748
+ full_message = build_message(message,
749
+ "!<?>.const_defined\\?(<?>) expected.",
750
+ object, constant_name)
751
+ assert_block(full_message) do
752
+ !object.const_defined?(constant_name)
753
+ end
754
+ end
755
+ end
756
+
757
+ ##
758
+ # Passes if +object+.+predicate+
759
+ #
760
+ # Example:
761
+ # assert_predicate([], :empty?) # -> pass
762
+ # assert_predicate([1], :empty?) # -> fail
763
+ def assert_predicate(object, predicate, message=nil)
764
+ _wrap_assertion do
765
+ assert_respond_to(object, predicate, message)
766
+ actual = object.send(predicate)
767
+ full_message = build_message(message,
768
+ "<?>.? is true value expected but was\n" +
769
+ "<?>",
770
+ object,
771
+ AssertionMessage.literal(predicate),
772
+ actual)
773
+ assert_block(full_message) do
774
+ actual
775
+ end
776
+ end
777
+ end
778
+
779
+ ##
780
+ # Passes if +object+.+predicate+
781
+ #
782
+ # Example:
783
+ # assert_not_predicate([1], :empty?) # -> pass
784
+ # assert_not_predicate([], :empty?) # -> fail
785
+ def assert_not_predicate(object, predicate, message=nil)
786
+ _wrap_assertion do
787
+ assert_respond_to(object, predicate, message)
788
+ actual = object.send(predicate)
789
+ full_message = build_message(message,
790
+ "<?>.? is false value expected but was\n" +
791
+ "<?>",
792
+ object,
793
+ AssertionMessage.literal(predicate),
794
+ actual)
795
+ assert_block(full_message) do
796
+ not actual
797
+ end
798
+ end
799
+ end
800
+
801
+ ##
802
+ # Passes if +object+#+alias_name+ is an alias method of
803
+ # +object+#+original_name+.
804
+ #
805
+ # Example:
806
+ # assert_alias_method([], :length, :size) # -> pass
807
+ # assert_alias_method([], :size, :length) # -> pass
808
+ # assert_alias_method([], :each, :size) # -> fail
809
+ def assert_alias_method(object, alias_name, original_name, message=nil)
810
+ _wrap_assertion do
811
+ find_method_failure_message = Proc.new do |method_name|
812
+ build_message(message,
813
+ "<?>.? doesn't exist\n" +
814
+ "(Class: <?>)",
815
+ object,
816
+ AssertionMessage.literal(method_name),
817
+ object.class)
818
+ end
819
+
820
+ alias_method = original_method = nil
821
+ assert_block(find_method_failure_message.call(alias_name)) do
822
+ begin
823
+ alias_method = object.method(alias_name)
824
+ true
825
+ rescue NameError
826
+ false
827
+ end
828
+ end
829
+ assert_block(find_method_failure_message.call(original_name)) do
830
+ begin
831
+ original_method = object.method(original_name)
832
+ true
833
+ rescue NameError
834
+ false
835
+ end
836
+ end
837
+
838
+ full_message = build_message(message,
839
+ "<?> is alias of\n" +
840
+ "<?> expected",
841
+ alias_method,
842
+ original_method)
843
+ assert_block(full_message) do
844
+ alias_method == original_method
845
+ end
846
+ end
847
+ end
848
+
849
+ ##
850
+ # Builds a failure message. +head+ is added before the +template+ and
851
+ # +arguments+ replaces the '?'s positionally in the template.
852
+
853
+ public
854
+ def build_message(head, template=nil, *arguments)
855
+ template &&= template.chomp
856
+ return AssertionMessage.new(head, template, arguments)
857
+ end
858
+
859
+ private
860
+ def _wrap_assertion
861
+ @_assertion_wrapped ||= false
862
+ unless (@_assertion_wrapped)
863
+ @_assertion_wrapped = true
864
+ begin
865
+ add_assertion
866
+ return yield
867
+ ensure
868
+ @_assertion_wrapped = false
869
+ end
870
+ else
871
+ return yield
872
+ end
873
+ end
874
+
875
+ ##
876
+ # Called whenever an assertion is made. Define this in classes that
877
+ # include Test::Unit::Assertions to record assertion counts.
878
+
879
+ private
880
+ def add_assertion
881
+ end
882
+
883
+ ##
884
+ # Select whether or not to use the pretty-printer. If this option is set
885
+ # to false before any assertions are made, pp.rb will not be required.
886
+
887
+ public
888
+ def self.use_pp=(value)
889
+ AssertionMessage.use_pp = value
890
+ end
891
+
892
+ # :stopdoc:
893
+ private
894
+ def _assert_raise(assert_expected_exception, *args, &block)
895
+ _wrap_assertion do
896
+ if args.last.is_a?(String)
897
+ message = args.pop
898
+ else
899
+ message = ""
900
+ end
901
+
902
+ assert_exception_helper = AssertExceptionHelper.new(self, args)
903
+ expected = assert_exception_helper.expected_exceptions
904
+ actual_exception = nil
905
+ full_message = build_message(message,
906
+ "<?> exception expected " +
907
+ "but none was thrown.",
908
+ expected)
909
+ assert_block(full_message) do
910
+ begin
911
+ yield
912
+ false
913
+ rescue Exception => actual_exception
914
+ true
915
+ end
916
+ end
917
+ assert_expected_exception.call(message, assert_exception_helper,
918
+ actual_exception)
919
+ actual_exception
920
+ end
921
+ end
922
+
923
+ class AssertionMessage
924
+ @use_pp = true
925
+ class << self
926
+ attr_accessor :use_pp
927
+
928
+ def literal(value)
929
+ Literal.new(value)
930
+ end
931
+
932
+ def delayed_literal(&block)
933
+ DelayedLiteral.new(block)
934
+ end
935
+
936
+ def maybe_container(value, &formatter)
937
+ MaybeContainer.new(value, &formatter)
938
+ end
939
+
940
+ MAX_DIFF_TARGET_STRING_SIZE = 1000
941
+ def max_diff_target_string_size
942
+ size = ENV["TEST_UNIT_MAX_DIFF_TARGET_STRING_SIZE"]
943
+ if size
944
+ begin
945
+ size = Integer(size)
946
+ rescue ArgumentError
947
+ size = nil
948
+ end
949
+ end
950
+ size || MAX_DIFF_TARGET_STRING_SIZE
951
+ end
952
+
953
+ def diff_target_string?(string)
954
+ if string.respond_to?(:bytesize)
955
+ string.bytesize < max_diff_target_string_size
956
+ else
957
+ string.size < max_diff_target_string_size
958
+ end
959
+ end
960
+
961
+ def prepare_for_diff(from, to)
962
+ if !from.is_a?(String) or !to.is_a?(String)
963
+ from = convert(from)
964
+ to = convert(to)
965
+ end
966
+
967
+ if diff_target_string?(from) and diff_target_string?(to)
968
+ [from, to]
969
+ else
970
+ [nil, nil]
971
+ end
972
+ end
973
+
974
+ def delayed_diff(from, to)
975
+ delayed_literal do
976
+ from, to = prepare_for_diff(from, to)
977
+
978
+ diff = "" if from.nil? or to.nil?
979
+ diff ||= Diff.readable(from, to)
980
+ if /^[-+]/ !~ diff
981
+ diff = ""
982
+ elsif /^[ ?]/ =~ diff or /(?:.*\n){2,}/ =~ diff
983
+ diff = "\n\ndiff:\n#{diff}"
984
+ else
985
+ diff = ""
986
+ end
987
+
988
+ if Diff.need_fold?(diff)
989
+ folded_diff = Diff.folded_readable(from, to)
990
+ diff << "\n\nfolded diff:\n#{folded_diff}"
991
+ end
992
+
993
+ diff
994
+ end
995
+ end
996
+
997
+ def convert(object)
998
+ case object
999
+ when Exception
1000
+ <<EOM.chop
1001
+ Class: <#{convert(object.class)}>
1002
+ Message: <#{convert(object.message)}>
1003
+ ---Backtrace---
1004
+ #{Util::BacktraceFilter.filter_backtrace(object.backtrace).join("\n")}
1005
+ ---------------
1006
+ EOM
1007
+ else
1008
+ if use_pp
1009
+ begin
1010
+ require 'pp' unless defined?(PP)
1011
+ begin
1012
+ return PP.pp(object, '').chomp
1013
+ rescue NameError
1014
+ end
1015
+ rescue LoadError
1016
+ self.use_pp = false
1017
+ end
1018
+ end
1019
+ object.inspect
1020
+ end
1021
+ end
1022
+ end
1023
+
1024
+ class Literal
1025
+ def initialize(value)
1026
+ @value = value
1027
+ end
1028
+
1029
+ def inspect
1030
+ @value.to_s
1031
+ end
1032
+ end
1033
+
1034
+ class DelayedLiteral
1035
+ def initialize(value)
1036
+ @value = value
1037
+ end
1038
+
1039
+ def inspect
1040
+ @value.call.to_s
1041
+ end
1042
+ end
1043
+
1044
+ class MaybeContainer
1045
+ def initialize(value, &formatter)
1046
+ @value = value
1047
+ @formatter = formatter
1048
+ end
1049
+
1050
+ def inspect
1051
+ if @value.is_a?(Array)
1052
+ values = @value.collect do |value|
1053
+ @formatter.call(AssertionMessage.convert(value))
1054
+ end
1055
+ "[#{values.join(', ')}]"
1056
+ else
1057
+ @formatter.call(AssertionMessage.convert(@value))
1058
+ end
1059
+ end
1060
+ end
1061
+
1062
+ class Template
1063
+ def self.create(string)
1064
+ parts = (string ? string.scan(/(?=[^\\])\?|(?:\\\?|[^\?])+/m) : [])
1065
+ self.new(parts)
1066
+ end
1067
+
1068
+ attr_reader :count
1069
+
1070
+ def initialize(parts)
1071
+ @parts = parts
1072
+ @count = parts.find_all{|e| e == '?'}.size
1073
+ end
1074
+
1075
+ def result(parameters)
1076
+ raise "The number of parameters does not match the number of substitutions." if(parameters.size != count)
1077
+ params = parameters.dup
1078
+ @parts.collect{|e| e == '?' ? params.shift : e.gsub(/\\\?/m, '?')}.join('')
1079
+ end
1080
+ end
1081
+
1082
+ include Util::BacktraceFilter
1083
+
1084
+ def initialize(head, template_string, parameters)
1085
+ @head = head
1086
+ @template_string = template_string
1087
+ @parameters = parameters
1088
+ end
1089
+
1090
+ def convert(object)
1091
+ self.class.convert(object)
1092
+ end
1093
+
1094
+ def template
1095
+ @template ||= Template.create(@template_string)
1096
+ end
1097
+
1098
+ def add_period(string)
1099
+ (string =~ /\.\Z/ ? string : string + '.')
1100
+ end
1101
+
1102
+ def to_s
1103
+ message_parts = []
1104
+ if (@head)
1105
+ head = @head.to_s
1106
+ unless(head.empty?)
1107
+ message_parts << add_period(head)
1108
+ end
1109
+ end
1110
+ tail = template.result(@parameters.collect{|e| convert(e)})
1111
+ message_parts << tail unless(tail.empty?)
1112
+ message_parts.join("\n")
1113
+ end
1114
+ end
1115
+
1116
+ class AssertExceptionHelper
1117
+ class WrappedException
1118
+ def initialize(exception)
1119
+ @exception = exception
1120
+ end
1121
+
1122
+ def inspect
1123
+ if default_inspect?
1124
+ "#{@exception.class.inspect}(#{@exception.message.inspect})"
1125
+ else
1126
+ @exception.inspect
1127
+ end
1128
+ end
1129
+
1130
+ def method_missing(name, *args, &block)
1131
+ @exception.send(name, *args, &block)
1132
+ end
1133
+
1134
+ private
1135
+ def default_inspect?
1136
+ inspect_method = @exception.method(:inspect)
1137
+ if inspect_method.respond_to?(:owner) and
1138
+ inspect_method.owner == Exception
1139
+ true
1140
+ else
1141
+ default_inspect_method = Exception.instance_method(:inspect)
1142
+ default_inspect_method.bind(@exception).call == @exception.inspect
1143
+ end
1144
+ end
1145
+ end
1146
+
1147
+ def initialize(test_case, expected_exceptions)
1148
+ @test_case = test_case
1149
+ @expected_exceptions = expected_exceptions
1150
+ @expected_classes, @expected_modules, @expected_objects =
1151
+ split_expected_exceptions(expected_exceptions)
1152
+ end
1153
+
1154
+ def expected_exceptions
1155
+ exceptions = @expected_exceptions.collect do |exception|
1156
+ if exception.is_a?(Exception)
1157
+ WrappedException.new(exception)
1158
+ else
1159
+ exception
1160
+ end
1161
+ end
1162
+ if exceptions.size == 1
1163
+ exceptions[0]
1164
+ else
1165
+ exceptions
1166
+ end
1167
+ end
1168
+
1169
+ def expected?(actual_exception, equality=nil)
1170
+ equality ||= :instance_of?
1171
+ expected_class?(actual_exception, equality) or
1172
+ expected_module?(actual_exception) or
1173
+ expected_object?(actual_exception)
1174
+ end
1175
+
1176
+ private
1177
+ def split_expected_exceptions(expected_exceptions)
1178
+ exception_modules = []
1179
+ exception_objects = []
1180
+ exception_classes = []
1181
+ expected_exceptions.each do |exception_type|
1182
+ if exception_type.instance_of?(Module)
1183
+ exception_modules << exception_type
1184
+ elsif exception_type.is_a?(Exception)
1185
+ exception_objects << exception_type
1186
+ else
1187
+ @test_case.send(:assert,
1188
+ Exception >= exception_type,
1189
+ "Should expect a class of exception, " +
1190
+ "#{exception_type}")
1191
+ exception_classes << exception_type
1192
+ end
1193
+ end
1194
+ [exception_classes, exception_modules, exception_objects]
1195
+ end
1196
+
1197
+ def expected_class?(actual_exception, equality)
1198
+ @expected_classes.any? do |expected_class|
1199
+ actual_exception.send(equality, expected_class)
1200
+ end
1201
+ end
1202
+
1203
+ def expected_module?(actual_exception)
1204
+ @expected_modules.any? do |expected_module|
1205
+ actual_exception.is_a?(expected_module)
1206
+ end
1207
+ end
1208
+
1209
+ def expected_object?(actual_exception)
1210
+ @expected_objects.any? do |expected_object|
1211
+ expected_object == actual_exception or
1212
+ fallback_exception_object_equal(expected_object, actual_exception)
1213
+ end
1214
+ end
1215
+
1216
+ def fallback_exception_object_equal(expected_object, actual_exception)
1217
+ owner = Util::MethodOwnerFinder.find(expected_object, :==)
1218
+ if owner == Kernel or owner == Exception
1219
+ expected_object.class == actual_exception.class and
1220
+ expected_object.message == actual_exception.message
1221
+ else
1222
+ false
1223
+ end
1224
+ end
1225
+ end
1226
+
1227
+ # :startdoc:
1228
+ end
1229
+ end
1230
+ end