Skip to content

Commit

Permalink
Fix false positive conversion of #raise_error with #with_message
Browse files Browse the repository at this point in the history
This fixes #41.
  • Loading branch information
yujinakayama committed Feb 22, 2014
1 parent 09525b4 commit db4730f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 127 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@
## Development

* Fix a bug where `expect(method_returns_collection :some_arg).to have(2).items` was converted to `expect(method_returns_collection :some_arg.size).to eq(2)` ([#43](https://github.com/yujinakayama/transpec/issues/43))
* Fix false positive conversion of `raise_error` with `with_message` ([#41](https://github.com/yujinakayama/transpec/issues/41))

## v1.9.1

Expand Down
12 changes: 1 addition & 11 deletions lib/transpec/syntax/raise_error.rb
Expand Up @@ -14,7 +14,7 @@ def self.target_method?(receiver_node, method_name)
end

def remove_error_specification_with_negative_expectation!
return if positive?
return if expectation.positive?

_receiver_node, _method_name, *arg_nodes = *node
return if arg_nodes.empty?
Expand All @@ -24,16 +24,6 @@ def remove_error_specification_with_negative_expectation!
register_record
end

def positive?
@node.each_ancestor_node do |ancestor_node|
next unless ancestor_node.send_type?
expectation_method_name = ancestor_node.children[1]
return [:should, :to].include?(expectation_method_name)
end

false
end

private

def register_record
Expand Down
138 changes: 22 additions & 116 deletions spec/transpec/syntax/raise_error_spec.rb
Expand Up @@ -14,122 +14,6 @@ class Syntax

let(:record) { raise_error_object.report.records.first }

describe '#positive?' do
subject { raise_error_object.positive? }

context 'when it is `lambda { }.should raise_error` form' do
let(:source) do
<<-END
describe 'example' do
it 'raises error' do
lambda { do_something }.should raise_error
end
end
END
end

let(:raise_error_object) { should_object.raise_error_matcher }

it { should be_true }
end

context 'when it is `expect { }.to raise_error` form' do
let(:source) do
<<-END
describe 'example' do
it 'raises error' do
expect { do_something }.to raise_error
end
end
END
end

let(:raise_error_object) { expect_object.raise_error_matcher }

it { should be_true }
end

context 'when it is `lambda { }.should raise_error { |error| ... }` form' do
let(:source) do
<<-END
describe 'example' do
it 'raises error' do
lambda { do_something }.should raise_error { |error| do_anything }
end
end
END
end

let(:raise_error_object) { should_object.raise_error_matcher }

it { should be_true }
end

context 'when it is `expect { }.to raise_error { |error| ... }` form' do
let(:source) do
<<-END
describe 'example' do
it 'raises error' do
expect { do_something }.to raise_error { |error| do_anything }
end
end
END
end

let(:raise_error_object) { expect_object.raise_error_matcher }

it { should be_true }
end

context 'when it is `lambda { }.should_not raise_error` form' do
let(:source) do
<<-END
describe 'example' do
it 'does not raise error' do
lambda { do_something }.should_not raise_error
end
end
END
end

let(:raise_error_object) { should_object.raise_error_matcher }

it { should be_false }
end

context 'when it is `expect { }.not_to raise_error` form' do
let(:source) do
<<-END
describe 'example' do
it 'does not raise error' do
expect { do_something }.not_to raise_error
end
end
END
end

let(:raise_error_object) { expect_object.raise_error_matcher }

it { should be_false }
end

context 'when it is `expect { }.to_not raise_error` form' do
let(:source) do
<<-END
describe 'example' do
it 'does not raise error' do
expect { do_something }.to_not raise_error
end
end
END
end

let(:raise_error_object) { expect_object.raise_error_matcher }

it { should be_false }
end
end

describe '#remove_error_specification_with_negative_expectation!' do
before do
raise_error_object.remove_error_specification_with_negative_expectation!
Expand Down Expand Up @@ -223,6 +107,28 @@ class Syntax
end
end

context 'when it is `expect { }.to raise_error(SpecificErrorClass).with_message(message)` form' do
let(:source) do
<<-END
describe 'example' do
it 'raises SpecificErrorClass with message' do
expect { do_something }.to raise_error(SpecificErrorClass).with_message(message)
end
end
END
end

let(:raise_error_object) { expect_object.raise_error_matcher }

it 'does nothing' do
rewritten_source.should == source
end

it 'reports nothing' do
raise_error_object.report.records.should be_empty
end
end

context 'when it is `lambda { }.should_not raise_error(SpecificErrorClass)` form' do
let(:source) do
<<-END
Expand Down

0 comments on commit db4730f

Please sign in to comment.