Skip to content

Commit

Permalink
Refactor SpaceAroundOperators spec
Browse files Browse the repository at this point in the history
Use shared examples instead of helper methods.
  • Loading branch information
lumeet authored and Neodelf committed Oct 15, 2016
1 parent 45e546b commit 5d26c82
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 64 deletions.
12 changes: 6 additions & 6 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2016-07-13 10:13:21 +0300 using RuboCop version 0.41.2.
# on 2016-07-19 10:27:53 +0300 using RuboCop version 0.41.2.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 162
# Offense count: 150
Metrics/AbcSize:
Max: 23
Max: 22

# Offense count: 28
# Offense count: 29
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 158

# Offense count: 55
# Offense count: 58
Metrics/CyclomaticComplexity:
Max: 8

# Offense count: 210
# Offense count: 204
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 17
Expand Down
98 changes: 40 additions & 58 deletions spec/rubocop/cop/style/space_around_operators_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,22 @@
end

describe 'missing space around operators' do
shared_examples 'modifier with missing space' do |keyword|
it "registers an offense in presence of modifier #{keyword} statement" do
src = ["a=1 #{keyword} condition",
'c=2']
inspect_source(cop, src)
expect(cop.offenses.map(&:line)).to eq([1, 2])
expect(cop.messages).to eq(
['Surrounding space missing for operator `=`.'] * 2
)

new_source = autocorrect_source(cop, src)
expect(new_source)
.to eq(src.map { |line| line.sub('=', ' = ') }.join("\n"))
end
end

it 'registers an offense for assignment without space on both sides' do
inspect_source(cop, ['x=0', 'y+= 0', 'z[0] =0'])
expect(cop.messages)
Expand Down Expand Up @@ -232,35 +248,10 @@
end
end

it 'registers an offense in presence of modifier if statement' do
check_modifier('if')
end

it 'registers an offense in presence of modifier unless statement' do
check_modifier('unless')
end

it 'registers an offense in presence of modifier while statement' do
check_modifier('while')
end

it 'registers an offense in presence of modifier until statement' do
check_modifier('until')
end

def check_modifier(keyword)
src = ["a=1 #{keyword} condition",
'c=2']
inspect_source(cop, src)
expect(cop.offenses.map(&:line)).to eq([1, 2])
expect(cop.messages).to eq(
['Surrounding space missing for operator `=`.'] * 2
)

new_source = autocorrect_source(cop, src)
expect(new_source)
.to eq(src.map { |line| line.sub('=', ' = ') }.join("\n"))
end
it_behaves_like 'modifier with missing space', 'if'
it_behaves_like 'modifier with missing space', 'unless'
it_behaves_like 'modifier with missing space', 'while'
it_behaves_like 'modifier with missing space', 'until'

it 'registers an offense for binary operators that could be unary' do
inspect_source(cop, ['a-3', 'x&0xff', 'z+0'])
Expand Down Expand Up @@ -446,6 +437,22 @@ def check_modifier(keyword)
end

describe 'extra space around operators' do
shared_examples 'modifier with extra space' do |keyword|
it "registers an offense in presence of modifier #{keyword} statement" do
src = ["a = 1 #{keyword} condition",
'c = 2']
inspect_source(cop, src)
expect(cop.offenses.map(&:line)).to eq([1, 2])
expect(cop.messages).to eq(
['Operator `=` should be surrounded by a single space.'] * 2
)

new_source = autocorrect_source(cop, src)
expect(new_source)
.to eq(src.map { |line| line.sub(/\s*=\s*/, ' = ') }.join("\n"))
end
end

it 'registers an offense for assignment with many spaces on either side' do
inspect_source(cop, ['x = 0',
'y += 0',
Expand Down Expand Up @@ -476,35 +483,10 @@ def check_modifier(keyword)
expect(new_source).to eq('x == 0 ? 1 : 2')
end

it 'registers an offense in presence of modifier if statement' do
check_modifier('if')
end

it 'registers an offense in presence of modifier unless statement' do
check_modifier('unless')
end

it 'registers an offense in presence of modifier while statement' do
check_modifier('while')
end

it 'registers an offense in presence of modifier until statement' do
check_modifier('until')
end

def check_modifier(keyword)
src = ["a = 1 #{keyword} condition",
'c = 2']
inspect_source(cop, src)
expect(cop.offenses.map(&:line)).to eq([1, 2])
expect(cop.messages).to eq(
['Operator `=` should be surrounded by a single space.'] * 2
)

new_source = autocorrect_source(cop, src)
expect(new_source)
.to eq(src.map { |line| line.sub(/\s*=\s*/, ' = ') }.join("\n"))
end
it_behaves_like 'modifier with extra space', 'if'
it_behaves_like 'modifier with extra space', 'unless'
it_behaves_like 'modifier with extra space', 'while'
it_behaves_like 'modifier with extra space', 'until'

it 'registers an offense for binary operators that could be unary' do
inspect_source(cop, ['a - 3',
Expand Down

0 comments on commit 5d26c82

Please sign in to comment.