From 809d8843c73c23df06c15f2b1bbab59d71b028c1 Mon Sep 17 00:00:00 2001 From: Yuji Nakayama Date: Sat, 27 Jul 2013 02:19:46 +0900 Subject: [PATCH] Support conversion of any_number_of_times This closes #3. --- CHANGELOG.md | 2 + lib/transpec/rewriter.rb | 10 +- .../syntax/any_number_of_timesable.rb | 42 ++ lib/transpec/syntax/expectizable.rb | 10 +- lib/transpec/syntax/method_stub.rb | 3 +- lib/transpec/syntax/should_receive.rb | 61 +-- spec/transpec/rewriter_spec.rb | 433 +++++++++++++----- spec/transpec/syntax/method_stub_spec.rb | 80 ++++ spec/transpec/syntax/should_receive_spec.rb | 120 +++++ 9 files changed, 604 insertions(+), 157 deletions(-) create mode 100644 lib/transpec/syntax/any_number_of_timesable.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b016ce..ca0504b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Master +* Support conversion of `any_number_of_times` + ## v0.0.4 * Fix a bug where necessary parentheses were not added when converting operator matcher to non-operator matcher in some cases (e.g. `== (2 - 1) + (1 + 2)` was converted into `eq(2 - 1) + (1 + 2)` unintentionally) diff --git a/lib/transpec/rewriter.rb b/lib/transpec/rewriter.rb index dc85082..fb93ea3 100644 --- a/lib/transpec/rewriter.rb +++ b/lib/transpec/rewriter.rb @@ -99,7 +99,13 @@ def process_should(should) def process_should_receive(should_receive) if @configuration.convert_to_expect_to_receive? - should_receive.expectize!(@configuration.negative_form_of_to) + if should_receive.any_number_of_times? && @configuration.replace_deprecated_method? + should_receive.allowize_any_number_of_times!(@configuration.negative_form_of_to) + else + should_receive.expectize!(@configuration.negative_form_of_to) + end + elsif should_receive.any_number_of_times? && @configuration.replace_deprecated_method? + should_receive.stubize_any_number_of_times! end end @@ -113,6 +119,8 @@ def process_method_stub(method_stub) elsif @configuration.replace_deprecated_method? method_stub.replace_deprecated_method! end + + method_stub.remove_any_number_of_times! if @configuration.replace_deprecated_method? end def process_be_close(be_close) diff --git a/lib/transpec/syntax/any_number_of_timesable.rb b/lib/transpec/syntax/any_number_of_timesable.rb new file mode 100644 index 0000000..32b4c27 --- /dev/null +++ b/lib/transpec/syntax/any_number_of_timesable.rb @@ -0,0 +1,42 @@ +# coding: utf-8 + +require 'transpec/syntax' + +module Transpec + class Syntax + module AnyNumberOfTimesable + def any_number_of_times? + !any_number_of_times_node.nil? + end + + def remove_any_number_of_times! + return unless any_number_of_times? + + map = any_number_of_times_node.loc + dot_any_number_of_times_range = map.dot.join(map.selector) + remove(dot_any_number_of_times_range) + end + + private + + def any_number_of_times_node + each_following_chained_method_node do |chained_node| + method_name = chained_node.children[1] + return chained_node if method_name == :any_number_of_times + end + end + + def each_following_chained_method_node + return to_enum(__method__) unless block_given? + + @ancestor_nodes.reverse.reduce(@node) do |child_node, parent_node| + return unless [:send, :block].include?(parent_node.type) + return unless parent_node.children.first == child_node + yield parent_node, child_node + parent_node + end + nil + end + end + end +end diff --git a/lib/transpec/syntax/expectizable.rb b/lib/transpec/syntax/expectizable.rb index 23d60b0..bf2c29f 100644 --- a/lib/transpec/syntax/expectizable.rb +++ b/lib/transpec/syntax/expectizable.rb @@ -9,10 +9,16 @@ module Expectizable include SendNodeSyntax def wrap_subject_in_expect! + wrap_subject_with_method!('expect') + end + + private + + def wrap_subject_with_method!(method) if Util.in_parentheses?(subject_node) - insert_before(subject_range, 'expect') + insert_before(subject_range, method) else - insert_before(subject_range, 'expect(') + insert_before(subject_range, "#{method}(") insert_after(subject_range, ')') end end diff --git a/lib/transpec/syntax/method_stub.rb b/lib/transpec/syntax/method_stub.rb index 8af8792..6fd1ef9 100644 --- a/lib/transpec/syntax/method_stub.rb +++ b/lib/transpec/syntax/method_stub.rb @@ -2,13 +2,14 @@ require 'transpec/syntax' require 'transpec/syntax/any_instanceable' +require 'transpec/syntax/any_number_of_timesable' require 'transpec/util' require 'English' module Transpec class Syntax class MethodStub < Syntax - include AnyInstanceable, Util + include AnyInstanceable, AnyNumberOfTimesable, Util def allowize! # There's no way of unstubbing in #allow syntax. diff --git a/lib/transpec/syntax/should_receive.rb b/lib/transpec/syntax/should_receive.rb index 10a89a7..e94490a 100644 --- a/lib/transpec/syntax/should_receive.rb +++ b/lib/transpec/syntax/should_receive.rb @@ -3,25 +3,46 @@ require 'transpec/syntax' require 'transpec/syntax/expectizable' require 'transpec/syntax/any_instanceable' +require 'transpec/syntax/any_number_of_timesable' module Transpec class Syntax class ShouldReceive < Syntax - include Expectizable, AnyInstanceable + include Expectizable, AnyInstanceable, AnyNumberOfTimesable def positive? method_name == :should_receive end def expectize!(negative_form = 'not_to') + convert_to_syntax!('expect', negative_form) + end + + def allowize_any_number_of_times!(negative_form = 'not_to') + return unless any_number_of_times? + + convert_to_syntax!('allow', negative_form) + remove_any_number_of_times! + end + + def stubize_any_number_of_times!(negative_form = 'not_to') + return unless any_number_of_times? + + replace(selector_range, 'stub') + remove_any_number_of_times! + end + + private + + def convert_to_syntax!(syntax, negative_form) unless in_example_group_context? - fail NotInExampleGroupContextError.new(expression_range, "##{method_name}", '#expect') + fail NotInExampleGroupContextError.new(expression_range, "##{method_name}", "##{syntax}") end if any_instance? wrap_class_in_expect_any_instance_of! else - wrap_subject_in_expect! + wrap_subject_with_method!(syntax) end replace(selector_range, "#{positive? ? 'to' : negative_form} receive") @@ -40,8 +61,6 @@ def correct_block_style! end end - private - def self.target_receiver_node?(node) !node.nil? end @@ -60,7 +79,7 @@ def wrap_class_in_expect_any_instance_of! def broken_block_nodes @broken_block_nodes ||= [ block_node_taken_by_with_method_with_no_normal_args, - block_node_followed_by_message_expectation_method + block_node_following_message_expectation_method ].compact.uniq end @@ -76,20 +95,12 @@ def broken_block_nodes # (args # (arg :block_arg)) nil) def block_node_taken_by_with_method_with_no_normal_args - @ancestor_nodes.reverse.reduce(@node) do |child_node, parent_node| - return nil unless [:send, :block].include?(parent_node.type) - return nil unless parent_node.children.first == child_node - - if parent_node.type == :block - return nil unless child_node.children[1] == :with - return nil if child_node.children[2] - return parent_node - end - - parent_node + each_following_chained_method_node do |chained_node, child_node| + next unless chained_node.type == :block + return nil unless child_node.children[1] == :with + return nil if child_node.children[2] + return chained_node end - - nil end # subject.should_receive(:method_name) do |block_arg| @@ -102,15 +113,11 @@ def block_node_taken_by_with_method_with_no_normal_args # (sym :method_name)) # (args # (arg :block_arg)) nil) :once) - def block_node_followed_by_message_expectation_method - @ancestor_nodes.reverse.reduce(@node) do |child_node, parent_node| - return nil unless [:send, :block].include?(parent_node.type) - return nil unless parent_node.children.first == child_node - return child_node if child_node.type == :block && parent_node.type == :send - parent_node + def block_node_following_message_expectation_method + each_following_chained_method_node do |chained_node, child_node| + next unless chained_node.type == :send + return child_node if child_node.type == :block end - - nil end end end diff --git a/spec/transpec/rewriter_spec.rb b/spec/transpec/rewriter_spec.rb index 758195d..bb5cf1c 100644 --- a/spec/transpec/rewriter_spec.rb +++ b/spec/transpec/rewriter_spec.rb @@ -29,28 +29,87 @@ module Transpec let(:source) do <<-END - RSpec.configure do |config| - config.expect_with :rspec do |c| - c.syntax = :should - end - - config.mock_with :rspec do |c| - c.syntax = :should - end - end - describe 'example group' do it 'is an example' do - something = mock('something') - something.stub!(:message) + something.should == 'foo' something.should_receive(:message) - something.should_not == 'foo' - expect(1.0 / 3.0).to be_close(0.333, 0.001) end end END end + it 'dispatches found syntax objects to each handler method' do + rewriter.should_receive(:process_should).with(an_instance_of(Syntax::Should)) + rewriter.should_receive(:process_should_receive).with(an_instance_of(Syntax::ShouldReceive)) + rewriter.rewrite(source) + end + + context 'when the source has overlapped rewrite targets' do + let(:source) do + <<-END + describe 'example group' do + it 'is an example' do + object.stub(:message => mock('something')) + end + end + END + end + + let(:expected_source) do + <<-END + describe 'example group' do + it 'is an example' do + allow(object).to receive(:message).and_return(double('something')) + end + end + END + end + + it 'rewrites all targets properly' do + should == expected_source + end + end + + context 'when the source has a monkey-patched expectation outside of example group context' do + before do + configuration.convert_to_expect_to_matcher = true + rewriter.stub(:warn) + end + + let(:source) do + <<-END + describe 'example group' do + class SomeClass + def some_method + 1.should == 1 + end + end + + it 'is an example' do + SomeClass.new.some_method + end + end + END + end + + it 'does not rewrite the expectation to non-monkey-patch syntax' do + should == source + end + + it 'warns to user' do + rewriter.should_receive(:warn) do |message| + message.should =~ /cannot/i + message.should =~ /context/i + end + + rewriter.rewrite(source) + end + end + end + + describe '#process_should' do + let(:should_object) { double('should_object').as_null_object } + context 'when Configuration#convert_to_expect_to_matcher? is true' do before { configuration.convert_to_expect_to_matcher = true } @@ -58,8 +117,8 @@ module Transpec before { configuration.negative_form_of_to = 'not_to' } it 'invokes Should#expectize! with "not_to"' do - Syntax::Should.any_instance.should_receive(:expectize!).with('not_to', anything) - rewriter.rewrite(source) + should_object.should_receive(:expectize!).with('not_to', anything) + rewriter.process_should(should_object) end end @@ -67,8 +126,8 @@ module Transpec before { configuration.negative_form_of_to = 'to_not' } it 'invokes Should#expectize! with "to_not"' do - Syntax::Should.any_instance.should_receive(:expectize!).with('to_not', anything) - rewriter.rewrite(source) + should_object.should_receive(:expectize!).with('to_not', anything) + rewriter.process_should(should_object) end end @@ -76,8 +135,8 @@ module Transpec before { configuration.parenthesize_matcher_arg = true } it 'invokes Should#expectize! with true as second argument' do - Syntax::Should.any_instance.should_receive(:expectize!).with(anything, true) - rewriter.rewrite(source) + should_object.should_receive(:expectize!).with(anything, true) + rewriter.process_should(should_object) end end @@ -85,8 +144,8 @@ module Transpec before { configuration.parenthesize_matcher_arg = false } it 'invokes Should#expectize! with false as second argument' do - Syntax::Should.any_instance.should_receive(:expectize!).with(anything, false) - rewriter.rewrite(source) + should_object.should_receive(:expectize!).with(anything, false) + rewriter.process_should(should_object) end end end @@ -95,87 +154,273 @@ module Transpec before { configuration.convert_to_expect_to_matcher = false } it 'does not invoke Should#expectize!' do - Syntax::Should.any_instance.should_not_receive(:expectize!) - rewriter.rewrite(source) + should_object.should_not_receive(:expectize!) + rewriter.process_should(should_object) end end + end + + describe '#process_should_receive' do + let(:should_receive_object) { double('should_receive_object').as_null_object } context 'when Configuration#convert_to_expect_to_receive? is true' do before { configuration.convert_to_expect_to_receive = true } - context 'and Configuration#negative_form_of_to is "not_to"' do - before { configuration.negative_form_of_to = 'not_to' } + context 'and ShouldReceive#any_number_of_times? returns true' do + before { should_receive_object.stub(:any_number_of_times?).and_return(true) } + + context 'when Configuration#replace_deprecated_method? is true' do + before { configuration.replace_deprecated_method = true } + + context 'and Configuration#negative_form_of_to is "not_to"' do + before { configuration.negative_form_of_to = 'not_to' } - it 'invokes ShouldReceive#expectize! with "not_to"' do - Syntax::ShouldReceive.any_instance.should_receive(:expectize!).with('not_to') - rewriter.rewrite(source) + it 'invokes ShouldReceive#allowize_any_number_of_times! with "not_to"' do + should_receive_object.should_receive(:allowize_any_number_of_times!).with('not_to') + rewriter.process_should_receive(should_receive_object) + end + end + + context 'and Configuration#negative_form_of_to is "to_not"' do + before { configuration.negative_form_of_to = 'to_not' } + + it 'invokes ShouldReceive#allowize_any_number_of_times! with "to_not"' do + should_receive_object.should_receive(:allowize_any_number_of_times!).with('to_not') + rewriter.process_should_receive(should_receive_object) + end + end + end + + context 'when Configuration#replace_deprecated_method? is false' do + before { configuration.replace_deprecated_method = false } + + context 'and Configuration#negative_form_of_to is "not_to"' do + before { configuration.negative_form_of_to = 'not_to' } + + it 'invokes ShouldReceive#expectize! with "not_to"' do + should_receive_object.should_receive(:expectize!).with('not_to') + rewriter.process_should_receive(should_receive_object) + end + end + + context 'and Configuration#negative_form_of_to is "to_not"' do + before { configuration.negative_form_of_to = 'to_not' } + + it 'invokes ShouldReceive#expectize! with "to_not"' do + should_receive_object.should_receive(:expectize!).with('to_not') + rewriter.process_should_receive(should_receive_object) + end + end end end - context 'and Configuration#negative_form_of_to is "to_not"' do - before { configuration.negative_form_of_to = 'to_not' } + context 'and ShouldReceive#any_number_of_times? returns false' do + before { should_receive_object.stub(:any_number_of_times?).and_return(false) } + + [true, false].each do |replace_deprecated_method| + context "when Configuration#replace_deprecated_method? is #{replace_deprecated_method}" do + before { configuration.replace_deprecated_method = replace_deprecated_method } + + context 'and Configuration#negative_form_of_to is "not_to"' do + before { configuration.negative_form_of_to = 'not_to' } + + it 'invokes ShouldReceive#expectize! with "not_to"' do + should_receive_object.should_receive(:expectize!).with('not_to') + rewriter.process_should_receive(should_receive_object) + end + end - it 'invokes ShouldReceive#expectize! with "to_not"' do - Syntax::ShouldReceive.any_instance.should_receive(:expectize!).with('to_not') - rewriter.rewrite(source) + context 'and Configuration#negative_form_of_to is "to_not"' do + before { configuration.negative_form_of_to = 'to_not' } + + it 'invokes ShouldReceive#expectize! with "to_not"' do + should_receive_object.should_receive(:expectize!).with('to_not') + rewriter.process_should_receive(should_receive_object) + end + end + end end end end + shared_examples 'does nothing' do + it 'does nothing' do + should_receive_object.should_not_receive(:expectize!) + should_receive_object.should_not_receive(:allowize_any_number_of_times!) + should_receive_object.should_not_receive(:stubize_any_number_of_times!) + rewriter.process_should_receive(should_receive_object) + end + end + context 'when Configuration#convert_to_expect_to_receive? is false' do before { configuration.convert_to_expect_to_receive = false } - it 'does not invoke ShouldReceive#expectize!' do - Syntax::ShouldReceive.any_instance.should_not_receive(:expectize!) - rewriter.rewrite(source) + context 'and ShouldReceive#any_number_of_times? returns true' do + before { should_receive_object.stub(:any_number_of_times?).and_return(true) } + + context 'when Configuration#replace_deprecated_method? is true' do + before { configuration.replace_deprecated_method = true } + + it 'invokes ShouldReceive#stubize_any_number_of_times! with "not_to"' do + should_receive_object.should_receive(:stubize_any_number_of_times!) + rewriter.process_should_receive(should_receive_object) + end + end + + context 'when Configuration#replace_deprecated_method? is false' do + before { configuration.replace_deprecated_method = false } + + include_examples 'does nothing' + end + end + + context 'and ShouldReceive#any_number_of_times? returns false' do + before { should_receive_object.stub(:any_number_of_times?).and_return(false) } + + [true, false].each do |replace_deprecated_method| + context "when Configuration#replace_deprecated_method? is #{replace_deprecated_method}" do + before { configuration.replace_deprecated_method = replace_deprecated_method } + + include_examples 'does nothing' + end + end + end + + end + end + + describe '#process_method_stub' do + let(:method_stub_object) { double('method_stub_object').as_null_object } + + shared_examples 'invokes MethodStub#allowize!' do + it 'invokes MethodStub#allowize!' do + method_stub_object.should_receive(:allowize!) + rewriter.process_method_stub(method_stub_object) + end + end + + shared_examples 'does not invoke MethodStub#allowize!' do + it 'does not invoke MethodStub#allowize!' do + method_stub_object.should_not_receive(:allowize!) + rewriter.process_method_stub(method_stub_object) + end + end + + shared_examples 'invokes MethodStub#replace_deprecated_method!' do + it 'invokes MethodStub#replace_deprecated_method!' do + method_stub_object.should_receive(:replace_deprecated_method!) + rewriter.process_method_stub(method_stub_object) + end + end + + shared_examples 'does not invoke MethodStub#replace_deprecated_method!' do + it 'does not invoke MethodStub#replace_deprecated_method!' do + method_stub_object.should_not_receive(:replace_deprecated_method!) + rewriter.process_method_stub(method_stub_object) + end + end + + shared_examples 'invokes MethodStub#remove_any_number_of_times!' do + it 'invokes MethodStub#remove_any_number_of_times!' do + method_stub_object.should_receive(:remove_any_number_of_times!) + rewriter.process_method_stub(method_stub_object) + end + end + + shared_examples 'does not invoke MethodStub#remove_any_number_of_times!' do + it 'does not invoke MethodStub#remove_any_number_of_times!' do + method_stub_object.should_not_receive(:remove_any_number_of_times!) + rewriter.process_method_stub(method_stub_object) end end context 'when Configuration#convert_to_allow_to_receive? is true' do before { configuration.convert_to_allow_to_receive = true } - it 'invokes MethodStub#allowize!' do - Syntax::MethodStub.any_instance.should_receive(:allowize!) - rewriter.rewrite(source) + context 'and Configuration#replace_deprecated_method? is true' do + before { configuration.replace_deprecated_method = true } + + include_examples 'invokes MethodStub#allowize!' + include_examples 'does not invoke MethodStub#replace_deprecated_method!' + include_examples 'invokes MethodStub#remove_any_number_of_times!' + end + + context 'and Configuration#replace_deprecated_method? is false' do + before { configuration.replace_deprecated_method = false } + + include_examples 'invokes MethodStub#allowize!' + include_examples 'does not invoke MethodStub#replace_deprecated_method!' + include_examples 'does not invoke MethodStub#remove_any_number_of_times!' end end context 'when Configuration#convert_to_allow_to_receive? is false' do before { configuration.convert_to_allow_to_receive = false } - it 'does not invoke MethodStub#allowize!' do - Syntax::MethodStub.any_instance.should_not_receive(:allowize!) - rewriter.rewrite(source) + context 'and Configuration#replace_deprecated_method? is true' do + before { configuration.replace_deprecated_method = true } + + include_examples 'does not invoke MethodStub#allowize!' + include_examples 'invokes MethodStub#replace_deprecated_method!' + include_examples 'invokes MethodStub#remove_any_number_of_times!' + end + + context 'and Configuration#replace_deprecated_method? is false' do + before { configuration.replace_deprecated_method = false } + + include_examples 'does not invoke MethodStub#allowize!' + include_examples 'does not invoke MethodStub#replace_deprecated_method!' + include_examples 'does not invoke MethodStub#remove_any_number_of_times!' end end + end + + describe '#process_double' do + let(:double_object) { double('double_object').as_null_object } context 'when Configuration#replace_deprecated_method? is true' do before { configuration.replace_deprecated_method = true } it 'invokes Double#convert_to_double!' do - Syntax::Double.any_instance.should_receive(:convert_to_double!) - rewriter.rewrite(source) + double_object.should_receive(:convert_to_double!) + rewriter.process_double(double_object) end + end - it 'invokes BeClose#convert_to_be_within!' do - Syntax::BeClose.any_instance.should_receive(:convert_to_be_within!) - rewriter.rewrite(source) + context 'when Configuration#replace_deprecated_method? is false' do + before { configuration.replace_deprecated_method = false } + + it 'does not invoke Double#convert_to_double!' do + double_object.should_not_receive(:convert_to_double!) + rewriter.process_double(double_object) end end + end + + describe '#process_be_close' do + let(:be_close_object) { double('be_close_object').as_null_object } context 'when Configuration#replace_deprecated_method? is true' do - before { configuration.replace_deprecated_method = false } + before { configuration.replace_deprecated_method = true } - it 'does not invoke Double#convert_to_double!' do - Syntax::Double.any_instance.should_not_receive(:convert_to_double!) - rewriter.rewrite(source) + it 'invokes BeClose#convert_to_be_within!' do + be_close_object.should_receive(:convert_to_be_within!) + rewriter.process_be_close(be_close_object) end + end + + context 'when Configuration#replace_deprecated_method? is true' do + before { configuration.replace_deprecated_method = false } it 'does not invoke BeClose#convert_to_be_within!' do - Syntax::BeClose.any_instance.should_not_receive(:convert_to_be_within!) - rewriter.rewrite(source) + be_close_object.should_not_receive(:convert_to_be_within!) + rewriter.process_be_close(be_close_object) end end + end + + describe '#process_rspec_configure' do + let(:rspec_configure) { double('rspec_configure').as_null_object } context 'when #need_to_modify_expectation_syntax_configuration? returns true' do before do @@ -183,9 +428,8 @@ module Transpec end it 'invokes RSpecConfigure#modify_expectation_syntaxes! with :expect' do - Syntax::RSpecConfigure.any_instance - .should_receive(:modify_expectation_syntaxes!).with(:expect) - rewriter.rewrite(source) + rspec_configure.should_receive(:modify_expectation_syntaxes!).with(:expect) + rewriter.process_rspec_configure(rspec_configure) end end @@ -195,8 +439,8 @@ module Transpec end it 'does not invoke RSpecConfigure#modify_expectation_syntaxes!' do - Syntax::RSpecConfigure.any_instance.should_not_receive(:modify_expectation_syntaxes!) - rewriter.rewrite(source) + rspec_configure.should_not_receive(:modify_expectation_syntaxes!) + rewriter.process_rspec_configure(rspec_configure) end end @@ -206,9 +450,8 @@ module Transpec end it 'invokes RSpecConfigure#modify_mock_syntaxes! with :expect' do - Syntax::RSpecConfigure.any_instance - .should_receive(:modify_mock_syntaxes!).with(:expect) - rewriter.rewrite(source) + rspec_configure.should_receive(:modify_mock_syntaxes!).with(:expect) + rewriter.process_rspec_configure(rspec_configure) end end @@ -218,70 +461,8 @@ module Transpec end it 'does not invoke RSpecConfigure#modify_mock_syntaxes!' do - Syntax::RSpecConfigure.any_instance.should_not_receive(:modify_mock_syntaxes!) - rewriter.rewrite(source) - end - end - - context 'when the source have overlapped rewrite targets' do - let(:source) do - <<-END - describe 'example group' do - it 'is an example' do - object.stub(:message => mock('something')) - end - end - END - end - - let(:expected_source) do - <<-END - describe 'example group' do - it 'is an example' do - allow(object).to receive(:message).and_return(double('something')) - end - end - END - end - - it 'rewrites all targets properly' do - should == expected_source - end - end - - context 'when the source have a monkey-patched expectation outside of example group context' do - before do - configuration.convert_to_expect_to_matcher = true - rewriter.stub(:warn) - end - - let(:source) do - <<-END - describe 'example group' do - class SomeClass - def some_method - 1.should == 1 - end - end - - it 'is an example' do - SomeClass.new.some_method - end - end - END - end - - it 'does not rewrite the expectation to non-monkey-patch syntax' do - should == source - end - - it 'warns to user' do - rewriter.should_receive(:warn) do |message| - message.should =~ /cannot/i - message.should =~ /context/i - end - - rewriter.rewrite(source) + rspec_configure.should_not_receive(:modify_mock_syntaxes!) + rewriter.process_rspec_configure(rspec_configure) end end end diff --git a/spec/transpec/syntax/method_stub_spec.rb b/spec/transpec/syntax/method_stub_spec.rb index 4ec2054..9d2cbcf 100644 --- a/spec/transpec/syntax/method_stub_spec.rb +++ b/spec/transpec/syntax/method_stub_spec.rb @@ -382,6 +382,86 @@ class Syntax end end end + + describe '#any_number_of_times?' do + subject { method_stub_object.any_number_of_times? } + + context 'when it is `subject.stub(:method).any_number_of_times` form' do + let(:source) do + <<-END + it 'responds to #foo' do + subject.stub(:foo).any_number_of_times + end + END + end + + it { should be_true } + end + + context 'when it is `subject.stub(:method).with(arg).any_number_of_times` form' do + let(:source) do + <<-END + it 'responds to #foo with 1' do + subject.stub(:foo).with(1).any_number_of_times + end + END + end + + it { should be_true } + end + + context 'when it is `subject.stub(:method)` form' do + let(:source) do + <<-END + it 'responds to #foo' do + subject.stub(:foo) + end + END + end + + it { should be_false } + end + end + + describe '#remove_any_number_of_times!' do + context 'when it is `subject.stub(:method).any_number_of_times` form' do + let(:source) do + <<-END + it 'responds to #foo' do + subject.stub(:foo).any_number_of_times + end + END + end + + let(:expected_source) do + <<-END + it 'responds to #foo' do + subject.stub(:foo) + end + END + end + + it 'removes `.any_number_of_times`' do + method_stub_object.remove_any_number_of_times! + rewritten_source.should == expected_source + end + end + + context 'when it is `subject.stub(:method)` form' do + let(:source) do + <<-END + it 'responds to #foo' do + subject.stub(:foo) + end + END + end + + it 'does nothing' do + method_stub_object.remove_any_number_of_times! + rewritten_source.should == source + end + end + end end end end diff --git a/spec/transpec/syntax/should_receive_spec.rb b/spec/transpec/syntax/should_receive_spec.rb index 09d532c..bd70573 100644 --- a/spec/transpec/syntax/should_receive_spec.rb +++ b/spec/transpec/syntax/should_receive_spec.rb @@ -282,6 +282,126 @@ class Syntax end end end + + describe '#any_number_of_times?' do + subject { should_receive_object.any_number_of_times? } + + context 'when it is `subject.should_receive(:method).any_number_of_times` form' do + let(:source) do + <<-END + it 'responds to #foo' do + subject.should_receive(:foo).any_number_of_times + end + END + end + + it { should be_true } + end + + context 'when it is `subject.should_receive(:method).with(arg).any_number_of_times` form' do + let(:source) do + <<-END + it 'responds to #foo with 1' do + subject.should_receive(:foo).with(1).any_number_of_times + end + END + end + + it { should be_true } + end + + context 'when it is `subject.should_receive(:method)` form' do + let(:source) do + <<-END + it 'receives to #foo' do + subject.should_receive(:foo) + end + END + end + + it { should be_false } + end + end + + describe '#allowize_any_number_of_times!' do + context 'when it is `subject.should_receive(:method).any_number_of_times` form' do + let(:source) do + <<-END + it 'responds to #foo' do + subject.should_receive(:foo).any_number_of_times + end + END + end + + let(:expected_source) do + <<-END + it 'responds to #foo' do + allow(subject).to receive(:foo) + end + END + end + + it 'converts into `allow(subject).to receive(:method)` form' do + should_receive_object.allowize_any_number_of_times! + rewritten_source.should == expected_source + end + end + + context 'when it is `subject.should_receive(:method)` form' do + let(:source) do + <<-END + it 'receives to #foo' do + subject.should_receive(:foo) + end + END + end + + it 'does nothing' do + should_receive_object.allowize_any_number_of_times! + rewritten_source.should == source + end + end + end + + describe '#stubize_any_number_of_times!' do + context 'when it is `subject.should_receive(:method).any_number_of_times` form' do + let(:source) do + <<-END + it 'responds to #foo' do + subject.should_receive(:foo).any_number_of_times + end + END + end + + let(:expected_source) do + <<-END + it 'responds to #foo' do + subject.stub(:foo) + end + END + end + + it 'converts into `subject.stub(:method)` form' do + should_receive_object.stubize_any_number_of_times! + rewritten_source.should == expected_source + end + end + + context 'when it is `subject.should_receive(:method)` form' do + let(:source) do + <<-END + it 'receives to #foo' do + subject.should_receive(:foo) + end + END + end + + it 'does nothing' do + should_receive_object.stubize_any_number_of_times! + rewritten_source.should == source + end + end + end end end end