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"
|
2012-09-24 15:44:49 -04:00
|
|
|
|
|
|
|
class Foo; end
|
2012-11-04 09:07:06 -05:00
|
|
|
class Bar < Foo
|
2012-09-24 15:44:49 -04:00
|
|
|
def index; end
|
|
|
|
def self.index; end
|
|
|
|
end
|
|
|
|
module FooBar; end
|
|
|
|
|
2012-09-25 17:40:00 -04:00
|
|
|
class ConstantLookupTest < ActiveSupport::TestCase
|
2012-09-26 23:21:09 -04:00
|
|
|
include ActiveSupport::Testing::ConstantLookup
|
2012-09-24 15:44:49 -04:00
|
|
|
|
|
|
|
def find_foo(name)
|
|
|
|
self.class.determine_constant_from_test_name(name) do |constant|
|
|
|
|
Class === constant && constant < Foo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_module(name)
|
|
|
|
self.class.determine_constant_from_test_name(name) do |constant|
|
|
|
|
Module === constant
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_bar_from_foo
|
|
|
|
assert_equal Bar, find_foo("Bar")
|
|
|
|
assert_equal Bar, find_foo("Bar::index")
|
|
|
|
assert_equal Bar, find_foo("Bar::index::authenticated")
|
|
|
|
assert_equal Bar, find_foo("BarTest")
|
|
|
|
assert_equal Bar, find_foo("BarTest::index")
|
|
|
|
assert_equal Bar, find_foo("BarTest::index::authenticated")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_module
|
|
|
|
assert_equal FooBar, find_module("FooBar")
|
|
|
|
assert_equal FooBar, find_module("FooBar::index")
|
|
|
|
assert_equal FooBar, find_module("FooBar::index::authenticated")
|
|
|
|
assert_equal FooBar, find_module("FooBarTest")
|
|
|
|
assert_equal FooBar, find_module("FooBarTest::index")
|
|
|
|
assert_equal FooBar, find_module("FooBarTest::index::authenticated")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_returns_nil_when_cant_find_foo
|
|
|
|
assert_nil find_foo("DoesntExist")
|
|
|
|
assert_nil find_foo("DoesntExistTest")
|
|
|
|
assert_nil find_foo("DoesntExist::Nadda")
|
|
|
|
assert_nil find_foo("DoesntExist::Nadda::Nope")
|
|
|
|
assert_nil find_foo("DoesntExist::Nadda::Nope::NotHere")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_returns_nil_when_cant_find_module
|
|
|
|
assert_nil find_module("DoesntExist")
|
|
|
|
assert_nil find_module("DoesntExistTest")
|
|
|
|
assert_nil find_module("DoesntExist::Nadda")
|
|
|
|
assert_nil find_module("DoesntExist::Nadda::Nope")
|
|
|
|
assert_nil find_module("DoesntExist::Nadda::Nope::NotHere")
|
|
|
|
end
|
2013-03-28 05:04:39 -04:00
|
|
|
|
2021-03-19 19:32:44 -04:00
|
|
|
def test_does_not_shallow_ordinary_exceptions
|
|
|
|
test_name = "RaisesNameError"
|
|
|
|
file_name = File.expand_path("../autoloading_fixtures/raises_no_method_error.rb", __dir__)
|
2014-08-10 02:56:03 -04:00
|
|
|
assert_raises(NameError) do
|
2021-03-19 19:32:44 -04:00
|
|
|
Object.autoload(test_name, file_name)
|
|
|
|
self.class.determine_constant_from_test_name(test_name)
|
2014-08-10 02:56:03 -04:00
|
|
|
end
|
2021-03-19 19:32:44 -04:00
|
|
|
ensure
|
|
|
|
Object.send(:remove_const, test_name)
|
2014-08-10 02:56:03 -04:00
|
|
|
end
|
2012-09-24 15:44:49 -04:00
|
|
|
end
|