-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
token_spec.rb
377 lines (314 loc) · 11.5 KB
/
token_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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# frozen_string_literal: true
RSpec.describe RuboCop::AST::Token do
let(:processed_source) { parse_source(source) }
let(:source) { <<~RUBY }
# comment
def some_method
[ 1, 2 ];
foo[0] = 3
1..42
1...42
end
RUBY
let(:first_token) { processed_source.tokens.first }
let(:comment_token) do
processed_source.find_token do |t|
t.text.start_with?('#') && t.line == 1
end
end
let(:left_array_bracket_token) do
processed_source.find_token { |t| t.text == '[' && t.line == 3 }
end
let(:comma_token) { processed_source.find_token { |t| t.text == ',' } }
let(:irange_token) { processed_source.find_token { |t| t.text == '..' } }
let(:erange_token) { processed_source.find_token { |t| t.text == '...' } }
let(:right_array_bracket_token) do
processed_source.find_token { |t| t.text == ']' && t.line == 3 }
end
let(:semicolon_token) { processed_source.find_token { |t| t.text == ';' } }
let(:left_ref_bracket_token) do
processed_source.find_token { |t| t.text == '[' && t.line == 4 }
end
let(:zero_token) { processed_source.find_token { |t| t.text == '0' } }
let(:right_ref_bracket_token) do
processed_source.find_token { |t| t.text == ']' && t.line == 4 }
end
let(:equals_token) { processed_source.find_token { |t| t.text == '=' } }
let(:end_token) { processed_source.find_token { |t| t.text == 'end' } }
describe '.from_parser_token' do
subject(:token) { described_class.from_parser_token(parser_token) }
let(:parser_token) { [type, [text, range]] }
let(:type) { :kDEF }
let(:text) { 'def' }
let(:range) do
instance_double(Parser::Source::Range, line: 42, column: 30)
end
it "sets parser token's type to rubocop token's type" do
expect(token.type).to eq(type)
end
it "sets parser token's text to rubocop token's text" do
expect(token.text).to eq(text)
end
it "sets parser token's range to rubocop token's pos" do
expect(token.pos).to eq(range)
end
it 'returns a #to_s useful for debugging' do
expect(token.to_s).to eq('[[42, 30], kDEF, "def"]')
end
end
describe '#line' do
it 'returns line of token' do
expect(first_token.line).to eq 1
expect(zero_token.line).to eq 4
expect(end_token.line).to eq 7
end
end
describe '#column' do
it 'returns index of first char in token range on that line' do
expect(first_token.column).to eq 0
expect(zero_token.column).to eq 6
expect(end_token.column).to eq 0
end
end
describe '#begin_pos' do
it 'returns index of first char in token range of entire source' do
expect(first_token.begin_pos).to eq 0
expect(zero_token.begin_pos).to eq 44
expect(end_token.begin_pos).to eq 68
end
end
describe '#end_pos' do
it 'returns index of last char in token range of entire source' do
expect(first_token.end_pos).to eq 9
expect(zero_token.end_pos).to eq 45
expect(end_token.end_pos).to eq 71
end
end
describe '#space_after' do
it 'returns truthy MatchData when there is a space after token' do
expect(left_array_bracket_token.space_after?).to be_a(MatchData)
expect(right_ref_bracket_token.space_after?).to be_a(MatchData)
expect(left_array_bracket_token).to be_space_after
expect(right_ref_bracket_token).to be_space_after
end
it 'returns nil when there is not a space after token' do
expect(left_ref_bracket_token.space_after?).to be_nil
expect(zero_token.space_after?).to be_nil
end
end
describe '#to_s' do
it 'returns string of token data' do
expect(end_token.to_s).to include end_token.line.to_s
expect(end_token.to_s).to include end_token.column.to_s
expect(end_token.to_s).to include end_token.type.to_s
expect(end_token.to_s).to include end_token.text.to_s
end
end
describe '#space_before' do
it 'returns truthy MatchData when there is a space before token' do
expect(left_array_bracket_token.space_before?).to be_a(MatchData)
expect(equals_token.space_before?).to be_a(MatchData)
expect(left_array_bracket_token).to be_space_before
expect(equals_token).to be_space_before
end
it 'returns nil when there is not a space before token' do
expect(semicolon_token.space_before?).to be_nil
expect(zero_token.space_before?).to be_nil
end
it 'returns nil when it is on the first line' do
expect(processed_source.tokens[0].space_before?).to be_nil
end
end
context 'type predicates' do
describe '#comment?' do
it 'returns true for comment tokens' do
expect(comment_token).to be_comment
end
it 'returns false for non comment tokens' do
expect(zero_token).not_to be_comment
expect(semicolon_token).not_to be_comment
end
end
describe '#semicolon?' do
it 'returns true for semicolon tokens' do
expect(semicolon_token).to be_semicolon
end
it 'returns false for non semicolon tokens' do
expect(comment_token).not_to be_semicolon
expect(comma_token).not_to be_semicolon
end
end
describe '#left_array_bracket?' do
it 'returns true for left_array_bracket tokens' do
expect(left_array_bracket_token).to be_left_array_bracket
end
it 'returns false for non left_array_bracket tokens' do
expect(left_ref_bracket_token).not_to be_left_array_bracket
expect(right_array_bracket_token).not_to be_left_array_bracket
end
end
describe '#left_ref_bracket?' do
it 'returns true for left_ref_bracket tokens' do
expect(left_ref_bracket_token).to be_left_ref_bracket
end
it 'returns false for non left_ref_bracket tokens' do
expect(left_array_bracket_token).not_to be_left_ref_bracket
expect(right_ref_bracket_token).not_to be_left_ref_bracket
end
end
describe '#left_bracket?' do
it 'returns true for all left_bracket tokens' do
expect(left_ref_bracket_token).to be_left_bracket
expect(left_array_bracket_token).to be_left_bracket
end
it 'returns false for non left_bracket tokens' do
expect(right_ref_bracket_token).not_to be_left_bracket
expect(right_array_bracket_token).not_to be_left_bracket
end
end
describe '#right_bracket?' do
it 'returns true for all right_bracket tokens' do
expect(right_ref_bracket_token).to be_right_bracket
expect(right_array_bracket_token).to be_right_bracket
end
it 'returns false for non right_bracket tokens' do
expect(left_ref_bracket_token).not_to be_right_bracket
expect(left_array_bracket_token).not_to be_right_bracket
end
end
describe '#left_brace?' do
it 'returns true for right_bracket tokens' do
expect(right_ref_bracket_token).to be_right_bracket
expect(right_array_bracket_token).to be_right_bracket
end
it 'returns false for non right_bracket tokens' do
expect(left_ref_bracket_token).not_to be_right_bracket
expect(left_array_bracket_token).not_to be_right_bracket
end
end
describe '#comma?' do
it 'returns true for comma tokens' do
expect(comma_token).to be_comma
end
it 'returns false for non comma tokens' do
expect(semicolon_token).not_to be_comma
expect(right_ref_bracket_token).not_to be_comma
end
end
describe '#regexp_dots?' do
it 'returns true for regexp tokens' do
expect(irange_token).to be_regexp_dots
expect(erange_token).to be_regexp_dots
end
it 'returns false for non comma tokens' do
expect(semicolon_token).not_to be_regexp_dots
expect(right_ref_bracket_token).not_to be_regexp_dots
end
end
describe '#rescue_modifier?' do
let(:source) { <<~RUBY }
def foo
bar rescue qux
end
RUBY
let(:rescue_modifier_token) do
processed_source.find_token { |t| t.text == 'rescue' }
end
it 'returns true for rescue modifier tokens' do
expect(rescue_modifier_token).to be_rescue_modifier
end
it 'returns false for non rescue modifier tokens' do
expect(first_token).not_to be_rescue_modifier
expect(end_token).not_to be_rescue_modifier
end
end
describe '#end?' do
it 'returns true for end tokens' do
expect(end_token).to be_end
end
it 'returns false for non end tokens' do
expect(semicolon_token).not_to be_end
expect(comment_token).not_to be_end
end
end
describe '#equals_sign?' do
it 'returns true for equals sign tokens' do
expect(equals_token).to be_equal_sign
end
it 'returns false for non equals sign tokens' do
expect(semicolon_token).not_to be_equal_sign
expect(comma_token).not_to be_equal_sign
end
end
context 'with braces & parens' do
let(:source) { <<~RUBY }
{ a: 1 }
foo { |f| bar(f) }
RUBY
let(:left_hash_brace_token) do
processed_source.find_token { |t| t.text == '{' && t.line == 1 }
end
let(:right_hash_brace_token) do
processed_source.find_token { |t| t.text == '}' && t.line == 1 }
end
let(:left_block_brace_token) do
processed_source.find_token { |t| t.text == '{' && t.line == 2 }
end
let(:left_parens_token) do
processed_source.find_token { |t| t.text == '(' }
end
let(:right_parens_token) do
processed_source.find_token { |t| t.text == ')' }
end
let(:right_block_brace_token) do
processed_source.find_token { |t| t.text == '}' && t.line == 2 }
end
describe '#left_brace?' do
it 'returns true for left hash brace tokens' do
expect(left_hash_brace_token).to be_left_brace
end
it 'returns false for non left hash brace tokens' do
expect(left_block_brace_token).not_to be_left_brace
expect(right_hash_brace_token).not_to be_left_brace
end
end
describe '#left_curly_brace?' do
it 'returns true for left block brace tokens' do
expect(left_block_brace_token).to be_left_curly_brace
end
it 'returns false for non left block brace tokens' do
expect(left_hash_brace_token).not_to be_left_curly_brace
expect(right_block_brace_token).not_to be_left_curly_brace
end
end
describe '#right_curly_brace?' do
it 'returns true for all right brace tokens' do
expect(right_hash_brace_token).to be_right_curly_brace
expect(right_block_brace_token).to be_right_curly_brace
end
it 'returns false for non right brace tokens' do
expect(left_hash_brace_token).not_to be_right_curly_brace
expect(left_parens_token).not_to be_right_curly_brace
end
end
describe '#left_parens?' do
it 'returns true for left parens tokens' do
expect(left_parens_token).to be_left_parens
end
it 'returns false for non left parens tokens' do
expect(left_hash_brace_token).not_to be_left_parens
expect(right_parens_token).not_to be_left_parens
end
end
describe '#right_parens?' do
it 'returns true for right parens tokens' do
expect(right_parens_token).to be_right_parens
end
it 'returns false for non right parens tokens' do
expect(right_hash_brace_token).not_to be_right_parens
expect(left_parens_token).not_to be_right_parens
end
end
end
end
end