aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorNicholas Seckar <nseckar@gmail.com>2007-01-24 20:47:32 +0000
committerNicholas Seckar <nseckar@gmail.com>2007-01-24 20:47:32 +0000
commit6ee09b6a0a548362e8602d56b7db7c03c65512fa (patch)
tree2704079cb4b4697a1073cab47a01cf53e8851f2a /activesupport
parentef1d0c1259337638983804ea0f97f25f2b3f02e4 (diff)
downloadrails-6ee09b6a0a548362e8602d56b7db7c03c65512fa.tar.gz
rails-6ee09b6a0a548362e8602d56b7db7c03c65512fa.tar.bz2
rails-6ee09b6a0a548362e8602d56b7db7c03c65512fa.zip
Increase test coverage for subclasses_of. Closes #7335.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6036 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/core_ext/object/extending.rb10
-rw-r--r--activesupport/test/core_ext/object_and_class_ext_test.rb39
3 files changed, 45 insertions, 6 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index 2219d1fd7e..63cf492fe5 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Increase test coverage for subclasses_of. Closes #7335. [Roman2K, Nicholas Seckar]
+
* Remove unused code from Duration#inspect. Closes #7180. [Rich Collins]
* Added test coverage for Inflector.inflections.clear. Closes #7179. [Rich Collins]
diff --git a/activesupport/lib/active_support/core_ext/object/extending.rb b/activesupport/lib/active_support/core_ext/object/extending.rb
index e15b4bf385..8a82f71c0f 100644
--- a/activesupport/lib/active_support/core_ext/object/extending.rb
+++ b/activesupport/lib/active_support/core_ext/object/extending.rb
@@ -6,11 +6,11 @@ class Object #:nodoc:
def subclasses_of(*superclasses)
subclasses = []
ObjectSpace.each_object(Class) do |k|
- next if # Exclude this class if
- (k.ancestors & superclasses).empty? || # It's not a subclass of our supers
- superclasses.include?(k) || # It *is* one of the supers
- eval("! defined?(::#{k})") || # It's not defined.
- eval("::#{k}").object_id != k.object_id
+ next unless # Exclude this class unless
+ superclasses.any? { |superclass| k < superclass } && # It *is* a subclass of our supers
+ eval("defined?(::#{k}) && ::#{k}.object_id == k.object_id") # It *is* defined
+ # Note that we check defined? in case we find a removed class that has
+ # yet to be garbage collected.
subclasses << k
end
subclasses
diff --git a/activesupport/test/core_ext/object_and_class_ext_test.rb b/activesupport/test/core_ext/object_and_class_ext_test.rb
index e48d87a198..1fbb853bc9 100644
--- a/activesupport/test/core_ext/object_and_class_ext_test.rb
+++ b/activesupport/test/core_ext/object_and_class_ext_test.rb
@@ -11,6 +11,16 @@ class ClassJ < ClassI; end
class ClassK
end
module Nested
+ class << self
+ def on_const_missing(&callback)
+ @on_const_missing = callback
+ end
+ private
+ def const_missing(mod_id)
+ @on_const_missing[mod_id] if @on_const_missing
+ super
+ end
+ end
class ClassL < ClassK
end
end
@@ -41,9 +51,12 @@ class ClassExtTest < Test::Unit::TestCase
end
def test_subclasses_of
+ cj = ClassJ
assert_equal [ClassJ], Object.subclasses_of(ClassI)
ClassI.remove_subclasses
assert_equal [], Object.subclasses_of(ClassI)
+ ensure
+ Object.const_set :ClassJ, cj
end
def test_subclasses_of_should_find_nested_classes
@@ -60,7 +73,31 @@ class ClassExtTest < Test::Unit::TestCase
subclasses = Object.subclasses_of(ClassK)
assert subclasses.include?(new_class)
assert ! subclasses.include?(old_class)
- end
+ ensure
+ Nested.const_set :ClassL, old_class unless defined?(Nested::ClassL)
+ end
+
+ def test_subclasses_of_should_not_trigger_const_missing
+ const_missing = false
+ Nested.on_const_missing { const_missing = true }
+
+ subclasses = Object.subclasses_of ClassK
+ assert !const_missing
+ assert_equal [ Nested::ClassL ], subclasses
+
+ removed = Nested.send :remove_const, :ClassL # keep it in memory
+ subclasses = Object.subclasses_of ClassK
+ assert !const_missing
+ assert subclasses.empty?
+ ensure
+ Nested.const_set :ClassL, removed unless defined?(Nested::ClassL)
+ end
+
+ def test_subclasses_of_with_multiple_roots
+ classes = Object.subclasses_of(ClassI, ClassK)
+ assert_equal %w(ClassJ Nested::ClassL), classes.collect(&:to_s).sort
+ end
+
end
class ObjectTests < Test::Unit::TestCase