/
trailing_whitespace_spec.rb
125 lines (106 loc) · 3.91 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
# 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
offenses = inspect_source('x = 0 ')
expect(offenses.size).to eq(1)
end
it 'registers an offense for a blank line with space' do
offenses = inspect_source(' ')
expect(offenses.size).to eq(1)
end
it 'registers an offense for a line ending with tab' do
offenses = inspect_source("x = 0\t")
expect(offenses.size).to eq(1)
end
it 'registers an offense for trailing whitespace in a heredoc string' do
offenses = inspect_source(['x = <<RUBY',
' Hi ',
'RUBY'].join("\n"))
expect(offenses.size).to eq(1)
end
it 'registers offenses before __END__ but not after' do
offenses = inspect_source(["x = 0\t",
' ',
'__END__',
"x = 0\t"].join("\n"))
expect(offenses.map(&:line)).to eq([1, 2])
end
it 'is not fooled by __END__ within a documentation comment' do
offenses = inspect_source(["x = 0\t",
'=begin',
'__END__',
'=end',
"x = 0\t"].join("\n"))
expect(offenses.map(&:line)).to eq([1, 5])
end
it 'is not fooled by heredoc containing __END__' do
offenses = inspect_source(['x1 = <<RUBY ',
'__END__',
"x2 = 0\t",
'RUBY',
"x3 = 0\t"].join("\n"))
expect(offenses.map(&:line)).to eq([1, 3, 5])
end
it 'is not fooled by heredoc containing __END__ within a doc comment' do
offenses = inspect_source(['x1 = <<RUBY ',
'=begin ',
'__END__',
'=end',
"x2 = 0\t",
'RUBY',
"x3 = 0\t"].join("\n"))
expect(offenses.map(&:line)).to eq([1, 2, 5, 7])
end
it 'accepts a line without trailing whitespace' do
expect_no_offenses('x = 0')
end
it 'auto-corrects unwanted space' do
new_source = autocorrect_source(['x = 0 ',
"x = 0\t"].join("\n"))
expect(new_source).to eq(['x = 0',
'x = 0'].join("\n"))
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(['x = <<RUBY',
' Hi ',
'RUBY'].join("\n"))
end
it 'registers an offense for trailing whitespace at the heredoc begin' do
offenses = inspect_source(['x = <<RUBY ',
' Hi ',
'RUBY'].join("\n"))
expect(offenses.size).to eq(1)
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 '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