From 85416cee0896ff2a3d571191a70364f48883029f Mon Sep 17 00:00:00 2001 From: David Chelimsky Date: Wed, 22 May 2013 16:22:11 -0500 Subject: [PATCH] use new deprecation API --- features/test_frameworks/test_unit.feature | 2 +- lib/rspec/expectations/deprecation.rb | 45 ++++++-------------- lib/rspec/expectations/expectation_target.rb | 4 +- lib/rspec/expectations/extensions/object.rb | 14 +----- lib/rspec/matchers/be_close.rb | 2 +- lib/rspec/matchers/built_in/raise_error.rb | 2 +- spec/rspec/matchers/be_close_spec.rb | 2 +- spec/rspec/matchers/configuration_spec.rb | 4 +- spec/rspec/matchers/raise_error_spec.rb | 10 ++--- 9 files changed, 27 insertions(+), 58 deletions(-) diff --git a/features/test_frameworks/test_unit.feature b/features/test_frameworks/test_unit.feature index 33c58c31f..38ac66c2e 100644 --- a/features/test_frameworks/test_unit.feature +++ b/features/test_frameworks/test_unit.feature @@ -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 diff --git a/lib/rspec/expectations/deprecation.rb b/lib/rspec/expectations/deprecation.rb index 293b4f3b0..21cf61eb6 100644 --- a/lib/rspec/expectations/deprecation.rb +++ b/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 diff --git a/lib/rspec/expectations/expectation_target.rb b/lib/rspec/expectations/expectation_target.rb index 649fe74b1..68a8c604d 100644 --- a/lib/rspec/expectations/expectation_target.rb +++ b/lib/rspec/expectations/expectation_target.rb @@ -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 diff --git a/lib/rspec/expectations/extensions/object.rb b/lib/rspec/expectations/extensions/object.rb index 6bc97243a..a0cdf5b04 100644 --- a/lib/rspec/expectations/extensions/object.rb +++ b/lib/rspec/expectations/extensions/object.rb @@ -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 @@ -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 diff --git a/lib/rspec/matchers/be_close.rb b/lib/rspec/matchers/be_close.rb index 7ef1fae01..e00f89c06 100644 --- a/lib/rspec/matchers/be_close.rb +++ b/lib/rspec/matchers/be_close.rb @@ -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 diff --git a/lib/rspec/matchers/built_in/raise_error.rb b/lib/rspec/matchers/built_in/raise_error.rb index 843da6917..ebe080b43 100644 --- a/lib/rspec/matchers/built_in/raise_error.rb +++ b/lib/rspec/matchers/built_in/raise_error.rb @@ -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 diff --git a/spec/rspec/matchers/be_close_spec.rb b/spec/rspec/matchers/be_close_spec.rb index ff3e0bce1..84b6aa51c 100644 --- a/spec/rspec/matchers/be_close_spec.rb +++ b/spec/rspec/matchers/be_close_spec.rb @@ -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 diff --git a/spec/rspec/matchers/configuration_spec.rb b/spec/rspec/matchers/configuration_spec.rb index 4a750bc23..175965e6d 100644 --- a/spec/rspec/matchers/configuration_spec.rb +++ b/spec/rspec/matchers/configuration_spec.rb @@ -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 diff --git a/spec/rspec/matchers/raise_error_spec.rb b/spec/rspec/matchers/raise_error_spec.rb index 98dccd3e4..46b8a93ad 100644 --- a/spec/rspec/matchers/raise_error_spec.rb +++ b/spec/rspec/matchers/raise_error_spec.rb @@ -64,7 +64,7 @@ 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 @@ -72,7 +72,7 @@ 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 @@ -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 @@ -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 @@ -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