From 4a3feff6887a34ff7df5c0232f11ceff82c328e9 Mon Sep 17 00:00:00 2001 From: Rena Watson Date: Tue, 23 Jun 2020 12:45:15 +0900 Subject: [PATCH] Fix a build error for `Performance/ChainArrayAllocation` 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 ' ``` https://circleci.com/gh/rubocop-hq/rubocop-performance/1775 https://github.com/rubocop-hq/rubocop/pull/7868 has detected this wrong test case and this PR fixed it. --- .../chain_array_allocation_spec.rb | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/spec/rubocop/cop/performance/chain_array_allocation_spec.rb b/spec/rubocop/cop/performance/chain_array_allocation_spec.rb index 486d5ce..325b63c 100644 --- a/spec/rubocop/cop/performance/chain_array_allocation_spec.rb +++ b/spec/rubocop/cop/performance/chain_array_allocation_spec.rb @@ -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