Sha256: 1b667aebb1540a5745a145a2a203f535a0c299b74e0001349d23c68527e1498f

Contents?: true

Size: 1.56 KB

Versions: 76

Compression:

Stored size: 1.56 KB

Contents

require "active_support/concern"
require "active_support/inflector"

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  # :nodoc:
        def determine_constant_from_test_name(test_name)
          names = test_name.split "::"
          while names.size > 0 do
            names.last.sub!(/Test$/, "")
            begin
              constant = names.join("::").constantize
              break(constant) if yield(constant)
            rescue NoMethodError # subclass of NameError
              raise
            rescue NameError
              # Constant wasn't found, move on
            ensure
              names.pop
            end
          end
        end
      end

    end
  end
end

Version data entries

76 entries across 76 versions & 5 rubygems

Version Path
activesupport-4.1.16 lib/active_support/testing/constant_lookup.rb
activesupport-4.1.16.rc1 lib/active_support/testing/constant_lookup.rb
activesupport-4.1.15 lib/active_support/testing/constant_lookup.rb
activesupport-4.1.15.rc1 lib/active_support/testing/constant_lookup.rb
activesupport-4.1.14.2 lib/active_support/testing/constant_lookup.rb
activesupport-4.1.14.1 lib/active_support/testing/constant_lookup.rb
angular-rails4-templates-0.4.1 vendor/ruby/2.1.0/gems/activesupport-4.1.13/lib/active_support/testing/constant_lookup.rb
angular-rails4-templates-0.4.0 vendor/ruby/2.1.0/gems/activesupport-4.1.13/lib/active_support/testing/constant_lookup.rb
angular-rails4-templates-0.3.0 vendor/ruby/2.1.0/gems/activesupport-4.1.13/lib/active_support/testing/constant_lookup.rb
activesupport-4.1.14 lib/active_support/testing/constant_lookup.rb
activesupport-4.1.14.rc2 lib/active_support/testing/constant_lookup.rb
activesupport-4.1.14.rc1 lib/active_support/testing/constant_lookup.rb
activesupport-4.1.13 lib/active_support/testing/constant_lookup.rb
activesupport-4.1.13.rc1 lib/active_support/testing/constant_lookup.rb
activesupport-4.1.12 lib/active_support/testing/constant_lookup.rb
activesupport-4.1.12.rc1 lib/active_support/testing/constant_lookup.rb
activesupport-4.1.11 lib/active_support/testing/constant_lookup.rb
activesupport-4.1.10 lib/active_support/testing/constant_lookup.rb
activesupport-4.1.10.rc4 lib/active_support/testing/constant_lookup.rb
activesupport-4.1.10.rc3 lib/active_support/testing/constant_lookup.rb