ActiveSambaLdap 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (180) hide show
  1. data/NEWS.en +40 -0
  2. data/NEWS.ja +44 -0
  3. data/README.en +366 -0
  4. data/README.ja +361 -0
  5. data/Rakefile +168 -0
  6. data/bin/asl-groupadd +73 -0
  7. data/bin/asl-groupadd.help +1 -0
  8. data/bin/asl-groupdel +61 -0
  9. data/bin/asl-groupdel.help +1 -0
  10. data/bin/asl-groupmod +137 -0
  11. data/bin/asl-groupmod.help +1 -0
  12. data/bin/asl-groupshow +33 -0
  13. data/bin/asl-groupshow.help +1 -0
  14. data/bin/asl-passwd +90 -0
  15. data/bin/asl-passwd.help +1 -0
  16. data/bin/asl-populate +101 -0
  17. data/bin/asl-populate.help +1 -0
  18. data/bin/asl-purge +26 -0
  19. data/bin/asl-purge.help +1 -0
  20. data/bin/asl-samba-computeradd +96 -0
  21. data/bin/asl-samba-computeradd.help +1 -0
  22. data/bin/asl-samba-groupadd +57 -0
  23. data/bin/asl-samba-groupadd.help +1 -0
  24. data/bin/asl-samba-groupdel +55 -0
  25. data/bin/asl-samba-groupdel.help +1 -0
  26. data/bin/asl-samba-groupmod +99 -0
  27. data/bin/asl-samba-groupmod.help +1 -0
  28. data/bin/asl-samba-useradd +100 -0
  29. data/bin/asl-samba-useradd.help +1 -0
  30. data/bin/asl-samba-userdel +49 -0
  31. data/bin/asl-samba-userdel.help +1 -0
  32. data/bin/asl-samba-usermod +94 -0
  33. data/bin/asl-samba-usermod.help +1 -0
  34. data/bin/asl-useradd +264 -0
  35. data/bin/asl-useradd.help +1 -0
  36. data/bin/asl-userdel +84 -0
  37. data/bin/asl-userdel.help +1 -0
  38. data/bin/asl-usermod +335 -0
  39. data/bin/asl-usermod.help +1 -0
  40. data/bin/asl-usershow +33 -0
  41. data/bin/asl-usershow.help +1 -0
  42. data/lib/active_samba_ldap.rb +42 -0
  43. data/lib/active_samba_ldap/account_entry.rb +208 -0
  44. data/lib/active_samba_ldap/base.rb +137 -0
  45. data/lib/active_samba_ldap/command.rb +100 -0
  46. data/lib/active_samba_ldap/computer.rb +24 -0
  47. data/lib/active_samba_ldap/computer_account_entry.rb +34 -0
  48. data/lib/active_samba_ldap/configuration.rb +333 -0
  49. data/lib/active_samba_ldap/dc.rb +19 -0
  50. data/lib/active_samba_ldap/entry.rb +81 -0
  51. data/lib/active_samba_ldap/get_text_support.rb +12 -0
  52. data/lib/active_samba_ldap/group.rb +17 -0
  53. data/lib/active_samba_ldap/group_entry.rb +187 -0
  54. data/lib/active_samba_ldap/idmap.rb +19 -0
  55. data/lib/active_samba_ldap/ou.rb +20 -0
  56. data/lib/active_samba_ldap/populate.rb +257 -0
  57. data/lib/active_samba_ldap/reloadable.rb +15 -0
  58. data/lib/active_samba_ldap/samba_account_entry.rb +225 -0
  59. data/lib/active_samba_ldap/samba_entry.rb +26 -0
  60. data/lib/active_samba_ldap/samba_group_entry.rb +143 -0
  61. data/lib/active_samba_ldap/unix_id_pool.rb +43 -0
  62. data/lib/active_samba_ldap/user.rb +44 -0
  63. data/lib/active_samba_ldap/user_account_entry.rb +30 -0
  64. data/lib/active_samba_ldap/version.rb +3 -0
  65. data/lib/samba/encrypt.rb +86 -0
  66. data/misc/rd2html.rb +42 -0
  67. data/po/ja/active-samba-ldap.po +465 -0
  68. data/rails/README +30 -0
  69. data/rails/init.rb +33 -0
  70. data/rails_generators/scaffold_active_samba_ldap/scaffold_active_samba_ldap_generator.rb +27 -0
  71. data/rails_generators/scaffold_active_samba_ldap/templates/computer.rb +3 -0
  72. data/rails_generators/scaffold_active_samba_ldap/templates/dc.rb +3 -0
  73. data/rails_generators/scaffold_active_samba_ldap/templates/group.rb +3 -0
  74. data/rails_generators/scaffold_active_samba_ldap/templates/idmap.rb +3 -0
  75. data/rails_generators/scaffold_active_samba_ldap/templates/ldap.yml +24 -0
  76. data/rails_generators/scaffold_active_samba_ldap/templates/ou.rb +3 -0
  77. data/rails_generators/scaffold_active_samba_ldap/templates/samba_controller.rb +12 -0
  78. data/rails_generators/scaffold_active_samba_ldap/templates/samba_helper.rb +2 -0
  79. data/rails_generators/scaffold_active_samba_ldap/templates/samba_index.rhtml +17 -0
  80. data/rails_generators/scaffold_active_samba_ldap/templates/samba_populate.rhtml +15 -0
  81. data/rails_generators/scaffold_active_samba_ldap/templates/samba_purge.rhtml +10 -0
  82. data/rails_generators/scaffold_active_samba_ldap/templates/unix_id_pool.rb +3 -0
  83. data/rails_generators/scaffold_active_samba_ldap/templates/user.rb +3 -0
  84. data/setup.rb +1585 -0
  85. data/test-unit/History.txt +107 -0
  86. data/test-unit/README.txt +52 -0
  87. data/test-unit/Rakefile +40 -0
  88. data/test-unit/TODO +5 -0
  89. data/test-unit/bin/testrb +5 -0
  90. data/test-unit/lib/test/unit.rb +342 -0
  91. data/test-unit/lib/test/unit/assertionfailederror.rb +14 -0
  92. data/test-unit/lib/test/unit/assertions.rb +1149 -0
  93. data/test-unit/lib/test/unit/attribute.rb +125 -0
  94. data/test-unit/lib/test/unit/autorunner.rb +329 -0
  95. data/test-unit/lib/test/unit/collector.rb +43 -0
  96. data/test-unit/lib/test/unit/collector/descendant.rb +23 -0
  97. data/test-unit/lib/test/unit/collector/dir.rb +108 -0
  98. data/test-unit/lib/test/unit/collector/load.rb +135 -0
  99. data/test-unit/lib/test/unit/collector/objectspace.rb +34 -0
  100. data/test-unit/lib/test/unit/color-scheme.rb +86 -0
  101. data/test-unit/lib/test/unit/color.rb +96 -0
  102. data/test-unit/lib/test/unit/diff.rb +538 -0
  103. data/test-unit/lib/test/unit/error.rb +124 -0
  104. data/test-unit/lib/test/unit/exceptionhandler.rb +39 -0
  105. data/test-unit/lib/test/unit/failure.rb +110 -0
  106. data/test-unit/lib/test/unit/fixture.rb +176 -0
  107. data/test-unit/lib/test/unit/notification.rb +125 -0
  108. data/test-unit/lib/test/unit/omission.rb +143 -0
  109. data/test-unit/lib/test/unit/pending.rb +146 -0
  110. data/test-unit/lib/test/unit/priority.rb +181 -0
  111. data/test-unit/lib/test/unit/runner/console.rb +52 -0
  112. data/test-unit/lib/test/unit/runner/emacs.rb +8 -0
  113. data/test-unit/lib/test/unit/testcase.rb +425 -0
  114. data/test-unit/lib/test/unit/testresult.rb +89 -0
  115. data/test-unit/lib/test/unit/testsuite.rb +110 -0
  116. data/test-unit/lib/test/unit/ui/console/outputlevel.rb +14 -0
  117. data/test-unit/lib/test/unit/ui/console/testrunner.rb +223 -0
  118. data/test-unit/lib/test/unit/ui/emacs/testrunner.rb +49 -0
  119. data/test-unit/lib/test/unit/ui/testrunner.rb +20 -0
  120. data/test-unit/lib/test/unit/ui/testrunnermediator.rb +77 -0
  121. data/test-unit/lib/test/unit/ui/testrunnerutilities.rb +41 -0
  122. data/test-unit/lib/test/unit/util/backtracefilter.rb +41 -0
  123. data/test-unit/lib/test/unit/util/method-owner-finder.rb +28 -0
  124. data/test-unit/lib/test/unit/util/observable.rb +90 -0
  125. data/test-unit/lib/test/unit/util/procwrapper.rb +48 -0
  126. data/test-unit/lib/test/unit/version.rb +7 -0
  127. data/test-unit/sample/adder.rb +13 -0
  128. data/test-unit/sample/subtracter.rb +12 -0
  129. data/test-unit/sample/tc_adder.rb +18 -0
  130. data/test-unit/sample/tc_subtracter.rb +18 -0
  131. data/test-unit/sample/test_user.rb +22 -0
  132. data/test-unit/sample/ts_examples.rb +7 -0
  133. data/test-unit/test/collector/test-descendant.rb +135 -0
  134. data/test-unit/test/collector/test-load.rb +333 -0
  135. data/test-unit/test/collector/test_dir.rb +406 -0
  136. data/test-unit/test/collector/test_objectspace.rb +98 -0
  137. data/test-unit/test/run-test.rb +13 -0
  138. data/test-unit/test/test-attribute.rb +86 -0
  139. data/test-unit/test/test-color-scheme.rb +56 -0
  140. data/test-unit/test/test-color.rb +47 -0
  141. data/test-unit/test/test-diff.rb +477 -0
  142. data/test-unit/test/test-emacs-runner.rb +60 -0
  143. data/test-unit/test/test-fixture.rb +287 -0
  144. data/test-unit/test/test-notification.rb +33 -0
  145. data/test-unit/test/test-omission.rb +81 -0
  146. data/test-unit/test/test-pending.rb +70 -0
  147. data/test-unit/test/test-priority.rb +119 -0
  148. data/test-unit/test/test-testcase.rb +507 -0
  149. data/test-unit/test/test_assertions.rb +1082 -0
  150. data/test-unit/test/test_error.rb +26 -0
  151. data/test-unit/test/test_failure.rb +33 -0
  152. data/test-unit/test/test_testresult.rb +113 -0
  153. data/test-unit/test/test_testsuite.rb +129 -0
  154. data/test-unit/test/testunit-test-util.rb +14 -0
  155. data/test-unit/test/ui/test_testrunmediator.rb +20 -0
  156. data/test-unit/test/util/test-method-owner-finder.rb +38 -0
  157. data/test-unit/test/util/test_backtracefilter.rb +41 -0
  158. data/test-unit/test/util/test_observable.rb +102 -0
  159. data/test-unit/test/util/test_procwrapper.rb +36 -0
  160. data/test/asl-test-utils.rb +276 -0
  161. data/test/command.rb +67 -0
  162. data/test/config.yaml.sample +17 -0
  163. data/test/run-test.rb +20 -0
  164. data/test/test_asl_groupadd.rb +69 -0
  165. data/test/test_asl_groupdel.rb +88 -0
  166. data/test/test_asl_groupmod.rb +259 -0
  167. data/test/test_asl_groupshow.rb +21 -0
  168. data/test/test_asl_passwd.rb +126 -0
  169. data/test/test_asl_populate.rb +92 -0
  170. data/test/test_asl_purge.rb +21 -0
  171. data/test/test_asl_useradd.rb +712 -0
  172. data/test/test_asl_userdel.rb +75 -0
  173. data/test/test_asl_usermod.rb +549 -0
  174. data/test/test_asl_usershow.rb +27 -0
  175. data/test/test_entry.rb +21 -0
  176. data/test/test_group.rb +21 -0
  177. data/test/test_password.rb +51 -0
  178. data/test/test_samba_encrypt.rb +35 -0
  179. data/test/test_user_home_directory.rb +43 -0
  180. metadata +314 -0
@@ -0,0 +1,1082 @@
1
+ # Author:: Nathaniel Talbott.
2
+ # Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved.
3
+ # Copyright (c) 2009 Kouhei Sutou.
4
+ # License:: Ruby license.
5
+
6
+ require 'test/unit'
7
+
8
+ module Test
9
+ module Unit
10
+ class TC_Assertions < TestCase
11
+ backtrace_pre = "---Backtrace---"
12
+ backtrace_post = "---------------"
13
+ BACKTRACE_RE = /#{backtrace_pre}\n.+\n#{backtrace_post}/m
14
+
15
+ def check(value, message="")
16
+ add_assertion
17
+ raise AssertionFailedError.new(message) unless value
18
+ end
19
+
20
+ def check_assertions(expect_fail, expected_message="",
21
+ return_value_expected=false)
22
+ @actual_assertion_count = 0
23
+ failed = true
24
+ actual_message = nil
25
+ @catch_assertions = true
26
+ return_value = nil
27
+ begin
28
+ return_value = yield
29
+ failed = false
30
+ rescue AssertionFailedError => error
31
+ actual_message = error.message
32
+ end
33
+ @catch_assertions = false
34
+
35
+ if expect_fail
36
+ message = "Should have failed, but didn't"
37
+ else
38
+ message = "Should not have failed, but did with message\n" +
39
+ "<#{actual_message}>"
40
+ end
41
+ check(expect_fail == failed, message)
42
+
43
+ message = "Should have made one assertion but made\n" +
44
+ "<#{@actual_assertion_count}>"
45
+ check(1 == @actual_assertion_count, message)
46
+
47
+ if expect_fail
48
+ case expected_message
49
+ when String
50
+ check(actual_message == expected_message,
51
+ "Should have the correct message.\n" +
52
+ "<#{expected_message.inspect}> expected but was\n" +
53
+ "<#{actual_message.inspect}>")
54
+ when Regexp
55
+ check(actual_message =~ expected_message,
56
+ "The message should match correctly.\n" +
57
+ "</#{expected_message.source}/> expected to match\n" +
58
+ "<#{actual_message.inspect}>")
59
+ else
60
+ check(false,
61
+ "Incorrect expected message type in assert_nothing_failed")
62
+ end
63
+ else
64
+ if return_value_expected
65
+ check(!return_value.nil?, "Should return a value")
66
+ else
67
+ check(return_value.nil?,
68
+ "Should not return a value but returned <#{return_value}>")
69
+ end
70
+ end
71
+
72
+ return_value
73
+ end
74
+
75
+ def check_nothing_fails(return_value_expected=false, &proc)
76
+ check_assertions(false, "", return_value_expected, &proc)
77
+ end
78
+
79
+ def check_fails(expected_message="", &proc)
80
+ check_assertions(true, expected_message, &proc)
81
+ end
82
+
83
+ def inspect_tag(tag)
84
+ begin
85
+ throw tag
86
+ rescue NameError
87
+ tag.to_s.inspect
88
+ rescue ArgumentError
89
+ tag.inspect
90
+ end
91
+ end
92
+
93
+ def test_assert_block
94
+ check_nothing_fails {
95
+ assert_block {true}
96
+ }
97
+ check_nothing_fails {
98
+ assert_block("successful assert_block") {true}
99
+ }
100
+ check_nothing_fails {
101
+ assert_block("successful assert_block") {true}
102
+ }
103
+ check_fails("assert_block failed.") {
104
+ assert_block {false}
105
+ }
106
+ check_fails("failed assert_block") {
107
+ assert_block("failed assert_block") {false}
108
+ }
109
+ end
110
+
111
+ def test_assert
112
+ check_nothing_fails{assert("a")}
113
+ check_nothing_fails{assert(true)}
114
+ check_nothing_fails{assert(true, "successful assert")}
115
+ check_fails("<nil> is not true."){assert(nil)}
116
+ check_fails("<false> is not true."){assert(false)}
117
+ check_fails("failed assert.\n<false> is not true."){assert(false, "failed assert")}
118
+ end
119
+
120
+ def test_assert_equal
121
+ check_nothing_fails {
122
+ assert_equal("string1", "string1")
123
+ }
124
+ check_nothing_fails {
125
+ assert_equal("string1", "string1", "successful assert_equal")
126
+ }
127
+
128
+ message = <<-EOM.chomp
129
+ <"string1"> expected but was
130
+ <"string2">.
131
+
132
+ diff:
133
+ - string1
134
+ ? ^
135
+ + string2
136
+ ? ^
137
+ EOM
138
+ check_fails(message) {
139
+ assert_equal("string1", "string2")
140
+ }
141
+
142
+ message = <<-EOM.chomp
143
+ failed assert_equal.
144
+ <"string1"> expected but was
145
+ <"string2">.
146
+
147
+ diff:
148
+ - string1
149
+ ? ^
150
+ + string2
151
+ ? ^
152
+ EOM
153
+ check_fails(message) {
154
+ assert_equal("string1", "string2", "failed assert_equal")
155
+ }
156
+
157
+ message = <<-EOM.chomp
158
+ <"111111"> expected but was
159
+ <111111>.
160
+
161
+ diff:
162
+ - "111111"
163
+ ? - -
164
+ + 111111
165
+ EOM
166
+ check_fails(message) do
167
+ assert_equal("111111", 111111)
168
+ end
169
+ end
170
+
171
+ def test_assert_equal_with_long_line
172
+ expected = ["0123456789",
173
+ "1123456789",
174
+ "2123456789",
175
+ "3123456789",
176
+ "4123456789",
177
+ "5123456789",
178
+ "6123456789",
179
+ "7123456789",
180
+ "8123456789"].join
181
+ actual = ["0000000000",
182
+ "1123456789",
183
+ "2123456789",
184
+ "3123456789",
185
+ "4123456789",
186
+ "5123456789",
187
+ "6123456789",
188
+ "7123456789",
189
+ "8123456789"].join
190
+ message = <<-EOM.chomp
191
+ <"#{expected}"> expected but was
192
+ <"#{actual}">.
193
+
194
+ diff:
195
+ - #{expected}
196
+ ? ^^^^^^^^^
197
+ + #{actual}
198
+ ? ^^^^^^^^^
199
+
200
+ folded diff:
201
+ - 012345678911234567892123456789312345678941234567895123456789612345678971234567
202
+ ? ^^^^^^^^^
203
+ + 000000000011234567892123456789312345678941234567895123456789612345678971234567
204
+ ? ^^^^^^^^^
205
+ 898123456789
206
+ EOM
207
+ check_fails(message) do
208
+ assert_equal(expected, actual)
209
+ end
210
+ end
211
+
212
+ def test_assert_equal_for_too_small_difference
213
+ message = <<-EOM.chomp
214
+ <1> expected but was
215
+ <2>.
216
+ EOM
217
+ check_fails(message) do
218
+ assert_equal(1, 2)
219
+ end
220
+ end
221
+
222
+ def test_assert_equal_for_same_inspected_objects
223
+ now = Time.now
224
+ now_without_usec = Time.at(now.to_i)
225
+ message = <<-EOM.chomp
226
+ <#{now.inspect}> expected but was
227
+ <#{now.inspect}>.
228
+ EOM
229
+ check_fails(message) do
230
+ assert_equal(now, now_without_usec)
231
+ end
232
+ end
233
+
234
+ def test_assert_equal_with_multi_lines_result
235
+ message = <<-EOM.chomp
236
+ <#{"a\nb".inspect}> expected but was
237
+ <#{"x".inspect}>.
238
+
239
+ diff:
240
+ + x
241
+ - a
242
+ - b
243
+ EOM
244
+ check_fails(message) do
245
+ assert_equal("a\nb", "x")
246
+ end
247
+ end
248
+
249
+ def test_assert_equal_with_large_string
250
+ message = <<-EOM.chomp
251
+ <#{("a\n" + "x" * 297).inspect}> expected but was
252
+ <#{"x".inspect}>.
253
+
254
+ diff:
255
+ + x
256
+ - a
257
+ - #{"x" * 297}
258
+
259
+ folded diff:
260
+ + x
261
+ - a
262
+ - #{"x" * 78}
263
+ - #{"x" * 78}
264
+ - #{"x" * 78}
265
+ - #{"x" * 63}
266
+ EOM
267
+ check_fails(message) do
268
+ assert_equal("a\n" + "x" * 297, "x")
269
+ end
270
+
271
+ message = <<-EOM.chomp
272
+ <#{("a\n" + "x" * 298).inspect}> expected but was
273
+ <#{"x".inspect}>.
274
+ EOM
275
+ check_fails(message) do
276
+ assert_equal("a\n" + "x" * 298, "x")
277
+ end
278
+ end
279
+
280
+ def test_assert_raise_success
281
+ return_value = nil
282
+ check_nothing_fails(true) do
283
+ return_value = assert_raise(RuntimeError) do
284
+ raise "Error"
285
+ end
286
+ end
287
+ check(return_value.kind_of?(Exception),
288
+ "Should have returned the exception " +
289
+ "from a successful assert_raise")
290
+ check(return_value.message == "Error",
291
+ "Should have returned the correct exception " +
292
+ "from a successful assert_raise")
293
+
294
+ check_nothing_fails(true) do
295
+ assert_raise(ArgumentError, "successful assert_raise") do
296
+ raise ArgumentError.new("Error")
297
+ end
298
+ end
299
+
300
+ check_nothing_fails(true) do
301
+ assert_raise(RuntimeError) do
302
+ raise "Error"
303
+ end
304
+ end
305
+
306
+ check_nothing_fails(true) do
307
+ assert_raise(RuntimeError, "successful assert_raise") do
308
+ raise "Error"
309
+ end
310
+ end
311
+
312
+ check_nothing_fails(true) do
313
+ assert_raise do
314
+ raise Exception, "Any exception"
315
+ end
316
+ end
317
+ end
318
+
319
+ def test_assert_raise_fail
320
+ check_fails("<RuntimeError> exception expected but none was thrown.") do
321
+ assert_raise(RuntimeError) do
322
+ 1 + 1
323
+ end
324
+ end
325
+
326
+ message = <<-EOM
327
+ failed assert_raise.
328
+ <ArgumentError> exception expected but was
329
+ Class: <RuntimeError>
330
+ Message: <"Error">
331
+ EOM
332
+ check_fails(/\A#{message}#{BACKTRACE_RE}\Z/m) do
333
+ assert_raise(ArgumentError, "failed assert_raise") do
334
+ raise "Error"
335
+ end
336
+ end
337
+
338
+ message = <<-EOM
339
+ Should expect a class of exception, Object.
340
+ <false> is not true.
341
+ EOM
342
+ check_fails(message.chomp) do
343
+ assert_nothing_raised(Object) do
344
+ 1 + 1
345
+ end
346
+ end
347
+ end
348
+
349
+ def test_assert_raise_module
350
+ exceptions = [ArgumentError, TypeError]
351
+ modules = [Math, Comparable]
352
+ rescues = exceptions + modules
353
+
354
+ exceptions.each do |exc|
355
+ return_value = nil
356
+ check_nothing_fails(true) do
357
+ return_value = assert_raise(*rescues) do
358
+ raise exc, "Error"
359
+ end
360
+ end
361
+ check(return_value.instance_of?(exc),
362
+ "Should have returned #{exc} but was #{return_value.class}")
363
+ check(return_value.message == "Error",
364
+ "Should have returned the correct exception " +
365
+ "from a successful assert_raise")
366
+ end
367
+
368
+ modules.each do |mod|
369
+ return_value = nil
370
+ check_nothing_fails(true) do
371
+ return_value = assert_raise(*rescues) do
372
+ raise Exception.new("Error").extend(mod)
373
+ end
374
+ end
375
+ check(mod === return_value,
376
+ "Should have returned #{mod}")
377
+ check(return_value.message == "Error",
378
+ "Should have returned the correct exception " +
379
+ "from a successful assert_raise")
380
+ end
381
+
382
+ check_fails("<[ArgumentError, TypeError, Math, Comparable]> exception " +
383
+ "expected but none was thrown.") do
384
+ assert_raise(*rescues) do
385
+ 1 + 1
386
+ end
387
+ end
388
+
389
+ message = <<-EOM
390
+ failed assert_raise.
391
+ <[ArgumentError, TypeError]> exception expected but was
392
+ Class: <RuntimeError>
393
+ Message: <"Error">
394
+ EOM
395
+ message = Regexp.escape(message)
396
+ check_fails(/\A#{message}#{BACKTRACE_RE}\z/m) do
397
+ assert_raise(ArgumentError, TypeError, "failed assert_raise") do
398
+ raise "Error"
399
+ end
400
+ end
401
+ end
402
+
403
+ def test_assert_raise_instance
404
+ return_value = nil
405
+ check_nothing_fails(true) do
406
+ return_value = assert_raise(RuntimeError.new("Error")) do
407
+ raise "Error"
408
+ end
409
+ end
410
+ check(return_value.kind_of?(Exception),
411
+ "Should have returned the exception " +
412
+ "from a successful assert_raise")
413
+ check(return_value.message == "Error",
414
+ "Should have returned the correct exception " +
415
+ "from a successful assert_raise")
416
+
417
+ message = <<-EOM
418
+ <RuntimeError("XXX")> exception expected but was
419
+ Class: <RuntimeError>
420
+ Message: <"Error">
421
+ EOM
422
+ message = Regexp.escape(message)
423
+ check_fails(/\A#{message}#{BACKTRACE_RE}\z/) do
424
+ return_value = assert_raise(RuntimeError.new("XXX")) do
425
+ raise "Error"
426
+ end
427
+ end
428
+
429
+ different_error_class = Class.new(StandardError)
430
+ message = <<-EOM
431
+ <\#<Class:[xa-f\\d]+>\\("Error"\\)> exception expected but was
432
+ Class: <RuntimeError>
433
+ Message: <"Error">
434
+ EOM
435
+ check_fails(/\A#{message}#{BACKTRACE_RE}\z/) do
436
+ assert_raise(different_error_class.new("Error")) do
437
+ raise "Error"
438
+ end
439
+ end
440
+
441
+ different_error = different_error_class.new("Error")
442
+ def different_error.inspect
443
+ "DifferentError: \"Error\""
444
+ end
445
+ message = <<-EOM
446
+ <\DifferentError: \\"Error\\"> exception expected but was
447
+ Class: <RuntimeError>
448
+ Message: <"Error">
449
+ EOM
450
+ check_fails(/\A#{message}#{BACKTRACE_RE}\z/) do
451
+ assert_raise(different_error) do
452
+ raise "Error"
453
+ end
454
+ end
455
+
456
+ check_nothing_fails(true) do
457
+ assert_raise(different_error_class.new("Error"),
458
+ RuntimeError.new("Error"),
459
+ RuntimeError.new("XXX")) do
460
+ raise "Error"
461
+ end
462
+ end
463
+ end
464
+
465
+ def test_assert_instance_of
466
+ check_nothing_fails {
467
+ assert_instance_of(String, "string")
468
+ }
469
+ check_nothing_fails {
470
+ assert_instance_of(String, "string", "successful assert_instance_of")
471
+ }
472
+ check_nothing_fails {
473
+ assert_instance_of(String, "string", "successful assert_instance_of")
474
+ }
475
+ check_fails(%Q{<"string"> expected to be an instance of\n<Hash> but was\n<String>.}) {
476
+ assert_instance_of(Hash, "string")
477
+ }
478
+ check_fails(%Q{failed assert_instance_of.\n<"string"> expected to be an instance of\n<Hash> but was\n<String>.}) {
479
+ assert_instance_of(Hash, "string", "failed assert_instance_of")
480
+ }
481
+
482
+ check_nothing_fails do
483
+ assert_instance_of([Fixnum, NilClass], 100)
484
+ end
485
+ check_fails(%Q{<"string"> expected to be an instance of\n[<Fixnum>, <NilClass>] but was\n<String>.}) do
486
+ assert_instance_of([Fixnum, NilClass], "string")
487
+ end
488
+ check_fails(%Q{<100> expected to be an instance of\n[<Numeric>, <NilClass>] but was\n<Fixnum>.}) do
489
+ assert_instance_of([Numeric, NilClass], 100)
490
+ end
491
+ end
492
+
493
+ def test_assert_nil
494
+ check_nothing_fails {
495
+ assert_nil(nil)
496
+ }
497
+ check_nothing_fails {
498
+ assert_nil(nil, "successful assert_nil")
499
+ }
500
+ check_nothing_fails {
501
+ assert_nil(nil, "successful assert_nil")
502
+ }
503
+ check_fails(%Q{<"string"> expected to be nil.}) {
504
+ assert_nil("string")
505
+ }
506
+ check_fails(%Q{failed assert_nil.\n<"string"> expected to be nil.}) {
507
+ assert_nil("string", "failed assert_nil")
508
+ }
509
+ end
510
+
511
+ def test_assert_not_nil
512
+ check_nothing_fails{assert_not_nil(false)}
513
+ check_nothing_fails{assert_not_nil(false, "message")}
514
+ check_fails("<nil> expected to not be nil."){assert_not_nil(nil)}
515
+ check_fails("message.\n<nil> expected to not be nil.") {assert_not_nil(nil, "message")}
516
+ end
517
+
518
+ def test_assert_kind_of
519
+ check_nothing_fails {
520
+ assert_kind_of(Module, Array)
521
+ }
522
+ check_nothing_fails {
523
+ assert_kind_of(Object, "string", "successful assert_kind_of")
524
+ }
525
+ check_nothing_fails {
526
+ assert_kind_of(Object, "string", "successful assert_kind_of")
527
+ }
528
+ check_nothing_fails {
529
+ assert_kind_of(Comparable, 1)
530
+ }
531
+ check_fails(%Q{<"string"> expected to be kind_of?\n<Class> but was\n<String>.}) {
532
+ assert_kind_of(Class, "string")
533
+ }
534
+ check_fails(%Q{failed assert_kind_of.\n<"string"> expected to be kind_of?\n<Class> but was\n<String>.}) {
535
+ assert_kind_of(Class, "string", "failed assert_kind_of")
536
+ }
537
+
538
+ check_nothing_fails do
539
+ assert_kind_of([Fixnum, NilClass], 100)
540
+ end
541
+ check_fails(%Q{<"string"> expected to be kind_of?\n[<Fixnum>, <NilClass>] but was\n<String>.}) do
542
+ assert_kind_of([Fixnum, NilClass], "string")
543
+ end
544
+ end
545
+
546
+ def test_assert_match
547
+ check_nothing_fails {
548
+ assert_match(/strin./, "string")
549
+ }
550
+ check_nothing_fails {
551
+ assert_match("strin", "string")
552
+ }
553
+ check_nothing_fails {
554
+ assert_match(/strin./, "string", "successful assert_match")
555
+ }
556
+ check_nothing_fails {
557
+ assert_match(/strin./, "string", "successful assert_match")
558
+ }
559
+ check_fails(%Q{<"string"> expected to be =~\n</slin./>.}) {
560
+ assert_match(/slin./, "string")
561
+ }
562
+ check_fails(%Q{<"string"> expected to be =~\n</strin\\./>.}) {
563
+ assert_match("strin.", "string")
564
+ }
565
+ check_fails(%Q{failed assert_match.\n<"string"> expected to be =~\n</slin./>.}) {
566
+ assert_match(/slin./, "string", "failed assert_match")
567
+ }
568
+ end
569
+
570
+ def test_assert_same
571
+ thing = "thing"
572
+ check_nothing_fails {
573
+ assert_same(thing, thing)
574
+ }
575
+ check_nothing_fails {
576
+ assert_same(thing, thing, "successful assert_same")
577
+ }
578
+ check_nothing_fails {
579
+ assert_same(thing, thing, "successful assert_same")
580
+ }
581
+ thing2 = "thing"
582
+ check_fails(%Q{<"thing">\nwith id <#{thing.__id__}> expected to be equal? to\n<"thing">\nwith id <#{thing2.__id__}>.}) {
583
+ assert_same(thing, thing2)
584
+ }
585
+ check_fails(%Q{failed assert_same.\n<"thing">\nwith id <#{thing.__id__}> expected to be equal? to\n<"thing">\nwith id <#{thing2.__id__}>.}) {
586
+ assert_same(thing, thing2, "failed assert_same")
587
+ }
588
+ end
589
+
590
+ def test_assert_nothing_raised
591
+ check_nothing_fails {
592
+ assert_nothing_raised {
593
+ 1 + 1
594
+ }
595
+ }
596
+ check_nothing_fails {
597
+ assert_nothing_raised("successful assert_nothing_raised") {
598
+ 1 + 1
599
+ }
600
+ }
601
+ check_nothing_fails {
602
+ assert_nothing_raised("successful assert_nothing_raised") {
603
+ 1 + 1
604
+ }
605
+ }
606
+ check_nothing_fails {
607
+ begin
608
+ assert_nothing_raised(RuntimeError, StandardError, Comparable, "successful assert_nothing_raised") {
609
+ raise ZeroDivisionError.new("ArgumentError")
610
+ }
611
+ rescue ZeroDivisionError
612
+ end
613
+ }
614
+ check_fails("Should expect a class of exception, Object.\n<false> is not true.") {
615
+ assert_nothing_raised(Object) {
616
+ 1 + 1
617
+ }
618
+ }
619
+ check_fails(%r{\AException raised:\nClass: <RuntimeError>\nMessage: <"Error">\n---Backtrace---\n.+\n---------------\Z}m) {
620
+ assert_nothing_raised {
621
+ raise "Error"
622
+ }
623
+ }
624
+ check_fails(%r{\Afailed assert_nothing_raised\.\nException raised:\nClass: <RuntimeError>\nMessage: <"Error">\n---Backtrace---\n.+\n---------------\Z}m) {
625
+ assert_nothing_raised("failed assert_nothing_raised") {
626
+ raise "Error"
627
+ }
628
+ }
629
+ check_fails(%r{\AException raised:\nClass: <RuntimeError>\nMessage: <"Error">\n---Backtrace---\n.+\n---------------\Z}m) {
630
+ assert_nothing_raised(StandardError, RuntimeError) {
631
+ raise "Error"
632
+ }
633
+ }
634
+ check_fails("Failure.") do
635
+ assert_nothing_raised do
636
+ flunk("Failure")
637
+ end
638
+ end
639
+ end
640
+
641
+ def test_flunk
642
+ check_fails("Flunked.") {
643
+ flunk
644
+ }
645
+ check_fails("flunk message.") {
646
+ flunk("flunk message")
647
+ }
648
+ end
649
+
650
+ def test_assert_not_same
651
+ thing = "thing"
652
+ thing2 = "thing"
653
+ check_nothing_fails {
654
+ assert_not_same(thing, thing2)
655
+ }
656
+ check_nothing_fails {
657
+ assert_not_same(thing, thing2, "message")
658
+ }
659
+ check_fails(%Q{<"thing">\nwith id <#{thing.__id__}> expected to not be equal? to\n<"thing">\nwith id <#{thing.__id__}>.}) {
660
+ assert_not_same(thing, thing)
661
+ }
662
+ check_fails(%Q{message.\n<"thing">\nwith id <#{thing.__id__}> expected to not be equal? to\n<"thing">\nwith id <#{thing.__id__}>.}) {
663
+ assert_not_same(thing, thing, "message")
664
+ }
665
+ end
666
+
667
+ def test_assert_not_equal
668
+ check_nothing_fails {
669
+ assert_not_equal("string1", "string2")
670
+ }
671
+ check_nothing_fails {
672
+ assert_not_equal("string1", "string2", "message")
673
+ }
674
+ check_fails(%Q{<"string"> expected to be != to\n<"string">.}) {
675
+ assert_not_equal("string", "string")
676
+ }
677
+ check_fails(%Q{message.\n<"string"> expected to be != to\n<"string">.}) {
678
+ assert_not_equal("string", "string", "message")
679
+ }
680
+ end
681
+
682
+ def test_assert_no_match
683
+ check_nothing_fails{assert_no_match(/sling/, "string")}
684
+ check_nothing_fails{assert_no_match(/sling/, "string", "message")}
685
+ check_fails(%Q{The first argument to assert_no_match should be a Regexp.\n<"asdf"> expected to be an instance of\n<Regexp> but was\n<String>.}) do
686
+ assert_no_match("asdf", "asdf")
687
+ end
688
+ check_fails(%Q{</string/> expected to not match\n<"string">.}) do
689
+ assert_no_match(/string/, "string")
690
+ end
691
+ check_fails(%Q{message.\n</string/> expected to not match\n<"string">.}) do
692
+ assert_no_match(/string/, "string", "message")
693
+ end
694
+ end
695
+
696
+ def test_assert_throw
697
+ check_nothing_fails do
698
+ assert_throw(:thing, "message") do
699
+ throw :thing
700
+ end
701
+ end
702
+
703
+ tag = :thing2
704
+ check_fails("message.\n" +
705
+ "<:thing> expected to be thrown but\n" +
706
+ "<#{inspect_tag(tag)}> was thrown.") do
707
+ assert_throw(:thing, "message") do
708
+ throw :thing2
709
+ end
710
+ end
711
+ check_fails("message.\n" +
712
+ "<:thing> should have been thrown.") do
713
+ assert_throw(:thing, "message") do
714
+ 1 + 1
715
+ end
716
+ end
717
+ end
718
+
719
+ def test_assert_nothing_thrown
720
+ check_nothing_fails do
721
+ assert_nothing_thrown("message") do
722
+ 1 + 1
723
+ end
724
+ end
725
+
726
+ tag = :thing
727
+ inspected = inspect_tag(tag)
728
+ check_fails("message.\n" +
729
+ "<#{inspected}> was thrown when nothing was expected.") do
730
+ assert_nothing_thrown("message") do
731
+ throw tag
732
+ end
733
+ end
734
+ end
735
+
736
+ def test_assert_operator
737
+ check_nothing_fails {
738
+ assert_operator("thing", :==, "thing", "message")
739
+ }
740
+ check_fails(%Q{<0.15>\ngiven as the operator for #assert_operator must be a Symbol or #respond_to?(:to_str).}) do
741
+ assert_operator("thing", 0.15, "thing")
742
+ end
743
+ check_fails(%Q{message.\n<"thing1"> expected to be\n==\n<"thing2">.}) {
744
+ assert_operator("thing1", :==, "thing2", "message")
745
+ }
746
+ end
747
+
748
+ def test_assert_respond_to
749
+ check_nothing_fails {
750
+ assert_respond_to("thing", :to_s, "message")
751
+ }
752
+ check_nothing_fails {
753
+ assert_respond_to("thing", "to_s", "message")
754
+ }
755
+ check_fails("<0.15>.kind_of?(Symbol) or\n" +
756
+ "<0.15>.respond_to?(:to_str) expected") {
757
+ assert_respond_to("thing", 0.15)
758
+ }
759
+ check_fails("message.\n" +
760
+ "<:symbol>.respond_to?(:non_existent) expected\n" +
761
+ "(Class: <Symbol>)") {
762
+ assert_respond_to(:symbol, :non_existent, "message")
763
+ }
764
+ end
765
+
766
+ def test_assert_in_delta
767
+ check_nothing_fails {
768
+ assert_in_delta(1.4, 1.4, 0)
769
+ }
770
+ check_nothing_fails {
771
+ assert_in_delta(0.5, 0.4, 0.1, "message")
772
+ }
773
+ check_nothing_fails {
774
+ float_thing = Object.new
775
+ def float_thing.to_f
776
+ 0.2
777
+ end
778
+ assert_in_delta(0.1, float_thing, 0.1)
779
+ }
780
+ check_fails("message.\n<0.5> and\n<0.4> expected to be within\n<0.05> of each other.") {
781
+ assert_in_delta(0.5, 0.4, 0.05, "message")
782
+ }
783
+ object = Object.new
784
+ check_fails("The arguments must respond to to_f; " +
785
+ "the first float did not.\n" +
786
+ "<#{object.inspect}>.respond_to?(:to_f) expected\n" +
787
+ "(Class: <Object>)") {
788
+ assert_in_delta(object, 0.4, 0.1)
789
+ }
790
+ check_fails("The delta should not be negative.\n" +
791
+ "<-0.1> expected to be\n>=\n<0.0>.") {
792
+ assert_in_delta(0.5, 0.4, -0.1, "message")
793
+ }
794
+ end
795
+
796
+ def test_assert_send
797
+ object = Object.new
798
+ class << object
799
+ private
800
+ def return_argument(argument, bogus)
801
+ return argument
802
+ end
803
+ end
804
+ check_nothing_fails {
805
+ assert_send([object, :return_argument, true, "bogus"], "message")
806
+ }
807
+ check_fails(%r{\Amessage\.\n<.+> expected to respond to\n<return_argument\(\[false, "bogus"\]\)> with a true value.\Z}) {
808
+ assert_send([object, :return_argument, false, "bogus"], "message")
809
+ }
810
+ end
811
+
812
+ def test_condition_invariant
813
+ object = Object.new
814
+ def object.inspect
815
+ @changed = true
816
+ end
817
+ def object.==(other)
818
+ @changed ||= false
819
+ return (!@changed)
820
+ end
821
+ check_nothing_fails do
822
+ assert_equal(object, object, "message")
823
+ end
824
+ end
825
+
826
+ def test_assert_boolean
827
+ check_nothing_fails do
828
+ assert_boolean(true)
829
+ end
830
+ check_nothing_fails do
831
+ assert_boolean(false)
832
+ end
833
+
834
+ check_fails("<true> or <false> expected but was\n<1>") do
835
+ assert_boolean(1)
836
+ end
837
+
838
+ check_fails("<true> or <false> expected but was\n<nil>") do
839
+ assert_boolean(nil)
840
+ end
841
+
842
+ check_fails("message.\n<true> or <false> expected but was\n<\"XXX\">") do
843
+ assert_boolean("XXX", "message")
844
+ end
845
+ end
846
+
847
+ def test_assert_true
848
+ check_nothing_fails do
849
+ assert_true(true)
850
+ end
851
+
852
+ check_fails("<true> expected but was\n<false>") do
853
+ assert_true(false)
854
+ end
855
+
856
+ check_fails("<true> expected but was\n<1>") do
857
+ assert_true(1)
858
+ end
859
+
860
+ check_fails("message.\n<true> expected but was\n<nil>") do
861
+ assert_true(nil, "message")
862
+ end
863
+ end
864
+
865
+ def test_assert_false
866
+ check_nothing_fails do
867
+ assert_false(false)
868
+ end
869
+
870
+ check_fails("<false> expected but was\n<true>") do
871
+ assert_false(true)
872
+ end
873
+
874
+ check_fails("<false> expected but was\n<nil>") do
875
+ assert_false(nil)
876
+ end
877
+
878
+ check_fails("message.\n<false> expected but was\n<:false>") do
879
+ assert_false(:false, "message")
880
+ end
881
+ end
882
+
883
+ def test_assert_compare
884
+ check_nothing_fails do
885
+ assert_compare(1.4, "<", 10.0)
886
+ end
887
+
888
+ check_nothing_fails do
889
+ assert_compare(2, "<=", 2)
890
+ end
891
+
892
+ check_nothing_fails do
893
+ assert_compare(14, ">=", 10.0)
894
+ end
895
+
896
+ check_nothing_fails do
897
+ assert_compare(14, ">", 13.9)
898
+ end
899
+
900
+ expected_message = <<-EOM
901
+ <15> < <10> should be true
902
+ <15> expected less than
903
+ <10>.
904
+ EOM
905
+ check_fails(expected_message.chomp) do
906
+ assert_compare(15, "<", 10)
907
+ end
908
+
909
+ expected_message = <<-EOM
910
+ <15> <= <10> should be true
911
+ <15> expected less than or equal to
912
+ <10>.
913
+ EOM
914
+ check_fails(expected_message.chomp) do
915
+ assert_compare(15, "<=", 10)
916
+ end
917
+
918
+ expected_message = <<-EOM
919
+ <10> > <15> should be true
920
+ <10> expected greater than
921
+ <15>.
922
+ EOM
923
+ check_fails(expected_message.chomp) do
924
+ assert_compare(10, ">", 15)
925
+ end
926
+
927
+ expected_message = <<-EOM
928
+ <10> >= <15> should be true
929
+ <10> expected greater than or equal to
930
+ <15>.
931
+ EOM
932
+ check_fails(expected_message.chomp) do
933
+ assert_compare(10, ">=", 15)
934
+ end
935
+ end
936
+
937
+ def test_assert_fail_assertion
938
+ check_nothing_fails do
939
+ assert_fail_assertion do
940
+ flunk
941
+ end
942
+ end
943
+
944
+ check_fails("Failed assertion was expected.") do
945
+ assert_fail_assertion do
946
+ end
947
+ end
948
+ end
949
+
950
+ def test_assert_raise_message
951
+ check_nothing_fails do
952
+ assert_raise_message("Raise!") do
953
+ raise "Raise!"
954
+ end
955
+ end
956
+
957
+ check_nothing_fails do
958
+ assert_raise_message("Raise!") do
959
+ raise Exception, "Raise!"
960
+ end
961
+ end
962
+
963
+ check_nothing_fails do
964
+ assert_raise_message(/raise/i) do
965
+ raise "Raise!"
966
+ end
967
+ end
968
+
969
+ expected_message = <<-EOM
970
+ <"Expected message"> exception message expected but was
971
+ <"Actual message">.
972
+ EOM
973
+ check_fails(expected_message.chomp) do
974
+ assert_raise_message("Expected message") do
975
+ raise "Actual message"
976
+ end
977
+ end
978
+
979
+ expected_message = <<-EOM
980
+ <"Expected message"> exception message expected but none was thrown.
981
+ EOM
982
+ check_fails(expected_message.chomp) do
983
+ assert_raise_message("Expected message") do
984
+ end
985
+ end
986
+ end
987
+
988
+ def test_assert_raise_kind_of
989
+ check_nothing_fails(true) do
990
+ assert_raise_kind_of(SystemCallError) do
991
+ raise Errno::EACCES
992
+ end
993
+ end
994
+
995
+ expected_message = <<-EOM
996
+ <SystemCallError> family exception expected but was
997
+ Class: <RuntimeError>
998
+ Message: <"XXX">
999
+ ---Backtrace---
1000
+ EOM
1001
+ check_fails(/\A#{Regexp.escape(expected_message)}(?m).+\z/) do
1002
+ assert_raise_kind_of(SystemCallError) do
1003
+ raise RuntimeError, "XXX"
1004
+ end
1005
+ end
1006
+ end
1007
+
1008
+ def test_assert_const_defined
1009
+ check_nothing_fails do
1010
+ assert_const_defined(Test, :Unit)
1011
+ end
1012
+
1013
+ check_nothing_fails do
1014
+ assert_const_defined(Test, "Unit")
1015
+ end
1016
+
1017
+ check_fails("<Test>.const_defined?(<:Nonexistence>) expected.") do
1018
+ assert_const_defined(Test, :Nonexistence)
1019
+ end
1020
+ end
1021
+
1022
+ def test_assert_not_const_defined
1023
+ check_nothing_fails do
1024
+ assert_not_const_defined(Test, :Nonexistence)
1025
+ end
1026
+
1027
+ check_fails("!<Test>.const_defined?(<:Unit>) expected.") do
1028
+ assert_not_const_defined(Test, :Unit)
1029
+ end
1030
+
1031
+ check_fails("!<Test>.const_defined?(<\"Unit\">) expected.") do
1032
+ assert_not_const_defined(Test, "Unit")
1033
+ end
1034
+ end
1035
+
1036
+ def test_assert_predicate
1037
+ check_nothing_fails do
1038
+ assert_predicate([], :empty?)
1039
+ end
1040
+
1041
+ check_fails("<[1]>.empty? is true value expected but was\n<false>") do
1042
+ assert_predicate([1], :empty?)
1043
+ end
1044
+
1045
+ check_fails("<[1]>.respond_to?(:nonexistent?) expected\n" +
1046
+ "(Class: <Array>)") do
1047
+ assert_predicate([1], :nonexistent?)
1048
+ end
1049
+ end
1050
+
1051
+ def test_assert_not_predicate
1052
+ check_nothing_fails do
1053
+ assert_not_predicate([1], :empty?)
1054
+ end
1055
+
1056
+ check_fails("<[]>.empty? is false value expected but was\n<true>") do
1057
+ assert_not_predicate([], :empty?)
1058
+ end
1059
+
1060
+ check_fails("<[]>.respond_to?(:nonexistent?) expected\n" +
1061
+ "(Class: <Array>)") do
1062
+ assert_not_predicate([], :nonexistent?)
1063
+ end
1064
+ end
1065
+
1066
+ private
1067
+ def add_failure(message, location=caller)
1068
+ unless @catch_assertions
1069
+ super
1070
+ end
1071
+ end
1072
+
1073
+ def add_assertion
1074
+ if @catch_assertions
1075
+ @actual_assertion_count += 1
1076
+ else
1077
+ super
1078
+ end
1079
+ end
1080
+ end
1081
+ end
1082
+ end