Skip to content

Commit

Permalink
Merge pull request #6695 from rubocop-hq/rewrite-some-specs-with-expe…
Browse files Browse the repository at this point in the history
…ct-correction

Start using expect_correction helper
  • Loading branch information
Maxim Krizhanovsky committed Jan 26, 2019
2 parents 002aef5 + 5f62b7f commit 154dfd7
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 253 deletions.
24 changes: 2 additions & 22 deletions spec/rubocop/cop/style/colon_method_definition_spec.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
40 changes: 17 additions & 23 deletions spec/rubocop/cop/style/empty_block_parameter_spec.rb
Expand Up @@ -11,52 +11,46 @@
^^ 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
expect_offense(<<-RUBY.strip_indent)
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
Expand Down
8 changes: 1 addition & 7 deletions spec/rubocop/cop/style/empty_lambda_parameter_spec.rb
Expand Up @@ -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
Expand Down
82 changes: 32 additions & 50 deletions spec/rubocop/cop/style/expand_path_arguments_spec.rb
Expand Up @@ -8,13 +8,21 @@
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
expect_offense(<<-RUBY.strip_indent)
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 ' \
Expand All @@ -23,13 +31,21 @@
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
expect_offense(<<-RUBY.strip_indent)
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 ' \
Expand All @@ -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 ' \
Expand All @@ -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 ' \
Expand All @@ -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 ' \
Expand All @@ -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
Expand Down Expand Up @@ -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
44 changes: 13 additions & 31 deletions spec/rubocop/cop/style/module_function_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
16 changes: 6 additions & 10 deletions spec/rubocop/cop/style/nested_parenthesized_calls_spec.rb
Expand Up @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 154dfd7

Please sign in to comment.