Document ActiveSupport::Testing::Deprecation

Co-authored-by: Sam Jordan <sam.jordan@shopify.com>
Co-authored-by: Sam Bostock <sam.bostock@shopify.com>
This commit is contained in:
Sam Bostock 2021-12-03 14:41:41 -05:00
parent 0b10efcc4c
commit 7007d1b2b5
No known key found for this signature in database
GPG Key ID: 453DB0A5D5DAE5D2
2 changed files with 56 additions and 1 deletions

@ -1,3 +1,7 @@
* Document `ActiveSupport::Testing::Deprecation`.
*Sam Bostock & Sam Jordan*
* Add `Pathname#existence`.
```ruby

@ -4,7 +4,30 @@
module ActiveSupport
module Testing
module Deprecation # :nodoc:
module Deprecation
# Asserts that a matching deprecation warning was emitted by the given deprecator during the execution of the yielded block.
#
# assert_deprecated(/foo/, CustomDeprecator) do
# CustomDeprecator.warn "foo should no longer be used"
# end
#
# The +match+ object may be a +Regexp+, or +String+ appearing in the message.
#
# assert_deprecated('foo', CustomDeprecator) do
# CustomDeprecator.warn "foo should no longer be used"
# end
#
# If the +match+ is omitted (or explicitly +nil+), any deprecation warning will match.
#
# assert_deprecated(nil, CustomDeprecator) do
# CustomDeprecator.warn "foo should no longer be used"
# end
#
# If no +deprecator+ is given, defaults to ActiveSupport::Deprecation.
#
# assert_deprecated do
# ActiveSupport::Deprecation.warn "foo should no longer be used"
# end
def assert_deprecated(match = nil, deprecator = nil, &block)
result, warnings = collect_deprecations(deprecator, &block)
assert !warnings.empty?, "Expected a deprecation warning within the block but received none"
@ -15,12 +38,40 @@ def assert_deprecated(match = nil, deprecator = nil, &block)
result
end
# Asserts that no deprecation warnings are emitted by the given deprecator during the execution of the yielded block.
#
# assert_not_deprecated(CustomDeprecator) do
# CustomDeprecator.warn "message" # fails assertion
# end
#
# If no +deprecator+ is given, defaults to ActiveSupport::Deprecation.
#
# assert_not_deprecated do
# ActiveSupport::Deprecation.warn "message" # fails assertion
# end
#
# assert_not_deprecated do
# CustomDeprecator.warn "message" # passes assertion
# end
def assert_not_deprecated(deprecator = nil, &block)
result, deprecations = collect_deprecations(deprecator, &block)
assert deprecations.empty?, "Expected no deprecation warning within the block but received #{deprecations.size}: \n #{deprecations * "\n "}"
result
end
# Returns an array of all the deprecation warnings emitted by the given
# +deprecator+ during the execution of the yielded block.
#
# collect_deprecations(CustomDeprecator) do
# CustomDeprecator.warn "message"
# end # => ["message"]
#
# If no +deprecator+ is given, defaults to ActiveSupport::Deprecation.
#
# collect_deprecations do
# CustomDeprecator.warn "custom message"
# ActiveSupport::Deprecation.warn "message"
# end # => ["message"]
def collect_deprecations(deprecator = nil)
deprecator ||= ActiveSupport::Deprecation
old_behavior = deprecator.behavior