Skip to content

Commit

Permalink
Update should warnings for myron's changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Phippen committed Sep 3, 2013
1 parent ff83d8a commit 23eb526
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 24 deletions.
18 changes: 11 additions & 7 deletions lib/rspec/mocks/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def initialize
@yield_receiver_to_any_instance_implementation_blocks = true
@verify_doubled_constant_names = false
@transfer_nested_constants = false
@warn_about_should = true
end

def yield_receiver_to_any_instance_implementation_blocks?
Expand Down Expand Up @@ -46,6 +47,7 @@ def syntax=(values)
end

if Array(values).include?(:should)
@warn_about_should = false
Syntax.enable_should
else
Syntax.disable_should
Expand Down Expand Up @@ -84,15 +86,17 @@ def transfer_nested_constants=(val)

def reset_syntaxes_to_default
self.syntax = [:should, :expect]
@should_warn_about_should = true
@warn_about_should = true
end

def warn_about_should
if @should_warn_about_should
Kernel.warn("Should style expectations will be disabled by default in the\
next major version of RSpec (4), we recommend that you use expect style expectations\
or enable the syntax explicitly. <link_to_some_docs>.")
@should_warn_about_should = false
def warn_unless_should_configured(method_name)
if @warn_about_should
RSpec.deprecate(
"Using #{method_name} from the old `:should` syntax without explicitly enabling the syntax.",
:replacement => "the new `:expect` syntax or explicitly enable `:should`"
)

@warn_about_should = false
end
end
end
Expand Down
12 changes: 9 additions & 3 deletions lib/rspec/mocks/syntax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ def self.enable_should(syntax_host = default_should_syntax_host)

syntax_host.class_exec do
def should_receive(message, opts={}, &block)
::RSpec::Mocks.configuration.warn_about_should
::RSpec::Mocks.configuration.warn_unless_should_configured(__method__)
opts[:expected_from] ||= CallerFilter.first_non_rspec_line
::RSpec::Mocks.expect_message(self, message.to_sym, opts, &block)
end

def should_not_receive(message, &block)
::RSpec::Mocks.configuration.warn_about_should
::RSpec::Mocks.configuration.warn_unless_should_configured(__method__)
opts = {:expected_from => CallerFilter.first_non_rspec_line}
::RSpec::Mocks.expect_message(self, message.to_sym, opts, &block).never
end

def stub(message_or_hash, opts={}, &block)
::RSpec::Mocks.configuration.warn_about_should
::RSpec::Mocks.configuration.warn_unless_should_configured(__method__)
if ::Hash === message_or_hash
message_or_hash.each {|message, value| stub(message).and_return value }
else
Expand All @@ -34,29 +34,35 @@ def stub(message_or_hash, opts={}, &block)
end

def unstub(message)
::RSpec::Mocks.configuration.warn_unless_should_configured(__method__)
::RSpec::Mocks.space.proxy_for(self).remove_stub(message)
end

def stub_chain(*chain, &blk)
::RSpec::Mocks.configuration.warn_unless_should_configured(__method__)
::RSpec::Mocks::StubChain.stub_chain_on(self, *chain, &blk)
end

def as_null_object
::RSpec::Mocks.configuration.warn_unless_should_configured(__method__)
@_null_object = true
::RSpec::Mocks.proxy_for(self).as_null_object
end

def null_object?
::RSpec::Mocks.configuration.warn_unless_should_configured(__method__)
defined?(@_null_object)
end

def received_message?(message, *args, &block)
::RSpec::Mocks.configuration.warn_unless_should_configured(__method__)
::RSpec::Mocks.proxy_for(self).received_message?(message, *args, &block)
end

unless Class.respond_to? :any_instance
Class.class_exec do
def any_instance
::RSpec::Mocks.configuration.warn_unless_should_configured(__method__)
::RSpec::Mocks.any_instance_recorder_for(self)
end
end
Expand Down
51 changes: 37 additions & 14 deletions spec/rspec/mocks/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ def sandboxed
end

it "does not warn about the should syntax" do
Kernel.should_not_receive(:warn).with(/style expectations/i)
Object.new.should_not_receive(:nil?)
RSpec.should_not_receive(:deprecate)
Object.new.should_not_receive(:bees)
end

it 'is a no-op when configured a second time' do
Expand Down Expand Up @@ -117,41 +117,64 @@ def sandboxed
end

it "does not warn about the should syntax" do
Kernel.should_not_receive(:warn).with(/style expectations/i)
Object.new.should_not_receive(:nil?)
RSpec.should_not_receive(:deprecate)
Object.new.should_not_receive(:bees)
end
end

context "by default" do
before { configure_default_syntax }
before do
configure_default_syntax
end

after do
configure_default_syntax
end

let(:expected_arguments) {
[
/Using.*without explicitly enabling/,
{:replacement=>"the new `:expect` syntax or explicitly enable `:should`"}
]
}

it "it warns about should once, regardless of how many times it is called" do
expect(Kernel).to receive(:warn).once.with(/style expectations/i)
expect(RSpec).to receive(:deprecate).with(*expected_arguments)
o = Object.new
o2 = Object.new
o.should_receive(:nil?)
o2.should_receive(:nil?)
o.should_receive(:bees)
o2.should_receive(:bees)

o.nil?
o2.nil?
o.bees
o2.bees
end

it "warns about should not once, regardless of how many times it is called" do
expect(Kernel).to receive(:warn).once.with(/style expectations/i)
expect(RSpec).to receive(:deprecate).with(*expected_arguments)
o = Object.new
o2 = Object.new
o.should_not_receive(:nil?)
o2.should_not_receive(:nil?)
o.should_not_receive(:bees)
o2.should_not_receive(:bees)
end

it "warns about stubbing once, regardless of how many times it is called" do
expect(Kernel).to receive(:warn).once.with(/style expectations/i)
expect(RSpec).to receive(:deprecate).with(*expected_arguments)
o = Object.new
o2 = Object.new

o.stub(:faces)
o2.stub(:faces)
end

it "doesn't warn about stubbing after a reset and setting should" do
expect(RSpec).not_to receive(:deprecate)
RSpec::Mocks.configuration.reset_syntaxes_to_default
RSpec::Mocks.configuration.syntax = :should
o = Object.new
o2 = Object.new
o.stub(:faces)
o2.stub(:faces)
end
end
end

Expand Down

0 comments on commit 23eb526

Please sign in to comment.