Skip to content

Commit

Permalink
Refactored AS::DescendantsTracker test cases so they can be tested wi…
Browse files Browse the repository at this point in the history
…thout AS::Dependencies.
  • Loading branch information
Rolf Timmermans committed Mar 13, 2011
1 parent 56a0cba commit 76e4e2f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 27 deletions.
@@ -1,9 +1,4 @@
require 'abstract_unit' module DescendantsTrackerTestCases
require 'test/unit'
require 'active_support'
require 'active_support/core_ext/hash/slice'

class DescendantsTrackerTest < Test::Unit::TestCase
class Parent class Parent
extend ActiveSupport::DescendantsTracker extend ActiveSupport::DescendantsTracker
end end
Expand Down Expand Up @@ -34,7 +29,7 @@ def test_direct_descendants
assert_equal [], Child2.direct_descendants assert_equal [], Child2.direct_descendants
end end


def test_clear_with_autoloaded_parent_children_and_granchildren def test_clear
mark_as_autoloaded(*ALL) do mark_as_autoloaded(*ALL) do
ActiveSupport::DescendantsTracker.clear ActiveSupport::DescendantsTracker.clear
ALL.each do |k| ALL.each do |k|
Expand All @@ -43,35 +38,22 @@ def test_clear_with_autoloaded_parent_children_and_granchildren
end end
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 protected


def mark_as_autoloaded(*klasses) def mark_as_autoloaded(*klasses)
old_autoloaded = ActiveSupport::Dependencies.autoloaded_constants.dup # If ActiveSupport::Dependencies is not loaded, forget about autoloading.
ActiveSupport::Dependencies.autoloaded_constants = klasses.map(&:name) # 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 = ActiveSupport::DescendantsTracker.class_eval("@@direct_descendants").dup
old_descendants.each { |k, v| old_descendants[k] = v.dup } old_descendants.each { |k, v| old_descendants[k] = v.dup }


yield yield
ensure ensure
ActiveSupport::Dependencies.autoloaded_constants = old_autoloaded ActiveSupport::Dependencies.autoloaded_constants = old_autoloaded if defined? ActiveSupport::Dependencies
ActiveSupport::DescendantsTracker.class_eval("@@direct_descendants").replace(old_descendants) ActiveSupport::DescendantsTracker.class_eval("@@direct_descendants").replace(old_descendants)
end end
end end
35 changes: 35 additions & 0 deletions activesupport/test/descendants_tracker_with_autoloading_test.rb
@@ -0,0 +1,35 @@
require 'abstract_unit'
require 'test/unit'
require 'active_support/descendants_tracker'
require 'active_support/dependencies'
require 'descendants_tracker_test_cases'

class DescendantsTrackerWithAutoloadingTest < Test::Unit::TestCase
include DescendantsTrackerTestCases

def test_clear_with_autoloaded_parent_children_and_granchildren
mark_as_autoloaded(*ALL) do
ActiveSupport::DescendantsTracker.clear
ALL.each do |k|
assert ActiveSupport::DescendantsTracker.descendants(k).empty?
end
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
end
@@ -0,0 +1,8 @@
require 'abstract_unit'
require 'test/unit'
require 'active_support/descendants_tracker'
require 'descendants_tracker_test_cases'

class DescendantsTrackerWithoutAutoloadingTest < Test::Unit::TestCase
include DescendantsTrackerTestCases
end

0 comments on commit 76e4e2f

Please sign in to comment.