Skip to content

Commit

Permalink
Merge pull request #142 from koic/fix_build_error_for_chain_array_all…
Browse files Browse the repository at this point in the history
…ocation

Fix a build error for `Performance/ChainArrayAllocation`
  • Loading branch information
koic authored Jun 23, 2020
2 parents befd184 + eb45fb8 commit c0402b7
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
Original file line number Diff line number Diff line change
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 c0402b7

Please sign in to comment.