/
magic_comment_spec.rb
272 lines (192 loc) · 8.72 KB
/
magic_comment_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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# frozen_string_literal: true
RSpec.describe RuboCop::MagicComment do
shared_examples 'magic comment' do |comment, expectations = {}|
encoding = expectations[:encoding]
frozen_string = expectations[:frozen_string_literal]
shareable_constant_value = expectations[:shareable_constant_value]
it "returns #{encoding.inspect} for encoding when comment is #{comment}" do
expect(described_class.parse(comment).encoding).to eql(encoding)
end
it "returns #{frozen_string.inspect} for frozen_string_literal when comment is #{comment}" do
expect(described_class.parse(comment).frozen_string_literal).to eql(frozen_string)
end
it "returns #{shareable_constant_value.inspect} for shareable_constant_value " \
"when comment is #{comment}" do
expect(described_class.parse(comment)
.shareable_constant_value).to eql(shareable_constant_value)
end
end
include_examples 'magic comment', '#'
include_examples 'magic comment', '# encoding: utf-8', encoding: 'utf-8'
include_examples 'magic comment', '# ENCODING: utf-8', encoding: 'utf-8'
include_examples 'magic comment', '# eNcOdInG: utf-8', encoding: 'utf-8'
include_examples 'magic comment', '# coding: utf-8', encoding: 'utf-8'
include_examples 'magic comment', ' # coding: utf-8', encoding: 'utf-8'
include_examples 'magic comment', '# incoding: utf-8'
include_examples 'magic comment',
'# encoding: stateless-iso-2022-jp-kddi',
encoding: 'stateless-iso-2022-jp-kddi'
include_examples 'magic comment', '# frozen_string_literal: true', frozen_string_literal: true
include_examples 'magic comment', ' # frozen_string_literal: true', frozen_string_literal: true
include_examples 'magic comment', '# frozen_string_literal:true', frozen_string_literal: true
include_examples 'magic comment', '# frozen_string_literal: false', frozen_string_literal: false
include_examples 'magic comment', '# frozen-string-literal: true', frozen_string_literal: true
include_examples 'magic comment', '# FROZEN-STRING-LITERAL: true', frozen_string_literal: true
include_examples 'magic comment', '# fRoZeN-sTrInG_lItErAl: true', frozen_string_literal: true
include_examples 'magic comment', '# shareable_constant_value: literal', shareable_constant_value: 'literal'
include_examples 'magic comment', '# shareable_constant_value:literal', shareable_constant_value: 'literal'
include_examples 'magic comment', '# shareable-constant-value: literal', shareable_constant_value: 'literal'
include_examples 'magic comment', '# SHAREABLE-CONSTANT-VALUE: literal', shareable_constant_value: 'literal'
include_examples 'magic comment', '# sHaReaBLE-CoNstANT-ValUE: literal', shareable_constant_value: 'literal'
include_examples 'magic comment', '# shareable_constant_value: none', shareable_constant_value: 'none'
include_examples 'magic comment', '# xyz shareable_constant_value: literal'
include_examples 'magic comment', '# xyz shareable_constant_value: literal xyz'
include_examples(
'magic comment',
'# shareable_constant_value: experimental_everything',
shareable_constant_value: 'experimental_everything'
)
include_examples(
'magic comment',
'# shareable_constant_value: experimental_copy',
shareable_constant_value: 'experimental_copy'
)
include_examples 'magic comment',
'# -*- frozen-string-literal: true -*-',
frozen_string_literal: true
include_examples 'magic comment',
'# frozen_string_literal: invalid',
frozen_string_literal: 'invalid'
include_examples 'magic comment',
'# -*- encoding : ascii-8bit -*-',
encoding: 'ascii-8bit',
frozen_string_literal: nil
include_examples 'magic comment',
'# encoding: ascii-8bit frozen_string_literal: true',
encoding: 'ascii-8bit',
frozen_string_literal: nil
include_examples 'magic comment',
'# frozen_string_literal: true encoding: ascii-8bit',
encoding: 'ascii-8bit',
frozen_string_literal: nil
include_examples 'magic comment',
' CSV.generate(encoding: Encoding::UTF_8) do |csv|',
encoding: nil,
frozen_string_literal: nil
include_examples(
'magic comment',
'# -*- encoding: ASCII-8BIT; frozen_string_literal: true -*-',
encoding: 'ascii-8bit',
frozen_string_literal: true
)
include_examples(
'magic comment',
'# coding: utf-8 -*- encoding: ASCII-8BIT; frozen_string_literal: true -*-',
encoding: 'ascii-8bit',
frozen_string_literal: true
)
include_examples(
'magic comment',
'# -*- coding: ASCII-8BIT; frozen_string_literal: true -*-',
encoding: 'ascii-8bit',
frozen_string_literal: true
)
include_examples 'magic comment',
'# vim: filetype=ruby, fileencoding=ascii-8bit',
encoding: 'ascii-8bit'
include_examples 'magic comment', '# vim: filetype=ruby,fileencoding=ascii-8bit', encoding: nil
include_examples 'magic comment',
'# vim: filetype=ruby, fileencoding=ascii-8bit',
encoding: 'ascii-8bit'
include_examples 'magic comment',
'#vim: filetype=ruby, fileencoding=ascii-8bit',
encoding: 'ascii-8bit'
include_examples(
'magic comment',
'# coding: utf-8 vim: filetype=ruby, fileencoding=ascii-8bit',
encoding: 'utf-8'
)
include_examples 'magic comment',
'# vim: filetype=python, fileencoding=ascii-8bit',
encoding: 'ascii-8bit'
include_examples 'magic comment', '# vim:fileencoding=utf-8', encoding: nil
describe '#valid?' do
subject { described_class.parse(comment).valid? }
context 'with an empty string' do
let(:comment) { '' }
it { is_expected.to eq(false) }
end
context 'with a non magic comment' do
let(:comment) { '# do something' }
it { is_expected.to eq(false) }
end
context 'with an encoding comment' do
let(:comment) { '# encoding: utf-8' }
it { is_expected.to eq(true) }
end
context 'with an frozen string literal comment' do
let(:comment) { '# frozen-string-literal: true' }
it { is_expected.to eq(true) }
end
context 'with an shareable constant value comment' do
let(:comment) { '# shareable-constant-value: literal' }
it { is_expected.to eq(true) }
end
end
describe '#valid_shareable_constant_value?' do
subject { described_class.parse(comment).valid_shareable_constant_value? }
context 'when given comment specified as `none`' do
let(:comment) { '# shareable_constant_value: none' }
it { is_expected.to eq(true) }
end
context 'when given comment specified as `literal`' do
let(:comment) { '# shareable_constant_value: literal' }
it { is_expected.to eq(true) }
end
context 'when given comment specified as `experimental_everything`' do
let(:comment) { '# shareable_constant_value: experimental_everything' }
it { is_expected.to eq(true) }
end
context 'when given comment specified as `experimental_copy`' do
let(:comment) { '# shareable_constant_value: experimental_copy' }
it { is_expected.to eq(true) }
end
context 'when given comment specified as unknown value' do
let(:comment) { '# shareable_constant_value: unknown' }
it { is_expected.to eq(false) }
end
context 'when given comment is not specified' do
let(:comment) { '' }
it { is_expected.to eq(false) }
end
end
describe '#without' do
subject { described_class.parse(comment).without(:encoding) }
context 'simple format' do
context 'when the entire comment is a single value' do
let(:comment) { '# encoding: utf-8' }
it { is_expected.to eq('') }
end
context 'when the comment contains a different magic value' do
let(:comment) { '# frozen-string-literal: true' }
it { is_expected.to eq(comment) }
end
end
context 'emacs format' do
context 'with one token' do
let(:comment) { '# -*- coding: ASCII-8BIT -*-' }
it { is_expected.to eq('') }
end
context 'with multiple tokens' do
let(:comment) { '# -*- coding: ASCII-8BIT; frozen_string_literal: true -*-' }
it { is_expected.to eq('# -*- frozen_string_literal: true -*-') }
end
end
context 'vim format' do
context 'when the comment has multiple tokens' do
let(:comment) { '# vim: filetype=ruby, fileencoding=ascii-8bit' }
it { is_expected.to eq('# vim: filetype=ruby') }
end
end
end
end