Skip to content

Commit

Permalink
Merge pull request #6740 from rrosenblum/expect_correction
Browse files Browse the repository at this point in the history
Start converting tests to use expect_correction
  • Loading branch information
Drenmi committed Feb 7, 2019
2 parents 1c8335c + fd654d7 commit 648085e
Show file tree
Hide file tree
Showing 24 changed files with 553 additions and 695 deletions.
28 changes: 11 additions & 17 deletions spec/rubocop/cop/bundler/insecure_protocol_source_spec.rb
Expand Up @@ -10,37 +10,31 @@
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
expect_offense(<<-RUBY.strip_indent)
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
expect_offense(<<-RUBY.strip_indent)
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
25 changes: 5 additions & 20 deletions spec/rubocop/cop/bundler/ordered_gems_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
42 changes: 14 additions & 28 deletions spec/rubocop/cop/internal_affairs/offense_location_keyword_spec.rb
Expand Up @@ -5,21 +5,33 @@

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',
location: node.loc.selector
^^^^^^^^^^^^^^^^^ 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

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

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

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

0 comments on commit 648085e

Please sign in to comment.