From 6abf7938bface30fe562eb612bfb0ed66c6ff8d7 Mon Sep 17 00:00:00 2001 From: ko1 Date: Thu, 28 May 2015 19:40:04 +0000 Subject: * ext/objspace/objspace.c: add two methods to debug internals. * ObjectSpace.internal_class_of: return RBASIC_CLASS(obj). * ObjectSpace.internal_super_of: return RCLASS_SUPER(cls). * NEWS: add information about both methods. * test/objspace/test_objspace.rb: add tests for both methods. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50662 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/objspace/test_objspace.rb | 51 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'test/objspace') diff --git a/test/objspace/test_objspace.rb b/test/objspace/test_objspace.rb index 329e418008..8845b83c4e 100644 --- a/test/objspace/test_objspace.rb +++ b/test/objspace/test_objspace.rb @@ -285,4 +285,55 @@ class TestObjSpace < Test::Unit::TestCase assert_not_match /"fd":/, output end end + + def traverse_classes klass + h = {} + while klass && !h.has_key?(klass) + h[klass] = true + klass = ObjectSpace.internal_class_of(klass) + end + end + + def test_internal_class_of + i = 0 + ObjectSpace.each_object{|o| + traverse_classes ObjectSpace.internal_class_of(o) + i += 1 + } + assert_operator i, :>, 0 + end + + def traverse_super_classes klass + while klass + klass = ObjectSpace.internal_super_of(klass) + end + end + + def all_super_classes klass + klasses = [] + while klass + klasses << klass + klass = ObjectSpace.internal_super_of(klass) + end + klasses + end + + def test_internal_super_of + klasses = all_super_classes(String) + String.ancestors.each{|k| + case k + when Class + assert_equal(true, klasses.include?(k), k.inspect) + when Module + assert_equal(false, klasses.include?(k), k.inspect) # Internal object (T_ICLASS) + end + } + + i = 0 + ObjectSpace.each_object(Module){|o| + traverse_super_classes ObjectSpace.internal_super_of(o) + i += 1 + } + assert_operator i, :>, 0 + end end -- cgit v1.2.3