omg-activesupport 8.0.0.alpha1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +86 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +40 -0
- data/lib/active_support/actionable_error.rb +50 -0
- data/lib/active_support/all.rb +5 -0
- data/lib/active_support/array_inquirer.rb +50 -0
- data/lib/active_support/backtrace_cleaner.rb +163 -0
- data/lib/active_support/benchmark.rb +21 -0
- data/lib/active_support/benchmarkable.rb +53 -0
- data/lib/active_support/broadcast_logger.rb +251 -0
- data/lib/active_support/builder.rb +8 -0
- data/lib/active_support/cache/coder.rb +153 -0
- data/lib/active_support/cache/entry.rb +134 -0
- data/lib/active_support/cache/file_store.rb +244 -0
- data/lib/active_support/cache/mem_cache_store.rb +290 -0
- data/lib/active_support/cache/memory_store.rb +262 -0
- data/lib/active_support/cache/null_store.rb +62 -0
- data/lib/active_support/cache/redis_cache_store.rb +492 -0
- data/lib/active_support/cache/serializer_with_fallback.rb +152 -0
- data/lib/active_support/cache/strategy/local_cache.rb +201 -0
- data/lib/active_support/cache/strategy/local_cache_middleware.rb +45 -0
- data/lib/active_support/cache.rb +1104 -0
- data/lib/active_support/callbacks.rb +944 -0
- data/lib/active_support/class_attribute.rb +26 -0
- data/lib/active_support/code_generator.rb +79 -0
- data/lib/active_support/concern.rb +217 -0
- data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +72 -0
- data/lib/active_support/concurrency/null_lock.rb +13 -0
- data/lib/active_support/concurrency/share_lock.rb +225 -0
- data/lib/active_support/configurable.rb +159 -0
- data/lib/active_support/configuration_file.rb +60 -0
- data/lib/active_support/core_ext/array/access.rb +100 -0
- data/lib/active_support/core_ext/array/conversions.rb +213 -0
- data/lib/active_support/core_ext/array/extract.rb +21 -0
- data/lib/active_support/core_ext/array/extract_options.rb +31 -0
- data/lib/active_support/core_ext/array/grouping.rb +109 -0
- data/lib/active_support/core_ext/array/inquiry.rb +19 -0
- data/lib/active_support/core_ext/array/wrap.rb +48 -0
- data/lib/active_support/core_ext/array.rb +9 -0
- data/lib/active_support/core_ext/benchmark.rb +13 -0
- data/lib/active_support/core_ext/big_decimal/conversions.rb +14 -0
- data/lib/active_support/core_ext/big_decimal.rb +3 -0
- data/lib/active_support/core_ext/class/attribute.rb +122 -0
- data/lib/active_support/core_ext/class/attribute_accessors.rb +6 -0
- data/lib/active_support/core_ext/class/subclasses.rb +24 -0
- data/lib/active_support/core_ext/class.rb +4 -0
- data/lib/active_support/core_ext/date/acts_like.rb +10 -0
- data/lib/active_support/core_ext/date/blank.rb +18 -0
- data/lib/active_support/core_ext/date/calculations.rb +161 -0
- data/lib/active_support/core_ext/date/conversions.rb +98 -0
- data/lib/active_support/core_ext/date/zones.rb +8 -0
- data/lib/active_support/core_ext/date.rb +7 -0
- data/lib/active_support/core_ext/date_and_time/calculations.rb +374 -0
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +58 -0
- data/lib/active_support/core_ext/date_and_time/zones.rb +40 -0
- data/lib/active_support/core_ext/date_time/acts_like.rb +16 -0
- data/lib/active_support/core_ext/date_time/blank.rb +18 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +215 -0
- data/lib/active_support/core_ext/date_time/compatibility.rb +18 -0
- data/lib/active_support/core_ext/date_time/conversions.rb +106 -0
- data/lib/active_support/core_ext/date_time.rb +7 -0
- data/lib/active_support/core_ext/digest/uuid.rb +76 -0
- data/lib/active_support/core_ext/digest.rb +3 -0
- data/lib/active_support/core_ext/enumerable.rb +267 -0
- data/lib/active_support/core_ext/erb/util.rb +201 -0
- data/lib/active_support/core_ext/file/atomic.rb +72 -0
- data/lib/active_support/core_ext/file.rb +3 -0
- data/lib/active_support/core_ext/hash/conversions.rb +262 -0
- data/lib/active_support/core_ext/hash/deep_merge.rb +42 -0
- data/lib/active_support/core_ext/hash/deep_transform_values.rb +46 -0
- data/lib/active_support/core_ext/hash/except.rb +12 -0
- data/lib/active_support/core_ext/hash/indifferent_access.rb +24 -0
- data/lib/active_support/core_ext/hash/keys.rb +143 -0
- data/lib/active_support/core_ext/hash/reverse_merge.rb +25 -0
- data/lib/active_support/core_ext/hash/slice.rb +27 -0
- data/lib/active_support/core_ext/hash.rb +10 -0
- data/lib/active_support/core_ext/integer/inflections.rb +31 -0
- data/lib/active_support/core_ext/integer/multiple.rb +12 -0
- data/lib/active_support/core_ext/integer/time.rb +22 -0
- data/lib/active_support/core_ext/integer.rb +5 -0
- data/lib/active_support/core_ext/kernel/concern.rb +14 -0
- data/lib/active_support/core_ext/kernel/reporting.rb +45 -0
- data/lib/active_support/core_ext/kernel/singleton_class.rb +8 -0
- data/lib/active_support/core_ext/kernel.rb +5 -0
- data/lib/active_support/core_ext/load_error.rb +9 -0
- data/lib/active_support/core_ext/module/aliasing.rb +31 -0
- data/lib/active_support/core_ext/module/anonymous.rb +30 -0
- data/lib/active_support/core_ext/module/attr_internal.rb +49 -0
- data/lib/active_support/core_ext/module/attribute_accessors.rb +214 -0
- data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +175 -0
- data/lib/active_support/core_ext/module/concerning.rb +140 -0
- data/lib/active_support/core_ext/module/delegation.rb +225 -0
- data/lib/active_support/core_ext/module/deprecation.rb +25 -0
- data/lib/active_support/core_ext/module/introspection.rb +62 -0
- data/lib/active_support/core_ext/module/redefine_method.rb +40 -0
- data/lib/active_support/core_ext/module/remove_method.rb +17 -0
- data/lib/active_support/core_ext/module.rb +13 -0
- data/lib/active_support/core_ext/name_error.rb +59 -0
- data/lib/active_support/core_ext/numeric/bytes.rb +75 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +145 -0
- data/lib/active_support/core_ext/numeric/time.rb +66 -0
- data/lib/active_support/core_ext/numeric.rb +5 -0
- data/lib/active_support/core_ext/object/acts_like.rb +45 -0
- data/lib/active_support/core_ext/object/blank.rb +199 -0
- data/lib/active_support/core_ext/object/conversions.rb +6 -0
- data/lib/active_support/core_ext/object/deep_dup.rb +71 -0
- data/lib/active_support/core_ext/object/duplicable.rb +69 -0
- data/lib/active_support/core_ext/object/inclusion.rb +37 -0
- data/lib/active_support/core_ext/object/instance_variables.rb +32 -0
- data/lib/active_support/core_ext/object/json.rb +260 -0
- data/lib/active_support/core_ext/object/to_param.rb +3 -0
- data/lib/active_support/core_ext/object/to_query.rb +87 -0
- data/lib/active_support/core_ext/object/try.rb +158 -0
- data/lib/active_support/core_ext/object/with.rb +46 -0
- data/lib/active_support/core_ext/object/with_options.rb +101 -0
- data/lib/active_support/core_ext/object.rb +17 -0
- data/lib/active_support/core_ext/pathname/blank.rb +20 -0
- data/lib/active_support/core_ext/pathname/existence.rb +23 -0
- data/lib/active_support/core_ext/pathname.rb +4 -0
- data/lib/active_support/core_ext/range/compare_range.rb +57 -0
- data/lib/active_support/core_ext/range/conversions.rb +62 -0
- data/lib/active_support/core_ext/range/each.rb +24 -0
- data/lib/active_support/core_ext/range/overlap.rb +40 -0
- data/lib/active_support/core_ext/range.rb +6 -0
- data/lib/active_support/core_ext/regexp.rb +14 -0
- data/lib/active_support/core_ext/securerandom.rb +41 -0
- data/lib/active_support/core_ext/string/access.rb +95 -0
- data/lib/active_support/core_ext/string/behavior.rb +8 -0
- data/lib/active_support/core_ext/string/conversions.rb +60 -0
- data/lib/active_support/core_ext/string/exclude.rb +13 -0
- data/lib/active_support/core_ext/string/filters.rb +151 -0
- data/lib/active_support/core_ext/string/indent.rb +45 -0
- data/lib/active_support/core_ext/string/inflections.rb +300 -0
- data/lib/active_support/core_ext/string/inquiry.rb +16 -0
- data/lib/active_support/core_ext/string/multibyte.rb +58 -0
- data/lib/active_support/core_ext/string/output_safety.rb +228 -0
- data/lib/active_support/core_ext/string/starts_ends_with.rb +6 -0
- data/lib/active_support/core_ext/string/strip.rb +27 -0
- data/lib/active_support/core_ext/string/zones.rb +16 -0
- data/lib/active_support/core_ext/string.rb +15 -0
- data/lib/active_support/core_ext/symbol/starts_ends_with.rb +6 -0
- data/lib/active_support/core_ext/symbol.rb +3 -0
- data/lib/active_support/core_ext/thread/backtrace/location.rb +12 -0
- data/lib/active_support/core_ext/time/acts_like.rb +10 -0
- data/lib/active_support/core_ext/time/calculations.rb +386 -0
- data/lib/active_support/core_ext/time/compatibility.rb +32 -0
- data/lib/active_support/core_ext/time/conversions.rb +75 -0
- data/lib/active_support/core_ext/time/zones.rb +97 -0
- data/lib/active_support/core_ext/time.rb +7 -0
- data/lib/active_support/core_ext.rb +5 -0
- data/lib/active_support/current_attributes/test_helper.rb +13 -0
- data/lib/active_support/current_attributes.rb +233 -0
- data/lib/active_support/deep_mergeable.rb +53 -0
- data/lib/active_support/delegation.rb +202 -0
- data/lib/active_support/dependencies/autoload.rb +72 -0
- data/lib/active_support/dependencies/interlock.rb +49 -0
- data/lib/active_support/dependencies/require_dependency.rb +28 -0
- data/lib/active_support/dependencies.rb +98 -0
- data/lib/active_support/deprecation/behaviors.rb +148 -0
- data/lib/active_support/deprecation/constant_accessor.rb +74 -0
- data/lib/active_support/deprecation/deprecators.rb +104 -0
- data/lib/active_support/deprecation/disallowed.rb +54 -0
- data/lib/active_support/deprecation/method_wrappers.rb +68 -0
- data/lib/active_support/deprecation/proxy_wrappers.rb +189 -0
- data/lib/active_support/deprecation/reporting.rb +179 -0
- data/lib/active_support/deprecation.rb +81 -0
- data/lib/active_support/deprecator.rb +7 -0
- data/lib/active_support/descendants_tracker.rb +112 -0
- data/lib/active_support/digest.rb +22 -0
- data/lib/active_support/duration/iso8601_parser.rb +123 -0
- data/lib/active_support/duration/iso8601_serializer.rb +64 -0
- data/lib/active_support/duration.rb +520 -0
- data/lib/active_support/encrypted_configuration.rb +126 -0
- data/lib/active_support/encrypted_file.rb +133 -0
- data/lib/active_support/environment_inquirer.rb +40 -0
- data/lib/active_support/error_reporter/test_helper.rb +15 -0
- data/lib/active_support/error_reporter.rb +265 -0
- data/lib/active_support/evented_file_update_checker.rb +182 -0
- data/lib/active_support/execution_context/test_helper.rb +13 -0
- data/lib/active_support/execution_context.rb +53 -0
- data/lib/active_support/execution_wrapper.rb +150 -0
- data/lib/active_support/executor/test_helper.rb +7 -0
- data/lib/active_support/executor.rb +8 -0
- data/lib/active_support/file_update_checker.rb +164 -0
- data/lib/active_support/fork_tracker.rb +43 -0
- data/lib/active_support/gem_version.rb +17 -0
- data/lib/active_support/gzip.rb +40 -0
- data/lib/active_support/hash_with_indifferent_access.rb +445 -0
- data/lib/active_support/html_safe_translation.rb +56 -0
- data/lib/active_support/i18n.rb +17 -0
- data/lib/active_support/i18n_railtie.rb +138 -0
- data/lib/active_support/inflections.rb +72 -0
- data/lib/active_support/inflector/inflections.rb +273 -0
- data/lib/active_support/inflector/methods.rb +387 -0
- data/lib/active_support/inflector/transliterate.rb +149 -0
- data/lib/active_support/inflector.rb +9 -0
- data/lib/active_support/isolated_execution_state.rb +75 -0
- data/lib/active_support/json/decoding.rb +76 -0
- data/lib/active_support/json/encoding.rb +120 -0
- data/lib/active_support/json.rb +4 -0
- data/lib/active_support/key_generator.rb +66 -0
- data/lib/active_support/lazy_load_hooks.rb +107 -0
- data/lib/active_support/locale/en.rb +33 -0
- data/lib/active_support/locale/en.yml +141 -0
- data/lib/active_support/log_subscriber/test_helper.rb +106 -0
- data/lib/active_support/log_subscriber.rb +192 -0
- data/lib/active_support/logger.rb +55 -0
- data/lib/active_support/logger_silence.rb +21 -0
- data/lib/active_support/logger_thread_safe_level.rb +47 -0
- data/lib/active_support/message_encryptor.rb +374 -0
- data/lib/active_support/message_encryptors.rb +141 -0
- data/lib/active_support/message_pack/cache_serializer.rb +23 -0
- data/lib/active_support/message_pack/extensions.rb +305 -0
- data/lib/active_support/message_pack/serializer.rb +63 -0
- data/lib/active_support/message_pack.rb +50 -0
- data/lib/active_support/message_verifier.rb +368 -0
- data/lib/active_support/message_verifiers.rb +135 -0
- data/lib/active_support/messages/codec.rb +65 -0
- data/lib/active_support/messages/metadata.rb +146 -0
- data/lib/active_support/messages/rotation_configuration.rb +23 -0
- data/lib/active_support/messages/rotation_coordinator.rb +93 -0
- data/lib/active_support/messages/rotator.rb +59 -0
- data/lib/active_support/messages/serializer_with_fallback.rb +158 -0
- data/lib/active_support/multibyte/chars.rb +178 -0
- data/lib/active_support/multibyte/unicode.rb +42 -0
- data/lib/active_support/multibyte.rb +23 -0
- data/lib/active_support/notifications/fanout.rb +446 -0
- data/lib/active_support/notifications/instrumenter.rb +240 -0
- data/lib/active_support/notifications.rb +281 -0
- data/lib/active_support/number_helper/number_converter.rb +190 -0
- data/lib/active_support/number_helper/number_to_currency_converter.rb +46 -0
- data/lib/active_support/number_helper/number_to_delimited_converter.rb +30 -0
- data/lib/active_support/number_helper/number_to_human_converter.rb +69 -0
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +60 -0
- data/lib/active_support/number_helper/number_to_percentage_converter.rb +16 -0
- data/lib/active_support/number_helper/number_to_phone_converter.rb +60 -0
- data/lib/active_support/number_helper/number_to_rounded_converter.rb +59 -0
- data/lib/active_support/number_helper/rounding_helper.rb +46 -0
- data/lib/active_support/number_helper.rb +479 -0
- data/lib/active_support/option_merger.rb +38 -0
- data/lib/active_support/ordered_hash.rb +50 -0
- data/lib/active_support/ordered_options.rb +147 -0
- data/lib/active_support/parameter_filter.rb +157 -0
- data/lib/active_support/proxy_object.rb +20 -0
- data/lib/active_support/rails.rb +26 -0
- data/lib/active_support/railtie.rb +161 -0
- data/lib/active_support/reloader.rb +138 -0
- data/lib/active_support/rescuable.rb +176 -0
- data/lib/active_support/secure_compare_rotator.rb +58 -0
- data/lib/active_support/security_utils.rb +38 -0
- data/lib/active_support/string_inquirer.rb +35 -0
- data/lib/active_support/subscriber.rb +146 -0
- data/lib/active_support/syntax_error_proxy.rb +60 -0
- data/lib/active_support/tagged_logging.rb +152 -0
- data/lib/active_support/test_case.rb +304 -0
- data/lib/active_support/testing/assertions.rb +332 -0
- data/lib/active_support/testing/autorun.rb +5 -0
- data/lib/active_support/testing/constant_lookup.rb +51 -0
- data/lib/active_support/testing/constant_stubbing.rb +54 -0
- data/lib/active_support/testing/declarative.rb +28 -0
- data/lib/active_support/testing/deprecation.rb +82 -0
- data/lib/active_support/testing/error_reporter_assertions.rb +107 -0
- data/lib/active_support/testing/file_fixtures.rb +38 -0
- data/lib/active_support/testing/isolation.rb +121 -0
- data/lib/active_support/testing/method_call_assertions.rb +69 -0
- data/lib/active_support/testing/parallelization/server.rb +85 -0
- data/lib/active_support/testing/parallelization/worker.rb +103 -0
- data/lib/active_support/testing/parallelization.rb +55 -0
- data/lib/active_support/testing/parallelize_executor.rb +81 -0
- data/lib/active_support/testing/setup_and_teardown.rb +57 -0
- data/lib/active_support/testing/stream.rb +41 -0
- data/lib/active_support/testing/strict_warnings.rb +43 -0
- data/lib/active_support/testing/tagged_logging.rb +27 -0
- data/lib/active_support/testing/tests_without_assertions.rb +19 -0
- data/lib/active_support/testing/time_helpers.rb +269 -0
- data/lib/active_support/time.rb +20 -0
- data/lib/active_support/time_with_zone.rb +609 -0
- data/lib/active_support/values/time_zone.rb +614 -0
- data/lib/active_support/version.rb +10 -0
- data/lib/active_support/xml_mini/jdom.rb +175 -0
- data/lib/active_support/xml_mini/libxml.rb +80 -0
- data/lib/active_support/xml_mini/libxmlsax.rb +83 -0
- data/lib/active_support/xml_mini/nokogiri.rb +83 -0
- data/lib/active_support/xml_mini/nokogirisax.rb +86 -0
- data/lib/active_support/xml_mini/rexml.rb +137 -0
- data/lib/active_support/xml_mini.rb +211 -0
- data/lib/active_support.rb +144 -0
- metadata +526 -0
@@ -0,0 +1,304 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "minitest"
|
4
|
+
require "active_support/testing/tagged_logging"
|
5
|
+
require "active_support/testing/setup_and_teardown"
|
6
|
+
require "active_support/testing/tests_without_assertions"
|
7
|
+
require "active_support/testing/assertions"
|
8
|
+
require "active_support/testing/error_reporter_assertions"
|
9
|
+
require "active_support/testing/deprecation"
|
10
|
+
require "active_support/testing/declarative"
|
11
|
+
require "active_support/testing/isolation"
|
12
|
+
require "active_support/testing/constant_lookup"
|
13
|
+
require "active_support/testing/time_helpers"
|
14
|
+
require "active_support/testing/constant_stubbing"
|
15
|
+
require "active_support/testing/file_fixtures"
|
16
|
+
require "active_support/testing/parallelization"
|
17
|
+
require "active_support/testing/parallelize_executor"
|
18
|
+
require "concurrent/utility/processor_counter"
|
19
|
+
|
20
|
+
module ActiveSupport
|
21
|
+
class TestCase < ::Minitest::Test
|
22
|
+
Assertion = Minitest::Assertion
|
23
|
+
|
24
|
+
class << self
|
25
|
+
# Sets the order in which test cases are run.
|
26
|
+
#
|
27
|
+
# ActiveSupport::TestCase.test_order = :random # => :random
|
28
|
+
#
|
29
|
+
# Valid values are:
|
30
|
+
# * +:random+ (to run tests in random order)
|
31
|
+
# * +:parallel+ (to run tests in parallel)
|
32
|
+
# * +:sorted+ (to run tests alphabetically by method name)
|
33
|
+
# * +:alpha+ (equivalent to +:sorted+)
|
34
|
+
def test_order=(new_order)
|
35
|
+
ActiveSupport.test_order = new_order
|
36
|
+
end
|
37
|
+
|
38
|
+
# Returns the order in which test cases are run.
|
39
|
+
#
|
40
|
+
# ActiveSupport::TestCase.test_order # => :random
|
41
|
+
#
|
42
|
+
# Possible values are +:random+, +:parallel+, +:alpha+, +:sorted+.
|
43
|
+
# Defaults to +:random+.
|
44
|
+
def test_order
|
45
|
+
ActiveSupport.test_order ||= :random
|
46
|
+
end
|
47
|
+
|
48
|
+
# Parallelizes the test suite.
|
49
|
+
#
|
50
|
+
# Takes a +workers+ argument that controls how many times the process
|
51
|
+
# is forked. For each process a new database will be created suffixed
|
52
|
+
# with the worker number.
|
53
|
+
#
|
54
|
+
# test-database-0
|
55
|
+
# test-database-1
|
56
|
+
#
|
57
|
+
# If <tt>ENV["PARALLEL_WORKERS"]</tt> is set the workers argument will be ignored
|
58
|
+
# and the environment variable will be used instead. This is useful for CI
|
59
|
+
# environments, or other environments where you may need more workers than
|
60
|
+
# you do for local testing.
|
61
|
+
#
|
62
|
+
# If the number of workers is set to +1+ or fewer, the tests will not be
|
63
|
+
# parallelized.
|
64
|
+
#
|
65
|
+
# If +workers+ is set to +:number_of_processors+, the number of workers will be
|
66
|
+
# set to the actual core count on the machine you are on.
|
67
|
+
#
|
68
|
+
# The default parallelization method is to fork processes. If you'd like to
|
69
|
+
# use threads instead you can pass <tt>with: :threads</tt> to the +parallelize+
|
70
|
+
# method. Note the threaded parallelization does not create multiple
|
71
|
+
# databases and will not work with system tests.
|
72
|
+
#
|
73
|
+
# parallelize(workers: :number_of_processors, with: :threads)
|
74
|
+
#
|
75
|
+
# The threaded parallelization uses minitest's parallel executor directly.
|
76
|
+
# The processes parallelization uses a Ruby DRb server.
|
77
|
+
#
|
78
|
+
# Because parallelization presents an overhead, it is only enabled when the
|
79
|
+
# number of tests to run is above the +threshold+ param. The default value is
|
80
|
+
# 50, and it's configurable via +config.active_support.test_parallelization_threshold+.
|
81
|
+
def parallelize(workers: :number_of_processors, with: :processes, threshold: ActiveSupport.test_parallelization_threshold)
|
82
|
+
workers = Concurrent.processor_count if workers == :number_of_processors
|
83
|
+
workers = ENV["PARALLEL_WORKERS"].to_i if ENV["PARALLEL_WORKERS"]
|
84
|
+
|
85
|
+
Minitest.parallel_executor = ActiveSupport::Testing::ParallelizeExecutor.new(size: workers, with: with, threshold: threshold)
|
86
|
+
end
|
87
|
+
|
88
|
+
# Set up hook for parallel testing. This can be used if you have multiple
|
89
|
+
# databases or any behavior that needs to be run after the process is forked
|
90
|
+
# but before the tests run.
|
91
|
+
#
|
92
|
+
# Note: this feature is not available with the threaded parallelization.
|
93
|
+
#
|
94
|
+
# In your +test_helper.rb+ add the following:
|
95
|
+
#
|
96
|
+
# class ActiveSupport::TestCase
|
97
|
+
# parallelize_setup do
|
98
|
+
# # create databases
|
99
|
+
# end
|
100
|
+
# end
|
101
|
+
def parallelize_setup(&block)
|
102
|
+
ActiveSupport::Testing::Parallelization.after_fork_hook(&block)
|
103
|
+
end
|
104
|
+
|
105
|
+
# Clean up hook for parallel testing. This can be used to drop databases
|
106
|
+
# if your app uses multiple write/read databases or other clean up before
|
107
|
+
# the tests finish. This runs before the forked process is closed.
|
108
|
+
#
|
109
|
+
# Note: this feature is not available with the threaded parallelization.
|
110
|
+
#
|
111
|
+
# In your +test_helper.rb+ add the following:
|
112
|
+
#
|
113
|
+
# class ActiveSupport::TestCase
|
114
|
+
# parallelize_teardown do
|
115
|
+
# # drop databases
|
116
|
+
# end
|
117
|
+
# end
|
118
|
+
def parallelize_teardown(&block)
|
119
|
+
ActiveSupport::Testing::Parallelization.run_cleanup_hook(&block)
|
120
|
+
end
|
121
|
+
|
122
|
+
# :singleton-method: fixture_paths
|
123
|
+
#
|
124
|
+
# Returns the ActiveRecord::FixtureSet collection.
|
125
|
+
#
|
126
|
+
# In your +test_helper.rb+ you must have <tt>require "rails/test_help"</tt>.
|
127
|
+
|
128
|
+
# :singleton-method: fixture_paths=
|
129
|
+
#
|
130
|
+
# :call-seq:
|
131
|
+
# fixture_paths=(fixture_paths)
|
132
|
+
#
|
133
|
+
# Sets the given path to the fixture set.
|
134
|
+
#
|
135
|
+
# Can also append multiple paths.
|
136
|
+
#
|
137
|
+
# ActiveSupport::TestCase.fixture_paths << "component1/test/fixtures"
|
138
|
+
#
|
139
|
+
# In your +test_helper.rb+ you must have <tt>require "rails/test_help"</tt>.
|
140
|
+
end
|
141
|
+
|
142
|
+
alias_method :method_name, :name
|
143
|
+
|
144
|
+
include ActiveSupport::Testing::TaggedLogging
|
145
|
+
prepend ActiveSupport::Testing::SetupAndTeardown
|
146
|
+
prepend ActiveSupport::Testing::TestsWithoutAssertions
|
147
|
+
include ActiveSupport::Testing::Assertions
|
148
|
+
include ActiveSupport::Testing::ErrorReporterAssertions
|
149
|
+
include ActiveSupport::Testing::Deprecation
|
150
|
+
include ActiveSupport::Testing::ConstantStubbing
|
151
|
+
include ActiveSupport::Testing::TimeHelpers
|
152
|
+
include ActiveSupport::Testing::FileFixtures
|
153
|
+
extend ActiveSupport::Testing::Declarative
|
154
|
+
|
155
|
+
##
|
156
|
+
# :method: assert_not_empty
|
157
|
+
#
|
158
|
+
# :call-seq:
|
159
|
+
# assert_not_empty(obj, msg = nil)
|
160
|
+
#
|
161
|
+
# Alias for: refute_empty[https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-refute_empty]
|
162
|
+
|
163
|
+
#
|
164
|
+
alias :assert_not_empty :refute_empty
|
165
|
+
|
166
|
+
##
|
167
|
+
# :method: assert_not_equal
|
168
|
+
#
|
169
|
+
# :call-seq:
|
170
|
+
# assert_not_equal(exp, act, msg = nil)
|
171
|
+
#
|
172
|
+
# Alias for: refute_equal[https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-refute_equal]
|
173
|
+
|
174
|
+
#
|
175
|
+
alias :assert_not_equal :refute_equal
|
176
|
+
|
177
|
+
##
|
178
|
+
# :method: assert_not_in_delta
|
179
|
+
#
|
180
|
+
# :call-seq:
|
181
|
+
# assert_not_in_delta(exp, act, delta = 0.001, msg = nil)
|
182
|
+
#
|
183
|
+
# Alias for: refute_in_delta[https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-refute_in_delta]
|
184
|
+
|
185
|
+
#
|
186
|
+
alias :assert_not_in_delta :refute_in_delta
|
187
|
+
|
188
|
+
##
|
189
|
+
# :method: assert_not_in_epsilon
|
190
|
+
#
|
191
|
+
# :call-seq:
|
192
|
+
# assert_not_in_epsilon(a, b, epsilon = 0.001, msg = nil)
|
193
|
+
#
|
194
|
+
# Alias for: refute_in_epsilon[https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-refute_in_epsilon]
|
195
|
+
|
196
|
+
#
|
197
|
+
alias :assert_not_in_epsilon :refute_in_epsilon
|
198
|
+
|
199
|
+
##
|
200
|
+
# :method: assert_not_includes
|
201
|
+
#
|
202
|
+
# :call-seq:
|
203
|
+
# assert_not_includes(collection, obj, msg = nil)
|
204
|
+
#
|
205
|
+
# Alias for: refute_includes[https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-refute_includes]
|
206
|
+
|
207
|
+
#
|
208
|
+
alias :assert_not_includes :refute_includes
|
209
|
+
|
210
|
+
##
|
211
|
+
# :method: assert_not_instance_of
|
212
|
+
#
|
213
|
+
# :call-seq:
|
214
|
+
# assert_not_instance_of(cls, obj, msg = nil)
|
215
|
+
#
|
216
|
+
# Alias for: refute_instance_of[https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-refute_instance_of]
|
217
|
+
|
218
|
+
#
|
219
|
+
alias :assert_not_instance_of :refute_instance_of
|
220
|
+
|
221
|
+
##
|
222
|
+
# :method: assert_not_kind_of
|
223
|
+
#
|
224
|
+
# :call-seq:
|
225
|
+
# assert_not_kind_of(cls, obj, msg = nil)
|
226
|
+
#
|
227
|
+
# Alias for: refute_kind_of[https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-refute_kind_of]
|
228
|
+
|
229
|
+
#
|
230
|
+
alias :assert_not_kind_of :refute_kind_of
|
231
|
+
|
232
|
+
##
|
233
|
+
# :method: assert_no_match
|
234
|
+
#
|
235
|
+
# :call-seq:
|
236
|
+
# assert_no_match(matcher, obj, msg = nil)
|
237
|
+
#
|
238
|
+
# Alias for: refute_match[https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-refute_match]
|
239
|
+
|
240
|
+
#
|
241
|
+
alias :assert_no_match :refute_match
|
242
|
+
|
243
|
+
##
|
244
|
+
# :method: assert_not_nil
|
245
|
+
#
|
246
|
+
# :call-seq:
|
247
|
+
# assert_not_nil(obj, msg = nil)
|
248
|
+
#
|
249
|
+
# Alias for: refute_nil[https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-refute_nil]
|
250
|
+
|
251
|
+
#
|
252
|
+
alias :assert_not_nil :refute_nil
|
253
|
+
|
254
|
+
##
|
255
|
+
# :method: assert_not_operator
|
256
|
+
#
|
257
|
+
# :call-seq:
|
258
|
+
# assert_not_operator(o1, op, o2 = UNDEFINED, msg = nil)
|
259
|
+
#
|
260
|
+
# Alias for: refute_operator[https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-refute_operator]
|
261
|
+
|
262
|
+
#
|
263
|
+
alias :assert_not_operator :refute_operator
|
264
|
+
|
265
|
+
##
|
266
|
+
# :method: assert_not_predicate
|
267
|
+
#
|
268
|
+
# :call-seq:
|
269
|
+
# assert_not_predicate(o1, op, msg = nil)
|
270
|
+
#
|
271
|
+
# Alias for: refute_predicate[https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-refute_predicate]
|
272
|
+
|
273
|
+
#
|
274
|
+
alias :assert_not_predicate :refute_predicate
|
275
|
+
|
276
|
+
##
|
277
|
+
# :method: assert_not_respond_to
|
278
|
+
#
|
279
|
+
# :call-seq:
|
280
|
+
# assert_not_respond_to(obj, meth, msg = nil)
|
281
|
+
#
|
282
|
+
# Alias for: refute_respond_to[https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-refute_respond_to]
|
283
|
+
|
284
|
+
#
|
285
|
+
alias :assert_not_respond_to :refute_respond_to
|
286
|
+
|
287
|
+
##
|
288
|
+
# :method: assert_not_same
|
289
|
+
#
|
290
|
+
# :call-seq:
|
291
|
+
# assert_not_same(exp, act, msg = nil)
|
292
|
+
#
|
293
|
+
# Alias for: refute_same[https://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-refute_same]
|
294
|
+
|
295
|
+
#
|
296
|
+
alias :assert_not_same :refute_same
|
297
|
+
|
298
|
+
ActiveSupport.run_load_hooks(:active_support_test_case, self)
|
299
|
+
|
300
|
+
def inspect # :nodoc:
|
301
|
+
Object.instance_method(:to_s).bind_call(self)
|
302
|
+
end
|
303
|
+
end
|
304
|
+
end
|
@@ -0,0 +1,332 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/enumerable"
|
4
|
+
|
5
|
+
module ActiveSupport
|
6
|
+
module Testing
|
7
|
+
module Assertions
|
8
|
+
UNTRACKED = Object.new # :nodoc:
|
9
|
+
|
10
|
+
# Asserts that an expression is not truthy. Passes if +object+ is +nil+ or
|
11
|
+
# +false+. "Truthy" means "considered true in a conditional" like <tt>if
|
12
|
+
# foo</tt>.
|
13
|
+
#
|
14
|
+
# assert_not nil # => true
|
15
|
+
# assert_not false # => true
|
16
|
+
# assert_not 'foo' # => Expected "foo" to be nil or false
|
17
|
+
#
|
18
|
+
# An error message can be specified.
|
19
|
+
#
|
20
|
+
# assert_not foo, 'foo should be false'
|
21
|
+
def assert_not(object, message = nil)
|
22
|
+
message ||= -> { "Expected #{mu_pp(object)} to be nil or false" }
|
23
|
+
assert !object, message
|
24
|
+
end
|
25
|
+
|
26
|
+
# Asserts that a block raises one of +exp+. This is an enhancement of the
|
27
|
+
# standard Minitest assertion method with the ability to test error
|
28
|
+
# messages.
|
29
|
+
#
|
30
|
+
# assert_raises(ArgumentError, match: /incorrect param/i) do
|
31
|
+
# perform_service(param: 'exception')
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
def assert_raises(*exp, match: nil, &block)
|
35
|
+
error = super(*exp, &block)
|
36
|
+
assert_match(match, error.message) if match
|
37
|
+
error
|
38
|
+
end
|
39
|
+
alias :assert_raise :assert_raises
|
40
|
+
|
41
|
+
# Assertion that the block should not raise an exception.
|
42
|
+
#
|
43
|
+
# Passes if evaluated code in the yielded block raises no exception.
|
44
|
+
#
|
45
|
+
# assert_nothing_raised do
|
46
|
+
# perform_service(param: 'no_exception')
|
47
|
+
# end
|
48
|
+
def assert_nothing_raised
|
49
|
+
yield.tap { assert(true) }
|
50
|
+
rescue => error
|
51
|
+
raise Minitest::UnexpectedError.new(error)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Test numeric difference between the return value of an expression as a
|
55
|
+
# result of what is evaluated in the yielded block.
|
56
|
+
#
|
57
|
+
# assert_difference 'Article.count' do
|
58
|
+
# post :create, params: { article: {...} }
|
59
|
+
# end
|
60
|
+
#
|
61
|
+
# An arbitrary expression is passed in and evaluated.
|
62
|
+
#
|
63
|
+
# assert_difference 'Article.last.comments(:reload).size' do
|
64
|
+
# post :create, params: { comment: {...} }
|
65
|
+
# end
|
66
|
+
#
|
67
|
+
# An arbitrary positive or negative difference can be specified.
|
68
|
+
# The default is +1+.
|
69
|
+
#
|
70
|
+
# assert_difference 'Article.count', -1 do
|
71
|
+
# post :delete, params: { id: ... }
|
72
|
+
# end
|
73
|
+
#
|
74
|
+
# An array of expressions can also be passed in and evaluated.
|
75
|
+
#
|
76
|
+
# assert_difference [ 'Article.count', 'Post.count' ], 2 do
|
77
|
+
# post :create, params: { article: {...} }
|
78
|
+
# end
|
79
|
+
#
|
80
|
+
# A hash of expressions/numeric differences can also be passed in and evaluated.
|
81
|
+
#
|
82
|
+
# assert_difference ->{ Article.count } => 1, ->{ Notification.count } => 2 do
|
83
|
+
# post :create, params: { article: {...} }
|
84
|
+
# end
|
85
|
+
#
|
86
|
+
# A lambda or a list of lambdas can be passed in and evaluated:
|
87
|
+
#
|
88
|
+
# assert_difference ->{ Article.count }, 2 do
|
89
|
+
# post :create, params: { article: {...} }
|
90
|
+
# end
|
91
|
+
#
|
92
|
+
# assert_difference [->{ Article.count }, ->{ Post.count }], 2 do
|
93
|
+
# post :create, params: { article: {...} }
|
94
|
+
# end
|
95
|
+
#
|
96
|
+
# An error message can be specified.
|
97
|
+
#
|
98
|
+
# assert_difference 'Article.count', -1, 'An Article should be destroyed' do
|
99
|
+
# post :delete, params: { id: ... }
|
100
|
+
# end
|
101
|
+
def assert_difference(expression, *args, &block)
|
102
|
+
expressions =
|
103
|
+
if expression.is_a?(Hash)
|
104
|
+
message = args[0]
|
105
|
+
expression
|
106
|
+
else
|
107
|
+
difference = args[0] || 1
|
108
|
+
message = args[1]
|
109
|
+
Array(expression).index_with(difference)
|
110
|
+
end
|
111
|
+
|
112
|
+
exps = expressions.keys.map { |e|
|
113
|
+
e.respond_to?(:call) ? e : lambda { eval(e, block.binding) }
|
114
|
+
}
|
115
|
+
before = exps.map(&:call)
|
116
|
+
|
117
|
+
retval = _assert_nothing_raised_or_warn("assert_difference", &block)
|
118
|
+
|
119
|
+
expressions.zip(exps, before) do |(code, diff), exp, before_value|
|
120
|
+
actual = exp.call
|
121
|
+
rich_message = -> do
|
122
|
+
code_string = code.respond_to?(:call) ? _callable_to_source_string(code) : code
|
123
|
+
error = "`#{code_string}` didn't change by #{diff}, but by #{actual - before_value}"
|
124
|
+
error = "#{message}.\n#{error}" if message
|
125
|
+
error
|
126
|
+
end
|
127
|
+
assert_equal(before_value + diff, actual, rich_message)
|
128
|
+
end
|
129
|
+
|
130
|
+
retval
|
131
|
+
end
|
132
|
+
|
133
|
+
# Assertion that the numeric result of evaluating an expression is not
|
134
|
+
# changed before and after invoking the passed in block.
|
135
|
+
#
|
136
|
+
# assert_no_difference 'Article.count' do
|
137
|
+
# post :create, params: { article: invalid_attributes }
|
138
|
+
# end
|
139
|
+
#
|
140
|
+
# A lambda can be passed in and evaluated.
|
141
|
+
#
|
142
|
+
# assert_no_difference -> { Article.count } do
|
143
|
+
# post :create, params: { article: invalid_attributes }
|
144
|
+
# end
|
145
|
+
#
|
146
|
+
# An error message can be specified.
|
147
|
+
#
|
148
|
+
# assert_no_difference 'Article.count', 'An Article should not be created' do
|
149
|
+
# post :create, params: { article: invalid_attributes }
|
150
|
+
# end
|
151
|
+
#
|
152
|
+
# An array of expressions can also be passed in and evaluated.
|
153
|
+
#
|
154
|
+
# assert_no_difference [ 'Article.count', -> { Post.count } ] do
|
155
|
+
# post :create, params: { article: invalid_attributes }
|
156
|
+
# end
|
157
|
+
def assert_no_difference(expression, message = nil, &block)
|
158
|
+
assert_difference expression, 0, message, &block
|
159
|
+
end
|
160
|
+
|
161
|
+
# Assertion that the result of evaluating an expression is changed before
|
162
|
+
# and after invoking the passed in block.
|
163
|
+
#
|
164
|
+
# assert_changes 'Status.all_good?' do
|
165
|
+
# post :create, params: { status: { ok: false } }
|
166
|
+
# end
|
167
|
+
#
|
168
|
+
# You can pass the block as a string to be evaluated in the context of
|
169
|
+
# the block. A lambda can be passed for the block as well.
|
170
|
+
#
|
171
|
+
# assert_changes -> { Status.all_good? } do
|
172
|
+
# post :create, params: { status: { ok: false } }
|
173
|
+
# end
|
174
|
+
#
|
175
|
+
# The assertion is useful to test side effects. The passed block can be
|
176
|
+
# anything that can be converted to string with #to_s.
|
177
|
+
#
|
178
|
+
# assert_changes :@object do
|
179
|
+
# @object = 42
|
180
|
+
# end
|
181
|
+
#
|
182
|
+
# The keyword arguments +:from+ and +:to+ can be given to specify the
|
183
|
+
# expected initial value and the expected value after the block was
|
184
|
+
# executed.
|
185
|
+
#
|
186
|
+
# assert_changes :@object, from: nil, to: :foo do
|
187
|
+
# @object = :foo
|
188
|
+
# end
|
189
|
+
#
|
190
|
+
# An error message can be specified.
|
191
|
+
#
|
192
|
+
# assert_changes -> { Status.all_good? }, 'Expected the status to be bad' do
|
193
|
+
# post :create, params: { status: { incident: true } }
|
194
|
+
# end
|
195
|
+
def assert_changes(expression, message = nil, from: UNTRACKED, to: UNTRACKED, &block)
|
196
|
+
exp = expression.respond_to?(:call) ? expression : -> { eval(expression.to_s, block.binding) }
|
197
|
+
|
198
|
+
before = exp.call
|
199
|
+
retval = _assert_nothing_raised_or_warn("assert_changes", &block)
|
200
|
+
|
201
|
+
unless from == UNTRACKED
|
202
|
+
rich_message = -> do
|
203
|
+
error = "Expected change from #{from.inspect}, got #{before.inspect}"
|
204
|
+
error = "#{message}.\n#{error}" if message
|
205
|
+
error
|
206
|
+
end
|
207
|
+
assert from === before, rich_message
|
208
|
+
end
|
209
|
+
|
210
|
+
after = exp.call
|
211
|
+
|
212
|
+
rich_message = -> do
|
213
|
+
code_string = expression.respond_to?(:call) ? _callable_to_source_string(expression) : expression
|
214
|
+
error = "`#{code_string}` didn't change"
|
215
|
+
error = "#{error}. It was already #{to.inspect}" if before == to
|
216
|
+
error = "#{message}.\n#{error}" if message
|
217
|
+
error
|
218
|
+
end
|
219
|
+
refute_equal before, after, rich_message
|
220
|
+
|
221
|
+
unless to == UNTRACKED
|
222
|
+
rich_message = -> do
|
223
|
+
error = "Expected change to #{to.inspect}, got #{after.inspect}\n"
|
224
|
+
error = "#{message}.\n#{error}" if message
|
225
|
+
error
|
226
|
+
end
|
227
|
+
assert to === after, rich_message
|
228
|
+
end
|
229
|
+
|
230
|
+
retval
|
231
|
+
end
|
232
|
+
|
233
|
+
# Assertion that the result of evaluating an expression is not changed before
|
234
|
+
# and after invoking the passed in block.
|
235
|
+
#
|
236
|
+
# assert_no_changes 'Status.all_good?' do
|
237
|
+
# post :create, params: { status: { ok: true } }
|
238
|
+
# end
|
239
|
+
#
|
240
|
+
# Provide the optional keyword argument +:from+ to specify the expected
|
241
|
+
# initial value.
|
242
|
+
#
|
243
|
+
# assert_no_changes -> { Status.all_good? }, from: true do
|
244
|
+
# post :create, params: { status: { ok: true } }
|
245
|
+
# end
|
246
|
+
#
|
247
|
+
# An error message can be specified.
|
248
|
+
#
|
249
|
+
# assert_no_changes -> { Status.all_good? }, 'Expected the status to be good' do
|
250
|
+
# post :create, params: { status: { ok: false } }
|
251
|
+
# end
|
252
|
+
def assert_no_changes(expression, message = nil, from: UNTRACKED, &block)
|
253
|
+
exp = expression.respond_to?(:call) ? expression : -> { eval(expression.to_s, block.binding) }
|
254
|
+
|
255
|
+
before = exp.call
|
256
|
+
retval = _assert_nothing_raised_or_warn("assert_no_changes", &block)
|
257
|
+
|
258
|
+
unless from == UNTRACKED
|
259
|
+
rich_message = -> do
|
260
|
+
error = "Expected initial value of #{from.inspect}, got #{before.inspect}"
|
261
|
+
error = "#{message}.\n#{error}" if message
|
262
|
+
error
|
263
|
+
end
|
264
|
+
assert from === before, rich_message
|
265
|
+
end
|
266
|
+
|
267
|
+
after = exp.call
|
268
|
+
|
269
|
+
rich_message = -> do
|
270
|
+
code_string = expression.respond_to?(:call) ? _callable_to_source_string(expression) : expression
|
271
|
+
error = "`#{code_string}` changed"
|
272
|
+
error = "#{message}.\n#{error}" if message
|
273
|
+
error
|
274
|
+
end
|
275
|
+
|
276
|
+
if before.nil?
|
277
|
+
assert_nil after, rich_message
|
278
|
+
else
|
279
|
+
assert_equal before, after, rich_message
|
280
|
+
end
|
281
|
+
|
282
|
+
retval
|
283
|
+
end
|
284
|
+
|
285
|
+
private
|
286
|
+
def _assert_nothing_raised_or_warn(assertion, &block)
|
287
|
+
assert_nothing_raised(&block)
|
288
|
+
rescue Minitest::UnexpectedError => e
|
289
|
+
if tagged_logger && tagged_logger.warn?
|
290
|
+
warning = <<~MSG
|
291
|
+
#{self.class} - #{name}: #{e.error.class} raised.
|
292
|
+
If you expected this exception, use `assert_raises` as near to the code that raises as possible.
|
293
|
+
Other block based assertions (e.g. `#{assertion}`) can be used, as long as `assert_raises` is inside their block.
|
294
|
+
MSG
|
295
|
+
tagged_logger.warn warning
|
296
|
+
end
|
297
|
+
|
298
|
+
raise
|
299
|
+
end
|
300
|
+
|
301
|
+
def _callable_to_source_string(callable)
|
302
|
+
if defined?(RubyVM::AbstractSyntaxTree) && callable.is_a?(Proc)
|
303
|
+
ast = begin
|
304
|
+
RubyVM::AbstractSyntaxTree.of(callable, keep_script_lines: true)
|
305
|
+
rescue SystemCallError
|
306
|
+
# Failed to get the source somehow
|
307
|
+
return callable
|
308
|
+
end
|
309
|
+
return callable unless ast
|
310
|
+
|
311
|
+
source = ast.source
|
312
|
+
source.strip!
|
313
|
+
|
314
|
+
# We ignore procs defined with do/end as they are likely multi-line anyway.
|
315
|
+
if source.start_with?("{")
|
316
|
+
source.delete_suffix!("}")
|
317
|
+
source.delete_prefix!("{")
|
318
|
+
source.strip!
|
319
|
+
# It won't read nice if the callable contains multiple
|
320
|
+
# lines, and it should be a rare occurence anyway.
|
321
|
+
# Same if it takes arguments.
|
322
|
+
if !source.include?("\n") && !source.start_with?("|")
|
323
|
+
return source
|
324
|
+
end
|
325
|
+
end
|
326
|
+
end
|
327
|
+
|
328
|
+
callable
|
329
|
+
end
|
330
|
+
end
|
331
|
+
end
|
332
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/concern"
|
4
|
+
require "active_support/inflector"
|
5
|
+
|
6
|
+
module ActiveSupport
|
7
|
+
module Testing
|
8
|
+
# Resolves a constant from a minitest spec name.
|
9
|
+
#
|
10
|
+
# Given the following spec-style test:
|
11
|
+
#
|
12
|
+
# describe WidgetsController, :index do
|
13
|
+
# describe "authenticated user" do
|
14
|
+
# describe "returns widgets" do
|
15
|
+
# it "has a controller that exists" do
|
16
|
+
# assert_kind_of WidgetsController, @controller
|
17
|
+
# end
|
18
|
+
# end
|
19
|
+
# end
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# The test will have the following name:
|
23
|
+
#
|
24
|
+
# "WidgetsController::index::authenticated user::returns widgets"
|
25
|
+
#
|
26
|
+
# The constant WidgetsController can be resolved from the name.
|
27
|
+
# The following code will resolve the constant:
|
28
|
+
#
|
29
|
+
# controller = determine_constant_from_test_name(name) do |constant|
|
30
|
+
# Class === constant && constant < ::ActionController::Metal
|
31
|
+
# end
|
32
|
+
module ConstantLookup
|
33
|
+
extend ::ActiveSupport::Concern
|
34
|
+
|
35
|
+
module ClassMethods # :nodoc:
|
36
|
+
def determine_constant_from_test_name(test_name)
|
37
|
+
names = test_name.split "::"
|
38
|
+
while names.size > 0 do
|
39
|
+
names.last.sub!(/Test$/, "")
|
40
|
+
begin
|
41
|
+
constant = names.join("::").safe_constantize
|
42
|
+
break(constant) if yield(constant)
|
43
|
+
ensure
|
44
|
+
names.pop
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|