Skip to content

Commit

Permalink
Fix a build error for Performance/ChainArrayAllocation
Browse files Browse the repository at this point in the history
This PR fixes the following build error.

```console
Failures:

  1) RuboCop::Cop::Performance::ChainArrayAllocation Methods that
     require an argument first
     Failure/Error: expect(cop.messages.empty?).to be(false)

       expected false
            got true
       # ./spec/rubocop/cop/performance/chain_array_allocation_spec.rb:39:in
       `block (3 levels) in <top (required)>'
```

https://circleci.com/gh/rubocop-hq/rubocop-performance/1775

rubocop/rubocop#7868 has detected this wrong
test case and this PR fixed it.
  • Loading branch information
renawatson68 committed Jun 23, 2020
1 parent e57ff58 commit 4a3feff
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions spec/rubocop/cop/performance/chain_array_allocation_spec.rb
Expand Up @@ -24,22 +24,26 @@ def generate_message(method_one, method_two)
end

describe 'Methods that require an argument' do
it 'first' do
it 'does not register an offense for `first.uniq`' do
# Yes I know this is not valid Ruby
inspect_source('[1, 2, 3, 4].first.uniq')
expect(cop.messages.empty?).to be(true)
expect_no_offenses(<<~RUBY)
[1, 2, 3, 4].first.uniq
RUBY
end

inspect_source('[1, 2, 3, 4].first(10).uniq')
expect(cop.messages.empty?).to be(false)
expect(cop.messages)
.to eq([generate_message('first', 'uniq')])
expect(cop.highlights).to eq(['.uniq'])
it 'registers an offense for `first(10).uniq`' do
expect_offense(<<~RUBY)
[1, 2, 3, 4].first(10).uniq
^^^^^ Use unchained `first!` and `uniq!` (followed by `return array` if required) instead of chaining `first...uniq`.
RUBY
end

inspect_source('[1, 2, 3, 4].first(variable).uniq')
expect(cop.messages.empty?).to be(false)
expect(cop.messages)
.to eq([generate_message('first', 'uniq')])
expect(cop.highlights).to eq(['.uniq'])
it 'registers an offense for `first(variable).uniq`' do
expect_offense(<<~RUBY)
variable = 42
[1, 2, 3, 4].first(variable).uniq
^^^^^ Use unchained `first!` and `uniq!` (followed by `return array` if required) instead of chaining `first...uniq`.
RUBY
end
end

Expand Down

0 comments on commit 4a3feff

Please sign in to comment.