Skip to content

Commit aab0516

Browse files
Drenmibbatsov
authored andcommitted
Improve usage of offense matchers and heredocs in specs
1 parent ded14d8 commit aab0516

File tree

107 files changed

+1735
-2398
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+1735
-2398
lines changed

.rubocop.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ Layout/ClassStructure:
5151
Layout/IndentHeredoc:
5252
EnforcedStyle: powerpack
5353

54+
# Trailing white space is meaningful in code examples
55+
Layout/TrailingWhitespace:
56+
AllowInHeredoc: true
57+
5458
Lint/AmbiguousBlockAssociation:
5559
Exclude:
5660
- 'spec/**/*.rb'

spec/rubocop/cop/bundler/duplicated_gem_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
end
2525

2626
it 'does not register any offenses' do
27-
expect(cop.offenses.empty?).to be(true)
27+
expect(cop.offenses.empty?).to eq(true)
2828
end
2929
end
3030

spec/rubocop/cop/layout/access_modifier_indentation_spec.rb

Lines changed: 23 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -15,83 +15,67 @@
1515
let(:cop_config) { { 'EnforcedStyle' => 'indent' } }
1616

1717
it 'registers an offense for misaligned private' do
18-
inspect_source(<<-RUBY.strip_indent)
18+
expect_offense(<<-RUBY.strip_indent)
1919
class Test
2020
2121
private
22+
^^^^^^^ Indent access modifiers like `private`.
2223
2324
def test; end
2425
end
2526
RUBY
26-
expect(cop.offenses.size).to eq(1)
27-
expect(cop.messages)
28-
.to eq(['Indent access modifiers like `private`.'])
29-
expect(cop.config_to_allow_offenses).to eq('EnforcedStyle' => 'outdent')
3027
end
3128

3229
it 'registers an offense for misaligned private in module' do
33-
inspect_source(<<-RUBY.strip_indent)
30+
expect_offense(<<-RUBY.strip_indent)
3431
module Test
3532
3633
private
34+
^^^^^^^ Indent access modifiers like `private`.
3735
3836
def test; end
3937
end
4038
RUBY
41-
expect(cop.offenses.size).to eq(1)
42-
expect(cop.messages).to eq(['Indent access modifiers like `private`.'])
43-
# Not aligned according to `indent` or `outdent` style:
44-
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
4539
end
4640

4741
it 'registers an offense for misaligned module_function in module' do
48-
inspect_source(<<-RUBY.strip_indent)
42+
expect_offense(<<-RUBY.strip_indent)
4943
module Test
5044
5145
module_function
46+
^^^^^^^^^^^^^^^ Indent access modifiers like `module_function`.
5247
5348
def test; end
5449
end
5550
RUBY
56-
expect(cop.offenses.size).to eq(1)
57-
expect(cop.messages)
58-
.to eq(['Indent access modifiers like `module_function`.'])
59-
# Not aligned according to `indent` or `outdent` style:
60-
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
6151
end
6252

6353
it 'registers an offense for correct + opposite alignment' do
64-
inspect_source(<<-RUBY.strip_indent)
54+
expect_offense(<<-RUBY.strip_indent)
6555
module Test
6656
6757
public
6858
6959
private
60+
^^^^^^^ Indent access modifiers like `private`.
7061
7162
def test; end
7263
end
7364
RUBY
74-
expect(cop.offenses.size).to eq(1)
75-
expect(cop.messages).to eq(['Indent access modifiers like `private`.'])
76-
# No EnforcedStyle can allow both alignments:
77-
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
7865
end
7966

8067
it 'registers an offense for opposite + correct alignment' do
81-
inspect_source(<<-RUBY.strip_indent)
68+
expect_offense(<<-RUBY.strip_indent)
8269
module Test
8370
8471
public
72+
^^^^^^ Indent access modifiers like `public`.
8573
8674
private
8775
8876
def test; end
8977
end
9078
RUBY
91-
expect(cop.offenses.size).to eq(1)
92-
expect(cop.messages).to eq(['Indent access modifiers like `public`.'])
93-
# No EnforcedStyle can allow both alignments:
94-
expect(cop.config_to_allow_offenses).to eq('Enabled' => false)
9579
end
9680

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

10993
it 'registers an offense for misaligned private in class ' \
11094
'defined with Class.new' do
111-
inspect_source(<<-RUBY.strip_indent)
95+
expect_offense(<<-RUBY.strip_indent)
11296
Test = Class.new do
11397
11498
private
99+
^^^^^^^ Indent access modifiers like `private`.
115100
116101
def test; end
117102
end
118103
RUBY
119-
expect(cop.offenses.size).to eq(1)
120-
expect(cop.messages)
121-
.to eq(['Indent access modifiers like `private`.'])
122104
end
123105

124106
it 'accepts misaligned private in blocks that are not recognized as ' \
125107
'class/module definitions' do
126-
inspect_source(<<-RUBY.strip_indent)
108+
expect_no_offenses(<<-RUBY.strip_indent)
127109
Test = func do
128110
129111
private
130112
131113
def test; end
132114
end
133115
RUBY
134-
expect(cop.offenses.empty?).to be(true)
135116
end
136117

137118
it 'registers an offense for misaligned private in module ' \
138119
'defined with Module.new' do
139-
inspect_source(<<-RUBY.strip_indent)
120+
expect_offense(<<-RUBY.strip_indent)
140121
Test = Module.new do
141122
142123
private
124+
^^^^^^^ Indent access modifiers like `private`.
143125
144126
def test; end
145127
end
146128
RUBY
147-
expect(cop.offenses.size).to eq(1)
148-
expect(cop.messages)
149-
.to eq(['Indent access modifiers like `private`.'])
150129
end
151130

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

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

282260
it 'registers offense for private indented to method depth in a class' do
283-
inspect_source(<<-RUBY.strip_indent)
261+
expect_offense(<<-RUBY.strip_indent)
284262
class Test
285263
286264
private
265+
^^^^^^^ Outdent access modifiers like `private`.
287266
288267
def test; end
289268
end
290269
RUBY
291-
expect(cop.offenses.size).to eq(1)
292-
expect(cop.messages).to eq([indent_msg])
293-
expect(cop.config_to_allow_offenses).to eq('EnforcedStyle' => 'indent')
294270
end
295271

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

320296
it 'registers offense for private indented to method depth in singleton' \
321297
'class' do
322-
inspect_source(<<-RUBY.strip_indent)
298+
expect_offense(<<-RUBY.strip_indent)
323299
class << self
324300
325301
private
302+
^^^^^^^ Outdent access modifiers like `private`.
326303
327304
def test; end
328305
end
329306
RUBY
330-
expect(cop.offenses.size).to eq(1)
331-
expect(cop.messages).to eq([indent_msg])
332307
end
333308

334309
it 'registers offense for private indented to method depth in class ' \
335310
'defined with Class.new' do
336-
inspect_source(<<-RUBY.strip_indent)
311+
expect_offense(<<-RUBY.strip_indent)
337312
Test = Class.new do
338313
339314
private
315+
^^^^^^^ Outdent access modifiers like `private`.
340316
341317
def test; end
342318
end
343319
RUBY
344-
expect(cop.offenses.size).to eq(1)
345-
expect(cop.messages).to eq([indent_msg])
346320
end
347321

348322
it 'registers offense for private indented to method depth in module ' \
349323
'defined with Module.new' do
350-
inspect_source(<<-RUBY.strip_indent)
324+
expect_offense(<<-RUBY.strip_indent)
351325
Test = Module.new do
352326
353327
private
328+
^^^^^^^ Outdent access modifiers like `private`.
354329
355330
def test; end
356331
end
357332
RUBY
358-
expect(cop.offenses.size).to eq(1)
359-
expect(cop.messages).to eq([indent_msg])
360333
end
361334

362335
it 'accepts private indented to the containing class indent level' do

spec/rubocop/cop/layout/block_alignment_spec.rb

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -183,27 +183,22 @@ def foo(bar)
183183
end
184184

185185
it 'registers offenses for misaligned ends' do
186-
src = <<-RUBY.strip_indent
186+
expect_offense(<<-RUBY.strip_indent)
187187
def foo(bar)
188188
bar.get_stuffs
189189
.reject do |stuff|
190190
stuff.with_a_very_long_expression_that_doesnt_fit_the_line
191191
end.select do |stuff|
192+
^^^ `end` at 5, 8 is not aligned with `bar.get_stuffs` at 2, 2 or `.reject do |stuff|` at 3, 6.
192193
stuff.another_very_long_expression_that_doesnt_fit_the_line
193194
end
195+
^^^ `end` at 7, 4 is not aligned with `bar.get_stuffs` at 2, 2 or `end.select do |stuff|` at 5, 8.
194196
.select do |stuff|
195197
stuff.another_very_long_expression_that_doesnt_fit_the_line
196198
end
199+
^^^ `end` at 10, 8 is not aligned with `bar.get_stuffs` at 2, 2 or `.select do |stuff|` at 8, 6.
197200
end
198201
RUBY
199-
inspect_source(src)
200-
expect(cop.messages)
201-
.to eq(['`end` at 5, 8 is not aligned with `bar.get_stuffs` at 2, 2' \
202-
' or `.reject do |stuff|` at 3, 6.',
203-
'`end` at 7, 4 is not aligned with `bar.get_stuffs` at 2, 2' \
204-
' or `end.select do |stuff|` at 5, 8.',
205-
'`end` at 10, 8 is not aligned with `bar.get_stuffs` at 2, 2' \
206-
' or `.select do |stuff|` at 8, 6.'])
207202
end
208203

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

286281
it 'registers an offense for end aligned with the block' do
287-
src = <<-RUBY.strip_indent
282+
expect_offense(<<-RUBY.strip_indent)
288283
e,
289284
f = [5, 6].map do |i|
290285
i - 5
291286
end
287+
^^^ `end` at 4, 4 is not aligned with `e,` at 1, 0 or `f = [5, 6].map do |i|` at 2, 0.
292288
RUBY
293-
inspect_source(src)
294-
expect(cop.messages)
295-
.to eq(['`end` at 4, 4 is not aligned with `e,` at 1, 0 or' \
296-
' `f = [5, 6].map do |i|` at 2, 0.'])
297289
end
298290

299291
it 'auto-corrects' do
@@ -323,13 +315,11 @@ def foo(bar)
323315

324316
it 'registers an offense for mismatched block end with' \
325317
' an instance variable' do
326-
inspect_source(<<-RUBY.strip_indent)
318+
expect_offense(<<-RUBY.strip_indent)
327319
@variable = test do |ala|
328320
end
321+
^^^ `end` at 2, 2 is not aligned with `@variable = test do |ala|` at 1, 0.
329322
RUBY
330-
expect(cop.messages)
331-
.to eq(['`end` at 2, 2 is not aligned with `@variable = test do |ala|`' \
332-
' at 1, 0.'])
333323
end
334324

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

405395
it 'registers an offense for mismatched end with a method call' \
406396
' with arguments' do
407-
inspect_source(<<-RUBY.strip_indent)
397+
expect_offense(<<-RUBY.strip_indent)
408398
@h[:f] = f.each_pair.map do |f, v|
409399
v = 1
410400
end
401+
^^^ `end` at 3, 2 is not aligned with `@h[:f] = f.each_pair.map do |f, v|` at 1, 0.
411402
RUBY
412-
expect(cop.messages)
413-
.to eq(['`end` at 3, 2 is not aligned with' \
414-
' `@h[:f] = f.each_pair.map do |f, v|` at 1, 0.'])
415403
end
416404

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

429417
it 'registers an offense for mismatched end not aligned with the block' \
430418
' that is an argument' do
431-
inspect_source(<<-RUBY.strip_indent)
419+
expect_offense(<<-RUBY.strip_indent)
432420
expect(arr.all? do |o|
433421
o.valid?
434422
end)
423+
^^^ `end` at 3, 2 is not aligned with `arr.all? do |o|` at 1, 7 or `expect(arr.all? do |o|` at 1, 0.
435424
RUBY
436-
expect(cop.messages)
437-
.to eq(['`end` at 3, 2 is not aligned with `arr.all? do |o|` at 1, 7 or' \
438-
' `expect(arr.all? do |o|` at 1, 0.'])
439425
end
440426

441427
it 'accepts end aligned with an op-asgn (+=, -=)' do
@@ -676,16 +662,13 @@ def abc
676662
end
677663

678664
it 'errors when do aligned' do
679-
src = <<-RUBY.strip_indent
665+
expect_offense(<<-RUBY.strip_indent)
680666
foo.bar
681667
.each do
682668
baz
683669
end
670+
^^^ `end` at 4, 2 is not aligned with `foo.bar` at 1, 0.
684671
RUBY
685-
inspect_source(src)
686-
expect(cop.messages)
687-
.to eq(['`end` at 4, 2 is not aligned with ' \
688-
'`foo.bar` at 1, 0.'])
689672
end
690673

691674
it 'autocorrects' do
@@ -722,16 +705,13 @@ def abc
722705
end
723706

724707
it 'errors when start_of_line aligned' do
725-
src = <<-RUBY.strip_indent
708+
expect_offense(<<-RUBY.strip_indent)
726709
foo.bar
727710
.each do
728711
baz
729712
end
713+
^^^ `end` at 4, 0 is not aligned with `.each do` at 2, 2.
730714
RUBY
731-
inspect_source(src)
732-
expect(cop.messages)
733-
.to eq(['`end` at 4, 0 is not aligned with ' \
734-
'`.each do` at 2, 2.'])
735715
end
736716

737717
it 'autocorrects' do

0 commit comments

Comments
 (0)