diff options
author | Jean Boussier <jean.boussier@gmail.com> | 2021-10-28 14:07:11 +0200 |
---|---|---|
committer | Jean Boussier <jean.boussier@gmail.com> | 2021-11-23 10:50:44 +0100 |
commit | c0c2b31a35e19a47b499b57807bc0a0f9325f6d3 (patch) | |
tree | ab7ee0710597903b43a8d086a3805059693de928 /test | |
parent | a88b19d3d08447eeb7045621f02a844173d64203 (diff) |
Add Class#subclasses
Implements [Feature #18273]
Returns an array containing the receiver's direct subclasses without
singleton classes.
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/5045
Diffstat (limited to 'test')
-rw-r--r-- | test/ruby/test_class.rb | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/test/ruby/test_class.rb b/test/ruby/test_class.rb index eb448e952a..0ceb36bb1d 100644 --- a/test/ruby/test_class.rb +++ b/test/ruby/test_class.rb @@ -770,6 +770,44 @@ class TestClass < Test::Unit::TestCase end end + def test_subclasses + c = Class.new + sc = Class.new(c) + ssc = Class.new(sc) + [c, sc, ssc].each do |k| + k.include Module.new + k.new.define_singleton_method(:force_singleton_class){} + end + assert_equal([sc], c.subclasses) + assert_equal([ssc], sc.subclasses) + assert_equal([], ssc.subclasses) + + object_subclasses = Object.subclasses + assert_include(object_subclasses, c) + assert_not_include(object_subclasses, sc) + assert_not_include(object_subclasses, ssc) + object_subclasses.each do |subclass| + assert_equal Object, subclass.superclass, "Expected #{subclass}.superclass to be Object" + end + end + + def test_subclass_gc + c = Class.new + 100000.times do + cc = Class.new(c) + 100.times { Class.new(cc) } + end + assert(c.subclasses.size <= 100000) + end + + def test_subclass_gc_stress + 10000.times do + c = Class.new + 100.times { Class.new(c) } + assert(c.subclasses.size <= 100) + end + end + def test_classext_memory_leak assert_no_memory_leak([], <<-PREP, <<-CODE, rss: true) code = proc { Class.new } |