rconditions 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -114,7 +114,7 @@ If you use another ruby version, please check whether the tests pass.
114
114
  * RConditions only processes predicate methods starting with a letter and
115
115
  containing only word characters except the question mark at the end.
116
116
  * <tt>Kernel#block_given?</tt> is not processed.
117
- * Singleton methods are not processed. These includes all class methods.
117
+ * Singleton methods on classes and modules are not processed.
118
118
 
119
119
  == License
120
120
 
data/lib/rconditions.rb CHANGED
@@ -6,7 +6,8 @@ module RConditions
6
6
  # Extends a given predicate method and defines a bang method for it. You
7
7
  # should never call this method yourself, it is called either from
8
8
  # RConditions#extend_predicate_methods_and_define_bang_methods, or from
9
- # <tt>Module#method_added</tt> which is extended by RConditions.
9
+ # <tt>Module#method_added</tt> or <tt>Object#singleton_method_added</tt>
10
+ # which are extended by RConditions.
10
11
  # * <tt>mod</tt> -- the module on which the method is defined.
11
12
  # * <tt>id</tt> -- the name of the method.
12
13
  def extend_predicate_method_and_define_bang_method(mod, id)
@@ -58,9 +59,9 @@ module RConditions
58
59
  predicate_method_without_questionmark = predicate_method[0...-1]
59
60
 
60
61
  positive_module = camelize("#{predicate_method_without_questionmark}_error")
61
- mod.const_set(positive_module, Module.new) unless mod.const_defined?(positive_module)
62
+ mod.const_set(positive_module, Module.new) unless mod.constants.include?(positive_module)
62
63
  negative_module = camelize("not_#{predicate_method_without_questionmark}_error")
63
- mod.const_set(negative_module, Module.new) unless mod.const_defined?(negative_module)
64
+ mod.const_set(negative_module, Module.new) unless mod.constants.include?(negative_module)
64
65
 
65
66
  time = Time.now
66
67
  predicate_method_without_rconditions = "#{predicate_method_without_questionmark}_rconditions_#{mod.object_id.abs}_#{time.to_i}_#{time.usec}"
@@ -134,3 +135,12 @@ class Module #:nodoc:
134
135
  method_added_without_rconditions(id)
135
136
  end
136
137
  end
138
+
139
+ class Object #:nodoc:
140
+ alias_method :singleton_method_added_without_rconditions, :singleton_method_added
141
+ private :singleton_method_added_without_rconditions
142
+ def singleton_method_added(id)
143
+ ::RConditions.extend_predicate_method_and_define_bang_method(class<<self; self; end, id) unless self.class <= Module
144
+ singleton_method_added_without_rconditions(id)
145
+ end
146
+ end
@@ -239,14 +239,6 @@ class RConditionsTest < Test::Unit::TestCase
239
239
  assert e.kind_of?(NowANamedModule::NotActivatedError)
240
240
  end
241
241
 
242
- def test_does_not_work_for_singleton_methods
243
- f = Foo.new
244
- class << f
245
- def x?; b?; end
246
- end
247
- assert !f.respond_to?(:x!)
248
- end
249
-
250
242
  def test_does_not_break_module_functions
251
243
  eval <<-EOS
252
244
  module WithModuleFunctions
@@ -267,6 +259,15 @@ class RConditionsTest < Test::Unit::TestCase
267
259
  assert_equal 'module_function_2?', WithModuleFunctions.module_function_2?
268
260
  end
269
261
 
262
+ def test_does_not_support_class_methods
263
+ eval <<-EOS
264
+ class Foo
265
+ def self.moppel?;end
266
+ end
267
+ EOS
268
+ assert !Foo.respond_to?(:moppel!)
269
+ end
270
+
270
271
  def test_module_functions_fix_ignores_no_method_error_from_non_module_function
271
272
  eval <<-EOS
272
273
  module ModuleWithNoMethodError
@@ -316,6 +317,42 @@ class RConditionsTest < Test::Unit::TestCase
316
317
  assert Foo.private_instance_methods(false).map_to_s.include?('private_p!')
317
318
  end
318
319
 
320
+ def test_exception_module_is_reused_if_defined_in_superclass
321
+ eval <<-EOS
322
+ class SuperClass
323
+ def x?
324
+ true
325
+ end
326
+ end
327
+ class SubClass < SuperClass
328
+ def x?
329
+ end
330
+ end
331
+ EOS
332
+ assert SuperClass::NotXError == SubClass::NotXError
333
+ assert SuperClass::XError == SubClass::XError
334
+ end
335
+
336
+ def test_does_work_for_singleton_methods
337
+ f = Foo.new
338
+ class << f
339
+ def x?; b?; end
340
+ end
341
+ assert f.respond_to?(:x!)
342
+ e = exception_raised_by { f.x! }
343
+ assert e.kind_of?((class<<f; self; end).const_get(:NotXError))
344
+ assert e.kind_of?(Foo::NotBError)
345
+ end
346
+
347
+ def test_does_work_for_overwritten_singleton_methods
348
+ f = Foo.new
349
+ class << f
350
+ def a?; false; end
351
+ end
352
+ e = exception_raised_by { f.a! }
353
+ assert e.kind_of?(Foo::NotAError)
354
+ end
355
+
319
356
  private
320
357
 
321
358
  def exception_raised_by(&block)
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.2
2
+ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: rconditions
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.3.1
7
- date: 2007-11-12 00:00:00 +01:00
6
+ version: 0.4.0
7
+ date: 2007-11-13 00:00:00 +01:00
8
8
  summary: Generates a bang method for each predicate method.
9
9
  require_paths:
10
10
  - lib