/
magic_comment_spec.rb
194 lines (147 loc) · 6 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
# 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]
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
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',
'# -*- 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_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
end