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