aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/testing
diff options
context:
space:
mode:
authorMike Moore <mike@blowmage.com>2012-09-24 13:44:49 -0600
committerMike Moore <mike@blowmage.com>2012-09-24 13:44:49 -0600
commit4894eef448b19b326946096d0ec08700f67cd649 (patch)
tree81f1c401e873ad20a130694e1b1eaa5a12802970 /activesupport/lib/active_support/testing
parent1dbe4baef5120dce845c46abe0014abf64e4b0ca (diff)
downloadrails-4894eef448b19b326946096d0ec08700f67cd649.tar.gz
rails-4894eef448b19b326946096d0ec08700f67cd649.tar.bz2
rails-4894eef448b19b326946096d0ec08700f67cd649.zip
Create ActiveSupport::Testing::ConstantLookup
AS::TC::ConstantLookup walks the test's name to find the constant it is describing. This additional lookup logic is needed to better support minitest's spec DSL.
Diffstat (limited to 'activesupport/lib/active_support/testing')
-rw-r--r--activesupport/lib/active_support/testing/constant_lookup.rb53
1 files changed, 53 insertions, 0 deletions
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