Initial Version of Deprecation for Rails[Koz]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4623 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Michael Koziarski 2006-07-27 00:10:06 +00:00
parent 45e319d0f8
commit 7692191f5a
4 changed files with 134 additions and 0 deletions

@ -1,5 +1,7 @@
*SVN*
* First cut of the Rails Deprecation system. [Koz]
* Strip boolean XML content before checking for 'true' [Rick Olson]
* Customize default BigDecimal formatting. References #5672 [dave@pragprog.com]

@ -32,6 +32,7 @@
require 'active_support/clean_logger'
require 'active_support/dependencies'
require 'active_support/reloadable'
require 'active_support/deprecation'
require 'active_support/ordered_options'
require 'active_support/option_merger'

@ -0,0 +1,46 @@
module ActiveSupport
module Deprecation
@@warning_method = :print
mattr_accessor :warning_method
class << self
def print_warning(lines)
lines.each {|l| $stderr.write("#{l}\n")}
end
def log_warning(lines)
if Object.const_defined?("RAILS_DEFAULT_LOGGER")
lines.each {|l| RAILS_DEFAULT_LOGGER.warn l}
else
print_warning(lines)
end
end
def issue_warning(line)
lines =
["@@@@@@@@@@ Deprecation Warning @@@@@@@@@@", line,
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"]
self.send("#{@@warning_method}_warning", lines)
end
def instance_method_warning(clazz, method)
issue_warning("Your application calls #{clazz}##{method}, which is now deprecated. Please see the API documents at http://api.rubyonrails.org/ for more information.")
end
end
module ClassMethods
def deprecate(method_name)
alias_method "#{method_name}_before_deprecation", method_name
class_eval(<<-EOS, __FILE__, __LINE__)
def #{method_name}(*args)
::ActiveSupport::Deprecation.instance_method_warning(self.class, :#{method_name})
#{method_name}_before_deprecation *args
end
EOS
end
end
end
end
Object.extend(ActiveSupport::Deprecation::ClassMethods)

@ -0,0 +1,85 @@
require 'test/unit'
require File.dirname(__FILE__) + '/../lib/active_support/deprecation'
# Stub out the warnings to allow assertions
module ActiveSupport
module Deprecation
class << self
def issue_warning(message)
@@warning = message
end
def last_warning
@@warning
end
end
end
end
class DeprecationTestingClass
def partiallly_deprecated(foo = nil)
if foo.nil?
ActiveSupport::Deprecation.issue_warning("calling partially_deprecated with foo=nil is now deprecated")
end
end
def not_deprecated
2
end
def deprecated_no_args
1
end
deprecate :deprecated_no_args
def deprecated_one_arg(a)
a
end
deprecate :deprecated_one_arg
def deprecated_multiple_args(a,b,c)
[a,b,c]
end
deprecate :deprecated_multiple_args
end
class DeprecationTest < Test::Unit::TestCase
def setup
@dtc = DeprecationTestingClass.new
ActiveSupport::Deprecation.issue_warning(nil) # reset
end
def test_partial_deprecation
@dtc.partiallly_deprecated
assert_warning_matches /foo=nil/
end
def test_raises_nothing
assert_equal 2, @dtc.not_deprecated
end
def test_deprecating_class_method
assert_equal 1, @dtc.deprecated_no_args
assert_deprecation_warning
assert_warning_matches /DeprecationTestingClass#deprecated_no_args/
end
def test_deprecating_class_method_with_argument
assert_equal 1, @dtc.deprecated_one_arg(1)
end
def test_deprecating_class_method_with_argument
assert_equal [1,2,3], @dtc.deprecated_multiple_args(1,2,3)
end
private
def assert_warning_matches(rx)
assert ActiveSupport::Deprecation.last_warning =~ rx, "The deprecation warning did not match #{rx}"
end
def assert_deprecation_warning
assert_not_nil ActiveSupport::Deprecation.last_warning, "No Deprecation warnings were issued"
end
end