2010-06-19 10:44:35 -04:00
|
|
|
require 'abstract_unit'
|
|
|
|
require 'test/unit'
|
|
|
|
require 'active_support'
|
2010-06-19 10:58:12 -04:00
|
|
|
require 'active_support/core_ext/hash/slice'
|
2010-06-19 10:44:35 -04:00
|
|
|
|
|
|
|
class DescendantsTrackerTest < Test::Unit::TestCase
|
|
|
|
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 10:58:12 -04:00
|
|
|
ALL = [Parent, Child1, Child2, Grandchild1, Grandchild2]
|
|
|
|
|
2010-06-19 10:44:35 -04:00
|
|
|
def test_descendants
|
|
|
|
assert_equal [Child1, Grandchild1, Grandchild2, Child2], Parent.descendants
|
|
|
|
assert_equal [Grandchild1, Grandchild2], Child1.descendants
|
|
|
|
assert_equal [], Child2.descendants
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_direct_descendants
|
|
|
|
assert_equal [Child1, Child2], Parent.direct_descendants
|
|
|
|
assert_equal [Grandchild1, Grandchild2], Child1.direct_descendants
|
|
|
|
assert_equal [], Child2.direct_descendants
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_clear_with_autoloaded_parent_children_and_granchildren
|
2010-06-20 17:29:37 -04:00
|
|
|
mark_as_autoloaded(*ALL) do
|
2010-06-19 10:44:35 -04:00
|
|
|
ActiveSupport::DescendantsTracker.clear
|
2010-07-05 06:50:08 -04:00
|
|
|
ALL.each do |k|
|
|
|
|
assert ActiveSupport::DescendantsTracker.descendants(k).empty?
|
|
|
|
end
|
2010-06-19 10:44:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_clear_with_autoloaded_children_and_granchildren
|
|
|
|
mark_as_autoloaded Child1, Grandchild1, Grandchild2 do
|
|
|
|
ActiveSupport::DescendantsTracker.clear
|
|
|
|
assert_equal [Child2], Parent.descendants
|
|
|
|
assert_equal [], Child2.descendants
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_clear_with_autoloaded_granchildren
|
|
|
|
mark_as_autoloaded Grandchild1, Grandchild2 do
|
|
|
|
ActiveSupport::DescendantsTracker.clear
|
|
|
|
assert_equal [Child1, Child2], Parent.descendants
|
|
|
|
assert_equal [], Child1.descendants
|
|
|
|
assert_equal [], Child2.descendants
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def mark_as_autoloaded(*klasses)
|
|
|
|
old_autoloaded = ActiveSupport::Dependencies.autoloaded_constants.dup
|
|
|
|
ActiveSupport::Dependencies.autoloaded_constants = klasses.map(&:name)
|
|
|
|
|
2010-07-05 06:50:08 -04:00
|
|
|
old_descendants = ActiveSupport::DescendantsTracker.class_eval("@@direct_descendants").dup
|
2010-06-19 10:44:35 -04:00
|
|
|
old_descendants.each { |k, v| old_descendants[k] = v.dup }
|
|
|
|
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
ActiveSupport::Dependencies.autoloaded_constants = old_autoloaded
|
2010-07-05 06:50:08 -04:00
|
|
|
ActiveSupport::DescendantsTracker.class_eval("@@direct_descendants").replace(old_descendants)
|
2010-06-19 10:44:35 -04:00
|
|
|
end
|
|
|
|
end
|