diff --git a/spec/rubocop/cop/bundler/insecure_protocol_source_spec.rb b/spec/rubocop/cop/bundler/insecure_protocol_source_spec.rb index 5fc4cd53011..8cb69661a0e 100644 --- a/spec/rubocop/cop/bundler/insecure_protocol_source_spec.rb +++ b/spec/rubocop/cop/bundler/insecure_protocol_source_spec.rb @@ -10,6 +10,10 @@ source :gemcutter ^^^^^^^^^^ The source `:gemcutter` is deprecated because HTTP requests are insecure. Please change your source to 'https://rubygems.org' if possible, or 'http://rubygems.org' if not. RUBY + + expect_correction(<<-RUBY.strip_indent) + source 'https://rubygems.org' + RUBY end it 'registers an offense when using `source :rubygems`' do @@ -17,6 +21,10 @@ source :rubygems ^^^^^^^^^ The source `:rubygems` is deprecated because HTTP requests are insecure. Please change your source to 'https://rubygems.org' if possible, or 'http://rubygems.org' if not. RUBY + + expect_correction(<<-RUBY.strip_indent) + source 'https://rubygems.org' + RUBY end it 'registers an offense when using `source :rubyforge`' do @@ -24,23 +32,9 @@ source :rubyforge ^^^^^^^^^^ The source `:rubyforge` is deprecated because HTTP requests are insecure. Please change your source to 'https://rubygems.org' if possible, or 'http://rubygems.org' if not. RUBY - end - - it 'autocorrects `source :gemcutter`' do - new_source = autocorrect_source('source :gemcutter') - expect(new_source).to eq("source 'https://rubygems.org'") - end - - it 'autocorrects `source :rubygems`' do - new_source = autocorrect_source('source :rubygems') - - expect(new_source).to eq("source 'https://rubygems.org'") - end - - it 'autocorrects `source :rubyforge`' do - new_source = autocorrect_source('source :rubyforge') - - expect(new_source).to eq("source 'https://rubygems.org'") + expect_correction(<<-RUBY.strip_indent) + source 'https://rubygems.org' + RUBY end end diff --git a/spec/rubocop/cop/bundler/ordered_gems_spec.rb b/spec/rubocop/cop/bundler/ordered_gems_spec.rb index 512a663f799..9ac8d013898 100644 --- a/spec/rubocop/cop/bundler/ordered_gems_spec.rb +++ b/spec/rubocop/cop/bundler/ordered_gems_spec.rb @@ -50,11 +50,8 @@ gem 'rspec' ^^^^^^^^^^^ Gems should be sorted in an alphabetical order within their section of the Gemfile. Gem `rspec` should appear before `rubocop`. RUBY - end - it 'autocorrects' do - new_source = autocorrect_source_with_loop(source) - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) gem 'rspec' gem 'rubocop' RUBY @@ -87,11 +84,8 @@ gem 'rspec' ^^^^^^^^^^^ Gems should be sorted in an alphabetical order within their section of the Gemfile. Gem `rspec` should appear before `rubocop`. RUBY - end - it 'autocorrects' do - new_source = autocorrect_source_with_loop(source) - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) gem 'rspec' gem 'rubocop', '0.1.1' @@ -191,11 +185,8 @@ gem 'rspec' ^^^^^^^^^^^ Gems should be sorted in an alphabetical order within their section of the Gemfile. Gem `rspec` should appear before `rubocop`. RUBY - end - it 'autocorrects' do - new_source = autocorrect_source_with_loop(source) - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) # For # test gem 'rspec' @@ -262,11 +253,8 @@ gem 'a' ^^^^^^^ Gems should be sorted in an alphabetical order within their section of the Gemfile. Gem `a` should appear before `Z`. RUBY - end - it 'autocorrects' do - new_source = autocorrect_source_with_loop(source) - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) gem 'a' gem 'Z' RUBY @@ -295,11 +283,8 @@ ^^^^^^^ Gems should be sorted in an alphabetical order within their section of the Gemfile. Gem `b` should appear before `c`. end RUBY - end - it 'autocorrects' do - new_source = autocorrect_source_with_loop(source) - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) gem 'a' group :development do diff --git a/spec/rubocop/cop/internal_affairs/offense_location_keyword_spec.rb b/spec/rubocop/cop/internal_affairs/offense_location_keyword_spec.rb index 3ade36fd795..bdfb5b8a5d8 100644 --- a/spec/rubocop/cop/internal_affairs/offense_location_keyword_spec.rb +++ b/spec/rubocop/cop/internal_affairs/offense_location_keyword_spec.rb @@ -5,14 +5,18 @@ context 'when `node.loc.selector` is passed' do it 'registers an offense' do - expect_offense(<<-RUBY.strip_indent, 'example_cop.rb') + expect_offense(<<-RUBY.strip_indent) add_offense(node, location: node.loc.selector) ^^^^^^^^^^^^^^^^^ Use `:selector` as the location argument to `#add_offense`. RUBY + + expect_correction(<<-RUBY.strip_indent) + add_offense(node, location: :selector) + RUBY end it 'registers an offense if message argument is passed' do - expect_offense(<<-RUBY.strip_indent, 'example_cop.rb') + expect_offense(<<-RUBY.strip_indent) add_offense( node, message: 'message', @@ -20,6 +24,14 @@ ^^^^^^^^^^^^^^^^^ Use `:selector` as the location argument to `#add_offense`. ) RUBY + + expect_correction(<<-RUBY.strip_indent) + add_offense( + node, + message: 'message', + location: :selector + ) + RUBY end end @@ -34,30 +46,4 @@ add_offense(node, location: other_node.loc.selector) RUBY end - - it 'auto-corrects `location` when it is the only keyword' do - corrected = - autocorrect_source('add_offense(node, location: node.loc.selector)') - - expect(corrected).to eq('add_offense(node, location: :selector)') - end - - it 'auto-corrects `location` when there are other keywords' do - corrected = autocorrect_source(<<-RUBY.strip_indent) - add_offense( - node, - message: 'foo', - location: node.loc.selector, - severity: :warning - ) - RUBY - expect(corrected).to eq(<<-RUBY.strip_indent) - add_offense( - node, - message: 'foo', - location: :selector, - severity: :warning - ) - RUBY - end end diff --git a/spec/rubocop/cop/internal_affairs/redundant_location_argument_spec.rb b/spec/rubocop/cop/internal_affairs/redundant_location_argument_spec.rb index fcd9b831618..38a7c4572fe 100644 --- a/spec/rubocop/cop/internal_affairs/redundant_location_argument_spec.rb +++ b/spec/rubocop/cop/internal_affairs/redundant_location_argument_spec.rb @@ -10,6 +10,10 @@ add_offense(node, location: :expression) ^^^^^^^^^^^^^^^^^^^^^ Redundant location argument to `#add_offense`. RUBY + + expect_correction(<<-RUBY.strip_indent) + add_offense(node) + RUBY end context 'when there is a message argument' do @@ -18,50 +22,36 @@ add_offense(node, location: :expression, message: 'message') ^^^^^^^^^^^^^^^^^^^^^ Redundant location argument to `#add_offense`. RUBY - end - end - it 'removes default `location` when there are no other keywords' do - corrected = autocorrect_source(<<-RUBY.strip_indent) - add_offense(node, location: :expression) - RUBY - - expect(corrected).to eq(<<-RUBY.strip_indent) - add_offense(node) - RUBY + expect_correction(<<-RUBY.strip_indent) + add_offense(node, message: 'message') + RUBY + end end it 'removes default `location` when preceded by another keyword' do - corrected = autocorrect_source(<<-RUBY.strip_indent) + expect_offense(<<-RUBY.strip_indent) add_offense(node, message: 'foo', location: :expression) + ^^^^^^^^^^^^^^^^^^^^^ Redundant location argument to `#add_offense`. RUBY - expect(corrected).to eq(<<-RUBY.strip_indent) - add_offense(node, message: 'foo') - RUBY - end - - it 'removes default `location` when followed by another keyword' do - corrected = autocorrect_source(<<-RUBY.strip_indent) - add_offense(node, location: :expression, message: 'foo') - RUBY - - expect(corrected).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) add_offense(node, message: 'foo') RUBY end it 'removes default `location` surrounded by other keywords' do - corrected = autocorrect_source(<<-RUBY.strip_indent) + expect_offense(<<-RUBY.strip_indent) add_offense( node, severity: :error, location: :expression, + ^^^^^^^^^^^^^^^^^^^^^ Redundant location argument to `#add_offense`. message: 'message' ) RUBY - expect(corrected).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) add_offense( node, severity: :error, diff --git a/spec/rubocop/cop/internal_affairs/redundant_message_argument_spec.rb b/spec/rubocop/cop/internal_affairs/redundant_message_argument_spec.rb index b1570d8ddf3..86ca9541d5a 100644 --- a/spec/rubocop/cop/internal_affairs/redundant_message_argument_spec.rb +++ b/spec/rubocop/cop/internal_affairs/redundant_message_argument_spec.rb @@ -6,15 +6,13 @@ context 'when `MSG` is passed' do it 'registers an offense' do expect_offense(<<-RUBY.strip_indent, 'example_cop.rb') - add_offense(node, message: MSG) - ^^^^^^^^^^^^ Redundant message argument to `#add_offense`. + add_offense(node, message: MSG) + ^^^^^^^^^^^^ Redundant message argument to `#add_offense`. RUBY - end - - it 'auto-corrects' do - new_source = autocorrect_source('add_offense(node, message: MSG)') - expect(new_source).to eq('add_offense(node)') + expect_correction(<<-RUBY.strip_indent) + add_offense(node) + RUBY end end @@ -26,23 +24,17 @@ context 'when `#message` is passed' do it 'registers an offense' do - expect_offense(<<-RUBY.strip_indent, 'example_cop.rb') - add_offense(node, location: :expression, message: message) - ^^^^^^^^^^^^^^^^ Redundant message argument to `#add_offense`. - RUBY - end - - it 'auto-corrects' do - new_source = autocorrect_source(<<-RUBY.strip_indent) + expect_offense(<<-RUBY.strip_indent) add_offense( node, location: :expression, message: message, + ^^^^^^^^^^^^^^^^ Redundant message argument to `#add_offense`. severity: :error ) RUBY - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) add_offense( node, location: :expression, @@ -59,13 +51,10 @@ add_offense(node, message: message(node)) ^^^^^^^^^^^^^^^^^^^^^^ Redundant message argument to `#add_offense`. RUBY - end - - it 'auto-corrects' do - new_source = - autocorrect_source('add_offense(node, message: message(node))') - expect(new_source).to eq('add_offense(node)') + expect_correction(<<-RUBY.strip_indent) + add_offense(node) + RUBY end end @@ -78,18 +67,8 @@ ^^^^^^^^^^^^^^^^^^^^^^ Redundant message argument to `#add_offense`. severity: :fatal) RUBY - end - - it 'auto-corrects' do - new_source = - autocorrect_source(<<-RUBY.strip_indent) - add_offense(node, - location: :selector, - message: message(node), - severity: :fatal) - RUBY - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) add_offense(node, location: :selector, severity: :fatal) diff --git a/spec/rubocop/cop/rails/active_record_aliases_spec.rb b/spec/rubocop/cop/rails/active_record_aliases_spec.rb index 8be77ddbe58..2fd8fdd97b7 100644 --- a/spec/rubocop/cop/rails/active_record_aliases_spec.rb +++ b/spec/rubocop/cop/rails/active_record_aliases_spec.rb @@ -4,34 +4,28 @@ subject(:cop) { described_class.new } describe '#update_attributes' do - it 'registers an offense' do + it 'registers an offense and corrects' do expect_offense(<<-RUBY.strip_indent) book.update_attributes(author: "Alice") ^^^^^^^^^^^^^^^^^ Use `update` instead of `update_attributes`. RUBY - end - it 'is autocorrected' do - new_source = autocorrect_source( - 'book.update_attributes(author: "Alice")' - ) - expect(new_source).to eq 'book.update(author: "Alice")' + expect_correction(<<-RUBY.strip_indent) + book.update(author: "Alice") + RUBY end end describe '#update_attributes!' do - it 'registers an offense' do + it 'registers an offense and corrects' do expect_offense(<<-RUBY.strip_indent) book.update_attributes!(author: "Bob") ^^^^^^^^^^^^^^^^^^ Use `update!` instead of `update_attributes!`. RUBY - end - it 'is autocorrected' do - new_source = autocorrect_source( - 'book.update_attributes!(author: "Bob")' - ) - expect(new_source).to eq 'book.update!(author: "Bob")' + expect_correction(<<-RUBY.strip_indent) + book.update!(author: "Bob") + RUBY end end @@ -39,32 +33,24 @@ it 'does not register an offense' do expect_no_offenses('book.update(author: "Alice")') end - - it 'is not autocorrected' do - source = 'book.update(author: "Alice")' - new_source = autocorrect_source(source) - expect(new_source).to eq source - end end describe '#update!' do it 'does not register an offense' do expect_no_offenses('book.update!(author: "Bob")') end - - it 'is not autocorrected' do - source = 'book.update!(author: "Bob")' - new_source = autocorrect_source(source) - expect(new_source).to eq source - end end describe 'other use of the `update_attributes` string' do it 'does not autocorrect the other usage' do - new_source = autocorrect_source( - 'update_attributes_book.update_attributes(author: "Alice")' - ) - expect(new_source).to eq 'update_attributes_book.update(author: "Alice")' + expect_offense(<<-RUBY.strip_indent) + update_attributes_book.update_attributes(author: "Alice") + ^^^^^^^^^^^^^^^^^ Use `update` instead of `update_attributes`. + RUBY + + expect_correction(<<-RUBY.strip_indent) + update_attributes_book.update(author: "Alice") + RUBY end end end diff --git a/spec/rubocop/cop/rails/active_support_aliases_spec.rb b/spec/rubocop/cop/rails/active_support_aliases_spec.rb index 200d9f04921..10d1b122390 100644 --- a/spec/rubocop/cop/rails/active_support_aliases_spec.rb +++ b/spec/rubocop/cop/rails/active_support_aliases_spec.rb @@ -5,18 +5,15 @@ describe 'String' do describe '#starts_with?' do - it 'is registered as an offense' do + it 'registers as an offense and corrects' do expect_offense(<<-RUBY.strip_indent) 'some_string'.starts_with?('prefix') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `start_with?` instead of `starts_with?`. RUBY - end - it 'is autocorrected' do - new_source = autocorrect_source( - "'some_string'.starts_with?('prefix')" - ) - expect(new_source).to eq "'some_string'.start_with?('prefix')" + expect_correction(<<-RUBY.strip_indent) + 'some_string'.start_with?('prefix') + RUBY end end @@ -27,18 +24,15 @@ end describe '#ends_with?' do - it 'is registered as an offense' do + it 'registers as an offense and corrects' do expect_offense(<<-RUBY.strip_indent) 'some_string'.ends_with?('prefix') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `end_with?` instead of `ends_with?`. RUBY - end - it 'is autocorrected' do - new_source = autocorrect_source( - "'some_string'.ends_with?('prefix')" - ) - expect(new_source).to eq "'some_string'.end_with?('prefix')" + expect_correction(<<-RUBY.strip_indent) + 'some_string'.end_with?('prefix') + RUBY end end @@ -51,17 +45,15 @@ describe 'Array' do describe '#append' do - it 'is registered as an offense' do + it 'registers as an offense and does not correct' do expect_offense(<<-RUBY.strip_indent) [1, 'a', 3].append('element') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `<<` instead of `append`. RUBY - end - it 'is not autocorrected' do - source = "[1, 'a', 3].append('element')" - new_source = autocorrect_source(source) - expect(new_source).to eq source + expect_correction(<<-RUBY.strip_indent) + [1, 'a', 3].append('element') + RUBY end end @@ -72,18 +64,15 @@ end describe '#prepend' do - it 'is registered as an offense' do + it 'registers as an offense and corrects' do expect_offense(<<-RUBY.strip_indent) [1, 'a', 3].prepend('element') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `unshift` instead of `prepend`. RUBY - end - it 'is autocorrected' do - new_source = autocorrect_source( - "[1, 'a', 3].prepend('element')" - ) - expect(new_source).to eq "[1, 'a', 3].unshift('element')" + expect_correction(<<-RUBY.strip_indent) + [1, 'a', 3].unshift('element') + RUBY end end diff --git a/spec/rubocop/cop/rails/application_job_spec.rb b/spec/rubocop/cop/rails/application_job_spec.rb index ddbe6e31b0d..786df4175e6 100644 --- a/spec/rubocop/cop/rails/application_job_spec.rb +++ b/spec/rubocop/cop/rails/application_job_spec.rb @@ -70,11 +70,10 @@ class ApplicationJob < ActiveJob::Base; end class MyJob < ActiveJob::Base; end ^^^^^^^^^^^^^^^ Jobs should subclass `ApplicationJob`. RUBY - end - it 'auto-corrects' do - expect(autocorrect_source('class MyJob < ActiveJob::Base; end')) - .to eq('class MyJob < ApplicationJob; end') + expect_correction(<<-RUBY.strip_indent) + class MyJob < ApplicationJob; end + RUBY end end @@ -99,30 +98,36 @@ class Nested::MyJob < ActiveJob::Base; end end it 'corrects jobs defined using Class.new' do - source = 'MyJob = Class.new(ActiveJob::Base)' - inspect_source(source) - expect(cop.messages).to eq(['Jobs should subclass `ApplicationJob`.']) - expect(cop.highlights).to eq(['ActiveJob::Base']) - expect(autocorrect_source(source)) - .to eq('MyJob = Class.new(ApplicationJob)') + expect_offense(<<-RUBY.strip_indent) + MyJob = Class.new(ActiveJob::Base) + ^^^^^^^^^^^^^^^ Jobs should subclass `ApplicationJob`. + RUBY + + expect_correction(<<-RUBY.strip_indent) + MyJob = Class.new(ApplicationJob) + RUBY end it 'corrects nested jobs defined using Class.new' do - source = 'Nested::MyJob = Class.new(ActiveJob::Base)' - inspect_source(source) - expect(cop.messages).to eq(['Jobs should subclass `ApplicationJob`.']) - expect(cop.highlights).to eq(['ActiveJob::Base']) - expect(autocorrect_source(source)) - .to eq('Nested::MyJob = Class.new(ApplicationJob)') + expect_offense(<<-RUBY.strip_indent) + Nested::MyJob = Class.new(ActiveJob::Base) + ^^^^^^^^^^^^^^^ Jobs should subclass `ApplicationJob`. + RUBY + + expect_correction(<<-RUBY.strip_indent) + Nested::MyJob = Class.new(ApplicationJob) + RUBY end it 'corrects anonymous jobs' do - source = 'Class.new(ActiveJob::Base) {}' - inspect_source(source) - expect(cop.messages).to eq(['Jobs should subclass `ApplicationJob`.']) - expect(cop.highlights).to eq(['ActiveJob::Base']) - expect(autocorrect_source(source)) - .to eq('Class.new(ApplicationJob) {}') + expect_offense(<<-RUBY.strip_indent) + Class.new(ActiveJob::Base) {} + ^^^^^^^^^^^^^^^ Jobs should subclass `ApplicationJob`. + RUBY + + expect_correction(<<-RUBY.strip_indent) + Class.new(ApplicationJob) {} + RUBY end it 'allows ApplicationJob defined using Class.new' do diff --git a/spec/rubocop/cop/rails/application_record_spec.rb b/spec/rubocop/cop/rails/application_record_spec.rb index 1beb0770fe0..60c33cd941e 100644 --- a/spec/rubocop/cop/rails/application_record_spec.rb +++ b/spec/rubocop/cop/rails/application_record_spec.rb @@ -1,8 +1,6 @@ # frozen_string_literal: true RSpec.describe RuboCop::Cop::Rails::ApplicationRecord do - let(:msgs) { ['Models should subclass `ApplicationRecord`.'] } - context 'rails 4', :rails4, :config do subject(:cop) { described_class.new(config) } @@ -64,66 +62,90 @@ class ApplicationRecord < ActiveRecord::Base end it 'corrects models that subclass ActiveRecord::Base' do - source = "class MyModel < ActiveRecord::Base\nend" - inspect_source(source) - expect(cop.messages).to eq(msgs) - expect(cop.highlights).to eq(['ActiveRecord::Base']) - expect(autocorrect_source(source)) - .to eq("class MyModel < ApplicationRecord\nend") + expect_offense(<<-RUBY.strip_indent) + class MyModel < ActiveRecord::Base + ^^^^^^^^^^^^^^^^^^ Models should subclass `ApplicationRecord`. + end + RUBY + + expect_correction(<<-RUBY.strip_indent) + class MyModel < ApplicationRecord + end + RUBY end it 'corrects single-line class definitions' do - source = 'class MyModel < ActiveRecord::Base; end' - inspect_source(source) - expect(cop.messages).to eq(msgs) - expect(cop.highlights).to eq(['ActiveRecord::Base']) - expect(autocorrect_source(source)) - .to eq('class MyModel < ApplicationRecord; end') + expect_offense(<<-RUBY.strip_indent) + class MyModel < ActiveRecord::Base; end + ^^^^^^^^^^^^^^^^^^ Models should subclass `ApplicationRecord`. + RUBY + + expect_correction(<<-RUBY.strip_indent) + class MyModel < ApplicationRecord; end + RUBY end it 'corrects namespaced models that subclass ActiveRecord::Base' do - source = "module Nested\n class MyModel < ActiveRecord::Base\n end\nend" - inspect_source(source) - expect(cop.messages).to eq(msgs) - expect(cop.highlights).to eq(['ActiveRecord::Base']) - expect(autocorrect_source(source)) - .to eq("module Nested\n class MyModel < ApplicationRecord\n end\nend") + expect_offense(<<-RUBY.strip_indent) + module Nested + class MyModel < ActiveRecord::Base + ^^^^^^^^^^^^^^^^^^ Models should subclass `ApplicationRecord`. + end + end + RUBY + + expect_correction(<<-RUBY.strip_indent) + module Nested + class MyModel < ApplicationRecord + end + end + RUBY end it 'corrects models defined using nested constants' do - source = "class Nested::MyModel < ActiveRecord::Base\nend" - inspect_source(source) - expect(cop.messages).to eq(msgs) - expect(cop.highlights).to eq(['ActiveRecord::Base']) - expect(autocorrect_source(source)) - .to eq("class Nested::MyModel < ApplicationRecord\nend") + expect_offense(<<-RUBY.strip_indent) + class Nested::MyModel < ActiveRecord::Base + ^^^^^^^^^^^^^^^^^^ Models should subclass `ApplicationRecord`. + end + RUBY + + expect_correction(<<-RUBY.strip_indent) + class Nested::MyModel < ApplicationRecord + end + RUBY end it 'corrects models defined using Class.new' do - source = 'MyModel = Class.new(ActiveRecord::Base)' - inspect_source(source) - expect(cop.messages).to eq(msgs) - expect(cop.highlights).to eq(['ActiveRecord::Base']) - expect(autocorrect_source(source)) - .to eq('MyModel = Class.new(ApplicationRecord)') + expect_offense(<<-RUBY.strip_indent) + MyModel = Class.new(ActiveRecord::Base) + ^^^^^^^^^^^^^^^^^^ Models should subclass `ApplicationRecord`. + RUBY + + expect_correction(<<-RUBY.strip_indent) + MyModel = Class.new(ApplicationRecord) + RUBY end it 'corrects nested models defined using Class.new' do - source = 'Nested::MyModel = Class.new(ActiveRecord::Base)' - inspect_source(source) - expect(cop.messages).to eq(msgs) - expect(cop.highlights).to eq(['ActiveRecord::Base']) - expect(autocorrect_source(source)) - .to eq('Nested::MyModel = Class.new(ApplicationRecord)') + expect_offense(<<-RUBY.strip_indent) + Nested::MyModel = Class.new(ActiveRecord::Base) + ^^^^^^^^^^^^^^^^^^ Models should subclass `ApplicationRecord`. + RUBY + + expect_correction(<<-RUBY.strip_indent) + Nested::MyModel = Class.new(ApplicationRecord) + RUBY end it 'corrects anonymous models' do - source = 'Class.new(ActiveRecord::Base) {}' - inspect_source(source) - expect(cop.messages).to eq(msgs) - expect(cop.highlights).to eq(['ActiveRecord::Base']) - expect(autocorrect_source(source)) - .to eq('Class.new(ApplicationRecord) {}') + expect_offense(<<-RUBY.strip_indent) + Class.new(ActiveRecord::Base) {} + ^^^^^^^^^^^^^^^^^^ Models should subclass `ApplicationRecord`. + RUBY + + expect_correction(<<-RUBY.strip_indent) + Class.new(ApplicationRecord) {} + RUBY end it 'allows ApplicationRecord defined using Class.new' do diff --git a/spec/rubocop/cop/rails/assert_not_spec.rb b/spec/rubocop/cop/rails/assert_not_spec.rb index 8b5dc33564c..7e317b4cc8d 100644 --- a/spec/rubocop/cop/rails/assert_not_spec.rb +++ b/spec/rubocop/cop/rails/assert_not_spec.rb @@ -3,63 +3,59 @@ RSpec.describe RuboCop::Cop::Rails::AssertNot do subject(:cop) { described_class.new } - it 'registers an offense when using `assert !`' do + it 'registers an offense and corrects using `assert !`' do expect_offense(<<-RUBY.strip_indent) assert !foo ^^^^^^^^^^^ Prefer `assert_not` over `assert !`. RUBY + + expect_correction(<<-RUBY.strip_indent) + assert_not foo + RUBY end - it 'registers an offense when using `assert !` with a failure message' do + it 'registers an offense and corrects using `assert !` ' \ + 'with a failure message' do expect_offense(<<-RUBY.strip_indent) assert !foo, 'a failure message' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `assert_not` over `assert !`. RUBY + + expect_correction(<<-RUBY.strip_indent) + assert_not foo, 'a failure message' + RUBY end - it 'registers an offense when using `assert !` with a more complex value' do + it 'registers an offense and corrects using `assert !` ' \ + 'with a more complex value' do expect_offense(<<-RUBY.strip_indent) assert !foo.bar(baz) ^^^^^^^^^^^^^^^^^^^^ Prefer `assert_not` over `assert !`. RUBY - end - - it 'autocorrects `assert !`' do - new_source = autocorrect_source(<<-RUBY.strip_indent) - assert !foo - RUBY - expect(new_source).to eq(<<-RUBY.strip_indent) - assert_not foo - RUBY - end - - it 'autocorrects `assert !` with a failure message' do - new_source = autocorrect_source(<<-RUBY.strip_indent) - assert !foo, 'a failure message' - RUBY - - expect(new_source).to eq(<<-RUBY.strip_indent) - assert_not foo, 'a failure message' + expect_correction(<<-RUBY.strip_indent) + assert_not foo.bar(baz) RUBY end it 'autocorrects `assert !` with extra spaces' do - new_source = autocorrect_source(<<-RUBY.strip_indent) + expect_offense(<<-RUBY.strip_indent) assert ! foo + ^^^^^^^^^^^^^^^ Prefer `assert_not` over `assert !`. RUBY - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) assert_not foo RUBY end it 'autocorrects `assert !` with parentheses' do - new_source = autocorrect_source(<<-RUBY.strip_indent) + expect_offense(<<-RUBY.strip_indent) assert(!foo) + ^^^^^^^^^^^^ Prefer `assert_not` over `assert !`. RUBY - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) assert_not(foo) RUBY end diff --git a/spec/rubocop/cop/rails/belongs_to_spec.rb b/spec/rubocop/cop/rails/belongs_to_spec.rb index deaf1d5c333..3784893e446 100644 --- a/spec/rubocop/cop/rails/belongs_to_spec.rb +++ b/spec/rubocop/cop/rails/belongs_to_spec.rb @@ -5,31 +5,26 @@ let(:config) { RuboCop::Config.new } - it 'registers an offense when specifying `required: false`' do + it 'registers an offense and corrects when specifying `required: false`' do expect_offense(<<-RUBY.strip_indent) belongs_to :foo, required: false ^^^^^^^^^^ You specified `required: false`, in Rails > 5.0 the required option is deprecated and you want to use `optional: true`. RUBY + + expect_correction(<<-RUBY.strip_indent) + belongs_to :foo, optional: true + RUBY end - it 'registers an offense when specifying `required: true`' do + it 'registers an offense and corrects when specifying `required: true`' do expect_offense(<<-RUBY.strip_indent) belongs_to :foo, required: true ^^^^^^^^^^ You specified `required: true`, in Rails > 5.0 the required option is deprecated and you want to use `optional: false`. In most configurations, this is the default and you can omit this option altogether RUBY - end - it 'auto-corrects `required: false` to `optional: true`' do - expect(autocorrect_source('belongs_to :foo, required: false')) - .to eq('belongs_to :foo, optional: true') - expect(cop.offenses.last.status).to eq(:corrected) - end - - it 'auto-corrects `required: true` to `optional: false`' do - code = 'belongs_to :foo, required: true' - expect(autocorrect_source(code)) - .to eq('belongs_to :foo, optional: false') - expect(cop.offenses.last.status).to eq(:corrected) + expect_correction(<<-RUBY.strip_indent) + belongs_to :foo, optional: false + RUBY end it 'registers no offense when setting `optional: true`' do diff --git a/spec/rubocop/cop/rails/blank_spec.rb b/spec/rubocop/cop/rails/blank_spec.rb index ae98e7780d5..48e274efe0b 100644 --- a/spec/rubocop/cop/rails/blank_spec.rb +++ b/spec/rubocop/cop/rails/blank_spec.rb @@ -176,58 +176,42 @@ context 'modifier unless' do context 'with a receiver' do - it 'registers an offense' do + it 'registers an offense and corrects' do expect_offense(<<-RUBY.strip_indent) - something unless foo.present? - ^^^^^^^^^^^^^^^^^^^ Use `if foo.blank?` instead of `unless foo.present?`. + something unless foo.present? + ^^^^^^^^^^^^^^^^^^^ Use `if foo.blank?` instead of `unless foo.present?`. RUBY - end - - it 'auto-corrects' do - new_source = autocorrect_source('something unless foo.present?') - expect(new_source).to eq('something if foo.blank?') + expect_correction(<<-RUBY.strip_indent) + something if foo.blank? + RUBY end end context 'without a receiver' do - it 'registers an offense' do + it 'registers an offense and corrects' do expect_offense(<<-RUBY.strip_indent) - something unless present? - ^^^^^^^^^^^^^^^ Use `if blank?` instead of `unless present?`. + something unless present? + ^^^^^^^^^^^^^^^ Use `if blank?` instead of `unless present?`. RUBY - end - - it 'auto-corrects' do - new_source = autocorrect_source('something unless present?') - expect(new_source).to eq('something if blank?') + expect_correction(<<-RUBY.strip_indent) + something if blank? + RUBY end end end context 'normal unless present?' do - let(:source) do - <<-RUBY.strip_indent - unless foo.present? - something - end - RUBY - end - - it 'registers an offense' do + it 'registers an offense and corrects' do expect_offense(<<-RUBY.strip_indent) unless foo.present? ^^^^^^^^^^^^^^^^^^^ Use `if foo.blank?` instead of `unless foo.present?`. something end RUBY - end - - it 'auto-corrects' do - new_source = autocorrect_source(source) - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) if foo.blank? something end @@ -236,17 +220,7 @@ end context 'unless present? with an else' do - let(:source) do - <<-RUBY.strip_indent - unless foo.present? - something - else - something_else - end - RUBY - end - - it 'registers an offense' do + it 'registers an offense and corrects' do expect_offense(<<-RUBY.strip_indent) unless foo.present? ^^^^^^^^^^^^^^^^^^^ Use `if foo.blank?` instead of `unless foo.present?`. @@ -255,12 +229,8 @@ something_else end RUBY - end - - it 'auto-corrects' do - new_source = autocorrect_source(source) - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) if foo.blank? something else diff --git a/spec/rubocop/cop/rails/delegate_allow_blank_spec.rb b/spec/rubocop/cop/rails/delegate_allow_blank_spec.rb index a93ba2cda54..0f0591bd0fc 100644 --- a/spec/rubocop/cop/rails/delegate_allow_blank_spec.rb +++ b/spec/rubocop/cop/rails/delegate_allow_blank_spec.rb @@ -3,11 +3,15 @@ RSpec.describe RuboCop::Cop::Rails::DelegateAllowBlank do subject(:cop) { described_class.new } - it 'registers an offense when using allow_blank' do + it 'registers an offense and corrects when using allow_blank' do expect_offense(<<-RUBY.strip_indent) delegate :foo, to: :bar, allow_blank: true ^^^^^^^^^^^^^^^^^ `allow_blank` is not a valid option, use `allow_nil`. RUBY + + expect_correction(<<-RUBY.strip_indent) + delegate :foo, to: :bar, allow_nil: true + RUBY end it 'does not register an offense when using allow_nil' do @@ -17,11 +21,4 @@ it 'does not register an offense when no extra options given' do expect_no_offenses('delegate :foo, to: :bar') end - - it 'autocorrects allow_blank to allow_nil' do - source = 'delegate :foo, to: :bar, allow_blank: true' - new_source = autocorrect_source(source) - - expect(new_source).to eq('delegate :foo, to: :bar, allow_nil: true') - end end diff --git a/spec/rubocop/cop/rails/environment_comparison_spec.rb b/spec/rubocop/cop/rails/environment_comparison_spec.rb index 5e7ac951855..bd4c92d9ebf 100644 --- a/spec/rubocop/cop/rails/environment_comparison_spec.rb +++ b/spec/rubocop/cop/rails/environment_comparison_spec.rb @@ -5,32 +5,25 @@ let(:config) { RuboCop::Config.new } - it 'registers an offense when using `Rails.env == production`' do + it 'registers an offense and corrects comparing Rails.env to a string' do expect_offense(<<-RUBY.strip_indent) Rails.env == 'production' ^^^^^^^^^^^^^^^^^^^^^^^^^ Favor `Rails.env.production?` over `Rails.env == 'production'`. - Rails.env == :development - ^^^^^^^^^^^^^^^^^^^^^^^^^ Do not compare `Rails.env` with a symbol, it will always evaluate to `false`. - RUBY - end - - it 'autocorrects a string' do - new_source = autocorrect_source(<<-RUBY.strip_indent) - Rails.env == 'development' RUBY - expect(new_source).to eq(<<-RUBY.strip_indent) - Rails.env.development? + expect_correction(<<-RUBY.strip_indent) + Rails.env.production? RUBY end - it 'autocorrects a symbol' do - new_source = autocorrect_source(<<-RUBY.strip_indent) - Rails.env == :test + it 'registers an offense and corrects comparing Rails.env to a symbol' do + expect_offense(<<-RUBY.strip_indent) + Rails.env == :production + ^^^^^^^^^^^^^^^^^^^^^^^^ Do not compare `Rails.env` with a symbol, it will always evaluate to `false`. RUBY - expect(new_source).to eq(<<-RUBY.strip_indent) - Rails.env.test? + expect_correction(<<-RUBY.strip_indent) + Rails.env.production? RUBY end diff --git a/spec/rubocop/cop/rails/find_by_spec.rb b/spec/rubocop/cop/rails/find_by_spec.rb index e830dadf07a..4b7ef7b9c56 100644 --- a/spec/rubocop/cop/rails/find_by_spec.rb +++ b/spec/rubocop/cop/rails/find_by_spec.rb @@ -3,11 +3,15 @@ RSpec.describe RuboCop::Cop::Rails::FindBy do subject(:cop) { described_class.new } - it 'registers an offense when using `#first`' do + it 'registers an offense when using `#first` and does not auto-correct' do expect_offense(<<-RUBY.strip_indent) User.where(id: x).first ^^^^^^^^^^^^^^^^^^ Use `find_by` instead of `where.first`. RUBY + + expect_correction(<<-RUBY.strip_indent) + User.where(id: x).first + RUBY end it 'registers an offense when using `#take`' do @@ -15,21 +19,13 @@ User.where(id: x).take ^^^^^^^^^^^^^^^^^ Use `find_by` instead of `where.take`. RUBY + + expect_correction(<<-RUBY.strip_indent) + User.find_by(id: x) + RUBY end it 'does not register an offense when using find_by' do expect_no_offenses('User.find_by(id: x)') end - - it 'autocorrects where.take to find_by' do - new_source = autocorrect_source('User.where(id: x).take') - - expect(new_source).to eq('User.find_by(id: x)') - end - - it 'does not autocorrect where.first' do - new_source = autocorrect_source('User.where(id: x).first') - - expect(new_source).to eq('User.where(id: x).first') - end end diff --git a/spec/rubocop/cop/rails/find_each_spec.rb b/spec/rubocop/cop/rails/find_each_spec.rb index 6806ea33642..44c9f85f538 100644 --- a/spec/rubocop/cop/rails/find_each_spec.rb +++ b/spec/rubocop/cop/rails/find_each_spec.rb @@ -45,15 +45,25 @@ end it 'auto-corrects each to find_each' do - new_source = autocorrect_source('User.all.each { |u| u.x }') + expect_offense(<<-RUBY.strip_indent) + User.all.each { |u| u.x } + ^^^^ Use `find_each` instead of `each`. + RUBY - expect(new_source).to eq('User.all.find_each { |u| u.x }') + expect_correction(<<-RUBY.strip_indent) + User.all.find_each { |u| u.x } + RUBY end it 'registers an offense with non-send ancestors' do - inspect_source('class C; User.all.each { |u| u.x }; end') + expect_offense(<<-RUBY.strip_indent) + class C; User.all.each { |u| u.x }; end + ^^^^ Use `find_each` instead of `each`. + RUBY - expect(cop.messages).to eq(['Use `find_each` instead of `each`.']) + expect_correction(<<-RUBY.strip_indent) + class C; User.all.find_each { |u| u.x }; end + RUBY end it 'does not register an offense when using order(...) earlier' do diff --git a/spec/rubocop/cop/rails/http_positional_arguments_spec.rb b/spec/rubocop/cop/rails/http_positional_arguments_spec.rb index 558d391dd1c..8fa2a10ed26 100644 --- a/spec/rubocop/cop/rails/http_positional_arguments_spec.rb +++ b/spec/rubocop/cop/rails/http_positional_arguments_spec.rb @@ -107,6 +107,10 @@ get :create, user_id: @user.id ^^^ Use keyword arguments instead of positional arguments for http call: `get`. RUBY + + expect_correction(<<-RUBY.strip_indent) + get :create, params: { user_id: @user.id } + RUBY end it 'registers an offense for post method' do @@ -114,6 +118,10 @@ post :create, user_id: @user.id ^^^^ Use keyword arguments instead of positional arguments for http call: `post`. RUBY + + expect_correction(<<-RUBY.strip_indent) + post :create, params: { user_id: @user.id } + RUBY end it 'registers an offense for patch method' do @@ -121,6 +129,10 @@ patch :update, user_id: @user.id ^^^^^ Use keyword arguments instead of positional arguments for http call: `patch`. RUBY + + expect_correction(<<-RUBY.strip_indent) + patch :update, params: { user_id: @user.id } + RUBY end it 'registers an offense for put method' do @@ -128,6 +140,10 @@ put :create, user_id: @user.id ^^^ Use keyword arguments instead of positional arguments for http call: `put`. RUBY + + expect_correction(<<-RUBY.strip_indent) + put :create, params: { user_id: @user.id } + RUBY end it 'registers an offense for delete method' do @@ -135,6 +151,10 @@ delete :create, user_id: @user.id ^^^^^^ Use keyword arguments instead of positional arguments for http call: `delete`. RUBY + + expect_correction(<<-RUBY.strip_indent) + delete :create, params: { user_id: @user.id } + RUBY end it 'registers an offense for head method' do @@ -142,6 +162,10 @@ head :create, user_id: @user.id ^^^^ Use keyword arguments instead of positional arguments for http call: `head`. RUBY + + expect_correction(<<-RUBY.strip_indent) + head :create, params: { user_id: @user.id } + RUBY end it 'accepts non HTTP methods' do @@ -181,11 +205,10 @@ get :new, user_id: @user.id ^^^ Use keyword arguments instead of positional arguments for http call: `get`. RUBY - end - it 'autocorrects offense' do - new_source = autocorrect_source('get :new, user_id: @user.id') - expect(new_source).to eq('get :new, params: { user_id: @user.id }') + expect_correction(<<-RUBY.strip_indent) + get :new, params: { user_id: @user.id } + RUBY end describe 'no params' do @@ -197,8 +220,9 @@ describe '.patch' do it 'autocorrects offense' do - new_source = autocorrect_source(<<-RUBY.strip_indent) + expect_offense(<<-RUBY.strip_indent) patch :update, + ^^^^^ Use keyword arguments instead of positional arguments for http call: `patch`. id: @user.id, ac: { article_id: @article1.id, @@ -207,7 +231,7 @@ } RUBY - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) patch :update, params: { id: @user.id, ac: { article_id: @article1.id, profile_id: @profile1.id, @@ -219,8 +243,9 @@ describe '.post' do it 'autocorrects offense' do - new_source = autocorrect_source(<<-RUBY.strip_indent) + expect_offense(<<-RUBY.strip_indent) post :create, + ^^^^ Use keyword arguments instead of positional arguments for http call: `post`. id: @user.id, ac: { article_id: @article1.id, @@ -229,7 +254,7 @@ } RUBY - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) post :create, params: { id: @user.id, ac: { article_id: @article1.id, profile_id: @profile1.id, @@ -246,77 +271,113 @@ end it 'auto-corrects http action when method' do - new_source = autocorrect_source('post user_attrs, id: 1') - expect(new_source).to eq('post user_attrs, params: { id: 1 }') + expect_offense(<<-RUBY.strip_indent) + post user_attrs, id: 1 + ^^^^ Use keyword arguments instead of positional arguments for http call: `post`. + RUBY + + expect_correction(<<-RUBY.strip_indent) + post user_attrs, params: { id: 1 } + RUBY end it 'auto-corrects http action when symbol' do - new_source = autocorrect_source('post :user_attrs, id: 1') - expect(new_source).to eq('post :user_attrs, params: { id: 1 }') + expect_offense(<<-RUBY.strip_indent) + post :user_attrs, id: 1 + ^^^^ Use keyword arguments instead of positional arguments for http call: `post`. + RUBY + + expect_correction(<<-RUBY.strip_indent) + post :user_attrs, params: { id: 1 } + RUBY end it 'maintains parentheses when auto-correcting' do - new_source = autocorrect_source('post(:user_attrs, id: 1)') - expect(new_source).to eq('post(:user_attrs, params: { id: 1 })') + expect_offense(<<-RUBY.strip_indent) + post(:user_attrs, id: 1) + ^^^^ Use keyword arguments instead of positional arguments for http call: `post`. + RUBY + + expect_correction(<<-RUBY.strip_indent) + post(:user_attrs, params: { id: 1 }) + RUBY end it 'maintains quotes when auto-correcting' do - new_source = autocorrect_source(<<-RUBY.strip_indent) + expect_offense(<<-RUBY.strip_indent) get '/auth/linkedin/callback', id: 1 + ^^^ Use keyword arguments instead of positional arguments for http call: `get`. RUBY - expect(new_source).to eq(<<-RUBY.strip_indent) + + expect_correction(<<-RUBY.strip_indent) get '/auth/linkedin/callback', params: { id: 1 } RUBY end it 'does add session keyword when session is used' do - new_source = autocorrect_source(<<-RUBY.strip_indent) + expect_offense(<<-RUBY.strip_indent) get some_path(profile.id), {}, 'HTTP_REFERER' => p_url(p.id).to_s + ^^^ Use keyword arguments instead of positional arguments for http call: `get`. RUBY - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) get some_path(profile.id), session: { 'HTTP_REFERER' => p_url(p.id).to_s } RUBY end it 'does not duplicate brackets when hash is already supplied' do - new_source = autocorrect_source(<<-RUBY.strip_indent) + expect_offense(<<-RUBY.strip_indent) get some_path(profile.id), { user_id: @user.id, profile_id: p.id }, 'HTTP_REFERER' => p_url(p.id).to_s + ^^^ Use keyword arguments instead of positional arguments for http call: `get`. RUBY - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) get some_path(profile.id), params: { user_id: @user.id, profile_id: p.id }, session: { 'HTTP_REFERER' => p_url(p.id).to_s } RUBY end it 'auto-corrects http action when params is a method call' do - new_source = autocorrect_source('post :create, confirmation_data') - expect(new_source).to eq('post :create, params: confirmation_data') + expect_offense(<<-RUBY.strip_indent) + post :create, confirmation_data + ^^^^ Use keyword arguments instead of positional arguments for http call: `post`. + RUBY + + expect_correction(<<-RUBY.strip_indent) + post :create, params: confirmation_data + RUBY end it 'auto-corrects http action when parameter matches ' \ 'special keyword name' do - new_source = autocorrect_source(<<-RUBY) + expect_offense(<<-RUBY.strip_indent) post :create, id: 7, comment: { body: "hei" } + ^^^^ Use keyword arguments instead of positional arguments for http call: `post`. RUBY - expect(new_source).to eq(<<-RUBY) + expect_correction(<<-RUBY.strip_indent) post :create, params: { id: 7, comment: { body: "hei" } } RUBY end it 'auto-corrects http action when format keyword included but not alone' do - new_source = autocorrect_source('post :create, id: 7, format: :rss') - expect(new_source).to eq('post :create, params: { id: 7, format: :rss }') + expect_offense(<<-RUBY.strip_indent) + post :create, id: 7, format: :rss + ^^^^ Use keyword arguments instead of positional arguments for http call: `post`. + RUBY + + expect_correction(<<-RUBY.strip_indent) + post :create, params: { id: 7, format: :rss } + RUBY end it 'auto-corrects http action when params is a lvar' do - new_source = autocorrect_source(<<-RUBY.strip_indent) + expect_offense(<<-RUBY.strip_indent) params = { id: 1 } post user_attrs, params + ^^^^ Use keyword arguments instead of positional arguments for http call: `post`. RUBY - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) params = { id: 1 } post user_attrs, params: params RUBY @@ -324,16 +385,23 @@ it 'auto-corrects http action when params and action name ' \ 'are method calls' do - new_source = autocorrect_source('post user_attrs, params') - expect(new_source).to eq('post user_attrs, params: params') + expect_offense(<<-RUBY.strip_indent) + post user_attrs, params + ^^^^ Use keyword arguments instead of positional arguments for http call: `post`. + RUBY + + expect_correction(<<-RUBY.strip_indent) + post user_attrs, params: params + RUBY end it 'auto-corrects http action when params is a method call with chain' do - new_source = autocorrect_source(<<-RUBY.strip_indent) + expect_offense(<<-RUBY.strip_indent) post user_attrs, params.merge(foo: bar) + ^^^^ Use keyword arguments instead of positional arguments for http call: `post`. RUBY - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) post user_attrs, params: params.merge(foo: bar) RUBY end diff --git a/spec/rubocop/cop/rails/http_status_spec.rb b/spec/rubocop/cop/rails/http_status_spec.rb index 74a07086340..da5763b7a4f 100644 --- a/spec/rubocop/cop/rails/http_status_spec.rb +++ b/spec/rubocop/cop/rails/http_status_spec.rb @@ -6,7 +6,7 @@ context 'when EnforcedStyle is `symbolic`' do let(:cop_config) { { 'EnforcedStyle' => 'symbolic' } } - it 'registers an offense when using numeric value' do + it 'registers an offense and corrects using numeric value' do expect_offense(<<-RUBY.strip_indent) render :foo, status: 200 ^^^ Prefer `:ok` over `200` to define HTTP status code. @@ -21,6 +21,15 @@ redirect_to action: 'index', status: 301 ^^^ Prefer `:moved_permanently` over `301` to define HTTP status code. RUBY + + expect_correction(<<-RUBY.strip_indent) + render :foo, status: :ok + render json: { foo: 'bar' }, status: :not_found + render status: :not_found, json: { foo: 'bar' } + render plain: 'foo/bar', status: :not_modified + redirect_to root_url, status: :moved_permanently + redirect_to action: 'index', status: :moved_permanently + RUBY end it 'does not register an offense when using symbolic value' do @@ -44,7 +53,7 @@ context 'when rack is not loaded' do before { stub_const("#{described_class}::RACK_LOADED", false) } - it 'registers an offense when using numeric value' do + it 'registers an offense and does not correct using numeric value' do expect_offense(<<-RUBY) render :foo, status: 200 ^^^ Prefer `symbolic` over `numeric` to define HTTP status code. @@ -55,40 +64,13 @@ redirect_to root_url, status: 301 ^^^ Prefer `symbolic` over `numeric` to define HTTP status code. RUBY - end - end - describe 'autocorrect' do - context 'when render action' do - it 'autocorrects to symbolic style' do - bad = 'render :foo, status: 200' - good = 'render :foo, status: :ok' - expect(autocorrect_source(bad)).to eq(good) - end - end - - context 'when render json' do - it 'autocorrects to symbolic style' do - bad = "render json: { foo: 'bar' }, status: 404" - good = "render json: { foo: 'bar' }, status: :not_found" - expect(autocorrect_source(bad)).to eq(good) - end - end - - context 'when render plain' do - it 'autocorrects to symbolic style' do - bad = "render plain: 'foo/bar', status: 304" - good = "render plain: 'foo/bar', status: :not_modified" - expect(autocorrect_source(bad)).to eq(good) - end - end - - context 'when redirect_to' do - it 'autocorrects to symbolic style' do - bad = 'redirect_to root_url, status: 301' - good = 'redirect_to root_url, status: :moved_permanently' - expect(autocorrect_source(bad)).to eq(good) - end + expect_correction(<<-RUBY) + render :foo, status: 200 + render json: { foo: 'bar' }, status: 404 + render plain: 'foo/bar', status: 304 + redirect_to root_url, status: 301 + RUBY end end end @@ -111,6 +93,15 @@ redirect_to action: 'index', status: :moved_permanently ^^^^^^^^^^^^^^^^^^ Prefer `301` over `:moved_permanently` to define HTTP status code. RUBY + + expect_correction(<<-RUBY.strip_indent) + render :foo, status: 200 + render json: { foo: 'bar' }, status: 404 + render status: 404, json: { foo: 'bar' } + render plain: 'foo/bar', status: 304 + redirect_to root_url, status: 301 + redirect_to action: 'index', status: 301 + RUBY end it 'does not register an offense when using numeric value' do @@ -134,7 +125,7 @@ context 'when rack is not loaded' do before { stub_const("#{described_class}::RACK_LOADED", false) } - it 'registers an offense when using symbolic value' do + it 'registers an offense and corrects using symbolic value' do expect_offense(<<-RUBY) render :foo, status: :ok ^^^ Prefer `numeric` over `symbolic` to define HTTP status code. @@ -147,39 +138,5 @@ RUBY end end - - describe 'autocorrect' do - context 'when render action' do - it 'autocorrects to symbolic style' do - bad = 'render :foo, status: :ok' - good = 'render :foo, status: 200' - expect(autocorrect_source(bad)).to eq(good) - end - end - - context 'when render json' do - it 'autocorrects to symbolic style' do - bad = "render json: { foo: 'bar' }, status: :not_found" - good = "render json: { foo: 'bar' }, status: 404" - expect(autocorrect_source(bad)).to eq(good) - end - end - - context 'when render plain' do - it 'autocorrects to symbolic style' do - bad = "render plain: 'foo/bar', status: :not_modified" - good = "render plain: 'foo/bar', status: 304" - expect(autocorrect_source(bad)).to eq(good) - end - end - - context 'when redirect_to' do - it 'autocorrects to symbolic style' do - bad = 'redirect_to root_url, status: :moved_permanently' - good = 'redirect_to root_url, status: 301' - expect(autocorrect_source(bad)).to eq(good) - end - end - end end end diff --git a/spec/rubocop/cop/rails/link_to_blank_spec.rb b/spec/rubocop/cop/rails/link_to_blank_spec.rb index 71c0f1da880..dd1120f0c57 100644 --- a/spec/rubocop/cop/rails/link_to_blank_spec.rb +++ b/spec/rubocop/cop/rails/link_to_blank_spec.rb @@ -27,18 +27,13 @@ context 'when using target_blank' do context 'when using no rel' do - it 'registers an offence' do + it 'registers and corrects an offence' do expect_offense(<<-RUBY.strip_indent) link_to 'Click here', 'https://www.example.com', target: '_blank' ^^^^^^^^^^^^^^^^ Specify a `:rel` option containing noopener. RUBY - end - it 'autocorrects with a new rel' do - new_source = autocorrect_source(<<-RUBY.strip_indent) - link_to 'Click here', 'https://www.example.com', target: '_blank' - RUBY - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) link_to 'Click here', 'https://www.example.com', target: '_blank', rel: 'noopener' RUBY end @@ -50,22 +45,15 @@ RUBY end - it 'registers an offence when using the block syntax' do + it 'registers an offence and auto-corrects when using the block syntax' do expect_offense(<<-RUBY.strip_indent) link_to 'https://www.example.com', target: '_blank' do ^^^^^^^^^^^^^^^^ Specify a `:rel` option containing noopener. "Click here" end RUBY - end - it 'autocorrects with a new rel when using the block syntax' do - new_source = autocorrect_source(<<-RUBY.strip_indent) - link_to 'https://www.example.com', target: '_blank' do - "Click here" - end - RUBY - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) link_to 'https://www.example.com', target: '_blank', rel: 'noopener' do "Click here" end @@ -75,19 +63,14 @@ context 'when using rel' do context 'when the rel does not contain noopener' do - it 'registers an offence ' do + it 'registers an offence and corrects' do expect_offense(<<-RUBY.strip_indent) link_to 'Click here', 'https://www.example.com', "target" => '_blank', rel: 'unrelated' ^^^^^^^^^^^^^^^^^^^^ Specify a `:rel` option containing noopener. RUBY - end - it 'autocorrects by appending to the existing rel' do - new_source = autocorrect_source(<<-RUBY.strip_indent) - link_to 'Click here', 'https://www.example.com', target: '_blank', rel: 'foo' - RUBY - expect(new_source).to eq(<<-RUBY.strip_indent) - link_to 'Click here', 'https://www.example.com', target: '_blank', rel: 'foo noopener' + expect_correction(<<-RUBY.strip_indent) + link_to 'Click here', 'https://www.example.com', "target" => '_blank', rel: 'unrelated noopener' RUBY end end diff --git a/spec/rubocop/cop/rails/present_spec.rb b/spec/rubocop/cop/rails/present_spec.rb index fb30a2bab69..9b17a46d25a 100644 --- a/spec/rubocop/cop/rails/present_spec.rb +++ b/spec/rubocop/cop/rails/present_spec.rb @@ -154,58 +154,42 @@ context 'unless blank?' do context 'modifier unless' do context 'with a receiver' do - it 'registers an offense' do + it 'registers an offense and corrects' do expect_offense(<<-RUBY.strip_indent) something unless foo.blank? ^^^^^^^^^^^^^^^^^ Use `if foo.present?` instead of `unless foo.blank?`. RUBY - end - - it 'auto-corrects' do - new_source = autocorrect_source('something unless foo.blank?') - expect(new_source).to eq('something if foo.present?') + expect_correction(<<-RUBY.strip_indent) + something if foo.present? + RUBY end end context 'without a receiver' do - it 'registers an offense' do + it 'registers an offense and corrects' do expect_offense(<<-RUBY.strip_indent) something unless blank? ^^^^^^^^^^^^^ Use `if present?` instead of `unless blank?`. RUBY - end - it 'auto-corrects' do - new_source = autocorrect_source('something unless blank?') - - expect(new_source).to eq('something if present?') + expect_correction(<<-RUBY.strip_indent) + something if present? + RUBY end end end context 'normal unless blank?' do - let(:source) do - <<-RUBY.strip_indent - unless foo.blank? - something - end - RUBY - end - - it 'registers an offense' do + it 'registers an offense and corrects' do expect_offense(<<-RUBY.strip_indent) unless foo.blank? ^^^^^^^^^^^^^^^^^ Use `if foo.present?` instead of `unless foo.blank?`. something end RUBY - end - - it 'auto-corrects' do - new_source = autocorrect_source(source) - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) if foo.present? something end @@ -214,16 +198,6 @@ end context 'unless blank? with an else' do - let(:source) do - <<-RUBY.strip_indent - unless foo.blank? - something - else - something_else - end - RUBY - end - it 'registers an offense' do expect_offense(<<-RUBY.strip_indent) unless foo.blank? @@ -233,12 +207,8 @@ something_else end RUBY - end - - it 'auto-corrects' do - new_source = autocorrect_source(source) - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) if foo.present? something else diff --git a/spec/rubocop/cop/rails/read_write_attribute_spec.rb b/spec/rubocop/cop/rails/read_write_attribute_spec.rb index 6e6ba152c22..c5b183410c6 100644 --- a/spec/rubocop/cop/rails/read_write_attribute_spec.rb +++ b/spec/rubocop/cop/rails/read_write_attribute_spec.rb @@ -4,125 +4,141 @@ subject(:cop) { described_class.new } context 'read_attribute' do - it 'registers an offense' do + it 'registers an offense and corrects a symbol' do expect_offense(<<-RUBY.strip_indent) res = read_attribute(:test) ^^^^^^^^^^^^^^ Prefer `self[:attr]` over `read_attribute(:attr)`. RUBY - end - it 'registers no offense with explicit receiver' do - expect_no_offenses('res = object.read_attribute(:test)') + expect_correction(<<-RUBY.strip_indent) + res = self[:test] + RUBY end - end - context 'write_attribute' do - it 'registers an offense' do + it 'register an offense and corrects a string' do expect_offense(<<-RUBY.strip_indent) - write_attribute(:test, val) - ^^^^^^^^^^^^^^^ Prefer `self[:attr] = val` over `write_attribute(:attr, val)`. + res = read_attribute('test') + ^^^^^^^^^^^^^^ Prefer `self[:attr]` over `read_attribute(:attr)`. RUBY - end - it 'registers no offense with explicit receiver' do - expect_no_offenses('object.write_attribute(:test, val)') + expect_correction(<<-RUBY.strip_indent) + res = self['test'] + RUBY end - end - describe '#autocorrect' do - context 'write_attribute' do - it 'autocorrects symbol' do - source = 'write_attribute(:attr, var)' - corrected_source = 'self[:attr] = var' - - expect(autocorrect_source(source)).to eq(corrected_source) - end + it 'autocorrects without parentheses' do + expect_offense(<<-RUBY.strip_indent) + res = read_attribute 'test' + ^^^^^^^^^^^^^^ Prefer `self[:attr]` over `read_attribute(:attr)`. + RUBY - it 'autocorrects string' do - source = "write_attribute('attr', 'test')" - corrected_source = "self['attr'] = 'test'" + expect_correction(<<-RUBY.strip_indent) + res = self['test'] + RUBY + end - expect(autocorrect_source(source)).to eq(corrected_source) - end + it 'corrects an expression' do + expect_offense(<<-RUBY.strip_indent) + res = read_attribute('test_' + postfix) + ^^^^^^^^^^^^^^ Prefer `self[:attr]` over `read_attribute(:attr)`. + RUBY - it 'autocorrects without parentheses' do - source = "write_attribute 'attr', 'test'" - corrected_source = "self['attr'] = 'test'" + expect_correction(<<-RUBY.strip_indent) + res = self['test_' + postfix] + RUBY + end - expect(autocorrect_source(source)).to eq(corrected_source) - end + it 'corrects multiline' do + expect_offense(<<-RUBY.strip_indent) + res = read_attribute( + ^^^^^^^^^^^^^^ Prefer `self[:attr]` over `read_attribute(:attr)`. + ( + 'test_' + postfix + ).to_sym + ) + RUBY - it 'autocorrects expression' do - source = "write_attribute(:attr, 'test_' + postfix)" - corrected_source = "self[:attr] = 'test_' + postfix" + expect_correction(<<-RUBY.strip_indent) + res = self[( + 'test_' + postfix + ).to_sym] + RUBY + end - expect(autocorrect_source(source)).to eq(corrected_source) - end + it 'registers no offense with explicit receiver' do + expect_no_offenses('res = object.read_attribute(:test)') + end + end - it 'autocorrects multiline' do - source = <<-RUBY.strip_indent - write_attribute( - :attr, - ( - 'test_' + postfix - ).to_sym - ) - RUBY - corrected_source = <<-RUBY.strip_indent - self[:attr] = ( - 'test_' + postfix - ).to_sym + context 'write_attribute' do + context 'when using a symbol for the attribute' do + it 'registers an offense and corrects' do + expect_offense(<<-RUBY.strip_indent) + write_attribute(:test, val) + ^^^^^^^^^^^^^^^ Prefer `self[:attr] = val` over `write_attribute(:attr, val)`. RUBY - expect(autocorrect_source(source)).to eq(corrected_source) + expect_correction(<<-RUBY.strip_indent) + self[:test] = val + RUBY end end - context 'read_attribute' do - it 'autocorrects symbol' do - source = 'res = read_attribute(:test)' - corrected_source = 'res = self[:test]' + context 'when using a string for the attribute' do + it 'registers an offense and corrects' do + expect_offense(<<-RUBY.strip_indent) + write_attribute('attr', 'test') + ^^^^^^^^^^^^^^^ Prefer `self[:attr] = val` over `write_attribute(:attr, val)`. + RUBY - expect(autocorrect_source(source)).to eq(corrected_source) + expect_correction(<<-RUBY.strip_indent) + self['attr'] = 'test' + RUBY end + end - it 'autocorrects string' do - source = "res = read_attribute('test')" - corrected_source = "res = self['test']" - - expect(autocorrect_source(source)).to eq(corrected_source) - end + it 'registers an offense and corrects without parentheses' do + expect_offense(<<-RUBY.strip_indent) + write_attribute 'attr', 'test' + ^^^^^^^^^^^^^^^ Prefer `self[:attr] = val` over `write_attribute(:attr, val)`. + RUBY - it 'autocorrects without parentheses' do - source = "res = read_attribute 'test'" - corrected_source = "res = self['test']" + expect_correction(<<-RUBY.strip_indent) + self['attr'] = 'test' + RUBY + end - expect(autocorrect_source(source)).to eq(corrected_source) - end + it 'corrects assignment with chained methods' do + expect_offense(<<-RUBY.strip_indent) + write_attribute(:attr, 'test_' + postfix) + ^^^^^^^^^^^^^^^ Prefer `self[:attr] = val` over `write_attribute(:attr, val)`. + RUBY - it 'autocorrects expression' do - source = "res = read_attribute('test_' + postfix)" - corrected_source = "res = self['test_' + postfix]" + expect_correction(<<-RUBY.strip_indent) + self[:attr] = 'test_' + postfix + RUBY + end - expect(autocorrect_source(source)).to eq(corrected_source) - end + it 'autocorrects multiline' do + expect_offense(<<-RUBY.strip_indent) + write_attribute( + ^^^^^^^^^^^^^^^ Prefer `self[:attr] = val` over `write_attribute(:attr, val)`. + :attr, + ( + 'test_' + postfix + ).to_sym + ) + RUBY - it 'autocorrects multiline' do - source = <<-RUBY.strip_indent - res = read_attribute( - ( - 'test_' + postfix - ).to_sym - ) - RUBY - corrected_source = <<-RUBY.strip_indent - res = self[( - 'test_' + postfix - ).to_sym] - RUBY + expect_correction(<<-RUBY.strip_indent) + self[:attr] = ( + 'test_' + postfix + ).to_sym + RUBY + end - expect(autocorrect_source(source)).to eq(corrected_source) - end + it 'registers no offense with explicit receiver' do + expect_no_offenses('object.write_attribute(:test, val)') end end end diff --git a/spec/rubocop/cop/rails/redundant_receiver_in_with_options_spec.rb b/spec/rubocop/cop/rails/redundant_receiver_in_with_options_spec.rb index c1548ae6240..35859c69134 100644 --- a/spec/rubocop/cop/rails/redundant_receiver_in_with_options_spec.rb +++ b/spec/rubocop/cop/rails/redundant_receiver_in_with_options_spec.rb @@ -6,7 +6,8 @@ context 'rails >= 4.2' do let(:rails_version) { 4.2 } - it 'registers an offense when using explicit receiver in `with_options`' do + it 'registers an offense and corrects using explicit receiver ' \ + 'in `with_options`' do expect_offense(<<-RUBY.strip_indent) class Account < ApplicationRecord with_options dependent: :destroy do |assoc| @@ -21,11 +22,8 @@ class Account < ApplicationRecord end end RUBY - end - it 'does not register an offense when using inplicit receiver ' \ - 'in `with_options`' do - expect_no_offenses(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) class Account < ApplicationRecord with_options dependent: :destroy do has_many :customers @@ -37,40 +35,9 @@ class Account < ApplicationRecord RUBY end - it 'registers an offense when including multiple redendant receivers ' \ - 'in single line' do - expect_offense(<<-RUBY.strip_indent) - with_options options: false do |merger| - merger.invoke(merger.something) - ^^^^^^ Redundant receiver in `with_options`. - ^^^^^^ Redundant receiver in `with_options`. - end - RUBY - end - - it 'does not register an offense when including method invocations ' \ - 'to different receivers' do + it 'does not register an offense when using inplicit receiver ' \ + 'in `with_options`' do expect_no_offenses(<<-RUBY.strip_indent) - client = ApplicationClient.new - with_options options: false do |merger| - client.invoke(merger.something, something) - end - RUBY - end - - it 'autocorrects to implicit receiver in `with_options`' do - new_source = autocorrect_source(<<-RUBY.strip_indent) - class Account < ApplicationRecord - with_options dependent: :destroy do |assoc| - assoc.has_many :customers - assoc.has_many :products - assoc.has_many :invoices - assoc.has_many :expenses - end - end - RUBY - - expect(new_source).to eq(<<-RUBY.strip_indent) class Account < ApplicationRecord with_options dependent: :destroy do has_many :customers @@ -82,20 +49,33 @@ class Account < ApplicationRecord RUBY end - it 'autocorrects to implicit receiver when including multiple receivers' do - new_source = autocorrect_source(<<-RUBY.strip_indent) + it 'registers an offense and corrects when including multiple ' \ + 'redendant receivers in single line' do + expect_offense(<<-RUBY.strip_indent) with_options options: false do |merger| merger.invoke(merger.something) + ^^^^^^ Redundant receiver in `with_options`. + ^^^^^^ Redundant receiver in `with_options`. end RUBY - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) with_options options: false do invoke(something) end RUBY end + it 'does not register an offense when including method invocations ' \ + 'to different receivers' do + expect_no_offenses(<<-RUBY.strip_indent) + client = ApplicationClient.new + with_options options: false do |merger| + client.invoke(merger.something, something) + end + RUBY + end + it 'does not register an offense when including block node' \ 'in `with_options`' do expect_no_offenses(<<-RUBY.strip_indent) diff --git a/spec/rubocop/cop/rails/refute_methods_spec.rb b/spec/rubocop/cop/rails/refute_methods_spec.rb index b86133fd590..fb61b41b9b6 100644 --- a/spec/rubocop/cop/rails/refute_methods_spec.rb +++ b/spec/rubocop/cop/rails/refute_methods_spec.rb @@ -3,18 +3,27 @@ RSpec.describe RuboCop::Cop::Rails::RefuteMethods do subject(:cop) { described_class.new } - it 'registers an offense when using `refute` with a single argument' do + it 'registers an offense and correct using `refute` with a single argument' do expect_offense(<<-RUBY.strip_indent) refute foo ^^^^^^ Prefer `assert_not` over `refute`. RUBY + + expect_correction(<<-RUBY.strip_indent) + assert_not foo + RUBY end - it 'registers an offense when using `refute` with multiple arguments' do + it 'registers an offense and corrects using `refute` ' \ + 'with multiple arguments' do expect_offense(<<-RUBY.strip_indent) refute foo, bar, baz ^^^^^^ Prefer `assert_not` over `refute`. RUBY + + expect_correction(<<-RUBY.strip_indent) + assert_not foo, bar, baz + RUBY end it 'registers an offense when using `refute_empty`' do @@ -22,25 +31,9 @@ refute_empty foo ^^^^^^^^^^^^ Prefer `assert_not_empty` over `refute_empty`. RUBY - end - - it 'autocorrects `refute` with a single argument' do - new_source = autocorrect_source(<<-RUBY.strip_indent) - refute foo - RUBY - - expect(new_source).to eq(<<-RUBY.strip_indent) - assert_not foo - RUBY - end - it 'autocorrects `refute` with multiple arguments' do - new_source = autocorrect_source(<<-RUBY.strip_indent) - refute foo, bar, baz - RUBY - - expect(new_source).to eq(<<-RUBY.strip_indent) - assert_not foo, bar, baz + expect_correction(<<-RUBY.strip_indent) + assert_not_empty foo RUBY end diff --git a/spec/rubocop/cop/rails/request_referer_spec.rb b/spec/rubocop/cop/rails/request_referer_spec.rb index 8dbc345bcd9..1d7a3e7af34 100644 --- a/spec/rubocop/cop/rails/request_referer_spec.rb +++ b/spec/rubocop/cop/rails/request_referer_spec.rb @@ -6,32 +6,30 @@ context 'when EnforcedStyle is referer' do let(:cop_config) { { 'EnforcedStyle' => 'referer' } } - it 'registers an offense for request.referrer' do + it 'registers an offense and corrects request.referrer' do expect_offense(<<-RUBY.strip_indent) puts request.referrer ^^^^^^^^^^^^^^^^ Use `request.referer` instead of `request.referrer`. RUBY - end - it 'autocorrects referrer with referer' do - corrected = autocorrect_source('puts request.referrer') - expect(corrected).to eq 'puts request.referer' + expect_correction(<<-RUBY.strip_indent) + puts request.referer + RUBY end end context 'when EnforcedStyle is referrer' do let(:cop_config) { { 'EnforcedStyle' => 'referrer' } } - it 'registers an offense for request.referer' do + it 'registers an offense and corrects request.referer' do expect_offense(<<-RUBY.strip_indent) puts request.referer ^^^^^^^^^^^^^^^ Use `request.referrer` instead of `request.referer`. RUBY - end - it 'autocorrects referer with referrer' do - corrected = autocorrect_source('puts request.referer') - expect(corrected).to eq 'puts request.referrer' + expect_correction(<<-RUBY.strip_indent) + puts request.referrer + RUBY end end end