Skip to content

Commit

Permalink
Improve usage of offense matchers and heredocs in specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Drenmi authored and bbatsov committed Jun 9, 2018
1 parent ded14d8 commit aab0516
Show file tree
Hide file tree
Showing 107 changed files with 1,735 additions and 2,398 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ Layout/ClassStructure:
Layout/IndentHeredoc:
EnforcedStyle: powerpack

# Trailing white space is meaningful in code examples
Layout/TrailingWhitespace:
AllowInHeredoc: true

Lint/AmbiguousBlockAssociation:
Exclude:
- 'spec/**/*.rb'
Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/cop/bundler/duplicated_gem_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
end

it 'does not register any offenses' do
expect(cop.offenses.empty?).to be(true)
expect(cop.offenses.empty?).to eq(true)
end
end

Expand Down
73 changes: 23 additions & 50 deletions spec/rubocop/cop/layout/access_modifier_indentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,83 +15,67 @@
let(:cop_config) { { 'EnforcedStyle' => 'indent' } }

it 'registers an offense for misaligned private' do
inspect_source(<<-RUBY.strip_indent)
expect_offense(<<-RUBY.strip_indent)
class Test
private
^^^^^^^ Indent access modifiers like `private`.
def test; end
end
RUBY
expect(cop.offenses.size).to eq(1)
expect(cop.messages)
.to eq(['Indent access modifiers like `private`.'])
expect(cop.config_to_allow_offenses).to eq('EnforcedStyle' => 'outdent')
end

it 'registers an offense for misaligned private in module' do
inspect_source(<<-RUBY.strip_indent)
expect_offense(<<-RUBY.strip_indent)
module Test
private
^^^^^^^ Indent access modifiers like `private`.
def test; end
end
RUBY
expect(cop.offenses.size).to eq(1)
expect(cop.messages).to eq(['Indent access modifiers like `private`.'])
# Not aligned according to `indent` or `outdent` style:
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
end

it 'registers an offense for misaligned module_function in module' do
inspect_source(<<-RUBY.strip_indent)
expect_offense(<<-RUBY.strip_indent)
module Test
module_function
^^^^^^^^^^^^^^^ Indent access modifiers like `module_function`.
def test; end
end
RUBY
expect(cop.offenses.size).to eq(1)
expect(cop.messages)
.to eq(['Indent access modifiers like `module_function`.'])
# Not aligned according to `indent` or `outdent` style:
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
end

it 'registers an offense for correct + opposite alignment' do
inspect_source(<<-RUBY.strip_indent)
expect_offense(<<-RUBY.strip_indent)
module Test
public
private
^^^^^^^ Indent access modifiers like `private`.
def test; end
end
RUBY
expect(cop.offenses.size).to eq(1)
expect(cop.messages).to eq(['Indent access modifiers like `private`.'])
# No EnforcedStyle can allow both alignments:
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
end

it 'registers an offense for opposite + correct alignment' do
inspect_source(<<-RUBY.strip_indent)
expect_offense(<<-RUBY.strip_indent)
module Test
public
^^^^^^ Indent access modifiers like `public`.
private
def test; end
end
RUBY
expect(cop.offenses.size).to eq(1)
expect(cop.messages).to eq(['Indent access modifiers like `public`.'])
# No EnforcedStyle can allow both alignments:
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
end

it 'registers an offense for misaligned private in singleton class' do
Expand All @@ -108,45 +92,40 @@ def test; end

it 'registers an offense for misaligned private in class ' \
'defined with Class.new' do
inspect_source(<<-RUBY.strip_indent)
expect_offense(<<-RUBY.strip_indent)
Test = Class.new do
private
^^^^^^^ Indent access modifiers like `private`.
def test; end
end
RUBY
expect(cop.offenses.size).to eq(1)
expect(cop.messages)
.to eq(['Indent access modifiers like `private`.'])
end

it 'accepts misaligned private in blocks that are not recognized as ' \
'class/module definitions' do
inspect_source(<<-RUBY.strip_indent)
expect_no_offenses(<<-RUBY.strip_indent)
Test = func do
private
def test; end
end
RUBY
expect(cop.offenses.empty?).to be(true)
end

it 'registers an offense for misaligned private in module ' \
'defined with Module.new' do
inspect_source(<<-RUBY.strip_indent)
expect_offense(<<-RUBY.strip_indent)
Test = Module.new do
private
^^^^^^^ Indent access modifiers like `private`.
def test; end
end
RUBY
expect(cop.offenses.size).to eq(1)
expect(cop.messages)
.to eq(['Indent access modifiers like `private`.'])
end

it 'registers an offense for misaligned protected' do
Expand Down Expand Up @@ -277,20 +256,17 @@ def test; end

context 'when EnforcedStyle is set to outdent' do
let(:cop_config) { { 'EnforcedStyle' => 'outdent' } }
let(:indent_msg) { 'Outdent access modifiers like `private`.' }

it 'registers offense for private indented to method depth in a class' do
inspect_source(<<-RUBY.strip_indent)
expect_offense(<<-RUBY.strip_indent)
class Test
private
^^^^^^^ Outdent access modifiers like `private`.
def test; end
end
RUBY
expect(cop.offenses.size).to eq(1)
expect(cop.messages).to eq([indent_msg])
expect(cop.config_to_allow_offenses).to eq('EnforcedStyle' => 'indent')
end

it 'registers offense for private indented to method depth in a module' do
Expand Down Expand Up @@ -319,44 +295,41 @@ def test; end

it 'registers offense for private indented to method depth in singleton' \
'class' do
inspect_source(<<-RUBY.strip_indent)
expect_offense(<<-RUBY.strip_indent)
class << self
private
^^^^^^^ Outdent access modifiers like `private`.
def test; end
end
RUBY
expect(cop.offenses.size).to eq(1)
expect(cop.messages).to eq([indent_msg])
end

it 'registers offense for private indented to method depth in class ' \
'defined with Class.new' do
inspect_source(<<-RUBY.strip_indent)
expect_offense(<<-RUBY.strip_indent)
Test = Class.new do
private
^^^^^^^ Outdent access modifiers like `private`.
def test; end
end
RUBY
expect(cop.offenses.size).to eq(1)
expect(cop.messages).to eq([indent_msg])
end

it 'registers offense for private indented to method depth in module ' \
'defined with Module.new' do
inspect_source(<<-RUBY.strip_indent)
expect_offense(<<-RUBY.strip_indent)
Test = Module.new do
private
^^^^^^^ Outdent access modifiers like `private`.
def test; end
end
RUBY
expect(cop.offenses.size).to eq(1)
expect(cop.messages).to eq([indent_msg])
end

it 'accepts private indented to the containing class indent level' do
Expand Down
52 changes: 16 additions & 36 deletions spec/rubocop/cop/layout/block_alignment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,27 +183,22 @@ def foo(bar)
end

it 'registers offenses for misaligned ends' do
src = <<-RUBY.strip_indent
expect_offense(<<-RUBY.strip_indent)
def foo(bar)
bar.get_stuffs
.reject do |stuff|
stuff.with_a_very_long_expression_that_doesnt_fit_the_line
end.select do |stuff|
^^^ `end` at 5, 8 is not aligned with `bar.get_stuffs` at 2, 2 or `.reject do |stuff|` at 3, 6.
stuff.another_very_long_expression_that_doesnt_fit_the_line
end
^^^ `end` at 7, 4 is not aligned with `bar.get_stuffs` at 2, 2 or `end.select do |stuff|` at 5, 8.
.select do |stuff|
stuff.another_very_long_expression_that_doesnt_fit_the_line
end
^^^ `end` at 10, 8 is not aligned with `bar.get_stuffs` at 2, 2 or `.select do |stuff|` at 8, 6.
end
RUBY
inspect_source(src)
expect(cop.messages)
.to eq(['`end` at 5, 8 is not aligned with `bar.get_stuffs` at 2, 2' \
' or `.reject do |stuff|` at 3, 6.',
'`end` at 7, 4 is not aligned with `bar.get_stuffs` at 2, 2' \
' or `end.select do |stuff|` at 5, 8.',
'`end` at 10, 8 is not aligned with `bar.get_stuffs` at 2, 2' \
' or `.select do |stuff|` at 8, 6.'])
end

# Example from issue 393 of rubocop-hq/rubocop on github:
Expand Down Expand Up @@ -284,16 +279,13 @@ def foo(bar)
end

it 'registers an offense for end aligned with the block' do
src = <<-RUBY.strip_indent
expect_offense(<<-RUBY.strip_indent)
e,
f = [5, 6].map do |i|
i - 5
end
^^^ `end` at 4, 4 is not aligned with `e,` at 1, 0 or `f = [5, 6].map do |i|` at 2, 0.
RUBY
inspect_source(src)
expect(cop.messages)
.to eq(['`end` at 4, 4 is not aligned with `e,` at 1, 0 or' \
' `f = [5, 6].map do |i|` at 2, 0.'])
end

it 'auto-corrects' do
Expand Down Expand Up @@ -323,13 +315,11 @@ def foo(bar)

it 'registers an offense for mismatched block end with' \
' an instance variable' do
inspect_source(<<-RUBY.strip_indent)
expect_offense(<<-RUBY.strip_indent)
@variable = test do |ala|
end
^^^ `end` at 2, 2 is not aligned with `@variable = test do |ala|` at 1, 0.
RUBY
expect(cop.messages)
.to eq(['`end` at 2, 2 is not aligned with `@variable = test do |ala|`' \
' at 1, 0.'])
end

it 'accepts end aligned with a class variable' do
Expand Down Expand Up @@ -404,14 +394,12 @@ def foo(bar)

it 'registers an offense for mismatched end with a method call' \
' with arguments' do
inspect_source(<<-RUBY.strip_indent)
expect_offense(<<-RUBY.strip_indent)
@h[:f] = f.each_pair.map do |f, v|
v = 1
end
^^^ `end` at 3, 2 is not aligned with `@h[:f] = f.each_pair.map do |f, v|` at 1, 0.
RUBY
expect(cop.messages)
.to eq(['`end` at 3, 2 is not aligned with' \
' `@h[:f] = f.each_pair.map do |f, v|` at 1, 0.'])
end

it 'does not raise an error for nested block in a method call' do
Expand All @@ -428,14 +416,12 @@ def foo(bar)

it 'registers an offense for mismatched end not aligned with the block' \
' that is an argument' do
inspect_source(<<-RUBY.strip_indent)
expect_offense(<<-RUBY.strip_indent)
expect(arr.all? do |o|
o.valid?
end)
^^^ `end` at 3, 2 is not aligned with `arr.all? do |o|` at 1, 7 or `expect(arr.all? do |o|` at 1, 0.
RUBY
expect(cop.messages)
.to eq(['`end` at 3, 2 is not aligned with `arr.all? do |o|` at 1, 7 or' \
' `expect(arr.all? do |o|` at 1, 0.'])
end

it 'accepts end aligned with an op-asgn (+=, -=)' do
Expand Down Expand Up @@ -676,16 +662,13 @@ def abc
end

it 'errors when do aligned' do
src = <<-RUBY.strip_indent
expect_offense(<<-RUBY.strip_indent)
foo.bar
.each do
baz
end
^^^ `end` at 4, 2 is not aligned with `foo.bar` at 1, 0.
RUBY
inspect_source(src)
expect(cop.messages)
.to eq(['`end` at 4, 2 is not aligned with ' \
'`foo.bar` at 1, 0.'])
end

it 'autocorrects' do
Expand Down Expand Up @@ -722,16 +705,13 @@ def abc
end

it 'errors when start_of_line aligned' do
src = <<-RUBY.strip_indent
expect_offense(<<-RUBY.strip_indent)
foo.bar
.each do
baz
end
^^^ `end` at 4, 0 is not aligned with `.each do` at 2, 2.
RUBY
inspect_source(src)
expect(cop.messages)
.to eq(['`end` at 4, 0 is not aligned with ' \
'`.each do` at 2, 2.'])
end

it 'autocorrects' do
Expand Down
Loading

0 comments on commit aab0516

Please sign in to comment.