2009-05-07 11:03:39 -04:00
|
|
|
require 'abstract_unit'
|
2009-05-07 11:29:22 -04:00
|
|
|
require 'active_support/dependency_module'
|
2009-05-07 11:03:39 -04:00
|
|
|
|
2009-05-07 11:29:22 -04:00
|
|
|
class DependencyModuleTest < Test::Unit::TestCase
|
2009-05-07 11:03:39 -04:00
|
|
|
module Baz
|
2009-05-07 11:29:22 -04:00
|
|
|
extend ActiveSupport::DependencyModule
|
|
|
|
|
2009-05-07 11:03:39 -04:00
|
|
|
module ClassMethods
|
|
|
|
def baz
|
|
|
|
"baz"
|
|
|
|
end
|
|
|
|
|
2009-05-07 11:38:57 -04:00
|
|
|
def included_ran=(value)
|
|
|
|
@@included_ran = value
|
2009-05-07 11:03:39 -04:00
|
|
|
end
|
|
|
|
|
2009-05-07 11:38:57 -04:00
|
|
|
def included_ran
|
|
|
|
@@included_ran
|
2009-05-07 11:03:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-05-07 11:38:57 -04:00
|
|
|
included do
|
|
|
|
self.included_ran = true
|
2009-05-07 11:03:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def baz
|
|
|
|
"baz"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module Bar
|
2009-05-07 11:29:22 -04:00
|
|
|
extend ActiveSupport::DependencyModule
|
|
|
|
|
2009-05-07 11:03:39 -04:00
|
|
|
depends_on Baz
|
|
|
|
|
|
|
|
def bar
|
|
|
|
"bar"
|
|
|
|
end
|
|
|
|
|
|
|
|
def baz
|
|
|
|
"bar+" + super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-05-20 21:04:17 -04:00
|
|
|
module Foo
|
|
|
|
extend ActiveSupport::DependencyModule
|
|
|
|
|
|
|
|
depends_on Bar, Baz
|
|
|
|
end
|
|
|
|
|
2009-05-07 11:03:39 -04:00
|
|
|
def setup
|
|
|
|
@klass = Class.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_module_is_included_normally
|
2009-05-07 11:29:22 -04:00
|
|
|
@klass.send(:include, Baz)
|
2009-05-07 11:03:39 -04:00
|
|
|
assert_equal "baz", @klass.new.baz
|
2009-05-07 11:29:22 -04:00
|
|
|
assert_equal DependencyModuleTest::Baz, @klass.included_modules[0]
|
2009-05-07 11:03:39 -04:00
|
|
|
|
2009-05-07 11:29:22 -04:00
|
|
|
@klass.send(:include, Baz)
|
2009-05-07 11:03:39 -04:00
|
|
|
assert_equal "baz", @klass.new.baz
|
2009-05-07 11:29:22 -04:00
|
|
|
assert_equal DependencyModuleTest::Baz, @klass.included_modules[0]
|
2009-05-07 11:03:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_class_methods_are_extended
|
2009-05-07 11:29:22 -04:00
|
|
|
@klass.send(:include, Baz)
|
2009-05-07 11:03:39 -04:00
|
|
|
assert_equal "baz", @klass.baz
|
2009-05-07 11:29:22 -04:00
|
|
|
assert_equal DependencyModuleTest::Baz::ClassMethods, (class << @klass; self.included_modules; end)[0]
|
2009-05-07 11:03:39 -04:00
|
|
|
end
|
|
|
|
|
2009-05-07 11:38:57 -04:00
|
|
|
def test_included_block_is_ran
|
2009-05-07 11:29:22 -04:00
|
|
|
@klass.send(:include, Baz)
|
2009-05-07 11:38:57 -04:00
|
|
|
assert_equal true, @klass.included_ran
|
2009-05-07 11:03:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_modules_dependencies_are_met
|
2009-05-07 11:29:22 -04:00
|
|
|
@klass.send(:include, Bar)
|
2009-05-07 11:03:39 -04:00
|
|
|
assert_equal "bar", @klass.new.bar
|
|
|
|
assert_equal "bar+baz", @klass.new.baz
|
|
|
|
assert_equal "baz", @klass.baz
|
2009-05-07 11:29:22 -04:00
|
|
|
assert_equal [DependencyModuleTest::Bar, DependencyModuleTest::Baz], @klass.included_modules[0..1]
|
2009-05-07 11:03:39 -04:00
|
|
|
end
|
2009-05-20 21:04:17 -04:00
|
|
|
|
|
|
|
def test_depends_on_with_multiple_modules
|
|
|
|
@klass.send(:include, Foo)
|
|
|
|
assert_equal [DependencyModuleTest::Foo, DependencyModuleTest::Bar, DependencyModuleTest::Baz], @klass.included_modules[0..2]
|
|
|
|
end
|
2009-05-07 11:03:39 -04:00
|
|
|
end
|