From 8db8c6f4ce3e8dd7f90553ab7866bf9991773b98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 19 Jun 2010 16:44:35 +0200 Subject: Add ActiveSupport::DescendantsTracker. --- activesupport/test/descendants_tracker_test.rb | 72 ++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 activesupport/test/descendants_tracker_test.rb (limited to 'activesupport/test/descendants_tracker_test.rb') diff --git a/activesupport/test/descendants_tracker_test.rb b/activesupport/test/descendants_tracker_test.rb new file mode 100644 index 0000000000..57c97b45d2 --- /dev/null +++ b/activesupport/test/descendants_tracker_test.rb @@ -0,0 +1,72 @@ +require 'abstract_unit' +require 'test/unit' +require 'active_support' + +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 + + 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 + mark_as_autoloaded Parent, Child1, Child2, Grandchild1, Grandchild2 do + ActiveSupport::DescendantsTracker.clear + assert_equal Hash.new, ActiveSupport::DescendantsTracker.descendants + 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) + + old_descendants = ActiveSupport::DescendantsTracker.descendants.dup + old_descendants.each { |k, v| old_descendants[k] = v.dup } + + yield + ensure + ActiveSupport::Dependencies.autoloaded_constants = old_autoloaded + ActiveSupport::DescendantsTracker.descendants = old_descendants + end +end \ No newline at end of file -- cgit v1.2.3 From a2b7fcb07ca47ca2285dee2afe97050532e94d07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 19 Jun 2010 16:58:12 +0200 Subject: Change callbacks to automatically include DescendantsTracker and rename descendents to descendants. --- activesupport/test/descendants_tracker_test.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'activesupport/test/descendants_tracker_test.rb') diff --git a/activesupport/test/descendants_tracker_test.rb b/activesupport/test/descendants_tracker_test.rb index 57c97b45d2..e3eccb8aa9 100644 --- a/activesupport/test/descendants_tracker_test.rb +++ b/activesupport/test/descendants_tracker_test.rb @@ -1,6 +1,7 @@ require 'abstract_unit' require 'test/unit' require 'active_support' +require 'active_support/core_ext/hash/slice' class DescendantsTrackerTest < Test::Unit::TestCase class Parent @@ -19,6 +20,8 @@ class DescendantsTrackerTest < Test::Unit::TestCase 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 @@ -32,9 +35,9 @@ class DescendantsTrackerTest < Test::Unit::TestCase end def test_clear_with_autoloaded_parent_children_and_granchildren - mark_as_autoloaded Parent, Child1, Child2, Grandchild1, Grandchild2 do + mark_as_autoloaded *ALL do ActiveSupport::DescendantsTracker.clear - assert_equal Hash.new, ActiveSupport::DescendantsTracker.descendants + assert ActiveSupport::DescendantsTracker.descendants.slice(*ALL).empty? end end -- cgit v1.2.3 From d430db9fd407fd6aee25ca13538d8988dc7ee297 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 19 Jun 2010 17:16:11 +0200 Subject: Remove descendants warning while executing tests. --- activesupport/test/descendants_tracker_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport/test/descendants_tracker_test.rb') diff --git a/activesupport/test/descendants_tracker_test.rb b/activesupport/test/descendants_tracker_test.rb index e3eccb8aa9..3a424180de 100644 --- a/activesupport/test/descendants_tracker_test.rb +++ b/activesupport/test/descendants_tracker_test.rb @@ -70,6 +70,6 @@ class DescendantsTrackerTest < Test::Unit::TestCase yield ensure ActiveSupport::Dependencies.autoloaded_constants = old_autoloaded - ActiveSupport::DescendantsTracker.descendants = old_descendants + ActiveSupport::DescendantsTracker.descendants.replace(old_descendants) end end \ No newline at end of file -- cgit v1.2.3