activesupport 4.2.11.1 → 5.2.8.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activesupport might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +441 -399
- data/MIT-LICENSE +2 -2
- data/README.rdoc +4 -5
- data/lib/active_support/all.rb +5 -3
- data/lib/active_support/array_inquirer.rb +48 -0
- data/lib/active_support/backtrace_cleaner.rb +7 -5
- data/lib/active_support/benchmarkable.rb +6 -4
- data/lib/active_support/builder.rb +3 -1
- data/lib/active_support/cache/file_store.rb +41 -35
- data/lib/active_support/cache/mem_cache_store.rb +91 -91
- data/lib/active_support/cache/memory_store.rb +27 -30
- data/lib/active_support/cache/null_store.rb +7 -8
- data/lib/active_support/cache/redis_cache_store.rb +466 -0
- data/lib/active_support/cache/strategy/local_cache.rb +67 -34
- data/lib/active_support/cache/strategy/local_cache_middleware.rb +10 -9
- data/lib/active_support/cache.rb +287 -196
- data/lib/active_support/callbacks.rb +640 -590
- data/lib/active_support/concern.rb +11 -5
- data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +35 -0
- data/lib/active_support/concurrency/share_lock.rb +227 -0
- data/lib/active_support/configurable.rb +8 -5
- data/lib/active_support/core_ext/array/access.rb +29 -1
- data/lib/active_support/core_ext/array/conversions.rb +22 -18
- data/lib/active_support/core_ext/array/extract_options.rb +2 -0
- data/lib/active_support/core_ext/array/grouping.rb +11 -18
- data/lib/active_support/core_ext/array/inquiry.rb +19 -0
- data/lib/active_support/core_ext/array/prepend_and_append.rb +5 -3
- data/lib/active_support/core_ext/array/wrap.rb +7 -4
- data/lib/active_support/core_ext/array.rb +9 -6
- data/lib/active_support/core_ext/benchmark.rb +3 -1
- data/lib/active_support/core_ext/big_decimal/conversions.rb +10 -12
- data/lib/active_support/core_ext/big_decimal.rb +3 -1
- data/lib/active_support/core_ext/class/attribute.rb +41 -22
- data/lib/active_support/core_ext/class/attribute_accessors.rb +3 -1
- data/lib/active_support/core_ext/class/subclasses.rb +20 -6
- data/lib/active_support/core_ext/class.rb +4 -3
- data/lib/active_support/core_ext/date/acts_like.rb +3 -1
- data/lib/active_support/core_ext/date/blank.rb +14 -0
- data/lib/active_support/core_ext/date/calculations.rb +11 -9
- data/lib/active_support/core_ext/date/conversions.rb +25 -23
- data/lib/active_support/core_ext/date/zones.rb +4 -2
- data/lib/active_support/core_ext/date.rb +6 -4
- data/lib/active_support/core_ext/date_and_time/calculations.rb +170 -58
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +4 -3
- data/lib/active_support/core_ext/date_and_time/zones.rb +12 -12
- data/lib/active_support/core_ext/date_time/acts_like.rb +4 -2
- data/lib/active_support/core_ext/date_time/blank.rb +14 -0
- data/lib/active_support/core_ext/date_time/calculations.rb +36 -18
- data/lib/active_support/core_ext/date_time/compatibility.rb +8 -6
- data/lib/active_support/core_ext/date_time/conversions.rb +16 -12
- data/lib/active_support/core_ext/date_time.rb +7 -5
- data/lib/active_support/core_ext/digest/uuid.rb +7 -5
- data/lib/active_support/core_ext/digest.rb +3 -0
- data/lib/active_support/core_ext/enumerable.rb +101 -33
- data/lib/active_support/core_ext/file/atomic.rb +38 -31
- data/lib/active_support/core_ext/file.rb +3 -1
- data/lib/active_support/core_ext/hash/compact.rb +14 -9
- data/lib/active_support/core_ext/hash/conversions.rb +62 -41
- data/lib/active_support/core_ext/hash/deep_merge.rb +9 -13
- data/lib/active_support/core_ext/hash/except.rb +11 -8
- data/lib/active_support/core_ext/hash/indifferent_access.rb +4 -3
- data/lib/active_support/core_ext/hash/keys.rb +33 -27
- data/lib/active_support/core_ext/hash/reverse_merge.rb +5 -2
- data/lib/active_support/core_ext/hash/slice.rb +8 -8
- data/lib/active_support/core_ext/hash/transform_values.rb +14 -5
- data/lib/active_support/core_ext/hash.rb +11 -9
- data/lib/active_support/core_ext/integer/inflections.rb +3 -1
- data/lib/active_support/core_ext/integer/multiple.rb +2 -0
- data/lib/active_support/core_ext/integer/time.rb +11 -18
- data/lib/active_support/core_ext/integer.rb +5 -3
- data/lib/active_support/core_ext/kernel/agnostics.rb +2 -0
- data/lib/active_support/core_ext/kernel/concern.rb +5 -1
- data/lib/active_support/core_ext/kernel/reporting.rb +4 -84
- data/lib/active_support/core_ext/kernel/singleton_class.rb +2 -0
- data/lib/active_support/core_ext/kernel.rb +6 -5
- data/lib/active_support/core_ext/load_error.rb +3 -22
- data/lib/active_support/core_ext/marshal.rb +8 -8
- data/lib/active_support/core_ext/module/aliasing.rb +6 -44
- data/lib/active_support/core_ext/module/anonymous.rb +12 -1
- data/lib/active_support/core_ext/module/attr_internal.rb +8 -9
- data/lib/active_support/core_ext/module/attribute_accessors.rb +43 -40
- data/lib/active_support/core_ext/module/attribute_accessors_per_thread.rb +150 -0
- data/lib/active_support/core_ext/module/concerning.rb +11 -12
- data/lib/active_support/core_ext/module/delegation.rb +99 -29
- data/lib/active_support/core_ext/module/deprecation.rb +4 -2
- data/lib/active_support/core_ext/module/introspection.rb +9 -9
- data/lib/active_support/core_ext/module/reachable.rb +5 -2
- data/lib/active_support/core_ext/module/redefine_method.rb +49 -0
- data/lib/active_support/core_ext/module/remove_method.rb +8 -3
- data/lib/active_support/core_ext/module.rb +14 -11
- data/lib/active_support/core_ext/name_error.rb +22 -2
- data/lib/active_support/core_ext/numeric/bytes.rb +22 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +78 -81
- data/lib/active_support/core_ext/numeric/inquiry.rb +28 -0
- data/lib/active_support/core_ext/numeric/time.rb +35 -23
- data/lib/active_support/core_ext/numeric.rb +6 -3
- data/lib/active_support/core_ext/object/acts_like.rb +12 -1
- data/lib/active_support/core_ext/object/blank.rb +27 -2
- data/lib/active_support/core_ext/object/conversions.rb +6 -4
- data/lib/active_support/core_ext/object/deep_dup.rb +13 -4
- data/lib/active_support/core_ext/object/duplicable.rb +41 -14
- data/lib/active_support/core_ext/object/inclusion.rb +5 -3
- data/lib/active_support/core_ext/object/instance_variables.rb +3 -1
- data/lib/active_support/core_ext/object/json.rb +49 -19
- data/lib/active_support/core_ext/object/to_param.rb +3 -1
- data/lib/active_support/core_ext/object/to_query.rb +10 -5
- data/lib/active_support/core_ext/object/try.rb +69 -21
- data/lib/active_support/core_ext/object/with_options.rb +16 -3
- data/lib/active_support/core_ext/object.rb +14 -13
- data/lib/active_support/core_ext/range/compare_range.rb +61 -0
- data/lib/active_support/core_ext/range/conversions.rb +27 -7
- data/lib/active_support/core_ext/range/each.rb +19 -17
- data/lib/active_support/core_ext/range/include_range.rb +2 -22
- data/lib/active_support/core_ext/range/include_time_with_zone.rb +23 -0
- data/lib/active_support/core_ext/range/overlaps.rb +2 -0
- data/lib/active_support/core_ext/range.rb +7 -4
- data/lib/active_support/core_ext/regexp.rb +6 -0
- data/lib/active_support/core_ext/securerandom.rb +25 -0
- data/lib/active_support/core_ext/string/access.rb +8 -6
- data/lib/active_support/core_ext/string/behavior.rb +3 -1
- data/lib/active_support/core_ext/string/conversions.rb +7 -4
- data/lib/active_support/core_ext/string/exclude.rb +2 -0
- data/lib/active_support/core_ext/string/filters.rb +6 -5
- data/lib/active_support/core_ext/string/indent.rb +6 -4
- data/lib/active_support/core_ext/string/inflections.rb +61 -24
- data/lib/active_support/core_ext/string/inquiry.rb +3 -1
- data/lib/active_support/core_ext/string/multibyte.rb +15 -7
- data/lib/active_support/core_ext/string/output_safety.rb +62 -38
- data/lib/active_support/core_ext/string/starts_ends_with.rb +2 -0
- data/lib/active_support/core_ext/string/strip.rb +4 -5
- data/lib/active_support/core_ext/string/zones.rb +4 -2
- data/lib/active_support/core_ext/string.rb +15 -13
- data/lib/active_support/core_ext/time/acts_like.rb +3 -1
- data/lib/active_support/core_ext/time/calculations.rb +85 -51
- data/lib/active_support/core_ext/time/compatibility.rb +4 -2
- data/lib/active_support/core_ext/time/conversions.rb +20 -13
- data/lib/active_support/core_ext/time/zones.rb +41 -7
- data/lib/active_support/core_ext/time.rb +7 -6
- data/lib/active_support/core_ext/uri.rb +6 -8
- data/lib/active_support/core_ext.rb +3 -1
- data/lib/active_support/current_attributes.rb +195 -0
- data/lib/active_support/dependencies/autoload.rb +2 -0
- data/lib/active_support/dependencies/interlock.rb +57 -0
- data/lib/active_support/dependencies.rb +152 -161
- data/lib/active_support/deprecation/behaviors.rb +44 -11
- data/lib/active_support/deprecation/constant_accessor.rb +52 -0
- data/lib/active_support/deprecation/instance_delegator.rb +17 -2
- data/lib/active_support/deprecation/method_wrappers.rb +66 -20
- data/lib/active_support/deprecation/proxy_wrappers.rb +56 -28
- data/lib/active_support/deprecation/reporting.rb +32 -12
- data/lib/active_support/deprecation.rb +12 -9
- data/lib/active_support/descendants_tracker.rb +2 -0
- data/lib/active_support/digest.rb +20 -0
- data/lib/active_support/duration/iso8601_parser.rb +125 -0
- data/lib/active_support/duration/iso8601_serializer.rb +55 -0
- data/lib/active_support/duration.rb +314 -38
- data/lib/active_support/encrypted_configuration.rb +49 -0
- data/lib/active_support/encrypted_file.rb +99 -0
- data/lib/active_support/evented_file_update_checker.rb +205 -0
- data/lib/active_support/execution_wrapper.rb +131 -0
- data/lib/active_support/executor.rb +8 -0
- data/lib/active_support/file_update_checker.rb +63 -37
- data/lib/active_support/gem_version.rb +5 -3
- data/lib/active_support/gzip.rb +7 -5
- data/lib/active_support/hash_with_indifferent_access.rb +123 -28
- data/lib/active_support/i18n.rb +8 -6
- data/lib/active_support/i18n_railtie.rb +37 -13
- data/lib/active_support/inflections.rb +13 -11
- data/lib/active_support/inflector/inflections.rb +61 -12
- data/lib/active_support/inflector/methods.rb +163 -136
- data/lib/active_support/inflector/transliterate.rb +48 -27
- data/lib/active_support/inflector.rb +7 -5
- data/lib/active_support/json/decoding.rb +16 -13
- data/lib/active_support/json/encoding.rb +11 -58
- data/lib/active_support/json.rb +4 -2
- data/lib/active_support/key_generator.rb +25 -25
- data/lib/active_support/lazy_load_hooks.rb +50 -20
- data/lib/active_support/locale/en.yml +2 -0
- data/lib/active_support/log_subscriber/test_helper.rb +14 -12
- data/lib/active_support/log_subscriber.rb +13 -10
- data/lib/active_support/logger.rb +8 -7
- data/lib/active_support/logger_silence.rb +6 -4
- data/lib/active_support/logger_thread_safe_level.rb +7 -5
- data/lib/active_support/message_encryptor.rb +168 -53
- data/lib/active_support/message_verifier.rb +150 -17
- data/lib/active_support/messages/metadata.rb +71 -0
- data/lib/active_support/messages/rotation_configuration.rb +22 -0
- data/lib/active_support/messages/rotator.rb +56 -0
- data/lib/active_support/multibyte/chars.rb +36 -23
- data/lib/active_support/multibyte/unicode.rb +100 -96
- data/lib/active_support/multibyte.rb +4 -2
- data/lib/active_support/notifications/fanout.rb +11 -9
- data/lib/active_support/notifications/instrumenter.rb +27 -7
- data/lib/active_support/notifications.rb +11 -7
- data/lib/active_support/number_helper/number_converter.rb +13 -11
- data/lib/active_support/number_helper/number_to_currency_converter.rb +9 -9
- data/lib/active_support/number_helper/number_to_delimited_converter.rb +9 -3
- data/lib/active_support/number_helper/number_to_human_converter.rb +11 -9
- data/lib/active_support/number_helper/number_to_human_size_converter.rb +9 -8
- data/lib/active_support/number_helper/number_to_percentage_converter.rb +3 -1
- data/lib/active_support/number_helper/number_to_phone_converter.rb +13 -4
- data/lib/active_support/number_helper/number_to_rounded_converter.rb +23 -56
- data/lib/active_support/number_helper/rounding_helper.rb +66 -0
- data/lib/active_support/number_helper.rb +94 -68
- data/lib/active_support/option_merger.rb +3 -1
- data/lib/active_support/ordered_hash.rb +6 -4
- data/lib/active_support/ordered_options.rb +23 -5
- data/lib/active_support/per_thread_registry.rb +9 -4
- data/lib/active_support/proxy_object.rb +2 -0
- data/lib/active_support/rails.rb +16 -8
- data/lib/active_support/railtie.rb +43 -9
- data/lib/active_support/reloader.rb +131 -0
- data/lib/active_support/rescuable.rb +108 -53
- data/lib/active_support/security_utils.rb +15 -11
- data/lib/active_support/string_inquirer.rb +11 -3
- data/lib/active_support/subscriber.rb +21 -16
- data/lib/active_support/tagged_logging.rb +14 -11
- data/lib/active_support/test_case.rb +19 -47
- data/lib/active_support/testing/assertions.rb +137 -20
- data/lib/active_support/testing/autorun.rb +4 -2
- data/lib/active_support/testing/constant_lookup.rb +2 -1
- data/lib/active_support/testing/declarative.rb +3 -1
- data/lib/active_support/testing/deprecation.rb +14 -10
- data/lib/active_support/testing/file_fixtures.rb +36 -0
- data/lib/active_support/testing/isolation.rb +34 -25
- data/lib/active_support/testing/method_call_assertions.rb +43 -0
- data/lib/active_support/testing/setup_and_teardown.rb +13 -8
- data/lib/active_support/testing/stream.rb +44 -0
- data/lib/active_support/testing/tagged_logging.rb +3 -1
- data/lib/active_support/testing/time_helpers.rb +81 -15
- data/lib/active_support/time.rb +14 -12
- data/lib/active_support/time_with_zone.rb +169 -39
- data/lib/active_support/values/time_zone.rb +196 -61
- data/lib/active_support/values/unicode_tables.dat +0 -0
- data/lib/active_support/version.rb +3 -1
- data/lib/active_support/xml_mini/jdom.rb +116 -114
- data/lib/active_support/xml_mini/libxml.rb +16 -13
- data/lib/active_support/xml_mini/libxmlsax.rb +15 -14
- data/lib/active_support/xml_mini/nokogiri.rb +14 -12
- data/lib/active_support/xml_mini/nokogirisax.rb +14 -13
- data/lib/active_support/xml_mini/rexml.rb +11 -9
- data/lib/active_support/xml_mini.rb +37 -37
- data/lib/active_support.rb +12 -11
- metadata +57 -27
- data/lib/active_support/concurrency/latch.rb +0 -27
- data/lib/active_support/core_ext/big_decimal/yaml_conversions.rb +0 -16
- data/lib/active_support/core_ext/class/delegating_attributes.rb +0 -45
- data/lib/active_support/core_ext/date_time/zones.rb +0 -6
- data/lib/active_support/core_ext/kernel/debugger.rb +0 -10
- data/lib/active_support/core_ext/module/method_transplanting.rb +0 -13
- data/lib/active_support/core_ext/module/qualified_const.rb +0 -52
- data/lib/active_support/core_ext/object/itself.rb +0 -15
- data/lib/active_support/core_ext/struct.rb +0 -6
- data/lib/active_support/core_ext/thread.rb +0 -86
- data/lib/active_support/core_ext/time/marshal.rb +0 -30
@@ -1,9 +1,11 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module ActiveSupport
|
4
4
|
module Testing
|
5
5
|
module Assertions
|
6
|
-
|
6
|
+
UNTRACKED = Object.new # :nodoc:
|
7
|
+
|
8
|
+
# Asserts that an expression is not truthy. Passes if <tt>object</tt> is
|
7
9
|
# +nil+ or +false+. "Truthy" means "considered true in a conditional"
|
8
10
|
# like <tt>if foo</tt>.
|
9
11
|
#
|
@@ -19,79 +21,194 @@ module ActiveSupport
|
|
19
21
|
assert !object, message
|
20
22
|
end
|
21
23
|
|
24
|
+
# Assertion that the block should not raise an exception.
|
25
|
+
#
|
26
|
+
# Passes if evaluated code in the yielded block raises no exception.
|
27
|
+
#
|
28
|
+
# assert_nothing_raised do
|
29
|
+
# perform_service(param: 'no_exception')
|
30
|
+
# end
|
31
|
+
def assert_nothing_raised
|
32
|
+
yield
|
33
|
+
end
|
34
|
+
|
22
35
|
# Test numeric difference between the return value of an expression as a
|
23
36
|
# result of what is evaluated in the yielded block.
|
24
37
|
#
|
25
38
|
# assert_difference 'Article.count' do
|
26
|
-
# post :create, article: {...}
|
39
|
+
# post :create, params: { article: {...} }
|
27
40
|
# end
|
28
41
|
#
|
29
42
|
# An arbitrary expression is passed in and evaluated.
|
30
43
|
#
|
31
|
-
# assert_difference '
|
32
|
-
# post :create, comment: {...}
|
44
|
+
# assert_difference 'Article.last.comments(:reload).size' do
|
45
|
+
# post :create, params: { comment: {...} }
|
33
46
|
# end
|
34
47
|
#
|
35
48
|
# An arbitrary positive or negative difference can be specified.
|
36
49
|
# The default is <tt>1</tt>.
|
37
50
|
#
|
38
51
|
# assert_difference 'Article.count', -1 do
|
39
|
-
# post :delete, id: ...
|
52
|
+
# post :delete, params: { id: ... }
|
40
53
|
# end
|
41
54
|
#
|
42
55
|
# An array of expressions can also be passed in and evaluated.
|
43
56
|
#
|
44
57
|
# assert_difference [ 'Article.count', 'Post.count' ], 2 do
|
45
|
-
# post :create, article: {...}
|
58
|
+
# post :create, params: { article: {...} }
|
59
|
+
# end
|
60
|
+
#
|
61
|
+
# A hash of expressions/numeric differences can also be passed in and evaluated.
|
62
|
+
#
|
63
|
+
# assert_difference ->{ Article.count } => 1, ->{ Notification.count } => 2 do
|
64
|
+
# post :create, params: { article: {...} }
|
46
65
|
# end
|
47
66
|
#
|
48
67
|
# A lambda or a list of lambdas can be passed in and evaluated:
|
49
68
|
#
|
50
69
|
# assert_difference ->{ Article.count }, 2 do
|
51
|
-
# post :create, article: {...}
|
70
|
+
# post :create, params: { article: {...} }
|
52
71
|
# end
|
53
72
|
#
|
54
73
|
# assert_difference [->{ Article.count }, ->{ Post.count }], 2 do
|
55
|
-
# post :create, article: {...}
|
74
|
+
# post :create, params: { article: {...} }
|
56
75
|
# end
|
57
76
|
#
|
58
77
|
# An error message can be specified.
|
59
78
|
#
|
60
79
|
# assert_difference 'Article.count', -1, 'An Article should be destroyed' do
|
61
|
-
# post :delete, id: ...
|
80
|
+
# post :delete, params: { id: ... }
|
62
81
|
# end
|
63
|
-
def assert_difference(expression,
|
64
|
-
expressions =
|
82
|
+
def assert_difference(expression, *args, &block)
|
83
|
+
expressions =
|
84
|
+
if expression.is_a?(Hash)
|
85
|
+
message = args[0]
|
86
|
+
expression
|
87
|
+
else
|
88
|
+
difference = args[0] || 1
|
89
|
+
message = args[1]
|
90
|
+
Hash[Array(expression).map { |e| [e, difference] }]
|
91
|
+
end
|
65
92
|
|
66
|
-
exps = expressions.map { |e|
|
93
|
+
exps = expressions.keys.map { |e|
|
67
94
|
e.respond_to?(:call) ? e : lambda { eval(e, block.binding) }
|
68
95
|
}
|
69
|
-
before = exps.map
|
96
|
+
before = exps.map(&:call)
|
70
97
|
|
71
|
-
yield
|
98
|
+
retval = yield
|
72
99
|
|
73
|
-
expressions.zip(exps)
|
74
|
-
error = "#{code.inspect} didn't change by #{
|
100
|
+
expressions.zip(exps, before) do |(code, diff), exp, before_value|
|
101
|
+
error = "#{code.inspect} didn't change by #{diff}"
|
75
102
|
error = "#{message}.\n#{error}" if message
|
76
|
-
assert_equal(
|
103
|
+
assert_equal(before_value + diff, exp.call, error)
|
77
104
|
end
|
105
|
+
|
106
|
+
retval
|
78
107
|
end
|
79
108
|
|
80
109
|
# Assertion that the numeric result of evaluating an expression is not
|
81
110
|
# changed before and after invoking the passed in block.
|
82
111
|
#
|
83
112
|
# assert_no_difference 'Article.count' do
|
84
|
-
# post :create, article: invalid_attributes
|
113
|
+
# post :create, params: { article: invalid_attributes }
|
85
114
|
# end
|
86
115
|
#
|
87
116
|
# An error message can be specified.
|
88
117
|
#
|
89
118
|
# assert_no_difference 'Article.count', 'An Article should not be created' do
|
90
|
-
# post :create, article: invalid_attributes
|
119
|
+
# post :create, params: { article: invalid_attributes }
|
91
120
|
# end
|
92
121
|
def assert_no_difference(expression, message = nil, &block)
|
93
122
|
assert_difference expression, 0, message, &block
|
94
123
|
end
|
124
|
+
|
125
|
+
# Assertion that the result of evaluating an expression is changed before
|
126
|
+
# and after invoking the passed in block.
|
127
|
+
#
|
128
|
+
# assert_changes 'Status.all_good?' do
|
129
|
+
# post :create, params: { status: { ok: false } }
|
130
|
+
# end
|
131
|
+
#
|
132
|
+
# You can pass the block as a string to be evaluated in the context of
|
133
|
+
# the block. A lambda can be passed for the block as well.
|
134
|
+
#
|
135
|
+
# assert_changes -> { Status.all_good? } do
|
136
|
+
# post :create, params: { status: { ok: false } }
|
137
|
+
# end
|
138
|
+
#
|
139
|
+
# The assertion is useful to test side effects. The passed block can be
|
140
|
+
# anything that can be converted to string with #to_s.
|
141
|
+
#
|
142
|
+
# assert_changes :@object do
|
143
|
+
# @object = 42
|
144
|
+
# end
|
145
|
+
#
|
146
|
+
# The keyword arguments :from and :to can be given to specify the
|
147
|
+
# expected initial value and the expected value after the block was
|
148
|
+
# executed.
|
149
|
+
#
|
150
|
+
# assert_changes :@object, from: nil, to: :foo do
|
151
|
+
# @object = :foo
|
152
|
+
# end
|
153
|
+
#
|
154
|
+
# An error message can be specified.
|
155
|
+
#
|
156
|
+
# assert_changes -> { Status.all_good? }, 'Expected the status to be bad' do
|
157
|
+
# post :create, params: { status: { incident: true } }
|
158
|
+
# end
|
159
|
+
def assert_changes(expression, message = nil, from: UNTRACKED, to: UNTRACKED, &block)
|
160
|
+
exp = expression.respond_to?(:call) ? expression : -> { eval(expression.to_s, block.binding) }
|
161
|
+
|
162
|
+
before = exp.call
|
163
|
+
retval = yield
|
164
|
+
|
165
|
+
unless from == UNTRACKED
|
166
|
+
error = "#{expression.inspect} isn't #{from.inspect}"
|
167
|
+
error = "#{message}.\n#{error}" if message
|
168
|
+
assert from === before, error
|
169
|
+
end
|
170
|
+
|
171
|
+
after = exp.call
|
172
|
+
|
173
|
+
error = "#{expression.inspect} didn't change"
|
174
|
+
error = "#{error}. It was already #{to}" if before == to
|
175
|
+
error = "#{message}.\n#{error}" if message
|
176
|
+
assert before != after, error
|
177
|
+
|
178
|
+
unless to == UNTRACKED
|
179
|
+
error = "#{expression.inspect} didn't change to #{to}"
|
180
|
+
error = "#{message}.\n#{error}" if message
|
181
|
+
assert to === after, error
|
182
|
+
end
|
183
|
+
|
184
|
+
retval
|
185
|
+
end
|
186
|
+
|
187
|
+
# Assertion that the result of evaluating an expression is not changed before
|
188
|
+
# and after invoking the passed in block.
|
189
|
+
#
|
190
|
+
# assert_no_changes 'Status.all_good?' do
|
191
|
+
# post :create, params: { status: { ok: true } }
|
192
|
+
# end
|
193
|
+
#
|
194
|
+
# An error message can be specified.
|
195
|
+
#
|
196
|
+
# assert_no_changes -> { Status.all_good? }, 'Expected the status to be good' do
|
197
|
+
# post :create, params: { status: { ok: false } }
|
198
|
+
# end
|
199
|
+
def assert_no_changes(expression, message = nil, &block)
|
200
|
+
exp = expression.respond_to?(:call) ? expression : -> { eval(expression.to_s, block.binding) }
|
201
|
+
|
202
|
+
before = exp.call
|
203
|
+
retval = yield
|
204
|
+
after = exp.call
|
205
|
+
|
206
|
+
error = "#{expression.inspect} did change to #{after}"
|
207
|
+
error = "#{message}.\n#{error}" if message
|
208
|
+
assert before == after, error
|
209
|
+
|
210
|
+
retval
|
211
|
+
end
|
95
212
|
end
|
96
213
|
end
|
97
214
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveSupport
|
2
4
|
module Testing
|
3
5
|
module Declarative
|
@@ -9,7 +11,7 @@ module ActiveSupport
|
|
9
11
|
# ...
|
10
12
|
# end
|
11
13
|
def test(name, &block)
|
12
|
-
test_name = "test_#{name.gsub(/\s+/,'_')}".to_sym
|
14
|
+
test_name = "test_#{name.gsub(/\s+/, '_')}".to_sym
|
13
15
|
defined = method_defined? test_name
|
14
16
|
raise "#{test_name} is already defined in #{self}" if defined
|
15
17
|
if block_given?
|
@@ -1,34 +1,38 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/deprecation"
|
4
|
+
require "active_support/core_ext/regexp"
|
2
5
|
|
3
6
|
module ActiveSupport
|
4
7
|
module Testing
|
5
8
|
module Deprecation #:nodoc:
|
6
|
-
def assert_deprecated(match = nil, &block)
|
7
|
-
result, warnings = collect_deprecations(&block)
|
9
|
+
def assert_deprecated(match = nil, deprecator = nil, &block)
|
10
|
+
result, warnings = collect_deprecations(deprecator, &block)
|
8
11
|
assert !warnings.empty?, "Expected a deprecation warning within the block but received none"
|
9
12
|
if match
|
10
13
|
match = Regexp.new(Regexp.escape(match)) unless match.is_a?(Regexp)
|
11
|
-
assert warnings.any? { |w| w
|
14
|
+
assert warnings.any? { |w| match.match?(w) }, "No deprecation warning matched #{match}: #{warnings.join(', ')}"
|
12
15
|
end
|
13
16
|
result
|
14
17
|
end
|
15
18
|
|
16
|
-
def assert_not_deprecated(&block)
|
17
|
-
result, deprecations = collect_deprecations(&block)
|
19
|
+
def assert_not_deprecated(deprecator = nil, &block)
|
20
|
+
result, deprecations = collect_deprecations(deprecator, &block)
|
18
21
|
assert deprecations.empty?, "Expected no deprecation warning within the block but received #{deprecations.size}: \n #{deprecations * "\n "}"
|
19
22
|
result
|
20
23
|
end
|
21
24
|
|
22
|
-
def collect_deprecations
|
23
|
-
|
25
|
+
def collect_deprecations(deprecator = nil)
|
26
|
+
deprecator ||= ActiveSupport::Deprecation
|
27
|
+
old_behavior = deprecator.behavior
|
24
28
|
deprecations = []
|
25
|
-
|
29
|
+
deprecator.behavior = Proc.new do |message, callstack|
|
26
30
|
deprecations << message
|
27
31
|
end
|
28
32
|
result = yield
|
29
33
|
[result, deprecations]
|
30
34
|
ensure
|
31
|
-
|
35
|
+
deprecator.behavior = old_behavior
|
32
36
|
end
|
33
37
|
end
|
34
38
|
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveSupport
|
4
|
+
module Testing
|
5
|
+
# Adds simple access to sample files called file fixtures.
|
6
|
+
# File fixtures are normal files stored in
|
7
|
+
# <tt>ActiveSupport::TestCase.file_fixture_path</tt>.
|
8
|
+
#
|
9
|
+
# File fixtures are represented as +Pathname+ objects.
|
10
|
+
# This makes it easy to extract specific information:
|
11
|
+
#
|
12
|
+
# file_fixture("example.txt").read # get the file's content
|
13
|
+
# file_fixture("example.mp3").size # get the file size
|
14
|
+
module FileFixtures
|
15
|
+
extend ActiveSupport::Concern
|
16
|
+
|
17
|
+
included do
|
18
|
+
class_attribute :file_fixture_path, instance_writer: false
|
19
|
+
end
|
20
|
+
|
21
|
+
# Returns a +Pathname+ to the fixture file named +fixture_name+.
|
22
|
+
#
|
23
|
+
# Raises +ArgumentError+ if +fixture_name+ can't be found.
|
24
|
+
def file_fixture(fixture_name)
|
25
|
+
path = Pathname.new(File.join(file_fixture_path, fixture_name))
|
26
|
+
|
27
|
+
if path.exist?
|
28
|
+
path
|
29
|
+
else
|
30
|
+
msg = "the directory '%s' does not contain a file named '%s'"
|
31
|
+
raise ArgumentError, msg % [file_fixture_path, fixture_name]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -1,9 +1,9 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module ActiveSupport
|
4
4
|
module Testing
|
5
5
|
module Isolation
|
6
|
-
require
|
6
|
+
require "thread"
|
7
7
|
|
8
8
|
def self.included(klass) #:nodoc:
|
9
9
|
klass.class_eval do
|
@@ -12,18 +12,7 @@ module ActiveSupport
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def self.forking_env?
|
15
|
-
!ENV["NO_FORK"] && (
|
16
|
-
end
|
17
|
-
|
18
|
-
@@class_setup_mutex = Mutex.new
|
19
|
-
|
20
|
-
def _run_class_setup # class setup method should only happen in parent
|
21
|
-
@@class_setup_mutex.synchronize do
|
22
|
-
unless defined?(@@ran_class_setup) || ENV['ISOLATION_TEST']
|
23
|
-
self.class.setup if self.class.respond_to?(:setup)
|
24
|
-
@@ran_class_setup = true
|
25
|
-
end
|
26
|
-
end
|
15
|
+
!ENV["NO_FORK"] && Process.respond_to?(:fork)
|
27
16
|
end
|
28
17
|
|
29
18
|
def run
|
@@ -43,14 +32,31 @@ module ActiveSupport
|
|
43
32
|
pid = fork do
|
44
33
|
read.close
|
45
34
|
yield
|
46
|
-
|
35
|
+
begin
|
36
|
+
if error?
|
37
|
+
failures.map! { |e|
|
38
|
+
begin
|
39
|
+
Marshal.dump e
|
40
|
+
e
|
41
|
+
rescue TypeError
|
42
|
+
ex = Exception.new e.message
|
43
|
+
ex.set_backtrace e.backtrace
|
44
|
+
Minitest::UnexpectedError.new ex
|
45
|
+
end
|
46
|
+
}
|
47
|
+
end
|
48
|
+
test_result = defined?(Minitest::Result) ? Minitest::Result.from(self) : dup
|
49
|
+
result = Marshal.dump(test_result)
|
50
|
+
end
|
51
|
+
|
52
|
+
write.puts [result].pack("m")
|
47
53
|
exit!
|
48
54
|
end
|
49
55
|
|
50
56
|
write.close
|
51
57
|
result = read.read
|
52
58
|
Process.wait2(pid)
|
53
|
-
|
59
|
+
result.unpack("m")[0]
|
54
60
|
end
|
55
61
|
end
|
56
62
|
|
@@ -64,24 +70,27 @@ module ActiveSupport
|
|
64
70
|
|
65
71
|
if ENV["ISOLATION_TEST"]
|
66
72
|
yield
|
73
|
+
test_result = defined?(Minitest::Result) ? Minitest::Result.from(self) : dup
|
67
74
|
File.open(ENV["ISOLATION_OUTPUT"], "w") do |file|
|
68
|
-
file.puts [Marshal.dump(
|
75
|
+
file.puts [Marshal.dump(test_result)].pack("m")
|
69
76
|
end
|
70
77
|
exit!
|
71
78
|
else
|
72
79
|
Tempfile.open("isolation") do |tmpfile|
|
73
80
|
env = {
|
74
|
-
ISOLATION_TEST
|
75
|
-
ISOLATION_OUTPUT
|
81
|
+
"ISOLATION_TEST" => self.class.name,
|
82
|
+
"ISOLATION_OUTPUT" => tmpfile.path
|
76
83
|
}
|
77
84
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
85
|
+
test_opts = "-n#{self.class.name}##{name}"
|
86
|
+
|
87
|
+
load_path_args = []
|
88
|
+
$-I.each do |p|
|
89
|
+
load_path_args << "-I"
|
90
|
+
load_path_args << File.expand_path(p)
|
91
|
+
end
|
82
92
|
|
83
|
-
|
84
|
-
child = IO.popen([env, command])
|
93
|
+
child = IO.popen([env, Gem.ruby, *load_path_args, $0, *ORIG_ARGV, test_opts])
|
85
94
|
|
86
95
|
begin
|
87
96
|
Process.wait(child.pid)
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "minitest/mock"
|
4
|
+
|
5
|
+
module ActiveSupport
|
6
|
+
module Testing
|
7
|
+
module MethodCallAssertions # :nodoc:
|
8
|
+
private
|
9
|
+
def assert_called(object, method_name, message = nil, times: 1, returns: nil)
|
10
|
+
times_called = 0
|
11
|
+
|
12
|
+
object.stub(method_name, proc { times_called += 1; returns }) { yield }
|
13
|
+
|
14
|
+
error = "Expected #{method_name} to be called #{times} times, " \
|
15
|
+
"but was called #{times_called} times"
|
16
|
+
error = "#{message}.\n#{error}" if message
|
17
|
+
assert_equal times, times_called, error
|
18
|
+
end
|
19
|
+
|
20
|
+
def assert_called_with(object, method_name, args = [], returns: nil)
|
21
|
+
mock = Minitest::Mock.new
|
22
|
+
|
23
|
+
if args.all? { |arg| arg.is_a?(Array) }
|
24
|
+
args.each { |arg| mock.expect(:call, returns, arg) }
|
25
|
+
else
|
26
|
+
mock.expect(:call, returns, args)
|
27
|
+
end
|
28
|
+
|
29
|
+
object.stub(method_name, mock) { yield }
|
30
|
+
|
31
|
+
mock.verify
|
32
|
+
end
|
33
|
+
|
34
|
+
def assert_not_called(object, method_name, message = nil, &block)
|
35
|
+
assert_called(object, method_name, message, times: 0, &block)
|
36
|
+
end
|
37
|
+
|
38
|
+
def stub_any_instance(klass, instance: klass.new)
|
39
|
+
klass.stub(:new, instance) { yield instance }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -1,5 +1,6 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/callbacks"
|
3
4
|
|
4
5
|
module ActiveSupport
|
5
6
|
module Testing
|
@@ -17,11 +18,10 @@ module ActiveSupport
|
|
17
18
|
# end
|
18
19
|
# end
|
19
20
|
module SetupAndTeardown
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
define_callbacks :setup, :teardown
|
21
|
+
def self.prepended(klass)
|
22
|
+
klass.include ActiveSupport::Callbacks
|
23
|
+
klass.define_callbacks :setup, :teardown
|
24
|
+
klass.extend ClassMethods
|
25
25
|
end
|
26
26
|
|
27
27
|
module ClassMethods
|
@@ -42,7 +42,12 @@ module ActiveSupport
|
|
42
42
|
end
|
43
43
|
|
44
44
|
def after_teardown # :nodoc:
|
45
|
-
|
45
|
+
begin
|
46
|
+
run_callbacks :teardown
|
47
|
+
rescue => e
|
48
|
+
self.failures << Minitest::UnexpectedError.new(e)
|
49
|
+
end
|
50
|
+
|
46
51
|
super
|
47
52
|
end
|
48
53
|
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveSupport
|
4
|
+
module Testing
|
5
|
+
module Stream #:nodoc:
|
6
|
+
private
|
7
|
+
|
8
|
+
def silence_stream(stream)
|
9
|
+
old_stream = stream.dup
|
10
|
+
stream.reopen(IO::NULL)
|
11
|
+
stream.sync = true
|
12
|
+
yield
|
13
|
+
ensure
|
14
|
+
stream.reopen(old_stream)
|
15
|
+
old_stream.close
|
16
|
+
end
|
17
|
+
|
18
|
+
def quietly
|
19
|
+
silence_stream(STDOUT) do
|
20
|
+
silence_stream(STDERR) do
|
21
|
+
yield
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def capture(stream)
|
27
|
+
stream = stream.to_s
|
28
|
+
captured_stream = Tempfile.new(stream)
|
29
|
+
stream_io = eval("$#{stream}")
|
30
|
+
origin_stream = stream_io.dup
|
31
|
+
stream_io.reopen(captured_stream)
|
32
|
+
|
33
|
+
yield
|
34
|
+
|
35
|
+
stream_io.rewind
|
36
|
+
return captured_stream.read
|
37
|
+
ensure
|
38
|
+
captured_stream.close
|
39
|
+
captured_stream.unlink
|
40
|
+
stream_io.reopen(origin_stream)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveSupport
|
2
4
|
module Testing
|
3
5
|
# Logs a "PostsControllerTest: test name" heading before each test to
|
@@ -8,7 +10,7 @@ module ActiveSupport
|
|
8
10
|
def before_setup
|
9
11
|
if tagged_logger && tagged_logger.info?
|
10
12
|
heading = "#{self.class}: #{name}"
|
11
|
-
divider =
|
13
|
+
divider = "-" * heading.size
|
12
14
|
tagged_logger.info divider
|
13
15
|
tagged_logger.info heading
|
14
16
|
tagged_logger.info divider
|