Sha256: f259576fc5bac3e573461f4e1b8ce3e699c8d2d2aba29986f588c6296566a701

Contents?: true

Size: 1.44 KB

Versions: 225

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

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("::").safe_constantize
              break(constant) if yield(constant)
            ensure
              names.pop
            end
          end
        end
      end
    end
  end
end

Version data entries

225 entries across 210 versions & 25 rubygems

Version Path
activesupport-5.2.4.rc1 lib/active_support/testing/constant_lookup.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/testing/constant_lookup.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/testing/constant_lookup.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/activesupport-6.0.1/lib/active_support/testing/constant_lookup.rb
activesupport-6.0.1 lib/active_support/testing/constant_lookup.rb
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/activesupport-6.0.0/lib/active_support/testing/constant_lookup.rb
activesupport-6.0.1.rc1 lib/active_support/testing/constant_lookup.rb
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/activesupport-6.0.0/lib/active_support/testing/constant_lookup.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/testing/constant_lookup.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/testing/constant_lookup.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/testing/constant_lookup.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/testing/constant_lookup.rb
zuora_connect_ui-0.8.3 vendor/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/testing/constant_lookup.rb
zuora_connect_ui-0.8.3 vendor/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/testing/constant_lookup.rb
zuora_connect_ui-0.8.2 vendor/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/testing/constant_lookup.rb
zuora_connect_ui-0.8.2 vendor/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/testing/constant_lookup.rb
activesupport-6.0.0 lib/active_support/testing/constant_lookup.rb
zuora_connect_ui-0.8.1 vendor/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/testing/constant_lookup.rb
zuora_connect_ui-0.8.0 vendor/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/testing/constant_lookup.rb
spiral_form-0.1.1 vendor/bundle/gems/activesupport-5.2.3/lib/active_support/testing/constant_lookup.rb