activesambaldap 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. data/NEWS.en +20 -18
  2. data/NEWS.ja +23 -21
  3. data/README.en +122 -95
  4. data/README.ja +125 -98
  5. data/Rakefile +29 -64
  6. data/bin/asl-groupadd +1 -1
  7. data/bin/asl-groupadd.help +1 -0
  8. data/bin/asl-groupdel +1 -1
  9. data/bin/asl-groupdel.help +1 -0
  10. data/bin/asl-groupmod +1 -1
  11. data/bin/asl-groupmod.help +1 -0
  12. data/bin/asl-groupshow +1 -1
  13. data/bin/asl-groupshow.help +1 -0
  14. data/bin/asl-passwd +1 -1
  15. data/bin/asl-passwd.help +1 -0
  16. data/bin/asl-populate +2 -2
  17. data/bin/asl-populate.help +1 -0
  18. data/bin/asl-purge +1 -1
  19. data/bin/asl-purge.help +1 -0
  20. data/bin/asl-samba-computeradd +1 -1
  21. data/bin/asl-samba-computeradd.help +1 -0
  22. data/bin/asl-samba-groupadd +1 -1
  23. data/bin/asl-samba-groupadd.help +1 -0
  24. data/bin/asl-samba-groupdel +1 -1
  25. data/bin/asl-samba-groupdel.help +1 -0
  26. data/bin/asl-samba-groupmod +1 -1
  27. data/bin/asl-samba-groupmod.help +1 -0
  28. data/bin/asl-samba-useradd +1 -1
  29. data/bin/asl-samba-useradd.help +1 -0
  30. data/bin/asl-samba-userdel +1 -1
  31. data/bin/asl-samba-userdel.help +1 -0
  32. data/bin/asl-samba-usermod +1 -1
  33. data/bin/asl-samba-usermod.help +1 -0
  34. data/bin/asl-useradd +1 -1
  35. data/bin/asl-useradd.help +6 -4
  36. data/bin/asl-userdel +1 -1
  37. data/bin/asl-userdel.help +1 -0
  38. data/bin/asl-usermod +1 -1
  39. data/bin/asl-usermod.help +2 -0
  40. data/bin/asl-usershow +1 -1
  41. data/bin/asl-usershow.help +1 -0
  42. data/data/locale/ja/LC_MESSAGES/active-samba-ldap.mo +0 -0
  43. data/lib/active_samba_ldap/version.rb +1 -1
  44. data/lib/active_samba_ldap.rb +1 -1
  45. data/po/ja/active-samba-ldap.po +5 -5
  46. data/rails/{plugin/active_samba_ldap/README → README} +0 -0
  47. data/rails/{plugin/active_samba_ldap/init.rb → init.rb} +1 -1
  48. data/{rails/plugin/active_samba_ldap/generators → rails_generators}/scaffold_active_samba_ldap/scaffold_active_samba_ldap_generator.rb +0 -0
  49. data/{rails/plugin/active_samba_ldap/generators → rails_generators}/scaffold_active_samba_ldap/templates/computer.rb +0 -0
  50. data/{rails/plugin/active_samba_ldap/generators → rails_generators}/scaffold_active_samba_ldap/templates/dc.rb +0 -0
  51. data/{rails/plugin/active_samba_ldap/generators → rails_generators}/scaffold_active_samba_ldap/templates/group.rb +0 -0
  52. data/{rails/plugin/active_samba_ldap/generators → rails_generators}/scaffold_active_samba_ldap/templates/idmap.rb +0 -0
  53. data/{rails/plugin/active_samba_ldap/generators → rails_generators}/scaffold_active_samba_ldap/templates/ldap.yml +0 -0
  54. data/{rails/plugin/active_samba_ldap/generators → rails_generators}/scaffold_active_samba_ldap/templates/ou.rb +0 -0
  55. data/{rails/plugin/active_samba_ldap/generators → rails_generators}/scaffold_active_samba_ldap/templates/samba_controller.rb +0 -0
  56. data/{rails/plugin/active_samba_ldap/generators → rails_generators}/scaffold_active_samba_ldap/templates/samba_helper.rb +0 -0
  57. data/{rails/plugin/active_samba_ldap/generators → rails_generators}/scaffold_active_samba_ldap/templates/samba_index.rhtml +0 -0
  58. data/{rails/plugin/active_samba_ldap/generators → rails_generators}/scaffold_active_samba_ldap/templates/samba_populate.rhtml +0 -0
  59. data/{rails/plugin/active_samba_ldap/generators → rails_generators}/scaffold_active_samba_ldap/templates/samba_purge.rhtml +0 -0
  60. data/{rails/plugin/active_samba_ldap/generators → rails_generators}/scaffold_active_samba_ldap/templates/unix_id_pool.rb +0 -0
  61. data/{rails/plugin/active_samba_ldap/generators → rails_generators}/scaffold_active_samba_ldap/templates/user.rb +0 -0
  62. data/test/asl-test-utils.rb +2 -2
  63. data/test/test_asl_usershow.rb +2 -2
  64. data/test/test_password.rb +2 -2
  65. data/test/test_user_home_directory.rb +2 -2
  66. data/test-unit/History.txt +76 -1
  67. data/test-unit/README.txt +32 -12
  68. data/test-unit/Rakefile +19 -1
  69. data/test-unit/TODO +5 -0
  70. data/test-unit/lib/test/unit/assertions.rb +515 -88
  71. data/test-unit/lib/test/unit/autorunner.rb +93 -14
  72. data/test-unit/lib/test/unit/collector/load.rb +1 -1
  73. data/test-unit/lib/test/unit/collector.rb +1 -1
  74. data/test-unit/lib/test/unit/color-scheme.rb +86 -0
  75. data/test-unit/lib/test/unit/color.rb +40 -5
  76. data/test-unit/lib/test/unit/diff.rb +14 -0
  77. data/test-unit/lib/test/unit/fixture.rb +7 -16
  78. data/test-unit/lib/test/unit/notification.rb +9 -0
  79. data/test-unit/lib/test/unit/omission.rb +14 -0
  80. data/test-unit/lib/test/unit/pending.rb +16 -0
  81. data/test-unit/lib/test/unit/priority.rb +38 -3
  82. data/test-unit/lib/test/unit/runner/console.rb +8 -2
  83. data/test-unit/lib/test/unit/testcase.rb +263 -12
  84. data/test-unit/lib/test/unit/ui/console/testrunner.rb +58 -30
  85. data/test-unit/lib/test/unit/util/method-owner-finder.rb +28 -0
  86. data/test-unit/lib/test/unit/version.rb +1 -1
  87. data/test-unit/lib/test/unit.rb +62 -0
  88. data/test-unit/sample/test_user.rb +22 -0
  89. data/test-unit/test/collector/{test_descendant.rb → test-descendant.rb} +0 -0
  90. data/test-unit/test/collector/{test_load.rb → test-load.rb} +1 -1
  91. data/test-unit/test/{test_attribute.rb → test-attribute.rb} +0 -0
  92. data/test-unit/test/test-color-scheme.rb +56 -0
  93. data/test-unit/test/{test_color.rb → test-color.rb} +10 -0
  94. data/test-unit/test/{test_diff.rb → test-diff.rb} +0 -0
  95. data/test-unit/test/{test_emacs_runner.rb → test-emacs-runner.rb} +0 -0
  96. data/test-unit/test/test-fixture.rb +287 -0
  97. data/test-unit/test/{test_notification.rb → test-notification.rb} +4 -4
  98. data/test-unit/test/{test_omission.rb → test-omission.rb} +6 -6
  99. data/test-unit/test/{test_pending.rb → test-pending.rb} +12 -6
  100. data/test-unit/test/{test_priority.rb → test-priority.rb} +30 -0
  101. data/test-unit/test/{test_testcase.rb → test-testcase.rb} +100 -4
  102. data/test-unit/test/test_assertions.rb +462 -73
  103. data/test-unit/test/{testunit_test_util.rb → testunit-test-util.rb} +4 -2
  104. data/test-unit/test/ui/test_testrunmediator.rb +1 -1
  105. data/test-unit/test/util/test-method-owner-finder.rb +38 -0
  106. metadata +69 -79
  107. data/test-unit/test/test_fixture.rb +0 -275
@@ -1,9 +1,11 @@
1
1
  # Author:: Nathaniel Talbott.
2
2
  # Copyright:: Copyright (c) 2000-2003 Nathaniel Talbott. All rights reserved.
3
+ # Copyright (c) 2009 Kouhei Sutou.
3
4
  # License:: Ruby license.
4
5
 
5
6
  require 'test/unit/assertionfailederror'
6
7
  require 'test/unit/util/backtracefilter'
8
+ require 'test/unit/util/method-owner-finder'
7
9
  require 'test/unit/diff'
8
10
 
9
11
  module Test
@@ -77,21 +79,7 @@ module Test
77
79
 
78
80
  public
79
81
  def assert_equal(expected, actual, message=nil)
80
- diff = AssertionMessage.delayed_literal do
81
- if !expected.is_a?(String) or !actual.is_a?(String)
82
- expected = AssertionMessage.convert(expected)
83
- actual = AssertionMessage.convert(actual)
84
- end
85
- diff = Diff.readable(expected, actual)
86
- if /^[-+]/ !~ diff
87
- diff = ""
88
- elsif /^[ ?]/ =~ diff or /(?:.*\n){2,}/ =~ diff
89
- diff = "\n\ndiff:\n#{diff}"
90
- else
91
- diff = ""
92
- end
93
- diff
94
- end
82
+ diff = AssertionMessage.delayed_diff(expected, actual)
95
83
  full_message = build_message(message, <<EOT, expected, actual, diff)
96
84
  <?> expected but was
97
85
  <?>.?
@@ -99,53 +87,36 @@ EOT
99
87
  assert_block(full_message) { expected == actual }
100
88
  end
101
89
 
102
- private
103
- def _check_exception_class(args) # :nodoc:
104
- args.partition do |klass|
105
- next if klass.instance_of?(Module)
106
- assert(Exception >= klass, "Should expect a class of exception, #{klass}")
107
- true
108
- end
109
- end
110
-
111
- private
112
- def _expected_exception?(actual_exception, exceptions, modules) # :nodoc:
113
- exceptions.include?(actual_exception.class) or
114
- modules.any? {|mod| actual_exception.is_a?(mod)}
115
- end
116
-
117
90
  ##
118
- # Passes if the block raises one of the given exceptions.
91
+ # Passes if the block raises one of the expected
92
+ # exceptions. When an expected exception is an Exception
93
+ # object, passes if expected_exception == actual_exception.
119
94
  #
120
95
  # Example:
121
- # assert_raise RuntimeError, LoadError do
96
+ # assert_raise(RuntimeError, LoadError) do
122
97
  # raise 'Boom!!!'
123
- # end
124
-
98
+ # end # -> pass
99
+ #
100
+ # assert_raise do
101
+ # raise Exception, 'Any exception should be raised!!!'
102
+ # end # -> pass
103
+ #
104
+ # assert_raise(RuntimeError.new("XXX")) {raise "XXX"} # -> pass
105
+ # assert_raise(MyError.new("XXX")) {raise "XXX"} # -> fail
106
+ # assert_raise(RuntimeError.new("ZZZ")) {raise "XXX"} # -> fail
125
107
  public
126
- def assert_raise(*args)
127
- _wrap_assertion do
128
- if Module === args.last
129
- message = ""
130
- else
131
- message = args.pop
132
- end
133
- exceptions, modules = _check_exception_class(args)
134
- expected = args.size == 1 ? args.first : args
135
- actual_exception = nil
136
- full_message = build_message(message, "<?> exception expected but none was thrown.", expected)
108
+ def assert_raise(*args, &block)
109
+ assert_expected_exception = Proc.new do |*_args|
110
+ message, assert_exception_helper, actual_exception = _args
111
+ expected = assert_exception_helper.expected_exceptions
112
+ full_message = build_message(message,
113
+ "<?> exception expected but was\n?",
114
+ expected, actual_exception)
137
115
  assert_block(full_message) do
138
- begin
139
- yield
140
- rescue Exception => actual_exception
141
- break
142
- end
143
- false
116
+ expected == [] or assert_exception_helper.expected?(actual_exception)
144
117
  end
145
- full_message = build_message(message, "<?> exception expected but was\n?", expected, actual_exception)
146
- assert_block(full_message) {_expected_exception?(actual_exception, exceptions, modules)}
147
- actual_exception
148
118
  end
119
+ _assert_raise(assert_expected_exception, *args, &block)
149
120
  end
150
121
 
151
122
  ##
@@ -159,21 +130,67 @@ EOT
159
130
  end
160
131
 
161
132
  ##
162
- # Passes if +object+ .instance_of? +klass+
133
+ # Passes if the block raises one of the given
134
+ # exceptions or sub exceptions of the given exceptions.
163
135
  #
164
136
  # Example:
165
- # assert_instance_of String, 'foo'
137
+ # assert_raise_kind_of(SystemCallError) do
138
+ # raise Errno::EACCES
139
+ # end
140
+ def assert_raise_kind_of(*args, &block)
141
+ assert_expected_exception = Proc.new do |*_args|
142
+ message, assert_exception_helper, actual_exception = _args
143
+ expected = assert_exception_helper.expected_exceptions
144
+ full_message = build_message(message,
145
+ "<?> family exception expected " +
146
+ "but was\n?",
147
+ expected, actual_exception)
148
+ assert_block(full_message) do
149
+ assert_exception_helper.expected?(actual_exception, :kind_of?)
150
+ end
151
+ end
152
+ _assert_raise(assert_expected_exception, *args, &block)
153
+ end
154
+
155
+
156
+ ##
157
+ # Passes if +object+.instance_of?(+klass+). When +klass+ is
158
+ # an array of classes, it passes if any class
159
+ # satisfies +object.instance_of?(class).
160
+ #
161
+ # Example:
162
+ # assert_instance_of(String, 'foo') # -> pass
163
+ # assert_instance_of([Fixnum, NilClass], 100) # -> pass
164
+ # assert_instance_of([Numeric, NilClass], 100) # -> fail
166
165
 
167
166
  public
168
167
  def assert_instance_of(klass, object, message="")
169
168
  _wrap_assertion do
170
- assert_equal(Class, klass.class, "assert_instance_of takes a Class as its first argument")
171
- full_message = build_message(message, <<EOT, object, klass, object.class)
169
+ klasses = nil
170
+ klasses = klass if klass.is_a?(Array)
171
+ assert_block("The first parameter to assert_instance_of should be " +
172
+ "a Class or an Array of Class.") do
173
+ if klasses
174
+ klasses.all? {|k| k.is_a?(Class)}
175
+ else
176
+ klass.is_a?(Class)
177
+ end
178
+ end
179
+ klass_message = AssertionMessage.maybe_container(klass) do |value|
180
+ "<#{value}>"
181
+ end
182
+ full_message = build_message(message, <<EOT, object, klass_message, object.class)
172
183
  <?> expected to be an instance of
173
- <?> but was
184
+ ? but was
174
185
  <?>.
175
186
  EOT
176
- assert_block(full_message){object.instance_of?(klass)}
187
+ assert_block(full_message) do
188
+ if klasses
189
+ klasses.any? {|k| object.instance_of?(k)}
190
+ else
191
+ object.instance_of?(klass)
192
+ end
193
+ end
177
194
  end
178
195
  end
179
196
 
@@ -192,17 +209,45 @@ EOT
192
209
  end
193
210
 
194
211
  ##
195
- # Passes if +object+ .kind_of? +klass+
212
+ # Passes if +object+.kind_of?(+klass+). When +klass+ is
213
+ # an array of classes or modules, it passes if any
214
+ # class or module satisfies +object.kind_of?(class_or_module).
196
215
  #
197
216
  # Example:
198
- # assert_kind_of Object, 'foo'
217
+ # assert_kind_of(Object, 'foo') # -> pass
218
+ # assert_kind_of([Fixnum, NilClass], 100) # -> pass
219
+ # assert_kind_of([Fixnum, NilClass], "string") # -> fail
199
220
 
200
221
  public
201
222
  def assert_kind_of(klass, object, message="")
202
223
  _wrap_assertion do
203
- assert(klass.kind_of?(Module), "The first parameter to assert_kind_of should be a kind_of Module.")
204
- full_message = build_message(message, "<?>\nexpected to be kind_of\\?\n<?> but was\n<?>.", object, klass, object.class)
205
- assert_block(full_message){object.kind_of?(klass)}
224
+ klasses = nil
225
+ klasses = klass if klass.is_a?(Array)
226
+ assert_block("The first parameter to assert_kind_of should be " +
227
+ "a kind_of Module or an Array of a kind_of Module.") do
228
+ if klasses
229
+ klasses.all? {|k| k.kind_of?(Module)}
230
+ else
231
+ klass.kind_of?(Module)
232
+ end
233
+ end
234
+ klass_message = AssertionMessage.maybe_container(klass) do |value|
235
+ "<#{value}>"
236
+ end
237
+ full_message = build_message(message,
238
+ "<?> expected to be kind_of\\?\n" +
239
+ "? but was\n" +
240
+ "<?>.",
241
+ object,
242
+ klass_message,
243
+ object.class)
244
+ assert_block(full_message) do
245
+ if klasses
246
+ klasses.any? {|k| object.kind_of?(k)}
247
+ else
248
+ object.kind_of?(klass)
249
+ end
250
+ end
206
251
  end
207
252
  end
208
253
 
@@ -215,17 +260,18 @@ EOT
215
260
  public
216
261
  def assert_respond_to(object, method, message="")
217
262
  _wrap_assertion do
218
- full_message = build_message(nil, "<?>\ngiven as the method name argument to #assert_respond_to must be a Symbol or #respond_to\\?(:to_str).", method)
219
-
263
+ full_message = build_message(message,
264
+ "<?>.kind_of\\?(Symbol) or\n" +
265
+ "<?>.respond_to\\?(:to_str) expected",
266
+ method, method)
220
267
  assert_block(full_message) do
221
- method.kind_of?(Symbol) || method.respond_to?(:to_str)
268
+ method.kind_of?(Symbol) or method.respond_to?(:to_str)
222
269
  end
223
- full_message = build_message(message, <<EOT, object, object.class, method)
224
- <?>
225
- of type <?>
226
- expected to respond_to\\?<?>.
227
- EOT
228
- assert_block(full_message) { object.respond_to?(method) }
270
+ full_message = build_message(message,
271
+ "<?>.respond_to\\?(?) expected\n" +
272
+ "(Class: <?>)",
273
+ object, method, object.class)
274
+ assert_block(full_message) {object.respond_to?(method)}
229
275
  end
230
276
  end
231
277
 
@@ -301,18 +347,20 @@ EOT
301
347
  public
302
348
  def assert_nothing_raised(*args)
303
349
  _wrap_assertion do
304
- if Module === args.last
305
- message = ""
306
- else
350
+ if args.last.is_a?(String)
307
351
  message = args.pop
352
+ else
353
+ message = ""
308
354
  end
309
- exceptions, modules = _check_exception_class(args)
355
+
356
+ assert_exception_helper = AssertExceptionHelper.new(self, args)
310
357
  begin
311
358
  yield
312
359
  rescue Exception => e
313
360
  if ((args.empty? && !e.instance_of?(AssertionFailedError)) ||
314
- _expected_exception?(e, exceptions, modules))
315
- assert_block(build_message(message, "Exception raised:\n?", e)){false}
361
+ assert_exception_helper.expected?(e))
362
+ failure_message = build_message(message, "Exception raised:\n?", e)
363
+ assert_block(failure_message) {false}
316
364
  else
317
365
  raise
318
366
  end
@@ -398,12 +446,12 @@ EOT
398
446
  # Passes if the block throws +expected_object+
399
447
  #
400
448
  # Example:
401
- # assert_throws :done do
402
- # throw :done
449
+ # assert_throw(:done) do
450
+ # throw(:done)
403
451
  # end
404
452
 
405
453
  public
406
- def assert_throws(expected_object, message="", &proc)
454
+ def assert_throw(expected_object, message="", &proc)
407
455
  _wrap_assertion do
408
456
  begin
409
457
  catch([]) {}
@@ -411,7 +459,7 @@ EOT
411
459
  assert_instance_of(Symbol, expected_object,
412
460
  "assert_throws expects the symbol that should be thrown for its first argument")
413
461
  end
414
- assert_block("Should have passed a block to assert_throws.") do
462
+ assert_block("Should have passed a block to assert_throw.") do
415
463
  block_given?
416
464
  end
417
465
  caught = true
@@ -437,6 +485,14 @@ EOT
437
485
  end
438
486
  end
439
487
 
488
+ ##
489
+ # Alias of assert_throw.
490
+ #
491
+ # Will be deprecated in 1.9, and removed in 2.0.
492
+ def assert_throws(*args, &block)
493
+ assert_throw(*args, &block)
494
+ end
495
+
440
496
  ##
441
497
  # Passes if block does not throw anything.
442
498
  #
@@ -559,6 +615,180 @@ EOT
559
615
  end
560
616
  end
561
617
 
618
+ ##
619
+ # Passes if expression "+expected+ +operator+
620
+ # +actual+" is true.
621
+ #
622
+ # Example:
623
+ # assert_compare(1, "<", 10) # -> pass
624
+ # assert_compare(1, ">=", 10) # -> fail
625
+ def assert_compare(expected, operator, actual, message=nil)
626
+ _wrap_assertion do
627
+ assert_send([["<", "<=", ">", ">="], :include?, operator.to_s])
628
+ case operator.to_s
629
+ when "<"
630
+ operator_description = "less than"
631
+ when "<="
632
+ operator_description = "less than or equal to"
633
+ when ">"
634
+ operator_description = "greater than"
635
+ when ">="
636
+ operator_description = "greater than or equal to"
637
+ end
638
+ template = <<-EOT
639
+ <?> #{operator} <?> should be true
640
+ <?> expected #{operator_description}
641
+ <?>.
642
+ EOT
643
+ full_message = build_message(message, template,
644
+ expected, actual,
645
+ expected, actual)
646
+ assert_block(full_message) do
647
+ expected.send(operator, actual)
648
+ end
649
+ end
650
+ end
651
+
652
+ ##
653
+ # Passes if assertion is failed in block.
654
+ #
655
+ # Example:
656
+ # assert_fail_assertion {assert_equal("A", "B")} # -> pass
657
+ # assert_fail_assertion {assert_equal("A", "A")} # -> fail
658
+ def assert_fail_assertion(message=nil)
659
+ _wrap_assertion do
660
+ full_message = build_message(message,
661
+ "Failed assertion was expected.")
662
+ assert_block(full_message) do
663
+ begin
664
+ yield
665
+ false
666
+ rescue AssertionFailedError
667
+ true
668
+ end
669
+ end
670
+ end
671
+ end
672
+
673
+ ##
674
+ # Passes if an exception is raised in block and its
675
+ # message is +expected+.
676
+ #
677
+ # Example:
678
+ # assert_raise_message("exception") {raise "exception"} # -> pass
679
+ # assert_raise_message(/exc/i) {raise "exception"} # -> pass
680
+ # assert_raise_message("exception") {raise "EXCEPTION"} # -> fail
681
+ # assert_raise_message("exception") {} # -> fail
682
+ def assert_raise_message(expected, message=nil)
683
+ _wrap_assertion do
684
+ full_message = build_message(message,
685
+ "<?> exception message expected " +
686
+ "but none was thrown.",
687
+ expected)
688
+ exception = nil
689
+ assert_block(full_message) do
690
+ begin
691
+ yield
692
+ false
693
+ rescue Exception => exception
694
+ true
695
+ end
696
+ end
697
+
698
+ actual = exception.message
699
+ diff = AssertionMessage.delayed_diff(expected, actual)
700
+ full_message =
701
+ build_message(message,
702
+ "<?> exception message expected but was\n" +
703
+ "<?>.?", expected, actual, diff)
704
+ assert_block(full_message) do
705
+ if expected.is_a?(Regexp)
706
+ expected =~ actual
707
+ else
708
+ expected == actual
709
+ end
710
+ end
711
+ end
712
+ end
713
+
714
+ ##
715
+ # Passes if +object+.const_defined?(+constant_name+)
716
+ #
717
+ # Example:
718
+ # assert_const_defined(Test, :Unit) # -> pass
719
+ # assert_const_defined(Object, :Nonexistent) # -> fail
720
+ def assert_const_defined(object, constant_name, message=nil)
721
+ _wrap_assertion do
722
+ full_message = build_message(message,
723
+ "<?>.const_defined\\?(<?>) expected.",
724
+ object, constant_name)
725
+ assert_block(full_message) do
726
+ object.const_defined?(constant_name)
727
+ end
728
+ end
729
+ end
730
+
731
+ ##
732
+ # Passes if !+object+.const_defined?(+constant_name+)
733
+ #
734
+ # Example:
735
+ # assert_not_const_defined(Object, :Nonexistent) # -> pass
736
+ # assert_not_const_defined(Test, :Unit) # -> fail
737
+ def assert_not_const_defined(object, constant_name, message=nil)
738
+ _wrap_assertion do
739
+ full_message = build_message(message,
740
+ "!<?>.const_defined\\?(<?>) expected.",
741
+ object, constant_name)
742
+ assert_block(full_message) do
743
+ !object.const_defined?(constant_name)
744
+ end
745
+ end
746
+ end
747
+
748
+ ##
749
+ # Passes if +object+.+predicate+
750
+ #
751
+ # Example:
752
+ # assert_predicate([], :empty?) # -> pass
753
+ # assert_predicate([1], :empty?) # -> fail
754
+ def assert_predicate(object, predicate, message=nil)
755
+ _wrap_assertion do
756
+ assert_respond_to(object, predicate, message)
757
+ actual = object.send(predicate)
758
+ full_message = build_message(message,
759
+ "<?>.? is true value expected but was\n" +
760
+ "<?>",
761
+ object,
762
+ AssertionMessage.literal(predicate),
763
+ actual)
764
+ assert_block(full_message) do
765
+ actual
766
+ end
767
+ end
768
+ end
769
+
770
+ ##
771
+ # Passes if +object+.+predicate+
772
+ #
773
+ # Example:
774
+ # assert_not_predicate([1], :empty?) # -> pass
775
+ # assert_not_predicate([], :empty?) # -> fail
776
+ def assert_not_predicate(object, predicate, message=nil)
777
+ _wrap_assertion do
778
+ assert_respond_to(object, predicate, message)
779
+ actual = object.send(predicate)
780
+ full_message = build_message(message,
781
+ "<?>.? is false value expected but was\n" +
782
+ "<?>",
783
+ object,
784
+ AssertionMessage.literal(predicate),
785
+ actual)
786
+ assert_block(full_message) do
787
+ not actual
788
+ end
789
+ end
790
+ end
791
+
562
792
  ##
563
793
  # Builds a failure message. +head+ is added before the +template+ and
564
794
  # +arguments+ replaces the '?'s positionally in the template.
@@ -601,8 +831,37 @@ EOT
601
831
  def self.use_pp=(value)
602
832
  AssertionMessage.use_pp = value
603
833
  end
604
-
834
+
605
835
  # :stopdoc:
836
+ private
837
+ def _assert_raise(assert_expected_exception, *args, &block)
838
+ _wrap_assertion do
839
+ if args.last.is_a?(String)
840
+ message = args.pop
841
+ else
842
+ message = ""
843
+ end
844
+
845
+ assert_exception_helper = AssertExceptionHelper.new(self, args)
846
+ expected = assert_exception_helper.expected_exceptions
847
+ actual_exception = nil
848
+ full_message = build_message(message,
849
+ "<?> exception expected " +
850
+ "but none was thrown.",
851
+ expected)
852
+ assert_block(full_message) do
853
+ begin
854
+ yield
855
+ false
856
+ rescue Exception => actual_exception
857
+ true
858
+ end
859
+ end
860
+ assert_expected_exception.call(message, assert_exception_helper,
861
+ actual_exception)
862
+ actual_exception
863
+ end
864
+ end
606
865
 
607
866
  class AssertionMessage
608
867
  @use_pp = true
@@ -617,6 +876,46 @@ EOT
617
876
  DelayedLiteral.new(block)
618
877
  end
619
878
 
879
+ def maybe_container(value, &formatter)
880
+ MaybeContainer.new(value, &formatter)
881
+ end
882
+
883
+ MAX_DIFF_TARGET_STRING_SIZE = 300
884
+ def diff_target_string?(string)
885
+ if string.respond_to?(:bytesize)
886
+ string.bytesize < MAX_DIFF_TARGET_STRING_SIZE
887
+ else
888
+ string.size < MAX_DIFF_TARGET_STRING_SIZE
889
+ end
890
+ end
891
+
892
+ def delayed_diff(from, to)
893
+ delayed_literal do
894
+ if !from.is_a?(String) or !to.is_a?(String)
895
+ from = convert(from)
896
+ to = convert(to)
897
+ end
898
+
899
+ diff = nil
900
+ diff = "" if !diff_target_string?(from) or !diff_target_string?(to)
901
+ diff ||= Diff.readable(from, to)
902
+ if /^[-+]/ !~ diff
903
+ diff = ""
904
+ elsif /^[ ?]/ =~ diff or /(?:.*\n){2,}/ =~ diff
905
+ diff = "\n\ndiff:\n#{diff}"
906
+ else
907
+ diff = ""
908
+ end
909
+
910
+ if Diff.need_fold?(diff)
911
+ folded_diff = Diff.folded_readable(from, to)
912
+ diff << "\n\nfolded diff:\n#{folded_diff}"
913
+ end
914
+
915
+ diff
916
+ end
917
+ end
918
+
620
919
  def convert(object)
621
920
  case object
622
921
  when Exception
@@ -645,7 +944,7 @@ EOM
645
944
  def initialize(value)
646
945
  @value = value
647
946
  end
648
-
947
+
649
948
  def inspect
650
949
  @value.to_s
651
950
  end
@@ -655,12 +954,30 @@ EOM
655
954
  def initialize(value)
656
955
  @value = value
657
956
  end
658
-
957
+
659
958
  def inspect
660
959
  @value.call.to_s
661
960
  end
662
961
  end
663
962
 
963
+ class MaybeContainer
964
+ def initialize(value, &formatter)
965
+ @value = value
966
+ @formatter = formatter
967
+ end
968
+
969
+ def inspect
970
+ if @value.is_a?(Array)
971
+ values = @value.collect do |value|
972
+ @formatter.call(AssertionMessage.convert(value))
973
+ end
974
+ "[#{values.join(', ')}]"
975
+ else
976
+ @formatter.call(AssertionMessage.convert(@value))
977
+ end
978
+ end
979
+ end
980
+
664
981
  class Template
665
982
  def self.create(string)
666
983
  parts = (string ? string.scan(/(?=[^\\])\?|(?:\\\?|[^\?])+/m) : [])
@@ -715,8 +1032,118 @@ EOM
715
1032
  end
716
1033
  end
717
1034
 
718
- # :startdoc:
1035
+ class AssertExceptionHelper
1036
+ class WrappedException
1037
+ def initialize(exception)
1038
+ @exception = exception
1039
+ end
1040
+
1041
+ def inspect
1042
+ if default_inspect?
1043
+ "#{@exception.class.inspect}(#{@exception.message.inspect})"
1044
+ else
1045
+ @exception.inspect
1046
+ end
1047
+ end
1048
+
1049
+ def method_missing(name, *args, &block)
1050
+ @exception.send(name, *args, &block)
1051
+ end
1052
+
1053
+ private
1054
+ def default_inspect?
1055
+ inspect_method = @exception.method(:inspect)
1056
+ if inspect_method.respond_to?(:owner) and
1057
+ inspect_method.owner == Exception
1058
+ true
1059
+ else
1060
+ default_inspect_method = Exception.instance_method(:inspect)
1061
+ default_inspect_method.bind(@exception).call == @exception.inspect
1062
+ end
1063
+ end
1064
+ end
1065
+
1066
+ def initialize(test_case, expected_exceptions)
1067
+ @test_case = test_case
1068
+ @expected_exceptions = expected_exceptions
1069
+ @expected_classes, @expected_modules, @expected_objects =
1070
+ split_expected_exceptions(expected_exceptions)
1071
+ end
1072
+
1073
+ def expected_exceptions
1074
+ exceptions = @expected_exceptions.collect do |exception|
1075
+ if exception.is_a?(Exception)
1076
+ WrappedException.new(exception)
1077
+ else
1078
+ exception
1079
+ end
1080
+ end
1081
+ if exceptions.size == 1
1082
+ exceptions[0]
1083
+ else
1084
+ exceptions
1085
+ end
1086
+ end
1087
+
1088
+ def expected?(actual_exception, equality=nil)
1089
+ equality ||= :instance_of?
1090
+ expected_class?(actual_exception, equality) or
1091
+ expected_module?(actual_exception) or
1092
+ expected_object?(actual_exception)
1093
+ end
719
1094
 
1095
+ private
1096
+ def split_expected_exceptions(expected_exceptions)
1097
+ exception_modules = []
1098
+ exception_objects = []
1099
+ exception_classes = []
1100
+ expected_exceptions.each do |exception_type|
1101
+ if exception_type.instance_of?(Module)
1102
+ exception_modules << exception_type
1103
+ elsif exception_type.is_a?(Exception)
1104
+ exception_objects << exception_type
1105
+ else
1106
+ @test_case.send(:assert,
1107
+ Exception >= exception_type,
1108
+ "Should expect a class of exception, " +
1109
+ "#{exception_type}")
1110
+ exception_classes << exception_type
1111
+ end
1112
+ end
1113
+ [exception_classes, exception_modules, exception_objects]
1114
+ end
1115
+
1116
+ def expected_class?(actual_exception, equality)
1117
+ @expected_classes.any? do |expected_class|
1118
+ actual_exception.send(equality, expected_class)
1119
+ end
1120
+ end
1121
+
1122
+ def expected_module?(actual_exception)
1123
+ @expected_modules.any? do |expected_module|
1124
+ actual_exception.is_a?(expected_module)
1125
+ end
1126
+ end
1127
+
1128
+ def expected_object?(actual_exception)
1129
+ @expected_objects.any? do |expected_object|
1130
+ expected_object == actual_exception or
1131
+ fallback_exception_object_equal(expected_object, actual_exception)
1132
+ end
1133
+ end
1134
+
1135
+ def fallback_exception_object_equal(expected_object, actual_exception)
1136
+ owner = Util::MethodOwnerFinder.find(expected_object, :==)
1137
+ if owner == Kernel or owner == Exception
1138
+ expected_object.class == actual_exception.class and
1139
+ expected_object.message == actual_exception.message
1140
+ else
1141
+ false
1142
+ end
1143
+ end
1144
+ end
1145
+
1146
+ # :startdoc:
720
1147
  end
721
1148
  end
722
1149
  end