Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Extract constant lookup into a module

ActionMailer needs this functionality as well.
In preparation for submitting this to either Rails 4 or Minitest.
  • Loading branch information...
commit 32fea6ec5293d57a58657ea963d80b1315572574 1 parent 6ce4c14
blowmage authored
39 lib/minitest/rails/action_controller.rb
View
@@ -16,43 +16,10 @@ class TestCase < MiniTest::Rails::ActiveSupport::TestCase
include ::ActionController::TestCase::Behavior
def self.determine_default_controller_class(name)
- # Override this method to support nested describes
- #
- # describe WidgetsController do
- # describe "index" do
- # it "is successful" do
- # assert_response :success
- # end
- # end
- # end
- #
- # Original implementation:
- # name.sub(/Test$/, '').safe_constantize
- # safe_constantize is not supported in Rails 3.0...
- ControllerLookup.find name
- end
- end
-
- module ControllerLookup
- def self.find controller
- # HACK: I don't love this implementation
- # Please suggest a better one!
- names = controller.split "::"
- controller = while names.size > 0 do
- # Remove "Test" if present
- names.last.sub! /Test$/, ''
- begin
- constant = names.join("::").constantize
- break(constant) if constant
- rescue NameError
- # do nothing
- ensure
- names.pop
- end
- end
- if controller.nil?
- raise ::NameError.new("Unable to resolve controller for #{name}")
+ controller = determine_constant_from_test_name(name) do |constant|
+ Class === constant && constant < ::ActionController::Metal
end
+ raise ::NameError.new("Unable to resolve controller for #{name}") if controller.nil?
controller
end
end
2  lib/minitest/rails/active_support.rb
View
@@ -12,6 +12,7 @@
require 'minitest/rails/mochaing'
require 'active_support/core_ext/kernel/reporting'
+require "minitest/rails/constant_lookup"
module MiniTest
module Rails
module ActiveSupport
@@ -51,6 +52,7 @@ def self.for_tag(tag)
include ::ActiveSupport::Testing::Deprecation
include ::ActiveSupport::Testing::Pending
extend Testing::Declarative
+ include MiniTest::Rails::ActiveSupport::Testing::ConstantLookup
# test/unit backwards compatibility methods
alias :assert_raise :assert_raises
57 lib/minitest/rails/constant_lookup.rb
View
@@ -0,0 +1,57 @@
+require "active_support/concern"
+
+module MiniTest
+ module Rails
+ 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
+ end
+end
49 test/rails/action_controller/test_controller_lookup.rb
View
@@ -1,49 +0,0 @@
-require "minitest/autorun"
-require "rails"
-
-require "minitest/rails/action_controller"
-require "action_controller"
-
-class ApplicationController < ActionController::Base; end
-class ModelsController < ApplicationController; end
-module Admin
- class WidgetsController < ApplicationController; end
-end
-
-class TestControllerLookup < MiniTest::Unit::TestCase
- include MiniTest::Rails::ActionController
- def test_find_application_controller
- assert_equal ApplicationController, ControllerLookup.find("ApplicationController")
- assert_equal ApplicationController, ControllerLookup.find("ApplicationControllerTest")
- assert_equal ApplicationController, ControllerLookup.find("ApplicationController::index")
- assert_equal ApplicationController, ControllerLookup.find("ApplicationController::index::authenticated")
- assert_equal ApplicationController, ControllerLookup.find("ApplicationControllerTest::index")
- assert_equal ApplicationController, ControllerLookup.find("ApplicationControllerTest::index::authenticated")
- end
-
- def test_find_models_controller
- assert_equal ModelsController, ControllerLookup.find("ModelsController")
- assert_equal ModelsController, ControllerLookup.find("ModelsControllerTest")
- assert_equal ModelsController, ControllerLookup.find("ModelsController::index")
- assert_equal ModelsController, ControllerLookup.find("ModelsController::index::authenticated")
- assert_equal ModelsController, ControllerLookup.find("ModelsControllerTest::index")
- assert_equal ModelsController, ControllerLookup.find("ModelsControllerTest::index::authenticated")
- end
-
- def test_find_admin_widgets_controller
- assert_equal Admin::WidgetsController, ControllerLookup.find("Admin::WidgetsController")
- assert_equal Admin::WidgetsController, ControllerLookup.find("Admin::WidgetsControllerTest")
- assert_equal Admin::WidgetsController, ControllerLookup.find("Admin::WidgetsController::index")
- assert_equal Admin::WidgetsController, ControllerLookup.find("Admin::WidgetsController::index::authenticated")
- assert_equal Admin::WidgetsController, ControllerLookup.find("Admin::WidgetsControllerTest::index")
- assert_equal Admin::WidgetsController, ControllerLookup.find("Admin::WidgetsControllerTest::index::authenticated")
- end
-
- def test_raises_when_cant_find_controller
- assert_raises(NameError) { ControllerLookup.find("DoesntExistController") }
- assert_raises(NameError) { ControllerLookup.find("DoesntExistControllerTest") }
- assert_raises(NameError) { ControllerLookup.find("DoesntExistController::Nadda") }
- assert_raises(NameError) { ControllerLookup.find("DoesntExistController::Nadda::Nope") }
- assert_raises(NameError) { ControllerLookup.find("DoesntExistController::Nadda::Nope::NotHere") }
- end
-end
61 test/rails/test_constant_lookup.rb
View
@@ -0,0 +1,61 @@
+require "minitest/autorun"
+require "rails"
+
+require "minitest/rails/active_support"
+
+class Foo; end
+class Bar < Foo;
+ def index; end
+ def self.index; end
+end
+class Baz < Bar; end
+module FooBar; end
+
+class TestLookup < MiniTest::Rails::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
Please sign in to comment.
Something went wrong with that request. Please try again.