1 require "active_support/core_ext/module"
2
3 module ActiveSupport
4 module Testing
5 module Deprecation #:nodoc:
6 def assert_deprecated(match = nil, &block)
7 result, warnings = collect_deprecations(&block)
8 assert !warnings.empty?, "Expected a deprecation warning within the block but received none"
9 if match
10 match = Regexp.new(Regexp.escape(match)) unless match.is_a?(Regexp)
11 assert warnings.any? { |w| w =~ match }, "No deprecation warning matched #{match}: #{warnings.join(', ')}"
12 end
13 result
14 end
15
16 def assert_not_deprecated(&block)
17 result, deprecations = collect_deprecations(&block)
18 assert deprecations.empty?, "Expected no deprecation warning within the block but received #{deprecations.size}: \n #{deprecations * "\n "}"
19 result
20 end
21
22 private
23 def collect_deprecations
24 old_behavior = ActiveSupport::Deprecation.behavior
25 deprecations = []
26 ActiveSupport::Deprecation.behavior = Proc.new do |message, callstack|
27 deprecations << message
28 end
29 result = yield
30 [result, deprecations]
31 ensure
32 ActiveSupport::Deprecation.behavior = old_behavior
33 end
34 end
35 end
36 end
37
38 begin
39 require 'test/unit/error'
40
41 module Test
42 module Unit
43 class Error # :nodoc:
44 # Silence warnings when reporting test errors.
45 def message_with_silenced_deprecation
46 ActiveSupport::Deprecation.silence do
47 message_without_silenced_deprecation
48 end
49 end
50
51 alias_method_chain :message, :silenced_deprecation
52 end
53 end
54 end
55 rescue LoadError
56 # Using miniunit, ignore.
57 end