From 76e4e2fa468593c42623472055d329dfeb577c49 Mon Sep 17 00:00:00 2001 From: Rolf Timmermans Date: Sun, 13 Mar 2011 16:52:34 +0100 Subject: Refactored AS::DescendantsTracker test cases so they can be tested without AS::Dependencies. --- .../test/descendants_tracker_test_cases.rb | 59 ++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 activesupport/test/descendants_tracker_test_cases.rb (limited to 'activesupport/test/descendants_tracker_test_cases.rb') diff --git a/activesupport/test/descendants_tracker_test_cases.rb b/activesupport/test/descendants_tracker_test_cases.rb new file mode 100644 index 0000000000..066ec8549b --- /dev/null +++ b/activesupport/test/descendants_tracker_test_cases.rb @@ -0,0 +1,59 @@ +module DescendantsTrackerTestCases + 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] + + 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 + mark_as_autoloaded(*ALL) do + ActiveSupport::DescendantsTracker.clear + ALL.each do |k| + assert ActiveSupport::DescendantsTracker.descendants(k).empty? + end + end + end + + protected + + 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 } + + yield + ensure + ActiveSupport::Dependencies.autoloaded_constants = old_autoloaded if defined? ActiveSupport::Dependencies + ActiveSupport::DescendantsTracker.class_eval("@@direct_descendants").replace(old_descendants) + end +end \ No newline at end of file -- cgit v1.2.3