-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
end_of_line_spec.rb
172 lines (141 loc) · 4.95 KB
/
end_of_line_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Layout::EndOfLine, :config do
shared_examples 'all configurations' do
it 'accepts an empty file' do
inspect_source_file('')
expect(cop.offenses.empty?).to be(true)
end
end
shared_examples 'iso-8859-15' do |eol|
it 'can inspect non-UTF-8 encoded source with proper encoding comment' do
inspect_source_file(["# coding: ISO-8859-15#{eol}",
"# Euro symbol: \xa4#{eol}"].join("\n"))
expect(cop.offenses.size).to eq(1)
end
end
context 'when EnforcedStyle is native' do
let(:cop_config) { { 'EnforcedStyle' => 'native' } }
let(:messages) do
['Carriage return character ' \
"#{RuboCop::Platform.windows? ? 'missing' : 'detected'}."]
end
it 'registers an offense for an incorrect EOL' do
inspect_source_file(['x=0', '', "y=1\r"].join("\n"))
expect(cop.messages).to eq(messages)
expect(cop.offenses.map(&:line))
.to eq([RuboCop::Platform.windows? ? 1 : 3])
end
end
context 'when EnforcedStyle is crlf' do
let(:cop_config) { { 'EnforcedStyle' => 'crlf' } }
let(:messages) { ['Carriage return character missing.'] }
include_examples 'all configurations'
it 'registers an offense for CR+LF' do
inspect_source_file(['x=0', '', "y=1\r"].join("\n"))
expect(cop.messages).to eq(messages)
expect(cop.offenses.map(&:line)).to eq([1])
end
it 'highlights the whole offending line' do
inspect_source_file(['x=0', '', "y=1\r"].join("\n"))
expect(cop.highlights).to eq(["x=0\n"])
end
it 'does not register offense for no CR at end of file' do
inspect_source_file('x=0')
expect(cop.offenses.empty?).to be(true)
end
it 'does not register offenses after __END__' do
expect_no_offenses(<<~RUBY)
x=0\r
__END__
x=0
RUBY
end
context 'and there are many lines ending with LF' do
it 'registers only one offense' do
inspect_source_file(<<~RUBY)
x=0
y=1
RUBY
expect(cop.messages.size).to eq(1)
end
include_examples 'iso-8859-15', ''
end
context 'and the default external encoding is US_ASCII' do
around do |example|
orig_encoding = Encoding.default_external
Encoding.default_external = Encoding::US_ASCII
example.run
Encoding.default_external = orig_encoding
end
it 'does not crash on UTF-8 encoded non-ascii characters' do
source = ['class Epd::ReportsController < EpdAreaController',
" 'terecht bij uw ROM-coördinator.'",
'end'].join("\r\n")
inspect_source_file(source)
expect(cop.offenses.empty?).to be(true)
end
include_examples 'iso-8859-15', ''
end
context 'and source is a string' do
it 'registers an offense' do
inspect_source("x=0\ny=1")
expect(cop.messages).to eq(['Carriage return character missing.'])
end
end
end
context 'when EnforcedStyle is lf' do
let(:cop_config) { { 'EnforcedStyle' => 'lf' } }
include_examples 'all configurations'
it 'registers an offense for CR+LF' do
inspect_source_file(['x=0', '', "y=1\r"].join("\n"))
expect(cop.messages).to eq(['Carriage return character detected.'])
expect(cop.offenses.map(&:line)).to eq([3])
end
it 'highlights the whole offending line' do
inspect_source_file(['x=0', '', "y=1\r"].join("\n"))
expect(cop.highlights).to eq(["y=1\r"])
end
it 'registers an offense for CR at end of file' do
inspect_source_file("x=0\r")
expect(cop.messages).to eq(['Carriage return character detected.'])
end
it 'does not register offenses after __END__' do
expect_no_offenses(<<~RUBY)
x=0
__END__
x=0\r
RUBY
end
context 'and there are many lines ending with CR+LF' do
it 'registers only one offense' do
inspect_source_file(['x=0', '', 'y=1'].join("\r\n"))
expect(cop.messages.size).to eq(1)
end
include_examples 'iso-8859-15', "\r"
end
context 'and the default external encoding is US_ASCII' do
around do |example|
orig_encoding = Encoding.default_external
Encoding.default_external = Encoding::US_ASCII
example.run
Encoding.default_external = orig_encoding
end
it 'does not crash on UTF-8 encoded non-ascii characters' do
source = ['class Epd::ReportsController < EpdAreaController',
" 'terecht bij uw ROM-coördinator.'",
'end'].join("\n")
inspect_source_file(source)
expect(cop.offenses.empty?).to be(true)
end
include_examples 'iso-8859-15', "\r"
end
context 'and source is a string' do
it 'registers an offense' do
expect_offense(<<~RUBY)
x=0\r
^^^ Carriage return character detected.
RUBY
end
end
end
end