diff options
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/test_case.rb | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/testing/constant_lookup.rb | 53 | ||||
-rw-r--r-- | activesupport/test/testing/constant_lookup_test.rb | 58 |
3 files changed, 113 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/test_case.rb b/activesupport/lib/active_support/test_case.rb index d2b8e602f9..60bd4ace9b 100644 --- a/activesupport/lib/active_support/test_case.rb +++ b/activesupport/lib/active_support/test_case.rb @@ -5,6 +5,7 @@ require 'active_support/testing/assertions' require 'active_support/testing/deprecation' require 'active_support/testing/isolation' require 'active_support/testing/mocha_module' +require 'active_support/testing/constant_lookup' require 'active_support/core_ext/kernel/reporting' require 'active_support/deprecation' @@ -35,6 +36,7 @@ module ActiveSupport include ActiveSupport::Testing::SetupAndTeardown include ActiveSupport::Testing::Assertions include ActiveSupport::Testing::Deprecation + include ActiveSupport::Testing::ConstantLookup def self.describe(text) if block_given? diff --git a/activesupport/lib/active_support/testing/constant_lookup.rb b/activesupport/lib/active_support/testing/constant_lookup.rb new file mode 100644 index 0000000000..8cb84b3f20 --- /dev/null +++ b/activesupport/lib/active_support/testing/constant_lookup.rb @@ -0,0 +1,53 @@ +require "active_support/concern" + +module ActiveSupport + module Testing + # Resolves a constant from a minitest spec name. + # + # Given the following spec-style test: + # + # describe WidgetsController, :index do + # describe "authenticated user" do + # describe "returns widgets" do + # it "has a controller that exists" do + # assert_kind_of WidgetsController, @controller + # end + # end + # end + # end + # + # The test will have the following name: + # + # "WidgetsController::index::authenticated user::returns widgets" + # + # The constant WidgetsController can be resolved from the name. + # The following code will resolve the constant: + # + # controller = determine_constant_from_test_name(name) do |constant| + # Class === constant && constant < ::ActionController::Metal + # end + module ConstantLookup + extend ::ActiveSupport::Concern + + module ClassMethods + def determine_constant_from_test_name(test_name) + names = test_name.split "::" + while names.size > 0 do + names.last.sub! /Test$/, "" + # Rails 3.0 doesn't have safe_constantize, + # so we'll do it the hard way. + begin + constant = names.join("::").constantize + break(constant) if yield(constant) + rescue NameError + # Constant wasn't found, move on + ensure + names.pop + end + end + end + end + + end + end +end diff --git a/activesupport/test/testing/constant_lookup_test.rb b/activesupport/test/testing/constant_lookup_test.rb new file mode 100644 index 0000000000..f39d38e5cd --- /dev/null +++ b/activesupport/test/testing/constant_lookup_test.rb @@ -0,0 +1,58 @@ +require 'abstract_unit' + +class Foo; end +class Bar < Foo; + def index; end + def self.index; end +end +class Baz < Bar; end +module FooBar; end + +class TestLookup < ActiveSupport::TestCase + + 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 +end |