diff options
Diffstat (limited to 'spec/rubocop/cop/performance')
3 files changed, 15 insertions, 20 deletions
diff --git a/spec/rubocop/cop/performance/ar_count_each_spec.rb b/spec/rubocop/cop/performance/ar_count_each_spec.rb index 6242c7a4c5e..402e3e93147 100644 --- a/spec/rubocop/cop/performance/ar_count_each_spec.rb +++ b/spec/rubocop/cop/performance/ar_count_each_spec.rb @@ -5,8 +5,6 @@ require 'rubocop' require_relative '../../../../rubocop/cop/performance/ar_count_each.rb' RSpec.describe RuboCop::Cop::Performance::ARCountEach do - include CopHelper - subject(:cop) { described_class.new } context 'when it is not haml file' do @@ -32,8 +30,6 @@ RSpec.describe RuboCop::Cop::Performance::ARCountEach do ^^^^^^^^^^^^ If @users is AR relation, avoid `@users.count ...; @users.each... `, this will trigger two queries. Use `@users.load.size ...; @users.each... ` instead. If @users is an array, try to use @users.size. @users.each { |user| display(user) } SOURCE - - expect(cop.offenses.map(&:cop_name)).to contain_exactly('Performance/ARCountEach') end end diff --git a/spec/rubocop/cop/performance/ar_exists_and_present_blank_spec.rb b/spec/rubocop/cop/performance/ar_exists_and_present_blank_spec.rb index 3321d400ae1..8497ff0e909 100644 --- a/spec/rubocop/cop/performance/ar_exists_and_present_blank_spec.rb +++ b/spec/rubocop/cop/performance/ar_exists_and_present_blank_spec.rb @@ -5,8 +5,6 @@ require 'rubocop' require_relative '../../../../rubocop/cop/performance/ar_exists_and_present_blank.rb' RSpec.describe RuboCop::Cop::Performance::ARExistsAndPresentBlank do - include CopHelper - subject(:cop) { described_class.new } context 'when it is not haml file' do @@ -32,8 +30,6 @@ RSpec.describe RuboCop::Cop::Performance::ARExistsAndPresentBlank do show @users if @users.present? ^^^^^^^^^^^^^^^ Avoid `@users.present?`, because it will generate database query 'Select TABLE.*' which is expensive. Suggest to use `@users.any?` to replace `@users.present?` SOURCE - - expect(cop.offenses.map(&:cop_name)).to contain_exactly('Performance/ARExistsAndPresentBlank') end end @@ -44,8 +40,6 @@ RSpec.describe RuboCop::Cop::Performance::ARExistsAndPresentBlank do show @users if @users.blank? ^^^^^^^^^^^^^ Avoid `@users.blank?`, because it will generate database query 'Select TABLE.*' which is expensive. Suggest to use `@users.empty?` to replace `@users.blank?` SOURCE - - expect(cop.offenses.map(&:cop_name)).to contain_exactly('Performance/ARExistsAndPresentBlank') end end @@ -58,8 +52,6 @@ RSpec.describe RuboCop::Cop::Performance::ARExistsAndPresentBlank do show @users if @users.present? ^^^^^^^^^^^^^^^ Avoid `@users.present?`, because it will generate database query 'Select TABLE.*' which is expensive. Suggest to use `@users.any?` to replace `@users.present?` SOURCE - - expect(cop.offenses.map(&:cop_name)).to contain_exactly('Performance/ARExistsAndPresentBlank', 'Performance/ARExistsAndPresentBlank') end end diff --git a/spec/rubocop/cop/performance/readlines_each_spec.rb b/spec/rubocop/cop/performance/readlines_each_spec.rb index c19426606f6..5a30107722a 100644 --- a/spec/rubocop/cop/performance/readlines_each_spec.rb +++ b/spec/rubocop/cop/performance/readlines_each_spec.rb @@ -5,19 +5,21 @@ require 'rubocop' require_relative '../../../../rubocop/cop/performance/readlines_each' RSpec.describe RuboCop::Cop::Performance::ReadlinesEach do - include CopHelper - subject(:cop) { described_class.new } let(:message) { 'Avoid `IO.readlines.each`, since it reads contents into memory in full. Use `IO.each_line` or `IO.each` instead.' } shared_examples_for(:class_read) do |klass| context "and it is called as a class method on #{klass}" do - # We can't use `expect_offense` here because indentation changes based on `klass` it 'flags it as an offense' do - inspect_source "#{klass}.readlines(file_path).each { |line| puts line }" + leading_readline = "#{klass}.readlines(file_path)." + padding = " " * leading_readline.length + node = "#{leading_readline}each { |line| puts line }" - expect(cop.offenses.map(&:cop_name)).to contain_exactly('Performance/ReadlinesEach') + expect_offense(<<~CODE, node: node) + %{node} + #{padding}^^^^ Avoid `IO.readlines.each`, since it reads contents into memory in full. [...] + CODE end end @@ -62,9 +64,14 @@ RSpec.describe RuboCop::Cop::Performance::ReadlinesEach do end it 'autocorrects `readlines.each` to `each_line`' do - expect(autocorrect_source('obj.readlines.each { |line| line }')).to( - eq('obj.each_line { |line| line }') - ) + expect_offense(<<~CODE) + obj.readlines.each { |line| line } + ^^^^ Avoid `IO.readlines.each`, since it reads contents into memory in full. [...] + CODE + + expect_correction(<<~CODE) + obj.each_line { |line| line } + CODE end end |