2017-07-09 20:06:36 +08:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 21:39:13 +08:00
|
|
|
|
2021-08-23 06:24:26 +08:00
|
|
|
require_relative "abstract_unit"
|
2016-08-07 00:03:25 +08:00
|
|
|
require "set"
|
2021-08-23 06:24:26 +08:00
|
|
|
require "active_support/descendants_tracker"
|
2012-07-01 03:51:57 +08:00
|
|
|
|
2021-08-23 06:24:26 +08:00
|
|
|
class DescendantsTrackerTest < ActiveSupport::TestCase
|
|
|
|
setup do
|
2021-10-27 21:55:37 +08:00
|
|
|
if ActiveSupport::DescendantsTracker.class_variable_defined?(:@@direct_descendants)
|
|
|
|
@original_state = ActiveSupport::DescendantsTracker.class_variable_get(:@@direct_descendants).dup
|
|
|
|
@original_state.each { |k, v| @original_state[k] = v.dup }
|
|
|
|
end
|
2010-06-19 22:44:35 +08:00
|
|
|
|
2021-08-23 06:24:26 +08:00
|
|
|
eval <<~RUBY
|
|
|
|
class Parent
|
|
|
|
extend ActiveSupport::DescendantsTracker
|
|
|
|
end
|
2010-06-19 22:44:35 +08:00
|
|
|
|
2021-08-23 06:24:26 +08:00
|
|
|
class Child1 < Parent
|
|
|
|
end
|
2010-06-19 22:44:35 +08:00
|
|
|
|
2021-08-23 06:24:26 +08:00
|
|
|
class Child2 < Parent
|
|
|
|
end
|
|
|
|
|
|
|
|
class Grandchild1 < Child1
|
|
|
|
end
|
2010-06-19 22:44:35 +08:00
|
|
|
|
2021-08-23 06:24:26 +08:00
|
|
|
class Grandchild2 < Child1
|
|
|
|
end
|
|
|
|
RUBY
|
2010-06-19 22:44:35 +08:00
|
|
|
end
|
|
|
|
|
2021-08-23 06:24:26 +08:00
|
|
|
teardown do
|
2021-10-27 21:55:37 +08:00
|
|
|
if ActiveSupport::DescendantsTracker.class_variable_defined?(:@@direct_descendants)
|
|
|
|
ActiveSupport::DescendantsTracker.class_variable_get(:@@direct_descendants).replace(@original_state)
|
|
|
|
end
|
2021-08-23 06:24:26 +08:00
|
|
|
|
|
|
|
%i(Parent Child1 Child2 Grandchild1 Grandchild2).each do |name|
|
2021-10-27 21:55:37 +08:00
|
|
|
if DescendantsTrackerTest.const_defined?(name)
|
|
|
|
DescendantsTrackerTest.send(:remove_const, name)
|
|
|
|
end
|
2021-08-23 06:24:26 +08:00
|
|
|
end
|
|
|
|
end
|
2010-06-19 22:58:12 +08:00
|
|
|
|
2021-08-23 06:24:26 +08:00
|
|
|
test ".descendants" do
|
2012-07-01 03:51:57 +08: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 22:44:35 +08:00
|
|
|
end
|
|
|
|
|
2021-08-23 06:24:26 +08:00
|
|
|
test ".descendants with garbage collected classes" do
|
2021-07-23 02:45:57 +08: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 18:37:51 +08:00
|
|
|
child_klass = Class.new(Parent)
|
|
|
|
assert_equal_sets [Child1, Grandchild1, Grandchild2, Child2, child_klass], Parent.descendants
|
2021-07-23 02:45:57 +08:00
|
|
|
end.join
|
|
|
|
|
|
|
|
# Calling `GC.start` 4 times should trigger a full GC run
|
|
|
|
4.times do
|
|
|
|
GC.start
|
2017-12-14 18:37:51 +08:00
|
|
|
end
|
2021-07-23 02:45:57 +08:00
|
|
|
|
2017-12-14 18:37:51 +08:00
|
|
|
assert_equal_sets [Child1, Grandchild1, Grandchild2, Child2], Parent.descendants
|
|
|
|
end
|
|
|
|
|
2021-08-23 06:24:26 +08:00
|
|
|
test ".direct_descendants" do
|
2021-10-27 21:55:37 +08:00
|
|
|
assert_deprecated do
|
|
|
|
assert_equal_sets [Child1, Child2], Parent.direct_descendants
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_deprecated do
|
|
|
|
assert_equal_sets [Grandchild1, Grandchild2], Child1.direct_descendants
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_deprecated do
|
|
|
|
assert_equal_sets [], Child2.direct_descendants
|
|
|
|
end
|
2010-06-19 22:44:35 +08:00
|
|
|
end
|
|
|
|
|
2021-08-23 06:24:26 +08:00
|
|
|
test ".subclasses" do
|
2020-06-02 08:30:50 +08:00
|
|
|
[Parent, Child1, Child2].each do |klass|
|
2021-10-27 21:55:37 +08:00
|
|
|
assert_equal assert_deprecated { klass.direct_descendants }, klass.subclasses
|
2020-06-02 08:30:50 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-25 19:55:53 +08:00
|
|
|
test ".clear(classes) deletes the given classes only" do
|
|
|
|
ActiveSupport::DescendantsTracker.clear(Set[Child2, Grandchild1])
|
2010-06-19 22:44:35 +08:00
|
|
|
|
2021-08-23 06:24:26 +08:00
|
|
|
assert_equal_sets [Child1, Grandchild2], Parent.descendants
|
|
|
|
assert_equal_sets [Grandchild2], Child1.descendants
|
2016-08-07 01:55:02 +08:00
|
|
|
|
2021-10-27 21:55:37 +08:00
|
|
|
assert_equal_sets [Child1], assert_deprecated { Parent.direct_descendants }
|
|
|
|
assert_equal_sets [Grandchild2], assert_deprecated { Child1.direct_descendants }
|
2021-08-23 06:24:26 +08:00
|
|
|
end
|
2010-06-19 22:44:35 +08:00
|
|
|
|
2021-08-23 06:24:26 +08:00
|
|
|
private
|
|
|
|
def assert_equal_sets(expected, actual)
|
|
|
|
assert_equal Set.new(expected), Set.new(actual)
|
2016-08-07 01:55:02 +08:00
|
|
|
end
|
2012-10-19 04:14:43 +08:00
|
|
|
end
|