1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activesupport/test/descendants_tracker_test_cases.rb

68 lines
1.9 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "set"
module DescendantsTrackerTestCases
2010-06-19 10:44:35 -04:00
class Parent
extend ActiveSupport::DescendantsTracker
end
class Child1 < Parent
end
class Child2 < Parent
end
class Grandchild1 < Child1
end
class Grandchild2 < Child1
end
ALL = [Parent, Child1, Child2, Grandchild1, Grandchild2]
2010-06-19 10:44:35 -04:00
def test_descendants
assert_equal_sets [Child1, Grandchild1, Grandchild2, Child2], Parent.descendants
assert_equal_sets [Grandchild1, Grandchild2], Child1.descendants
assert_equal_sets [], Child2.descendants
2010-06-19 10:44:35 -04:00
end
def test_direct_descendants
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 10:44:35 -04:00
end
def test_clear
mark_as_autoloaded(*ALL) do
2010-06-19 10:44:35 -04:00
ActiveSupport::DescendantsTracker.clear
ALL.each do |k|
2018-01-25 18:16:57 -05:00
assert_empty ActiveSupport::DescendantsTracker.descendants(k)
end
2010-06-19 10:44:35 -04:00
end
end
private
2010-06-19 10:44:35 -04:00
def assert_equal_sets(expected, actual)
assert_equal Set.new(expected), Set.new(actual)
end
2010-06-19 10:44:35 -04: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 10:44:35 -04: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 16:14:43 -04:00
end