diff --git a/spec/rubocop/cop/style/colon_method_definition_spec.rb b/spec/rubocop/cop/style/colon_method_definition_spec.rb index ea96f2c70c4..c87c26e5300 100644 --- a/spec/rubocop/cop/style/colon_method_definition_spec.rb +++ b/spec/rubocop/cop/style/colon_method_definition_spec.rb @@ -23,18 +23,8 @@ def self::bar end end RUBY - end - - it 'corrects :: to .' do - new_source = autocorrect_source(<<-RUBY.strip_indent) - class Foo - def self::bar - something - end - end - RUBY - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) class Foo def self.bar something @@ -54,18 +44,8 @@ def Foo::bar end end RUBY - end - - it 'corrects :: to .' do - new_source = autocorrect_source(<<-RUBY.strip_indent) - class Foo - def Foo::bar - something - end - end - RUBY - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) class Foo def Foo.bar something diff --git a/spec/rubocop/cop/style/empty_block_parameter_spec.rb b/spec/rubocop/cop/style/empty_block_parameter_spec.rb index 7bd70eb7a04..55042dbb35f 100644 --- a/spec/rubocop/cop/style/empty_block_parameter_spec.rb +++ b/spec/rubocop/cop/style/empty_block_parameter_spec.rb @@ -11,6 +11,11 @@ ^^ Omit pipes for the empty block parameters. end RUBY + + expect_correction(<<-RUBY.strip_indent) + a do + end + RUBY end it 'registers an offense for an empty block parameter with {} style' do @@ -18,45 +23,34 @@ a { || do_something } ^^ Omit pipes for the empty block parameters. RUBY + + expect_correction(<<-RUBY.strip_indent) + a { do_something } + RUBY end it 'registers an offense for an empty block parameter with super' do - expect_offense(<<-RUBY) + expect_offense(<<-RUBY.strip_indent) def foo super { || do_something } ^^ Omit pipes for the empty block parameters. end RUBY - end - it 'auto-corrects for do-end style' do - new_source = autocorrect_source(<<-RUBY.strip_indent) - a do || - end - RUBY - - expect(new_source).to eq(<<-RUBY.strip_indent) - a do + expect_correction(<<-RUBY.strip_indent) + def foo + super { do_something } end RUBY end - it 'auto-corrects for {} style' do - new_source = autocorrect_source(<<-RUBY.strip_indent) - a { || do_something } - RUBY - - expect(new_source).to eq(<<-RUBY.strip_indent) - a { do_something } - RUBY - end - - it 'auto-corrects for a keyword lambda' do - new_source = autocorrect_source(<<-RUBY.strip_indent) + it 'registers an offense for an empty block parameter with lambda' do + expect_offense(<<-RUBY.strip_indent) lambda { || do_something } + ^^ Omit pipes for the empty block parameters. RUBY - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) lambda { do_something } RUBY end diff --git a/spec/rubocop/cop/style/empty_lambda_parameter_spec.rb b/spec/rubocop/cop/style/empty_lambda_parameter_spec.rb index d82f7775034..f258135a484 100644 --- a/spec/rubocop/cop/style/empty_lambda_parameter_spec.rb +++ b/spec/rubocop/cop/style/empty_lambda_parameter_spec.rb @@ -10,14 +10,8 @@ -> () { do_something } ^^ Omit parentheses for the empty lambda parameters. RUBY - end - - it 'auto-corrects for a lambda' do - new_source = autocorrect_source(<<-RUBY.strip_indent) - -> () { do_something } - RUBY - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) -> { do_something } RUBY end diff --git a/spec/rubocop/cop/style/expand_path_arguments_spec.rb b/spec/rubocop/cop/style/expand_path_arguments_spec.rb index 9f2c0d29b25..470e740ae63 100644 --- a/spec/rubocop/cop/style/expand_path_arguments_spec.rb +++ b/spec/rubocop/cop/style/expand_path_arguments_spec.rb @@ -8,6 +8,10 @@ File.expand_path('..', __FILE__) ^^^^^^^^^^^ Use `expand_path(__dir__)` instead of `expand_path('..', __FILE__)`. RUBY + + expect_correction(<<-RUBY.strip_indent) + File.expand_path(__dir__) + RUBY end it "registers an offense when using `File.expand_path('../..', __FILE__)`" do @@ -15,6 +19,10 @@ File.expand_path('../..', __FILE__) ^^^^^^^^^^^ Use `expand_path('..', __dir__)` instead of `expand_path('../..', __FILE__)`. RUBY + + expect_correction(<<-RUBY.strip_indent) + File.expand_path('..', __dir__) + RUBY end it 'registers an offense when using ' \ @@ -23,6 +31,10 @@ File.expand_path('../../..', __FILE__) ^^^^^^^^^^^ Use `expand_path('../..', __dir__)` instead of `expand_path('../../..', __FILE__)`. RUBY + + expect_correction(<<-RUBY.strip_indent) + File.expand_path('../..', __dir__) + RUBY end it "registers an offense when using `File.expand_path('.', __FILE__)`" do @@ -30,6 +42,10 @@ File.expand_path('.', __FILE__) ^^^^^^^^^^^ Use `expand_path(__FILE__)` instead of `expand_path('.', __FILE__)`. RUBY + + expect_correction(<<-RUBY.strip_indent) + File.expand_path(__FILE__) + RUBY end it 'registers an offense when using ' \ @@ -38,6 +54,10 @@ File.expand_path('../../lib', __FILE__) ^^^^^^^^^^^ Use `expand_path('../lib', __dir__)` instead of `expand_path('../../lib', __FILE__)`. RUBY + + expect_correction(<<-RUBY.strip_indent) + File.expand_path('../lib', __dir__) + RUBY end it 'registers an offense when using ' \ @@ -46,6 +66,10 @@ File.expand_path('./../..', __FILE__) ^^^^^^^^^^^ Use `expand_path('..', __dir__)` instead of `expand_path('./../..', __FILE__)`. RUBY + + expect_correction(<<-RUBY.strip_indent) + File.expand_path('..', __dir__) + RUBY end it 'registers an offense when using ' \ @@ -54,6 +78,10 @@ Pathname(__FILE__).parent.expand_path ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `Pathname(__dir__).expand_path` instead of `Pathname(__FILE__).parent.expand_path`. RUBY + + expect_correction(<<-RUBY.strip_indent) + Pathname(__dir__).expand_path + RUBY end it 'registers an offense when using ' \ @@ -62,6 +90,10 @@ Pathname.new(__FILE__).parent.expand_path ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `Pathname.new(__dir__).expand_path` instead of `Pathname.new(__FILE__).parent.expand_path`. RUBY + + expect_correction(<<-RUBY.strip_indent) + Pathname.new(__dir__).expand_path + RUBY end it 'does not register an offense when using `File.expand_path(__dir__)`' do @@ -103,54 +135,4 @@ Pathname(__dir__).expand_path RUBY end - - it 'autocorrects `File.expand_path(__dir__)`' do - new_source = autocorrect_source(<<-RUBY.strip_indent) - File.expand_path('..', __FILE__) - RUBY - - expect(new_source).to eq <<-RUBY.strip_indent - File.expand_path(__dir__) - RUBY - end - - it 'autocorrects `File.expand_path('..', __dir__)`' do - new_source = autocorrect_source(<<-RUBY.strip_indent) - File.expand_path('../..', __FILE__) - RUBY - - expect(new_source).to eq <<-RUBY.strip_indent - File.expand_path('..', __dir__) - RUBY - end - - it 'autocorrects `File.expand_path(__FILE__)`' do - new_source = autocorrect_source(<<-RUBY.strip_indent) - File.expand_path('.', __FILE__) - RUBY - - expect(new_source).to eq <<-RUBY.strip_indent - File.expand_path(__FILE__) - RUBY - end - - it 'autocorrects `Pathname(__dir__).expand_path`' do - new_source = autocorrect_source(<<-RUBY.strip_indent) - Pathname(__FILE__).parent.expand_path - RUBY - - expect(new_source).to eq <<-RUBY.strip_indent - Pathname(__dir__).expand_path - RUBY - end - - it 'autocorrects `Pathname.new(__dir__).expand_path`' do - new_source = autocorrect_source(<<-RUBY.strip_indent) - Pathname.new(__FILE__).parent.expand_path - RUBY - - expect(new_source).to eq <<-RUBY.strip_indent - Pathname.new(__dir__).expand_path - RUBY - end end diff --git a/spec/rubocop/cop/style/module_function_spec.rb b/spec/rubocop/cop/style/module_function_spec.rb index dd62566cbcb..c72d644c8b2 100644 --- a/spec/rubocop/cop/style/module_function_spec.rb +++ b/spec/rubocop/cop/style/module_function_spec.rb @@ -14,6 +14,13 @@ module Test def test; end end RUBY + + expect_correction(<<-RUBY.strip_indent) + module Test + module_function + def test; end + end + RUBY end it 'accepts for `extend self` in a module with private methods' do @@ -44,22 +51,6 @@ class Test end RUBY end - - it 'auto-corrects `extend self` to `module_function`' do - corrected = autocorrect_source(<<-RUBY.strip_indent) - module Foo - extend self - def test; end - end - RUBY - - expect(corrected).to eq <<-RUBY.strip_indent - module Foo - module_function - def test; end - end - RUBY - end end context 'when enforced style is `extend_self`' do @@ -73,29 +64,20 @@ module Test def test; end end RUBY - end - it 'accepts module_function with an argument' do - expect_no_offenses(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) module Test + extend self def test; end - module_function :test end RUBY end - it 'auto-corrects `module_function` to `extend self`' do - corrected = autocorrect_source(<<-RUBY.strip_indent) - module Foo - module_function - def test; end - end - RUBY - - expect(corrected).to eq <<-RUBY.strip_indent - module Foo - extend self + it 'accepts module_function with an argument' do + expect_no_offenses(<<-RUBY.strip_indent) + module Test def test; end + module_function :test end RUBY end diff --git a/spec/rubocop/cop/style/nested_parenthesized_calls_spec.rb b/spec/rubocop/cop/style/nested_parenthesized_calls_spec.rb index 5fb882223b2..1316f8882ca 100644 --- a/spec/rubocop/cop/style/nested_parenthesized_calls_spec.rb +++ b/spec/rubocop/cop/style/nested_parenthesized_calls_spec.rb @@ -29,18 +29,15 @@ context 'on a non-parenthesized call nested in a parenthesized one' do context 'with a single argument to the nested call' do - let(:source) { 'puts(compute something)' } - it 'registers an offense' do expect_offense(<<-RUBY.strip_indent) puts(compute something) ^^^^^^^^^^^^^^^^^ Add parentheses to nested method call `compute something`. RUBY - end - it 'auto-corrects by adding parentheses' do - new_source = autocorrect_source(source) - expect(new_source).to eq('puts(compute(something))') + expect_correction(<<-RUBY.strip_indent) + puts(compute(something)) + RUBY end end @@ -50,11 +47,10 @@ puts(compute first, second) ^^^^^^^^^^^^^^^^^^^^^ Add parentheses to nested method call `compute first, second`. RUBY - end - it 'auto-corrects by adding parentheses' do - new_source = autocorrect_source('puts(compute first, second)') - expect(new_source).to eq('puts(compute(first, second))') + expect_correction(<<-RUBY.strip_indent) + puts(compute(first, second)) + RUBY end end end diff --git a/spec/rubocop/cop/style/rescue_standard_error_spec.rb b/spec/rubocop/cop/style/rescue_standard_error_spec.rb index a8f8513f708..77d94919d2d 100644 --- a/spec/rubocop/cop/style/rescue_standard_error_spec.rb +++ b/spec/rubocop/cop/style/rescue_standard_error_spec.rb @@ -61,18 +61,8 @@ bar end RUBY - end - - it 'autocorrect will remove StandardError' do - new_source = autocorrect_source(<<-RUBY.strip_indent) - begin - foo - rescue StandardError - bar - end - RUBY - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) begin foo rescue @@ -91,18 +81,8 @@ bar end RUBY - end - - it 'autocorrect will remove StandardError' do - new_source = autocorrect_source(<<-RUBY.strip_indent) - begin - foo - rescue StandardError => e - bar - end - RUBY - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) begin foo rescue => e @@ -191,18 +171,8 @@ def foobar bar end RUBY - end - - it 'autocorrect will remove StandardError' do - new_source = autocorrect_source(<<-RUBY.strip_indent) - def foobar - foo - rescue StandardError - bar - end - RUBY - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) def foobar foo rescue @@ -221,18 +191,8 @@ def foobar bar end RUBY - end - - it 'autocorrect will remove StandardError' do - new_source = autocorrect_source(<<-RUBY.strip_indent) - def foobar - foo - rescue StandardError => e - bar - end - RUBY - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) def foobar foo rescue => e @@ -293,18 +253,8 @@ def foobar bar end RUBY - end - - it 'autocorrect will add StandardError' do - new_source = autocorrect_source(<<-RUBY.strip_indent) - begin - foo - rescue - bar - end - RUBY - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) begin foo rescue StandardError @@ -323,18 +273,8 @@ def foobar bar end RUBY - end - - it 'autocorrect will add StandardError' do - new_source = autocorrect_source(<<-RUBY.strip_indent) - begin - foo - rescue => e - bar - end - RUBY - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) begin foo rescue StandardError => e @@ -423,18 +363,8 @@ def baz bar end RUBY - end - - it 'autocorrect will add StandardError' do - new_source = autocorrect_source(<<-RUBY.strip_indent) - def baz - foo - rescue - bar - end - RUBY - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) def baz foo rescue StandardError @@ -454,18 +384,8 @@ def baz bar end RUBY - end - - it 'autocorrect will add StandardError' do - new_source = autocorrect_source(<<-RUBY.strip_indent) - def baz - foo - rescue => e - bar - end - RUBY - expect(new_source).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) def baz foo rescue StandardError => e diff --git a/spec/rubocop/cop/style/unless_else_spec.rb b/spec/rubocop/cop/style/unless_else_spec.rb index b2ed7972342..0c616fd78a3 100644 --- a/spec/rubocop/cop/style/unless_else_spec.rb +++ b/spec/rubocop/cop/style/unless_else_spec.rb @@ -4,24 +4,17 @@ subject(:cop) { described_class.new } context 'unless with else' do - let(:source) do - <<-RUBY.strip_indent + it 'registers an offense' do + expect_offense(<<-RUBY.strip_indent) unless x # negative 1 + ^^^^^^^^^^^^^^^^^^^^^ Do not use `unless` with `else`. Rewrite these with the positive case first. a = 1 # negative 2 else # positive 1 a = 0 # positive 2 end RUBY - end - - it 'registers an offense' do - inspect_source(source) - expect(cop.offenses.size).to eq(1) - end - it 'auto-corrects' do - corrected = autocorrect_source(source) - expect(corrected).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) if x # positive 1 a = 0 # positive 2 else # negative 1 @@ -32,9 +25,10 @@ end context 'unless with nested if-else' do - let(:source) do - <<-RUBY.strip_indent + it 'registers an offense' do + expect_offense(<<-RUBY.strip_indent) unless(x) + ^^^^^^^^^ Do not use `unless` with `else`. Rewrite these with the positive case first. if(y == 0) a = 0 elsif(z == 0) @@ -46,16 +40,8 @@ a = 3 end RUBY - end - - it 'registers an offense' do - inspect_source(source) - expect(cop.offenses.size).to eq(1) - end - it 'auto-corrects' do - corrected = autocorrect_source(source) - expect(corrected).to eq(<<-RUBY.strip_indent) + expect_correction(<<-RUBY.strip_indent) if(x) a = 3 else