2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2018-09-29 20:50:43 -04:00
|
|
|
require_relative "abstract_unit"
|
2009-12-02 23:01:01 -05:00
|
|
|
|
|
|
|
class TestAutoloadModule < ActiveSupport::TestCase
|
|
|
|
include ActiveSupport::Testing::Isolation
|
|
|
|
|
|
|
|
module ::Fixtures
|
|
|
|
extend ActiveSupport::Autoload
|
|
|
|
|
|
|
|
module Autoload
|
|
|
|
extend ActiveSupport::Autoload
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-05 11:58:33 -04:00
|
|
|
def setup
|
|
|
|
@some_class_path = File.expand_path("test/fixtures/autoload/some_class.rb")
|
|
|
|
@another_class_path = File.expand_path("test/fixtures/autoload/another_class.rb")
|
2018-09-29 20:50:43 -04:00
|
|
|
$LOAD_PATH << "test"
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
$LOAD_PATH.pop
|
2014-10-05 11:58:33 -04:00
|
|
|
end
|
|
|
|
|
2009-12-02 23:01:01 -05:00
|
|
|
test "the autoload module works like normal autoload" do
|
|
|
|
module ::Fixtures::Autoload
|
|
|
|
autoload :SomeClass, "fixtures/autoload/some_class"
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_nothing_raised { ::Fixtures::Autoload::SomeClass }
|
|
|
|
end
|
|
|
|
|
|
|
|
test "when specifying an :eager constant it still works like normal autoload by default" do
|
|
|
|
module ::Fixtures::Autoload
|
2014-10-05 11:58:33 -04:00
|
|
|
eager_autoload do
|
|
|
|
autoload :SomeClass, "fixtures/autoload/some_class"
|
|
|
|
end
|
2009-12-02 23:01:01 -05:00
|
|
|
end
|
|
|
|
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_not_includes $LOADED_FEATURES, @some_class_path
|
2009-12-02 23:01:01 -05:00
|
|
|
assert_nothing_raised { ::Fixtures::Autoload::SomeClass }
|
|
|
|
end
|
|
|
|
|
|
|
|
test "the location of autoloaded constants defaults to :name.underscore" do
|
|
|
|
module ::Fixtures::Autoload
|
|
|
|
autoload :SomeClass
|
|
|
|
end
|
|
|
|
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_not_includes $LOADED_FEATURES, @some_class_path
|
2009-12-02 23:01:01 -05:00
|
|
|
assert_nothing_raised { ::Fixtures::Autoload::SomeClass }
|
|
|
|
end
|
|
|
|
|
|
|
|
test "the location of :eager autoloaded constants defaults to :name.underscore" do
|
|
|
|
module ::Fixtures::Autoload
|
2014-10-05 11:58:33 -04:00
|
|
|
eager_autoload do
|
|
|
|
autoload :SomeClass
|
|
|
|
end
|
2009-12-02 23:01:01 -05:00
|
|
|
end
|
|
|
|
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_not_includes $LOADED_FEATURES, @some_class_path
|
2012-08-01 12:23:11 -04:00
|
|
|
::Fixtures::Autoload.eager_load!
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_includes $LOADED_FEATURES, @some_class_path
|
2009-12-02 23:01:01 -05:00
|
|
|
assert_nothing_raised { ::Fixtures::Autoload::SomeClass }
|
|
|
|
end
|
|
|
|
|
|
|
|
test "a directory for a block of autoloads can be specified" do
|
|
|
|
module ::Fixtures
|
|
|
|
autoload_under "autoload" do
|
|
|
|
autoload :AnotherClass
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_not_includes $LOADED_FEATURES, @another_class_path
|
2009-12-02 23:01:01 -05:00
|
|
|
assert_nothing_raised { ::Fixtures::AnotherClass }
|
|
|
|
end
|
|
|
|
|
|
|
|
test "a path for a block of autoloads can be specified" do
|
|
|
|
module ::Fixtures
|
|
|
|
autoload_at "fixtures/autoload/another_class" do
|
|
|
|
autoload :AnotherClass
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_not_includes $LOADED_FEATURES, @another_class_path
|
2009-12-02 23:01:01 -05:00
|
|
|
assert_nothing_raised { ::Fixtures::AnotherClass }
|
|
|
|
end
|
2016-08-06 13:55:02 -04:00
|
|
|
end
|