Skip to content

Commit

Permalink
Merge pull request #154 from koic/use_new_expect_correction
Browse files Browse the repository at this point in the history
Use new `expect_offense`, `expect_correction` instead of old APIs
  • Loading branch information
MarttiCheng committed Aug 4, 2020
2 parents dec1e3a + 394e479 commit 05634f1
Show file tree
Hide file tree
Showing 17 changed files with 906 additions and 788 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,10 @@
* [#140](https://github.com/rubocop-hq/rubocop-performance/pull/140): Add new `Performance/CollectionLiteralInLoop` cop. ([@fatkodima][])
* [#137](https://github.com/rubocop-hq/rubocop-performance/pull/137): Add new `Performance/Sum` cop. ([@fatkodima][])

### Changes

* [#154](https://github.com/rubocop-hq/rubocop-performance/pull/154): Require RuboCop 0.87 or higher. ([@koic][])

## 1.7.1 (2020-07-18)

### Bug fixes
Expand Down
2 changes: 1 addition & 1 deletion rubocop-performance.gemspec
Expand Up @@ -29,6 +29,6 @@ Gem::Specification.new do |s|
'bug_tracker_uri' => 'https://github.com/rubocop-hq/rubocop-performance/issues'
}

s.add_runtime_dependency('rubocop', '>= 0.82.0')
s.add_runtime_dependency('rubocop', '>= 0.87.0')
s.add_development_dependency('simplecov')
end
208 changes: 132 additions & 76 deletions spec/rubocop/cop/performance/casecmp_spec.rb
Expand Up @@ -4,114 +4,170 @@
subject(:cop) { described_class.new }

shared_examples 'selectors' do |selector|
it "autocorrects str.#{selector} ==" do
new_source = autocorrect_source("str.#{selector} == 'string'")
expect(new_source).to eq "str.casecmp('string').zero?"
it "registers an offense and corrects str.#{selector} ==" do
expect_offense(<<~RUBY, selector: selector)
str.#{selector} == 'string'
^^^^^{selector}^^^^^^^^^^^^ Use `str.casecmp('string').zero?` instead of `str.#{selector} == 'string'`.
RUBY

expect_correction(<<~RUBY)
str.casecmp('string').zero?
RUBY
end

it "autocorrects str.#{selector} == with parens around arg" do
new_source = autocorrect_source("str.#{selector} == ('string')")
expect(new_source).to eq "str.casecmp('string').zero?"
it "registers an offense and corrects str.#{selector} == with parens around arg" do
expect_offense(<<~RUBY, selector: selector)
str.#{selector} == ('string')
^^^^^{selector}^^^^^^^^^^^^^^ Use `str.casecmp('string').zero?` instead of `str.#{selector} == ('string')`.
RUBY

expect_correction(<<~RUBY)
str.casecmp('string').zero?
RUBY
end

it "autocorrects str.#{selector} !=" do
new_source = autocorrect_source("str.#{selector} != 'string'")
expect(new_source).to eq "!str.casecmp('string').zero?"
it "registers an offense and corrects str.#{selector} !=" do
expect_offense(<<~RUBY, selector: selector)
str.#{selector} != 'string'
^^^^^{selector}^^^^^^^^^^^^ Use `str.casecmp('string').zero?` instead of `str.#{selector} != 'string'`.
RUBY

expect_correction(<<~RUBY)
!str.casecmp('string').zero?
RUBY
end

it "autocorrects str.#{selector} != with parens around arg" do
new_source = autocorrect_source("str.#{selector} != ('string')")
expect(new_source).to eq "!str.casecmp('string').zero?"
it "registers an offense and corrects str.#{selector} != with parens around arg" do
expect_offense(<<~RUBY, selector: selector)
str.#{selector} != ('string')
^^^^^{selector}^^^^^^^^^^^^^^ Use `str.casecmp('string').zero?` instead of `str.#{selector} != ('string')`.
RUBY

expect_correction(<<~RUBY)
!str.casecmp('string').zero?
RUBY
end

it "autocorrects str.#{selector}.eql? without parens" do
new_source = autocorrect_source("str.#{selector}.eql? 'string'")
expect(new_source).to eq "str.casecmp('string').zero?"
it "registers an offense and corrects str.#{selector}.eql? without parens" do
expect_offense(<<~RUBY, selector: selector)
str.#{selector}.eql? 'string'
^^^^^{selector}^^^^^^^^^^^^^^ Use `str.casecmp('string').zero?` instead of `str.#{selector}.eql? 'string'`.
RUBY

expect_correction(<<~RUBY)
str.casecmp('string').zero?
RUBY
end

it "autocorrects str.#{selector}.eql? with parens" do
new_source = autocorrect_source("str.#{selector}.eql?('string')")
expect(new_source).to eq "str.casecmp('string').zero?"
it "registers an offense and corrects str.#{selector}.eql? with parens" do
expect_offense(<<~RUBY, selector: selector)
str.#{selector}.eql?('string')
^^^^^{selector}^^^^^^^^^^^^^^^ Use `str.casecmp('string').zero?` instead of `str.#{selector}.eql?('string')`.
RUBY

expect_correction(<<~RUBY)
str.casecmp('string').zero?
RUBY
end

it "autocorrects str.#{selector}.eql? with parens and funny spacing" do
new_source = autocorrect_source("str.#{selector}.eql? ( 'string' )")
expect(new_source).to eq "str.casecmp( 'string' ).zero?"
it "registers an offense and corrects str.#{selector}.eql? with parens and funny spacing" do
expect_offense(<<~RUBY, selector: selector)
str.#{selector}.eql? ( 'string' )
^^^^^{selector}^^^^^^^^^^^^^^^^^^ Use `str.casecmp( 'string' ).zero?` instead of `str.#{selector}.eql? ( 'string' )`.
RUBY

expect_correction(<<~RUBY)
str.casecmp( 'string' ).zero?
RUBY
end

it "autocorrects == str.#{selector}" do
new_source = autocorrect_source("'string' == str.#{selector}")
expect(new_source).to eq "str.casecmp('string').zero?"
it "registers an offense and corrects == str.#{selector}" do
expect_offense(<<~RUBY, selector: selector)
'string' == str.#{selector}
^^^^^^^^^^^^^^^^^{selector} Use `str.casecmp('string').zero?` instead of `'string' == str.#{selector}`.
RUBY

expect_correction(<<~RUBY)
str.casecmp('string').zero?
RUBY
end

it "autocorrects string with parens == str.#{selector}" do
new_source = autocorrect_source("('string') == str.#{selector}")
expect(new_source).to eq "str.casecmp('string').zero?"
it "registers an offense and corrects string with parens == str.#{selector}" do
expect_offense(<<~RUBY, selector: selector)
('string') == str.#{selector}
^^^^^^^^^^^^^^^^^^^{selector} Use `str.casecmp('string').zero?` instead of `('string') == str.#{selector}`.
RUBY

expect_correction(<<~RUBY)
str.casecmp('string').zero?
RUBY
end

it "autocorrects string != str.#{selector}" do
new_source = autocorrect_source("'string' != str.#{selector}")
expect(new_source).to eq "!str.casecmp('string').zero?"
it "registers an offense and corrects string != str.#{selector}" do
expect_offense(<<~RUBY, selector: selector)
'string' != str.#{selector}
^^^^^^^^^^^^^^^^^{selector} Use `str.casecmp('string').zero?` instead of `'string' != str.#{selector}`.
RUBY

expect_correction(<<~RUBY)
!str.casecmp('string').zero?
RUBY
end

it 'autocorrects string with parens and funny spacing ' \
it 'registers an offense and corrects string with parens and funny spacing ' \
"eql? str.#{selector}" do
new_source = autocorrect_source("( 'string' ).eql? str.#{selector}")
expect(new_source).to eq "str.casecmp( 'string' ).zero?"
end
expect_offense(<<~RUBY, selector: selector)
( 'string' ).eql? str.#{selector}
^^^^^^^^^^^^^^^^^^^^^^^{selector} Use `str.casecmp( 'string' ).zero?` instead of `( 'string' ).eql? str.#{selector}`.
RUBY

it "autocorrects string.eql? str.#{selector} without parens " do
new_source = autocorrect_source("'string'.eql? str.#{selector}")
expect(new_source).to eq "str.casecmp('string').zero?"
expect_correction(<<~RUBY)
str.casecmp( 'string' ).zero?
RUBY
end

it "autocorrects string.eql? str.#{selector} with parens " do
new_source = autocorrect_source("'string'.eql?(str.#{selector})")
expect(new_source).to eq "str.casecmp('string').zero?"
end
it "registers an offense and corrects string.eql? str.#{selector} without parens " do
expect_offense(<<~RUBY, selector: selector)
'string'.eql? str.#{selector}
^^^^^^^^^^^^^^^^^^^{selector} Use `str.casecmp('string').zero?` instead of `'string'.eql? str.#{selector}`.
RUBY

it "autocorrects obj.#{selector} == str.#{selector}" do
new_source = autocorrect_source("obj.#{selector} == str.#{selector}")
expect(new_source).to eq 'obj.casecmp(str).zero?'
expect_correction(<<~RUBY)
str.casecmp('string').zero?
RUBY
end

it "autocorrects obj.#{selector} eql? str.#{selector}" do
new_source = autocorrect_source("obj.#{selector}.eql? str.#{selector}")
expect(new_source).to eq 'obj.casecmp(str).zero?'
end
it "registers an offense and corrects string.eql? str.#{selector} with parens " do
expect_offense(<<~RUBY, selector: selector)
'string'.eql?(str.#{selector})
^^^^^^^^^^^^^^^^^^^{selector}^ Use `str.casecmp('string').zero?` instead of `'string'.eql?(str.#{selector})`.
RUBY

it "formats the error message correctly for str.#{selector} ==" do
inspect_source("str.#{selector} == 'string'")
expect(cop.highlights).to eq(["str.#{selector} == 'string'"])
expect(cop.messages).to eq(
[
"Use `str.casecmp('string').zero?` instead of " \
"`str.#{selector} == 'string'`."
]
)
expect_correction(<<~RUBY)
str.casecmp('string').zero?
RUBY
end

it "formats the error message correctly for == str.#{selector}" do
inspect_source("'string' == str.#{selector}")
expect(cop.highlights).to eq(["'string' == str.#{selector}"])
expect(cop.messages).to eq(
[
"Use `str.casecmp('string').zero?` instead of " \
"`'string' == str.#{selector}`."
]
)
it "registers an offense and corrects obj.#{selector} == str.#{selector}" do
expect_offense(<<~RUBY, selector: selector)
obj.#{selector} == str.#{selector}
^^^^^{selector}^^^^^^^^^{selector} Use `obj.casecmp(str).zero?` instead of `obj.#{selector} == str.#{selector}`.
RUBY

expect_correction(<<~RUBY)
obj.casecmp(str).zero?
RUBY
end

it 'formats the error message correctly for ' \
"obj.#{selector} == str.#{selector}" do
inspect_source("obj.#{selector} == str.#{selector}")
expect(cop.highlights).to eq(["obj.#{selector} == str.#{selector}"])
expect(cop.messages).to eq(
[
'Use `obj.casecmp(str).zero?` instead of ' \
"`obj.#{selector} == str.#{selector}`."
]
)
it "registers an offense and corrects obj.#{selector} eql? str.#{selector}" do
expect_offense(<<~RUBY, selector: selector)
obj.#{selector}.eql? str.#{selector}
^^^^^{selector}^^^^^^^^^^^{selector} Use `obj.casecmp(str).zero?` instead of `obj.#{selector}.eql? str.#{selector}`.
RUBY

expect_correction(<<~RUBY)
obj.casecmp(str).zero?
RUBY
end

it "doesn't report an offense for variable == str.#{selector}" do
Expand Down
20 changes: 11 additions & 9 deletions spec/rubocop/cop/performance/chain_array_allocation_spec.rb
Expand Up @@ -11,11 +11,10 @@ def generate_message(method_one, method_two)

shared_examples 'map_and_flat' do |method, method_two|
it "registers an offense when calling #{method}...#{method_two}" do
inspect_source("[1, 2, 3, 4].#{method} { |e| [e, e] }.#{method_two}")

expect(cop.messages)
.to eq([generate_message(method, method_two)])
expect(cop.highlights).to eq([".#{method_two}"])
expect_offense(<<~RUBY, method: method, method_two: method_two)
[1, 2, 3, 4].#{method} { |e| [e, e] }.#{method_two}
_{method} ^^{method_two} #{generate_message(method, method_two)}
RUBY
end
end

Expand Down Expand Up @@ -50,11 +49,14 @@ def generate_message(method_one, method_two)
describe 'methods that only return an array with no block' do
it 'zip' do
# Yes I know this is not valid Ruby
inspect_source('[1, 2, 3, 4].zip {|f| }.uniq')
expect(cop.messages.empty?).to be(true)
expect_no_offenses(<<~RUBY)
[1, 2, 3, 4].zip {|f| }.uniq
RUBY

inspect_source('[1, 2, 3, 4].zip.uniq')
expect(cop.messages.empty?).to be(false)
expect_offense(<<~RUBY)
[1, 2, 3, 4].zip {|f| }.zip.uniq
^^^^^ Use unchained `zip!` and `uniq!` (followed by `return array` if required) instead of chaining `zip...uniq`.
RUBY
end
end
end

0 comments on commit 05634f1

Please sign in to comment.