forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 1
/
empty_lines_around_block_body_spec.rb
124 lines (98 loc) · 3.25 KB
/
empty_lines_around_block_body_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Layout::EmptyLinesAroundBlockBody, :config do
# Test blocks using both {} and do..end
[%w[{ }], %w[do end]].each do |open, close|
context "when EnforcedStyle is no_empty_lines for #{open} #{close} block" do
let(:cop_config) { { 'EnforcedStyle' => 'no_empty_lines' } }
it 'registers an offense for block body starting with a blank' do
inspect_source(<<~RUBY)
some_method #{open}
do_something
#{close}
RUBY
expect(cop.messages)
.to eq(['Extra empty line detected at block body beginning.'])
end
it 'autocorrects block body containing only a blank' do
corrected = autocorrect_source(<<~RUBY)
some_method #{open}
#{close}
RUBY
expect(corrected).to eq(<<~RUBY)
some_method #{open}
#{close}
RUBY
end
it 'registers an offense for block body ending with a blank' do
inspect_source(<<~RUBY)
some_method #{open}
do_something
#{close}
RUBY
expect(cop.messages)
.to eq(['Extra empty line detected at block body end.'])
end
it 'accepts block body starting with a line with spaces' do
expect_no_offenses(<<~RUBY)
some_method #{open}
do_something
#{close}
RUBY
end
it 'registers an offense for block body starting with a blank passed to '\
'a multi-line method call' do
inspect_source(<<~RUBY)
some_method arg,
another_arg #{open}
do_something
#{close}
RUBY
expect(cop.messages)
.to eq(['Extra empty line detected at block body beginning.'])
end
it 'is not fooled by single line blocks' do
expect_no_offenses(<<~RUBY)
some_method #{open} do_something #{close}
something_else
RUBY
end
end
context "when EnforcedStyle is empty_lines for #{open} #{close} block" do
let(:cop_config) { { 'EnforcedStyle' => 'empty_lines' } }
it 'registers an offense for block body not starting or ending with a ' \
'blank' do
inspect_source(<<~RUBY)
some_method #{open}
do_something
#{close}
RUBY
expect(cop.messages).to eq(['Empty line missing at block body '\
'beginning.',
'Empty line missing at block body end.'])
end
it 'ignores block with an empty body' do
source = "some_method #{open}\n#{close}"
corrected = autocorrect_source(source)
expect(corrected).to eq(source)
end
it 'autocorrects beginning and end' do
new_source = autocorrect_source(<<~RUBY)
some_method #{open}
do_something
#{close}
RUBY
expect(new_source).to eq(<<~RUBY)
some_method #{open}
do_something
#{close}
RUBY
end
it 'is not fooled by single line blocks' do
expect_no_offenses(<<~RUBY)
some_method #{open} do_something #{close}
something_else
RUBY
end
end
end
end