summaryrefslogtreecommitdiff
path: root/spec/ruby/core
diff options
context:
space:
mode:
authorJeremy Evans <code@jeremyevans.net>2021-10-26 10:35:21 -0900
committerGitHub <noreply@github.com>2021-10-26 12:35:21 -0700
commit717ab0bb2ee63dfe76076e0c9f91fbac3a0de4fd (patch)
treed2b531647657dd58e6ce6e64e3f6fa0adaa75f40 /spec/ruby/core
parenta4d5ee4f31bf3ff36c1a8c8fe3cda16aa1016b12 (diff)
Add Class#descendants
Doesn't include receiver or singleton classes. Implements [Feature #14394] Co-authored-by: fatkodima <fatkodima123@gmail.com> Co-authored-by: Benoit Daloze <eregontp@gmail.com>
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/4974 Merged-By: jeremyevans <code@jeremyevans.net>
Diffstat (limited to 'spec/ruby/core')
-rw-r--r--spec/ruby/core/class/descendants_spec.rb38
1 files changed, 38 insertions, 0 deletions
diff --git a/spec/ruby/core/class/descendants_spec.rb b/spec/ruby/core/class/descendants_spec.rb
new file mode 100644
index 0000000000..f87cd68be8
--- /dev/null
+++ b/spec/ruby/core/class/descendants_spec.rb
@@ -0,0 +1,38 @@
+require_relative '../../spec_helper'
+require_relative '../module/fixtures/classes'
+
+ruby_version_is '3.1' do
+ describe "Class#descendants" do
+ it "returns a list of classes descended from self (excluding self)" do
+ assert_descendants(ModuleSpecs::Parent, [ModuleSpecs::Child, ModuleSpecs::Child2, ModuleSpecs::Grandchild])
+ end
+
+ it "does not return included modules" do
+ parent = Class.new
+ child = Class.new(parent)
+ mod = Module.new
+ parent.include(mod)
+
+ assert_descendants(parent, [child])
+ end
+
+ it "does not return singleton classes" do
+ a = Class.new
+
+ a_obj = a.new
+ def a_obj.force_singleton_class
+ 42
+ end
+
+ a.descendants.should_not include(a_obj.singleton_class)
+ end
+
+ it "has 1 entry per module or class" do
+ ModuleSpecs::Parent.descendants.should == ModuleSpecs::Parent.descendants.uniq
+ end
+
+ def assert_descendants(mod, descendants)
+ mod.descendants.sort_by(&:inspect).should == descendants.sort_by(&:inspect)
+ end
+ end
+end