From ce78101362d8826d0bfaf4e7935aa0643f3e4c53 Mon Sep 17 00:00:00 2001 From: Pedro Paiva Date: Wed, 15 Jul 2020 11:28:16 -0300 Subject: [PATCH] Use autoloading instead of requiring all files up front --- lib/shoulda/matchers.rb | 25 +++++------ lib/shoulda/matchers/action_controller.rb | 26 ++++++------ lib/shoulda/matchers/active_model.rb | 41 +++++++------------ .../active_model/allow_value_matcher.rb | 9 ++++ .../active_model/numericality_matchers.rb | 5 +++ .../active_model/validation_matcher.rb | 2 + lib/shoulda/matchers/active_record.rb | 38 ++++++----------- .../active_record/association_matchers.rb | 12 ++++++ .../matchers/active_record/uniqueness.rb | 10 ++--- lib/shoulda/matchers/doublespeak.rb | 15 +++---- lib/shoulda/matchers/independent.rb | 1 - .../independent/delegate_method_matcher.rb | 3 ++ lib/shoulda/matchers/integrations.rb | 12 +++--- .../matchers/integrations/test_frameworks.rb | 6 ++- lib/shoulda/matchers/util.rb | 2 - 15 files changed, 108 insertions(+), 99 deletions(-) diff --git a/lib/shoulda/matchers.rb b/lib/shoulda/matchers.rb index b4c00d1e5..c5856d500 100644 --- a/lib/shoulda/matchers.rb +++ b/lib/shoulda/matchers.rb @@ -1,21 +1,22 @@ require 'shoulda/matchers/configuration' -require 'shoulda/matchers/doublespeak' -require 'shoulda/matchers/error' -require 'shoulda/matchers/independent' -require 'shoulda/matchers/integrations' -require 'shoulda/matchers/matcher_context' -require 'shoulda/matchers/rails_shim' -require 'shoulda/matchers/util' require 'shoulda/matchers/version' require 'shoulda/matchers/warn' -require 'shoulda/matchers/action_controller' -require 'shoulda/matchers/active_model' -require 'shoulda/matchers/active_record' -require 'shoulda/matchers/routing' - module Shoulda module Matchers + autoload :ActionController, 'shoulda/matchers/action_controller' + autoload :ActiveModel, 'shoulda/matchers/active_model' + autoload :ActiveRecord, 'shoulda/matchers/active_record' + autoload :Doublespeak, 'shoulda/matchers/doublespeak' + autoload :Error, 'shoulda/matchers/error' + autoload :Independent, 'shoulda/matchers/independent' + autoload :Integrations, 'shoulda/matchers/integrations' + autoload :MatcherContext, 'shoulda/matchers/matcher_context' + autoload :RailsShim, 'shoulda/matchers/rails_shim' + autoload :Routing, 'shoulda/matchers/routing' + autoload :Util, 'shoulda/matchers/util' + autoload :WordWrap, 'shoulda/matchers/util/word_wrap' + class << self # @private attr_accessor :assertion_exception_class diff --git a/lib/shoulda/matchers/action_controller.rb b/lib/shoulda/matchers/action_controller.rb index 91dd016f6..755662b4e 100644 --- a/lib/shoulda/matchers/action_controller.rb +++ b/lib/shoulda/matchers/action_controller.rb @@ -1,24 +1,24 @@ -require 'shoulda/matchers/action_controller/filter_param_matcher' -require 'shoulda/matchers/action_controller/route_params' -require 'shoulda/matchers/action_controller/set_flash_matcher' -require 'shoulda/matchers/action_controller/render_with_layout_matcher' require 'shoulda/matchers/action_controller/respond_with_matcher' -require 'shoulda/matchers/action_controller/set_session_matcher' require 'shoulda/matchers/action_controller/route_matcher' -require 'shoulda/matchers/action_controller/redirect_to_matcher' -require 'shoulda/matchers/action_controller/render_template_matcher' -require 'shoulda/matchers/action_controller/rescue_from_matcher' -require 'shoulda/matchers/action_controller/callback_matcher' -require 'shoulda/matchers/action_controller/permit_matcher' -require 'shoulda/matchers/action_controller/set_session_or_flash_matcher' -require 'shoulda/matchers/action_controller/flash_store' -require 'shoulda/matchers/action_controller/session_store' module Shoulda module Matchers # This module provides matchers that are used to test behavior within # controllers. module ActionController + autoload :CallbackMatcher, 'shoulda/matchers/action_controller/callback_matcher' + autoload :FlashStore, 'shoulda/matchers/action_controller/flash_store' + autoload :FilterParamMatcher, 'shoulda/matchers/action_controller/filter_param_matcher' + autoload :PermitMatcher, 'shoulda/matchers/action_controller/permit_matcher' + autoload :RedirectToMatcher, 'shoulda/matchers/action_controller/redirect_to_matcher' + autoload :RenderTemplateMatcher, 'shoulda/matchers/action_controller/render_template_matcher' + autoload :RenderWithLayoutMatcher, 'shoulda/matchers/action_controller/render_with_layout_matcher' + autoload :RescueFromMatcher, 'shoulda/matchers/action_controller/rescue_from_matcher' + autoload :RouteParams, 'shoulda/matchers/action_controller/route_params' + autoload :SessionStore, 'shoulda/matchers/action_controller/session_store' + autoload :SetFlashMatcher, 'shoulda/matchers/action_controller/set_flash_matcher' + autoload :SetSessionMatcher, 'shoulda/matchers/action_controller/set_session_matcher' + autoload :SetSessionOrFlashMatcher, 'shoulda/matchers/action_controller/set_session_or_flash_matcher' end end end diff --git a/lib/shoulda/matchers/active_model.rb b/lib/shoulda/matchers/active_model.rb index cab68545d..085aef6a1 100644 --- a/lib/shoulda/matchers/active_model.rb +++ b/lib/shoulda/matchers/active_model.rb @@ -1,34 +1,9 @@ +require 'shoulda/matchers/active_model/allow_mass_assignment_of_matcher' require 'shoulda/matchers/active_model/helpers' require 'shoulda/matchers/active_model/qualifiers' require 'shoulda/matchers/active_model/validation_matcher' -require 'shoulda/matchers/active_model/validation_matcher/build_description' -require 'shoulda/matchers/active_model/validator' -require 'shoulda/matchers/active_model/allow_value_matcher' -require 'shoulda/matchers/active_model/allow_value_matcher/attribute_changed_value_error' -require 'shoulda/matchers/active_model/allow_value_matcher/attribute_does_not_exist_error' -require 'shoulda/matchers/active_model/allow_value_matcher/attribute_setter' -require 'shoulda/matchers/active_model/allow_value_matcher/attribute_setter_and_validator' -require 'shoulda/matchers/active_model/allow_value_matcher/attribute_setters' -require 'shoulda/matchers/active_model/allow_value_matcher/attribute_setters_and_validators' -require 'shoulda/matchers/active_model/allow_value_matcher/successful_check' -require 'shoulda/matchers/active_model/allow_value_matcher/successful_setting' -require 'shoulda/matchers/active_model/disallow_value_matcher' -require 'shoulda/matchers/active_model/validate_length_of_matcher' -require 'shoulda/matchers/active_model/validate_inclusion_of_matcher' -require 'shoulda/matchers/active_model/validate_exclusion_of_matcher' require 'shoulda/matchers/active_model/validate_absence_of_matcher' require 'shoulda/matchers/active_model/validate_presence_of_matcher' -require 'shoulda/matchers/active_model/validate_acceptance_of_matcher' -require 'shoulda/matchers/active_model/validate_confirmation_of_matcher' -require 'shoulda/matchers/active_model/validate_numericality_of_matcher' -require 'shoulda/matchers/active_model/numericality_matchers/numeric_type_matcher' -require 'shoulda/matchers/active_model/numericality_matchers/comparison_matcher' -require 'shoulda/matchers/active_model/numericality_matchers/odd_number_matcher' -require 'shoulda/matchers/active_model/numericality_matchers/even_number_matcher' -require 'shoulda/matchers/active_model/numericality_matchers/only_integer_matcher' -require 'shoulda/matchers/active_model/allow_mass_assignment_of_matcher' -require 'shoulda/matchers/active_model/errors' -require 'shoulda/matchers/active_model/have_secure_password_matcher' module Shoulda module Matchers @@ -89,6 +64,20 @@ module Matchers # end # module ActiveModel + autoload :AllowValueMatcher, 'shoulda/matchers/active_model/allow_value_matcher' + autoload :CouldNotDetermineValueOutsideOfArray, 'shoulda/matchers/active_model/errors' + autoload :CouldNotSetPasswordError, 'shoulda/matchers/active_model/errors' + autoload :DisallowValueMatcher, 'shoulda/matchers/active_model/disallow_value_matcher' + autoload :HaveSecurePasswordMatcher, 'shoulda/matchers/active_model/have_secure_password_matcher' + autoload :NonNullableBooleanError, 'shoulda/matchers/active_model/errors' + autoload :NumericalityMatchers, 'shoulda/matchers/active_model/numericality_matchers' + autoload :ValidateAcceptanceOfMatcher, 'shoulda/matchers/active_model/validate_acceptance_of_matcher' + autoload :ValidateConfirmationOfMatcher, 'shoulda/matchers/active_model/validate_confirmation_of_matcher' + autoload :ValidateExclusionOfMatcher, 'shoulda/matchers/active_model/validate_exclusion_of_matcher' + autoload :ValidateInclusionOfMatcher, 'shoulda/matchers/active_model/validate_inclusion_of_matcher' + autoload :ValidateLengthOfMatcher, 'shoulda/matchers/active_model/validate_length_of_matcher' + autoload :ValidateNumericalityOfMatcher, 'shoulda/matchers/active_model/validate_numericality_of_matcher' + autoload :Validator, 'shoulda/matchers/active_model/validator' end end end diff --git a/lib/shoulda/matchers/active_model/allow_value_matcher.rb b/lib/shoulda/matchers/active_model/allow_value_matcher.rb index bbf4b8fbe..2816725ed 100644 --- a/lib/shoulda/matchers/active_model/allow_value_matcher.rb +++ b/lib/shoulda/matchers/active_model/allow_value_matcher.rb @@ -305,6 +305,15 @@ def allow_value(*values) # @private class AllowValueMatcher + autoload :AttributeChangedValueError, 'shoulda/matchers/active_model/allow_value_matcher/attribute_changed_value_error' + autoload :AttributeDoesNotExistError, 'shoulda/matchers/active_model/allow_value_matcher/attribute_does_not_exist_error' + autoload :AttributeSetter, 'shoulda/matchers/active_model/allow_value_matcher/attribute_setter' + autoload :AttributeSetterAndValidator, 'shoulda/matchers/active_model/allow_value_matcher/attribute_setter_and_validator' + autoload :AttributeSetters, 'shoulda/matchers/active_model/allow_value_matcher/attribute_setters' + autoload :AttributeSettersAndValidators, 'shoulda/matchers/active_model/allow_value_matcher/attribute_setters_and_validators' + autoload :SuccessfulCheck, 'shoulda/matchers/active_model/allow_value_matcher/successful_check' + autoload :SuccessfulSetting, 'shoulda/matchers/active_model/allow_value_matcher/successful_setting' + include Helpers include Qualifiers::IgnoringInterferenceByWriter diff --git a/lib/shoulda/matchers/active_model/numericality_matchers.rb b/lib/shoulda/matchers/active_model/numericality_matchers.rb index fe649c66c..44b172f4a 100644 --- a/lib/shoulda/matchers/active_model/numericality_matchers.rb +++ b/lib/shoulda/matchers/active_model/numericality_matchers.rb @@ -3,6 +3,11 @@ module Matchers module ActiveModel # @private module NumericalityMatchers + autoload :ComparisonMatcher, 'shoulda/matchers/active_model/numericality_matchers/comparison_matcher' + autoload :EvenNumberMatcher, 'shoulda/matchers/active_model/numericality_matchers/even_number_matcher' + autoload :NumericTypeMatcher, 'shoulda/matchers/active_model/numericality_matchers/numeric_type_matcher' + autoload :OddNumberMatcher, 'shoulda/matchers/active_model/numericality_matchers/odd_number_matcher' + autoload :OnlyIntegerMatcher, 'shoulda/matchers/active_model/numericality_matchers/only_integer_matcher' end end end diff --git a/lib/shoulda/matchers/active_model/validation_matcher.rb b/lib/shoulda/matchers/active_model/validation_matcher.rb index e2a153c40..ad50839c0 100644 --- a/lib/shoulda/matchers/active_model/validation_matcher.rb +++ b/lib/shoulda/matchers/active_model/validation_matcher.rb @@ -3,6 +3,8 @@ module Matchers module ActiveModel # @private class ValidationMatcher + autoload :BuildDescription, 'shoulda/matchers/active_model/validation_matcher/build_description' + include Qualifiers::IgnoringInterferenceByWriter def initialize(attribute) diff --git a/lib/shoulda/matchers/active_record.rb b/lib/shoulda/matchers/active_record.rb index 9c97d7078..76d517fe5 100644 --- a/lib/shoulda/matchers/active_record.rb +++ b/lib/shoulda/matchers/active_record.rb @@ -1,35 +1,23 @@ -require "shoulda/matchers/active_record/association_matcher" -require "shoulda/matchers/active_record/association_matchers" -require "shoulda/matchers/active_record/association_matchers/counter_cache_matcher" -require "shoulda/matchers/active_record/association_matchers/inverse_of_matcher" -require "shoulda/matchers/active_record/association_matchers/join_table_matcher" -require "shoulda/matchers/active_record/association_matchers/order_matcher" -require "shoulda/matchers/active_record/association_matchers/through_matcher" -require "shoulda/matchers/active_record/association_matchers/dependent_matcher" -require "shoulda/matchers/active_record/association_matchers/required_matcher" -require "shoulda/matchers/active_record/association_matchers/optional_matcher" -require "shoulda/matchers/active_record/association_matchers/source_matcher" -require "shoulda/matchers/active_record/association_matchers/model_reflector" -require "shoulda/matchers/active_record/association_matchers/model_reflection" -require "shoulda/matchers/active_record/association_matchers/option_verifier" -require "shoulda/matchers/active_record/have_db_column_matcher" -require "shoulda/matchers/active_record/have_db_index_matcher" -require "shoulda/matchers/active_record/have_implicit_order_column" -require "shoulda/matchers/active_record/have_readonly_attribute_matcher" -require "shoulda/matchers/active_record/have_rich_text_matcher" -require "shoulda/matchers/active_record/have_secure_token_matcher" -require "shoulda/matchers/active_record/serialize_matcher" -require "shoulda/matchers/active_record/accept_nested_attributes_for_matcher" -require "shoulda/matchers/active_record/define_enum_for_matcher" -require "shoulda/matchers/active_record/uniqueness" require "shoulda/matchers/active_record/validate_uniqueness_of_matcher" -require "shoulda/matchers/active_record/have_attached_matcher" module Shoulda module Matchers # This module provides matchers that are used to test behavior within # ActiveRecord classes. module ActiveRecord + autoload :AcceptNestedAttributesForMatcher, 'shoulda/matchers/active_record/accept_nested_attributes_for_matcher' + autoload :AssociationMatcher, 'shoulda/matchers/active_record/association_matcher' + autoload :AssociationMatchers, 'shoulda/matchers/active_record/association_matchers' + autoload :DefineEnumForMatcher, 'shoulda/matchers/active_record/define_enum_for_matcher' + autoload :HaveAttachedMatcher, 'shoulda/matchers/active_record/have_attached_matcher' + autoload :HaveDbColumnMatcher, 'shoulda/matchers/active_record/have_db_column_matcher' + autoload :HaveDbIndexMatcher, 'shoulda/matchers/active_record/have_db_index_matcher' + autoload :HaveImplicitOrderColumnMatcher, 'shoulda/matchers/active_record/have_implicit_order_column' + autoload :HaveReadonlyAttributeMatcher, 'shoulda/matchers/active_record/have_readonly_attribute_matcher' + autoload :HaveRichText, 'shoulda/matchers/active_record/have_rich_text_matcher' + autoload :HaveSecureTokenMatcher, 'shoulda/matchers/active_record/have_secure_token_matcher' + autoload :SerializeMatcher, 'shoulda/matchers/active_record/serialize_matcher' + autoload :Uniqueness, 'shoulda/matchers/active_record/uniqueness' end end end diff --git a/lib/shoulda/matchers/active_record/association_matchers.rb b/lib/shoulda/matchers/active_record/association_matchers.rb index 802cb59bc..0d4c37a3d 100644 --- a/lib/shoulda/matchers/active_record/association_matchers.rb +++ b/lib/shoulda/matchers/active_record/association_matchers.rb @@ -3,6 +3,18 @@ module Matchers module ActiveRecord # @private module AssociationMatchers + autoload :CounterCacheMatcher, 'shoulda/matchers/active_record/association_matchers/counter_cache_matcher' + autoload :InverseOfMatcher, 'shoulda/matchers/active_record/association_matchers/inverse_of_matcher' + autoload :JoinTableMatcher, 'shoulda/matchers/active_record/association_matchers/join_table_matcher' + autoload :OrderMatcher, 'shoulda/matchers/active_record/association_matchers/order_matcher' + autoload :ThroughMatcher, 'shoulda/matchers/active_record/association_matchers/through_matcher' + autoload :DependentMatcher, 'shoulda/matchers/active_record/association_matchers/dependent_matcher' + autoload :RequiredMatcher, 'shoulda/matchers/active_record/association_matchers/required_matcher' + autoload :OptionalMatcher, 'shoulda/matchers/active_record/association_matchers/optional_matcher' + autoload :SourceMatcher, 'shoulda/matchers/active_record/association_matchers/source_matcher' + autoload :ModelReflector, 'shoulda/matchers/active_record/association_matchers/model_reflector' + autoload :ModelReflection, 'shoulda/matchers/active_record/association_matchers/model_reflection' + autoload :OptionVerifier, 'shoulda/matchers/active_record/association_matchers/option_verifier' end end end diff --git a/lib/shoulda/matchers/active_record/uniqueness.rb b/lib/shoulda/matchers/active_record/uniqueness.rb index db935631c..9202fbc69 100644 --- a/lib/shoulda/matchers/active_record/uniqueness.rb +++ b/lib/shoulda/matchers/active_record/uniqueness.rb @@ -1,14 +1,14 @@ module Shoulda module Matchers - module ActiveModel + module ActiveRecord # @private module Uniqueness + autoload :Model, 'shoulda/matchers/active_record/uniqueness/model' + autoload :Namespace, 'shoulda/matchers/active_record/uniqueness/namespace' + autoload :TestModelCreator, 'shoulda/matchers/active_record/uniqueness/test_model_creator' + autoload :TestModels, 'shoulda/matchers/active_record/uniqueness/test_models' end end end end -require 'shoulda/matchers/active_record/uniqueness/model' -require 'shoulda/matchers/active_record/uniqueness/namespace' -require 'shoulda/matchers/active_record/uniqueness/test_model_creator' -require 'shoulda/matchers/active_record/uniqueness/test_models' diff --git a/lib/shoulda/matchers/doublespeak.rb b/lib/shoulda/matchers/doublespeak.rb index e09b0082a..84f49ce3f 100644 --- a/lib/shoulda/matchers/doublespeak.rb +++ b/lib/shoulda/matchers/doublespeak.rb @@ -4,6 +4,14 @@ module Shoulda module Matchers # @private module Doublespeak + autoload :Double, 'shoulda/matchers/doublespeak/double' + autoload :DoubleCollection, 'shoulda/matchers/doublespeak/double_collection' + autoload :DoubleImplementationRegistry, 'shoulda/matchers/doublespeak/double_implementation_registry' + autoload :MethodCall, 'shoulda/matchers/doublespeak/method_call' + autoload :ObjectDouble, 'shoulda/matchers/doublespeak/object_double' + autoload :ProxyImplementation, 'shoulda/matchers/doublespeak/proxy_implementation' + autoload :World, 'shoulda/matchers/doublespeak/world' + class << self extend Forwardable @@ -28,11 +36,4 @@ def debug(&block) end end -require 'shoulda/matchers/doublespeak/double' -require 'shoulda/matchers/doublespeak/double_collection' -require 'shoulda/matchers/doublespeak/double_implementation_registry' -require 'shoulda/matchers/doublespeak/method_call' -require 'shoulda/matchers/doublespeak/object_double' -require 'shoulda/matchers/doublespeak/proxy_implementation' require 'shoulda/matchers/doublespeak/stub_implementation' -require 'shoulda/matchers/doublespeak/world' diff --git a/lib/shoulda/matchers/independent.rb b/lib/shoulda/matchers/independent.rb index 5ff4dca24..d7c40213f 100644 --- a/lib/shoulda/matchers/independent.rb +++ b/lib/shoulda/matchers/independent.rb @@ -1,5 +1,4 @@ require 'shoulda/matchers/independent/delegate_method_matcher' -require 'shoulda/matchers/independent/delegate_method_matcher/target_not_defined_error' module Shoulda module Matchers diff --git a/lib/shoulda/matchers/independent/delegate_method_matcher.rb b/lib/shoulda/matchers/independent/delegate_method_matcher.rb index 31fd52319..6d5866a32 100644 --- a/lib/shoulda/matchers/independent/delegate_method_matcher.rb +++ b/lib/shoulda/matchers/independent/delegate_method_matcher.rb @@ -176,6 +176,9 @@ def delegate_method(delegating_method) # @private class DelegateMethodMatcher + autoload :StubbedTarget, 'shoulda/matchers/independent/delegate_method_matcher/stubbed_target' + autoload :DelegateObjectNotSpecified, 'shoulda/matchers/independent/delegate_method_matcher/target_not_defined_error' + def initialize(delegating_method) @delegating_method = delegating_method diff --git a/lib/shoulda/matchers/integrations.rb b/lib/shoulda/matchers/integrations.rb index dc7ad4edd..b841d6b79 100644 --- a/lib/shoulda/matchers/integrations.rb +++ b/lib/shoulda/matchers/integrations.rb @@ -2,6 +2,12 @@ module Shoulda module Matchers # @private module Integrations + autoload :Configuration, 'shoulda/matchers/integrations/configuration' + autoload :ConfigurationError, 'shoulda/matchers/integrations/configuration_error' + autoload :Inclusion, 'shoulda/matchers/integrations/inclusion' + autoload :Rails, 'shoulda/matchers/integrations/rails' + autoload :Registry, 'shoulda/matchers/integrations/registry' + class << self def register_library(klass, name) library_registry.register(klass, name) @@ -33,11 +39,5 @@ def test_framework_registry end end -require 'shoulda/matchers/integrations/configuration' -require 'shoulda/matchers/integrations/configuration_error' -require 'shoulda/matchers/integrations/inclusion' -require 'shoulda/matchers/integrations/rails' -require 'shoulda/matchers/integrations/registry' - require 'shoulda/matchers/integrations/libraries' require 'shoulda/matchers/integrations/test_frameworks' diff --git a/lib/shoulda/matchers/integrations/test_frameworks.rb b/lib/shoulda/matchers/integrations/test_frameworks.rb index d7e800160..e62167b94 100644 --- a/lib/shoulda/matchers/integrations/test_frameworks.rb +++ b/lib/shoulda/matchers/integrations/test_frameworks.rb @@ -1,15 +1,17 @@ -require 'shoulda/matchers/integrations/test_frameworks/active_support_test_case' require 'shoulda/matchers/integrations/test_frameworks/minitest_4' require 'shoulda/matchers/integrations/test_frameworks/minitest_5' require 'shoulda/matchers/integrations/test_frameworks/missing_test_framework' require 'shoulda/matchers/integrations/test_frameworks/rspec' -require 'shoulda/matchers/integrations/test_frameworks/test_unit' module Shoulda module Matchers module Integrations # @private module TestFrameworks + autoload :ActiveSupportTestCase, 'shoulda/matchers/integrations/test_frameworks/active_support_test_case' + autoload :Minitest4, 'shoulda/matchers/integrations/test_frameworks/minitest_4' + autoload :Minitest5, 'shoulda/matchers/integrations/test_frameworks/minitest_5' + autoload :TestUnit, 'shoulda/matchers/integrations/test_frameworks/test_unit' end end end diff --git a/lib/shoulda/matchers/util.rb b/lib/shoulda/matchers/util.rb index 3f84fbbdf..acd1643d0 100644 --- a/lib/shoulda/matchers/util.rb +++ b/lib/shoulda/matchers/util.rb @@ -1,5 +1,3 @@ -require 'shoulda/matchers/util/word_wrap' - module Shoulda module Matchers # @private