activesupport 4.2.0 → 5.2.0
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 +5 -5
- data/CHANGELOG.md +366 -232
- data/MIT-LICENSE +2 -2
- data/README.rdoc +4 -5
- data/lib/active_support.rb +17 -7
- 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.rb +271 -177
- data/lib/active_support/cache/file_store.rb +41 -35
- data/lib/active_support/cache/mem_cache_store.rb +97 -88
- 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 +454 -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/callbacks.rb +654 -560
- data/lib/active_support/concern.rb +5 -3
- data/lib/active_support/concurrency/load_interlock_aware_monitor.rb +17 -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.rb +3 -1
- data/lib/active_support/core_ext/array.rb +9 -6
- 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/benchmark.rb +3 -1
- data/lib/active_support/core_ext/big_decimal.rb +3 -1
- data/lib/active_support/core_ext/big_decimal/conversions.rb +10 -12
- data/lib/active_support/core_ext/class.rb +4 -3
- 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 -8
- data/lib/active_support/core_ext/date.rb +6 -4
- 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 +31 -23
- data/lib/active_support/core_ext/date/zones.rb +4 -2
- data/lib/active_support/core_ext/date_and_time/calculations.rb +179 -56
- data/lib/active_support/core_ext/date_and_time/compatibility.rb +16 -0
- data/lib/active_support/core_ext/date_and_time/zones.rb +12 -12
- data/lib/active_support/core_ext/date_time.rb +7 -4
- 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 +58 -20
- data/lib/active_support/core_ext/date_time/compatibility.rb +18 -0
- data/lib/active_support/core_ext/date_time/conversions.rb +16 -12
- data/lib/active_support/core_ext/digest/uuid.rb +7 -5
- data/lib/active_support/core_ext/enumerable.rb +107 -28
- data/lib/active_support/core_ext/file.rb +3 -1
- data/lib/active_support/core_ext/file/atomic.rb +38 -31
- data/lib/active_support/core_ext/hash.rb +11 -9
- data/lib/active_support/core_ext/hash/compact.rb +24 -15
- data/lib/active_support/core_ext/hash/conversions.rb +63 -43
- 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 +16 -7
- data/lib/active_support/core_ext/integer.rb +5 -3
- 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 -33
- data/lib/active_support/core_ext/kernel.rb +6 -5
- 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 -83
- data/lib/active_support/core_ext/kernel/singleton_class.rb +2 -0
- data/lib/active_support/core_ext/load_error.rb +3 -22
- data/lib/active_support/core_ext/marshal.rb +13 -10
- data/lib/active_support/core_ext/module.rb +14 -11
- 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 +121 -39
- 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/name_error.rb +22 -2
- data/lib/active_support/core_ext/numeric.rb +6 -3
- data/lib/active_support/core_ext/numeric/bytes.rb +22 -0
- data/lib/active_support/core_ext/numeric/conversions.rb +79 -74
- data/lib/active_support/core_ext/numeric/inquiry.rb +28 -0
- data/lib/active_support/core_ext/numeric/time.rb +35 -38
- data/lib/active_support/core_ext/object.rb +14 -13
- data/lib/active_support/core_ext/object/acts_like.rb +12 -1
- data/lib/active_support/core_ext/object/blank.rb +29 -4
- 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 +98 -45
- 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 +6 -4
- data/lib/active_support/core_ext/object/try.rb +70 -22
- data/lib/active_support/core_ext/object/with_options.rb +16 -3
- data/lib/active_support/core_ext/range.rb +7 -4
- 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 +21 -19
- 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/regexp.rb +6 -0
- data/lib/active_support/core_ext/securerandom.rb +25 -0
- data/lib/active_support/core_ext/string.rb +15 -13
- data/lib/active_support/core_ext/string/access.rb +9 -7
- data/lib/active_support/core_ext/string/behavior.rb +3 -1
- data/lib/active_support/core_ext/string/conversions.rb +8 -5
- data/lib/active_support/core_ext/string/exclude.rb +2 -0
- data/lib/active_support/core_ext/string/filters.rb +10 -8
- 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 +35 -35
- 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/time.rb +7 -5
- data/lib/active_support/core_ext/time/acts_like.rb +3 -1
- data/lib/active_support/core_ext/time/calculations.rb +101 -51
- data/lib/active_support/core_ext/time/compatibility.rb +16 -0
- 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/uri.rb +5 -4
- data/lib/active_support/current_attributes.rb +195 -0
- data/lib/active_support/dependencies.rb +143 -160
- data/lib/active_support/dependencies/autoload.rb +2 -0
- data/lib/active_support/dependencies/interlock.rb +57 -0
- data/lib/active_support/deprecation.rb +12 -9
- data/lib/active_support/deprecation/behaviors.rb +41 -12
- 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 +54 -21
- data/lib/active_support/deprecation/proxy_wrappers.rb +56 -28
- data/lib/active_support/deprecation/reporting.rb +32 -12
- data/lib/active_support/descendants_tracker.rb +2 -0
- data/lib/active_support/digest.rb +20 -0
- data/lib/active_support/duration.rb +326 -30
- data/lib/active_support/duration/iso8601_parser.rb +125 -0
- data/lib/active_support/duration/iso8601_serializer.rb +55 -0
- 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 +128 -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 +4 -2
- data/lib/active_support/gzip.rb +7 -5
- data/lib/active_support/hash_with_indifferent_access.rb +130 -30
- data/lib/active_support/i18n.rb +8 -6
- data/lib/active_support/i18n_railtie.rb +34 -14
- data/lib/active_support/inflections.rb +13 -11
- data/lib/active_support/inflector.rb +7 -5
- data/lib/active_support/inflector/inflections.rb +61 -12
- data/lib/active_support/inflector/methods.rb +161 -136
- data/lib/active_support/inflector/transliterate.rb +48 -27
- data/lib/active_support/json.rb +4 -2
- data/lib/active_support/json/decoding.rb +16 -13
- data/lib/active_support/json/encoding.rb +15 -57
- 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.rb +13 -10
- data/lib/active_support/log_subscriber/test_helper.rb +14 -12
- data/lib/active_support/logger.rb +54 -3
- data/lib/active_support/logger_silence.rb +12 -7
- data/lib/active_support/logger_thread_safe_level.rb +33 -0
- data/lib/active_support/message_encryptor.rb +173 -51
- 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.rb +4 -2
- data/lib/active_support/multibyte/chars.rb +37 -24
- data/lib/active_support/multibyte/unicode.rb +100 -96
- data/lib/active_support/notifications.rb +11 -7
- data/lib/active_support/notifications/fanout.rb +10 -8
- data/lib/active_support/notifications/instrumenter.rb +27 -7
- data/lib/active_support/number_helper.rb +94 -68
- 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/option_merger.rb +3 -1
- data/lib/active_support/ordered_hash.rb +6 -4
- data/lib/active_support/ordered_options.rb +22 -4
- data/lib/active_support/per_thread_registry.rb +13 -6
- 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 +17 -6
- data/lib/active_support/string_inquirer.rb +11 -3
- data/lib/active_support/subscriber.rb +15 -14
- data/lib/active_support/tagged_logging.rb +14 -11
- data/lib/active_support/test_case.rb +18 -46
- 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 +12 -3
- 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 +96 -27
- data/lib/active_support/time.rb +14 -12
- data/lib/active_support/time_with_zone.rb +195 -53
- data/lib/active_support/values/time_zone.rb +200 -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.rb +69 -51
- data/lib/active_support/xml_mini/jdom.rb +116 -113
- data/lib/active_support/xml_mini/libxml.rb +17 -16
- data/lib/active_support/xml_mini/libxmlsax.rb +16 -18
- data/lib/active_support/xml_mini/nokogiri.rb +15 -15
- data/lib/active_support/xml_mini/nokogirisax.rb +15 -16
- data/lib/active_support/xml_mini/rexml.rb +17 -16
- metadata +55 -43
- data/lib/active_support/concurrency/latch.rb +0 -27
- data/lib/active_support/core_ext/big_decimal/yaml_conversions.rb +0 -14
- 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 -11
- 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,22 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveSupport
|
2
4
|
# Wrapping a string in this class gives you a prettier way to test
|
3
5
|
# for equality. The value returned by <tt>Rails.env</tt> is wrapped
|
4
|
-
# in a StringInquirer object so instead of calling this:
|
6
|
+
# in a StringInquirer object, so instead of calling this:
|
5
7
|
#
|
6
8
|
# Rails.env == 'production'
|
7
9
|
#
|
8
10
|
# you can call this:
|
9
11
|
#
|
10
12
|
# Rails.env.production?
|
13
|
+
#
|
14
|
+
# == Instantiating a new StringInquirer
|
15
|
+
#
|
16
|
+
# vehicle = ActiveSupport::StringInquirer.new('car')
|
17
|
+
# vehicle.car? # => true
|
18
|
+
# vehicle.bike? # => false
|
11
19
|
class StringInquirer < String
|
12
20
|
private
|
13
21
|
|
14
22
|
def respond_to_missing?(method_name, include_private = false)
|
15
|
-
method_name[-1] ==
|
23
|
+
(method_name[-1] == "?") || super
|
16
24
|
end
|
17
25
|
|
18
26
|
def method_missing(method_name, *arguments)
|
19
|
-
if method_name[-1] ==
|
27
|
+
if method_name[-1] == "?"
|
20
28
|
self == method_name[0..-2]
|
21
29
|
else
|
22
30
|
super
|
@@ -1,36 +1,33 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/per_thread_registry"
|
4
|
+
require "active_support/notifications"
|
2
5
|
|
3
6
|
module ActiveSupport
|
4
7
|
# ActiveSupport::Subscriber is an object set to consume
|
5
8
|
# ActiveSupport::Notifications. The subscriber dispatches notifications to
|
6
9
|
# a registered object based on its given namespace.
|
7
10
|
#
|
8
|
-
# An example would be Active Record subscriber responsible for collecting
|
11
|
+
# An example would be an Active Record subscriber responsible for collecting
|
9
12
|
# statistics about queries:
|
10
13
|
#
|
11
14
|
# module ActiveRecord
|
12
15
|
# class StatsSubscriber < ActiveSupport::Subscriber
|
16
|
+
# attach_to :active_record
|
17
|
+
#
|
13
18
|
# def sql(event)
|
14
19
|
# Statsd.timing("sql.#{event.payload[:name]}", event.duration)
|
15
20
|
# end
|
16
21
|
# end
|
17
22
|
# end
|
18
23
|
#
|
19
|
-
# And it's finally registered as:
|
20
|
-
#
|
21
|
-
# ActiveRecord::StatsSubscriber.attach_to :active_record
|
22
|
-
#
|
23
|
-
# Since we need to know all instance methods before attaching the log
|
24
|
-
# subscriber, the line above should be called after your subscriber definition.
|
25
|
-
#
|
26
24
|
# After configured, whenever a "sql.active_record" notification is published,
|
27
25
|
# it will properly dispatch the event (ActiveSupport::Notifications::Event) to
|
28
26
|
# the +sql+ method.
|
29
27
|
class Subscriber
|
30
28
|
class << self
|
31
|
-
|
32
29
|
# Attach the subscriber to a namespace.
|
33
|
-
def attach_to(namespace, subscriber=new, notifier=ActiveSupport::Notifications)
|
30
|
+
def attach_to(namespace, subscriber = new, notifier = ActiveSupport::Notifications)
|
34
31
|
@namespace = namespace
|
35
32
|
@subscriber = subscriber
|
36
33
|
@notifier = notifier
|
@@ -57,16 +54,20 @@ module ActiveSupport
|
|
57
54
|
@@subscribers ||= []
|
58
55
|
end
|
59
56
|
|
57
|
+
# TODO Change this to private once we've dropped Ruby 2.2 support.
|
58
|
+
# Workaround for Ruby 2.2 "private attribute?" warning.
|
60
59
|
protected
|
61
60
|
|
62
61
|
attr_reader :subscriber, :notifier, :namespace
|
63
62
|
|
64
|
-
|
63
|
+
private
|
64
|
+
|
65
|
+
def add_event_subscriber(event) # :doc:
|
65
66
|
return if %w{ start finish }.include?(event.to_s)
|
66
67
|
|
67
68
|
pattern = "#{event}.#{namespace}"
|
68
69
|
|
69
|
-
#
|
70
|
+
# Don't add multiple subscribers (eg. if methods are redefined).
|
70
71
|
return if subscriber.patterns.include?(pattern)
|
71
72
|
|
72
73
|
subscriber.patterns << pattern
|
@@ -96,7 +97,7 @@ module ActiveSupport
|
|
96
97
|
event.end = finished
|
97
98
|
event.payload.merge!(payload)
|
98
99
|
|
99
|
-
method = name.split(
|
100
|
+
method = name.split(".".freeze).first
|
100
101
|
send(method, event)
|
101
102
|
end
|
102
103
|
|
@@ -1,7 +1,9 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/module/delegation"
|
4
|
+
require "active_support/core_ext/object/blank"
|
5
|
+
require "logger"
|
6
|
+
require "active_support/logger"
|
5
7
|
|
6
8
|
module ActiveSupport
|
7
9
|
# Wraps any standard Logger object to provide tagging capabilities.
|
@@ -43,16 +45,17 @@ module ActiveSupport
|
|
43
45
|
end
|
44
46
|
|
45
47
|
def current_tags
|
46
|
-
|
48
|
+
# We use our object ID here to avoid conflicting with other instances
|
49
|
+
thread_key = @thread_key ||= "activesupport_tagged_logging_tags:#{object_id}".freeze
|
50
|
+
Thread.current[thread_key] ||= []
|
47
51
|
end
|
48
52
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
tags.collect { |tag| "[#{tag}] " }.join
|
54
|
-
end
|
53
|
+
def tags_text
|
54
|
+
tags = current_tags
|
55
|
+
if tags.any?
|
56
|
+
tags.collect { |tag| "[#{tag}] " }.join
|
55
57
|
end
|
58
|
+
end
|
56
59
|
end
|
57
60
|
|
58
61
|
def self.new(logger)
|
@@ -1,15 +1,16 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require
|
8
|
-
require
|
9
|
-
require
|
10
|
-
require
|
11
|
-
require
|
12
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
gem "minitest" # make sure we get the gem, not stdlib
|
4
|
+
require "minitest"
|
5
|
+
require "active_support/testing/tagged_logging"
|
6
|
+
require "active_support/testing/setup_and_teardown"
|
7
|
+
require "active_support/testing/assertions"
|
8
|
+
require "active_support/testing/deprecation"
|
9
|
+
require "active_support/testing/declarative"
|
10
|
+
require "active_support/testing/isolation"
|
11
|
+
require "active_support/testing/constant_lookup"
|
12
|
+
require "active_support/testing/time_helpers"
|
13
|
+
require "active_support/testing/file_fixtures"
|
13
14
|
|
14
15
|
module ActiveSupport
|
15
16
|
class TestCase < ::Minitest::Test
|
@@ -31,36 +32,13 @@ module ActiveSupport
|
|
31
32
|
|
32
33
|
# Returns the order in which test cases are run.
|
33
34
|
#
|
34
|
-
# ActiveSupport::TestCase.test_order # => :
|
35
|
+
# ActiveSupport::TestCase.test_order # => :random
|
35
36
|
#
|
36
37
|
# Possible values are +:random+, +:parallel+, +:alpha+, +:sorted+.
|
37
|
-
# Defaults to +:
|
38
|
+
# Defaults to +:random+.
|
38
39
|
def test_order
|
39
|
-
test_order
|
40
|
-
|
41
|
-
if test_order.nil?
|
42
|
-
ActiveSupport::Deprecation.warn "You did not specify a value for the " \
|
43
|
-
"configuration option `active_support.test_order`. In Rails 5, " \
|
44
|
-
"the default value of this option will change from `:sorted` to " \
|
45
|
-
"`:random`.\n" \
|
46
|
-
"To disable this warning and keep the current behavior, you can add " \
|
47
|
-
"the following line to your `config/environments/test.rb`:\n" \
|
48
|
-
"\n" \
|
49
|
-
" Rails.application.configure do\n" \
|
50
|
-
" config.active_support.test_order = :sorted\n" \
|
51
|
-
" end\n" \
|
52
|
-
"\n" \
|
53
|
-
"Alternatively, you can opt into the future behavior by setting this " \
|
54
|
-
"option to `:random`."
|
55
|
-
|
56
|
-
test_order = :sorted
|
57
|
-
self.test_order = test_order
|
58
|
-
end
|
59
|
-
|
60
|
-
test_order
|
40
|
+
ActiveSupport.test_order ||= :random
|
61
41
|
end
|
62
|
-
|
63
|
-
alias :my_tests_are_order_dependent! :i_suck_and_my_tests_are_order_dependent!
|
64
42
|
end
|
65
43
|
|
66
44
|
alias_method :method_name, :name
|
@@ -70,6 +48,7 @@ module ActiveSupport
|
|
70
48
|
include ActiveSupport::Testing::Assertions
|
71
49
|
include ActiveSupport::Testing::Deprecation
|
72
50
|
include ActiveSupport::Testing::TimeHelpers
|
51
|
+
include ActiveSupport::Testing::FileFixtures
|
73
52
|
extend ActiveSupport::Testing::Declarative
|
74
53
|
|
75
54
|
# test/unit backwards compatibility methods
|
@@ -88,13 +67,6 @@ module ActiveSupport
|
|
88
67
|
alias :assert_not_respond_to :refute_respond_to
|
89
68
|
alias :assert_not_same :refute_same
|
90
69
|
|
91
|
-
|
92
|
-
#
|
93
|
-
# assert_nothing_raised do
|
94
|
-
# ...
|
95
|
-
# end
|
96
|
-
def assert_nothing_raised(*args)
|
97
|
-
yield
|
98
|
-
end
|
70
|
+
ActiveSupport.run_load_hooks(:active_support_test_case, self)
|
99
71
|
end
|
100
72
|
end
|
@@ -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
|