2017-07-09 12:06:36 +00:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 13:39:13 +00:00
|
|
|
|
2016-08-06 16:03:25 +00:00
|
|
|
require "set"
|
2012-06-30 19:51:57 +00:00
|
|
|
|
2011-03-13 15:52:34 +00:00
|
|
|
module DescendantsTrackerTestCases
|
2010-06-19 14:44:35 +00:00
|
|
|
class Parent
|
|
|
|
extend ActiveSupport::DescendantsTracker
|
|
|
|
end
|
|
|
|
|
|
|
|
class Child1 < Parent
|
|
|
|
end
|
|
|
|
|
|
|
|
class Child2 < Parent
|
|
|
|
end
|
|
|
|
|
|
|
|
class Grandchild1 < Child1
|
|
|
|
end
|
|
|
|
|
|
|
|
class Grandchild2 < Child1
|
|
|
|
end
|
|
|
|
|
2010-06-19 14:58:12 +00:00
|
|
|
ALL = [Parent, Child1, Child2, Grandchild1, Grandchild2]
|
|
|
|
|
2010-06-19 14:44:35 +00:00
|
|
|
def test_descendants
|
2012-06-30 19:51:57 +00:00
|
|
|
assert_equal_sets [Child1, Grandchild1, Grandchild2, Child2], Parent.descendants
|
|
|
|
assert_equal_sets [Grandchild1, Grandchild2], Child1.descendants
|
|
|
|
assert_equal_sets [], Child2.descendants
|
2010-06-19 14:44:35 +00:00
|
|
|
end
|
|
|
|
|
2017-12-14 10:37:51 +00:00
|
|
|
def test_descendants_with_garbage_collected_classes
|
2021-07-22 18:45:57 +00:00
|
|
|
# The Ruby GC (and most other GCs for that matter) are not fully precise.
|
|
|
|
# When GC is run, the whole stack is scanned to mark any object reference
|
|
|
|
# in registers. But some of these references might simply be leftovers from
|
|
|
|
# previous method calls waiting to be overridden, and there's no definite
|
|
|
|
# way to clear them. By executing this code in a distinct thread, we ensure
|
|
|
|
# that such references are on a stack that will be entirely garbage
|
|
|
|
# collected, effectively working around the problem.
|
|
|
|
Thread.new do
|
2017-12-14 10:37:51 +00:00
|
|
|
child_klass = Class.new(Parent)
|
|
|
|
assert_equal_sets [Child1, Grandchild1, Grandchild2, Child2, child_klass], Parent.descendants
|
2021-07-22 18:45:57 +00:00
|
|
|
end.join
|
|
|
|
|
|
|
|
# Calling `GC.start` 4 times should trigger a full GC run
|
|
|
|
4.times do
|
|
|
|
GC.start
|
2017-12-14 10:37:51 +00:00
|
|
|
end
|
2021-07-22 18:45:57 +00:00
|
|
|
|
2017-12-14 10:37:51 +00:00
|
|
|
assert_equal_sets [Child1, Grandchild1, Grandchild2, Child2], Parent.descendants
|
|
|
|
end
|
|
|
|
|
2010-06-19 14:44:35 +00:00
|
|
|
def test_direct_descendants
|
2012-06-30 19:51:57 +00:00
|
|
|
assert_equal_sets [Child1, Child2], Parent.direct_descendants
|
|
|
|
assert_equal_sets [Grandchild1, Grandchild2], Child1.direct_descendants
|
|
|
|
assert_equal_sets [], Child2.direct_descendants
|
2010-06-19 14:44:35 +00:00
|
|
|
end
|
|
|
|
|
2020-06-02 00:30:50 +00:00
|
|
|
def test_subclasses
|
|
|
|
[Parent, Child1, Child2].each do |klass|
|
|
|
|
assert_equal klass.direct_descendants, klass.subclasses
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-03-13 15:52:34 +00:00
|
|
|
def test_clear
|
2010-06-20 21:29:37 +00:00
|
|
|
mark_as_autoloaded(*ALL) do
|
2010-06-19 14:44:35 +00:00
|
|
|
ActiveSupport::DescendantsTracker.clear
|
2010-07-05 10:50:08 +00:00
|
|
|
ALL.each do |k|
|
2018-01-25 23:16:57 +00:00
|
|
|
assert_empty ActiveSupport::DescendantsTracker.descendants(k)
|
2010-07-05 10:50:08 +00:00
|
|
|
end
|
2010-06-19 14:44:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-23 11:31:57 +00:00
|
|
|
private
|
2016-08-06 17:55:02 +00:00
|
|
|
def assert_equal_sets(expected, actual)
|
|
|
|
assert_equal Set.new(expected), Set.new(actual)
|
2011-03-13 15:52:34 +00:00
|
|
|
end
|
2010-06-19 14:44:35 +00:00
|
|
|
|
2016-08-06 17:55:02 +00:00
|
|
|
def mark_as_autoloaded(*klasses)
|
|
|
|
# If ActiveSupport::Dependencies is not loaded, forget about autoloading.
|
|
|
|
# This allows using AS::DescendantsTracker without AS::Dependencies.
|
|
|
|
if defined? ActiveSupport::Dependencies
|
|
|
|
old_autoloaded = ActiveSupport::Dependencies.autoloaded_constants.dup
|
|
|
|
ActiveSupport::Dependencies.autoloaded_constants = klasses.map(&:name)
|
|
|
|
end
|
|
|
|
|
|
|
|
old_descendants = ActiveSupport::DescendantsTracker.class_eval("@@direct_descendants").dup
|
|
|
|
old_descendants.each { |k, v| old_descendants[k] = v.dup }
|
2010-06-19 14:44:35 +00:00
|
|
|
|
2016-08-06 17:55:02 +00:00
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
ActiveSupport::Dependencies.autoloaded_constants = old_autoloaded if defined? ActiveSupport::Dependencies
|
|
|
|
ActiveSupport::DescendantsTracker.class_eval("@@direct_descendants").replace(old_descendants)
|
|
|
|
end
|
2012-10-18 20:14:43 +00:00
|
|
|
end
|