/
trailing_whitespace_spec.rb
202 lines (176 loc) · 4.9 KB
/
trailing_whitespace_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Layout::TrailingWhitespace, :config do
let(:cop_config) { { 'AllowInHeredoc' => false } }
it 'registers an offense for a line ending with space' do
expect_offense(<<~RUBY)
x = 0#{trailing_whitespace}
^ Trailing whitespace detected.
RUBY
end
it 'registers an offense for a blank line with space' do
expect_offense(<<~RUBY)
#{trailing_whitespace * 2}
^^ Trailing whitespace detected.
RUBY
end
it 'registers an offense for a line ending with tab' do
expect_offense(<<~RUBY)
x = 0\t
^ Trailing whitespace detected.
RUBY
end
it 'registers an offense for trailing whitespace in a heredoc string' do
expect_offense(<<~RUBY)
x = <<HEREDOC
Hi#{trailing_whitespace * 3}
^^^ Trailing whitespace detected.
HEREDOC
RUBY
end
it 'registers offenses before __END__ but not after' do
expect_offense(<<~RUBY)
x = 0\t
^ Trailing whitespace detected.
#{trailing_whitespace}
^ Trailing whitespace detected.
__END__
x = 0\t
RUBY
end
it 'is not fooled by __END__ within a documentation comment' do
expect_offense(<<~RUBY)
x = 0\t
^ Trailing whitespace detected.
=begin
__END__
=end
x = 0\t
^ Trailing whitespace detected.
RUBY
end
it 'is not fooled by heredoc containing __END__' do
expect_offense(<<~RUBY)
x1 = <<HEREDOC#{trailing_whitespace}
^ Trailing whitespace detected.
__END__
x2 = 0\t
^ Trailing whitespace detected.
HEREDOC
x3 = 0\t
^ Trailing whitespace detected.
RUBY
end
it 'is not fooled by heredoc containing __END__ within a doc comment' do
expect_offense(<<~RUBY)
x1 = <<HEREDOC#{trailing_whitespace}
^ Trailing whitespace detected.
=begin#{trailing_whitespace * 2}
^^ Trailing whitespace detected.
__END__
=end
x2 = 0\t
^ Trailing whitespace detected.
HEREDOC
x3 = 0\t
^ Trailing whitespace detected.
RUBY
end
it 'accepts a line without trailing whitespace' do
expect_no_offenses('x = 0')
end
it 'auto-corrects unwanted space' do
expect_offense(<<~RUBY)
x = 0#{trailing_whitespace}
^ Trailing whitespace detected.
x = 0\t
^ Trailing whitespace detected.
RUBY
expect_correction(<<~RUBY)
x = 0
x = 0
RUBY
end
context 'when `AllowInHeredoc` is set to true' do
let(:cop_config) { { 'AllowInHeredoc' => true } }
it 'accepts trailing whitespace in a heredoc string' do
expect_no_offenses(<<~RUBY)
x = <<HEREDOC
Hi#{trailing_whitespace * 3}
HEREDOC
RUBY
end
it 'registers an offense for trailing whitespace at the heredoc begin' do
expect_offense(<<~RUBY)
x = <<HEREDOC#{trailing_whitespace}
^ Trailing whitespace detected.
Hi#{trailing_whitespace * 3}
HEREDOC
RUBY
end
end
context 'when `AllowInHeredoc` is set to false' do
let(:cop_config) { { 'AllowInHeredoc' => false } }
it 'corrects safely trailing whitespace in a heredoc string' do
expect_offense(<<~RUBY)
x = <<~EXAMPLE
has trailing #{trailing_whitespace}
^^^^ Trailing whitespace detected.
no trailing
EXAMPLE
RUBY
expect_correction(<<~RUBY)
x = <<~EXAMPLE
has trailing\#{' '}
no trailing
EXAMPLE
RUBY
end
it 'corrects by removing trailing whitespace used for indentation in a heredoc string' do
expect_offense(<<~RUBY)
x = <<~EXAMPLE
no trailing
#{trailing_whitespace}
^^ Trailing whitespace detected.
no trailing
#{trailing_whitespace}
^ Trailing whitespace detected.
no trailing
EXAMPLE
RUBY
expect_correction(<<~RUBY)
x = <<~EXAMPLE
no trailing
no trailing
no trailing
EXAMPLE
RUBY
end
it 'corrects a whitespace line in a heredoc string that is longer than the indentation' do
expect_offense(<<~RUBY)
x = <<~EXAMPLE
no trailing
#{trailing_whitespace}
^^^ Trailing whitespace detected.
no trailing
EXAMPLE
RUBY
expect_correction(<<~RUBY)
x = <<~EXAMPLE
no trailing
\#{' '}
no trailing
EXAMPLE
RUBY
end
it 'does not correct trailing whitespace in a static heredoc string' do
expect_offense(<<~RUBY)
x = <<~'EXAMPLE'
has trailing#{trailing_whitespace}
^ Trailing whitespace detected.
no trailing
EXAMPLE
RUBY
expect_no_corrections
end
end
end