Skip to content

Commit

Permalink
Merge pull request #253 from rspec/new-deprecation-api
Browse files Browse the repository at this point in the history
use new deprecation API
  • Loading branch information
myronmarston committed May 23, 2013
2 parents 802b4ee + 85416ce commit ded52c2
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 58 deletions.
2 changes: 1 addition & 1 deletion features/test_frameworks/test_unit.feature
Expand Up @@ -17,7 +17,7 @@ Feature: Test::Unit integration
end
def be_an_int
RSpec.deprecate(:be_an_int, :be_an_integer)
RSpec.deprecate(:be_an_int, :replacement => :be_an_integer)
be_an_integer
end
Expand Down
45 changes: 12 additions & 33 deletions lib/rspec/expectations/deprecation.rb
@@ -1,38 +1,17 @@
module RSpec
unless respond_to?(:deprecate)
class << self
# Used internally by RSpec to display standard deprecation warnings.
# This is also defined in rspec-core, but we can't assume it's loaded
# since rspec-expectations should be usable w/o rspec-core.
def deprecate(method, alternate_method=nil, version=nil)
version_string = version ? "rspec-#{version}" : "a future version of RSpec"

message = <<-NOTICE
*****************************************************************
DEPRECATION WARNING: you are using deprecated behaviour that will
be removed from #{version_string}.
#{caller(0)[2]}
* #{method} is deprecated.
NOTICE
if alternate_method
message << <<-ADDITIONAL
* please use #{alternate_method} instead.
ADDITIONAL
end

message << "*****************************************************************"
warn_deprecation(message)
end

# Used internally by RSpec to display custom deprecation warnings. This
# is also defined in rspec-core, but we can't assume it's loaded since
# rspec-expectations should be usable w/o rspec-core.
def warn_deprecation(message)
warn(message)
module Expectations
module Deprecation
# @private
#
# Used internally to print deprecation warnings
def deprecate(deprecated, options={})
message = "DEPRECATION: #{deprecated} is deprecated."
message << " Use #{options[:replacement]} instead." if options[:replacement]
message << " Called from #{caller(0)[2]}."
warn message
end
end
end

extend(Expectations::Deprecation) unless respond_to?(:deprecate)
end
4 changes: 2 additions & 2 deletions lib/rspec/expectations/expectation_target.rb
Expand Up @@ -52,12 +52,12 @@ def self.enable_deprecated_should
return if deprecated_should_enabled?

def should(*args)
RSpec.deprecate "`expect { }.should`", "`expect { }.to`", 3
RSpec.deprecate "`expect { }.should`", :replacement => "`expect { }.to`"
@target.should(*args)
end

def should_not(*args)
RSpec.deprecate "`expect { }.should_not`", "`expect { }.not_to`", 3
RSpec.deprecate "`expect { }.should_not`", :replacement => "`expect { }.not_to`"
@target.should_not(*args)
end

Expand Down
14 changes: 2 additions & 12 deletions lib/rspec/expectations/extensions/object.rb
Expand Up @@ -6,17 +6,7 @@ module DeprecatedConstants
def const_missing(name)
case name
when :Rspec, :Spec
RSpec.warn_deprecation <<-WARNING
*****************************************************************
DEPRECATION WARNING: you are using a deprecated constant that will
be removed from a future version of RSpec.
#{caller(0)[2]}
* #{name} is deprecated.
* RSpec is the new top-level module in RSpec-2
***************************************************************
WARNING
RSpec.deprecate(name.to_s, :replacement => "RSpec")
RSpec
else
begin
Expand All @@ -31,7 +21,7 @@ def const_missing(name)

# @deprecated (no replacement)
def differ=(ignore)
RSpec.deprecate("RSpec::Expectations.differ=(differ)", "nothing at all (diffing is now automatic and no longer configurable)")
RSpec.deprecate("RSpec::Expectations.differ=(differ)")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/matchers/be_close.rb
Expand Up @@ -2,7 +2,7 @@ module RSpec
module Matchers
# @deprecated use +be_within+ instead.
def be_close(expected, delta)
RSpec.deprecate("be_close(#{expected}, #{delta})", "be_within(#{delta}).of(#{expected})")
RSpec.deprecate("be_close(#{expected}, #{delta})", :replacement => "be_within(#{delta}).of(#{expected})")
be_within(delta).of(expected)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/matchers/built_in/raise_error.rb
Expand Up @@ -22,7 +22,7 @@ def matches?(given_proc, negative_expectation = false)
elsif @expected_message
"`expect { }.not_to raise_error(message)`"
end
RSpec.deprecate(what_to_deprecate, "`expect { }.not_to raise_error()`", "3.0.0")
RSpec.deprecate(what_to_deprecate, :replacement => "`expect { }.not_to raise_error()`")
end
@raised_expected_error = false
@with_expected_message = false
Expand Down
2 changes: 1 addition & 1 deletion spec/rspec/matchers/be_close_spec.rb
Expand Up @@ -8,7 +8,7 @@ module Matchers
end

it "is deprecated" do
expect(RSpec).to receive(:deprecate).with(/be_close.*/,/be_within.*/)
expect(RSpec).to receive(:deprecate).with(/be_close.*/, :replacement => "be_within(0.5).of(3.0)")
be_close(3.0, 0.5)
end

Expand Down
4 changes: 2 additions & 2 deletions spec/rspec/matchers/configuration_spec.rb
Expand Up @@ -151,10 +151,10 @@ def delegated?; true; end
it 'prints a deprecation notice when `expect {}.should` is used' do
configure_syntax [:should, :expect]

expect(RSpec).to receive(:deprecate).with(/expect \{ \}.should\b/, /expect \{ \}.to/, 3)
expect(RSpec).to receive(:deprecate)
expect { raise "boom" }.should raise_error("boom")

expect(RSpec).to receive(:deprecate).with(/expect \{ \}.should_not/, /expect \{ \}.not_to/, 3)
expect(RSpec).to receive(:deprecate)
expect { }.should_not raise_error
end
end
Expand Down
10 changes: 5 additions & 5 deletions spec/rspec/matchers/raise_error_spec.rb
Expand Up @@ -64,15 +64,15 @@

context "with a specific error class" do
it "is deprecated" do
RSpec.should_receive :warn_deprecation
RSpec.should_receive :deprecate
expect {"bees"}.not_to raise_error(RuntimeError)
end
end

context "with no specific error class" do
it "is not deprecated" do
run = nil
RSpec.stub(:warn_deprecation) { run = true }
allow(RSpec).to receive(:deprecate) { run = true }
expect {"bees"}.not_to raise_error
expect(run).to be_nil
end
Expand Down Expand Up @@ -151,7 +151,7 @@
end

it "is deprecated" do
expect(RSpec).to receive(:deprecate).with(/not_to raise_error\(message\)/, /not_to raise_error\(\)/, "3.0.0")
expect(RSpec).to receive(:deprecate).with(/not_to raise_error\(message\)/, :replacement =>"`expect { }.not_to raise_error()`")
expect {raise 'blarg'}.not_to raise_error('blah')
end

Expand Down Expand Up @@ -206,7 +206,7 @@
end

it "is deprecated" do
expect(RSpec).to receive(:deprecate).with(/not_to raise_error\(SpecificErrorClass\)/, /not_to raise_error\(\)/, "3.0.0")
expect(RSpec).to receive(:deprecate).with(/not_to raise_error\(SpecificErrorClass\)/, :replacement => "`expect { }.not_to raise_error()`")
expect { }.not_to raise_error(NameError)
end

Expand Down Expand Up @@ -255,7 +255,7 @@
end

it "is deprecated" do
expect(RSpec).to receive(:deprecate).with(/not_to raise_error\(SpecificErrorClass, message\)/, /not_to raise_error\(\)/, "3.0.0")
expect(RSpec).to receive(:deprecate).with(/not_to raise_error\(SpecificErrorClass, message\)/, :replacement =>"`expect { }.not_to raise_error()`")
expect {}.not_to raise_error(RuntimeError, "example message")
end

Expand Down

0 comments on commit ded52c2

Please sign in to comment.