Skip to content

Commit

Permalink
Add Class#descendants
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
3 people committed Oct 28, 2021
1 parent 28c50ea commit dbff2dc
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions core/class/descendants_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit dbff2dc

Please sign in to comment.