File: token_spec.rb

package info (click to toggle)
ruby-rubocop-ast 0.3.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 892 kB
  • sloc: ruby: 10,886; makefile: 4
file content (361 lines) | stat: -rw-r--r-- 11,533 bytes parent folder | download
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
# 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
    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(: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 5
    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 51
    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 54
    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?.is_a?(MatchData)).to be true
      expect(right_ref_bracket_token.space_after?.is_a?(MatchData)).to be true

      expect(left_array_bracket_token.space_after?).to be_truthy
      expect(right_ref_bracket_token.space_after?).to be_truthy
    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?.is_a?(MatchData)).to be true
      expect(equals_token.space_before?.is_a?(MatchData)).to be true

      expect(left_array_bracket_token.space_before?).to be_truthy
      expect(equals_token.space_before?).to be_truthy
    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.comment?).to be true
      end

      it 'returns false for non comment tokens' do
        expect(zero_token.comment?).to be false
        expect(semicolon_token.comment?).to be false
      end
    end

    describe '#semicolon?' do
      it 'returns true for semicolon tokens' do
        expect(semicolon_token.semicolon?).to be true
      end

      it 'returns false for non semicolon tokens' do
        expect(comment_token.semicolon?).to be false
        expect(comma_token.semicolon?).to be false
      end
    end

    describe '#left_array_bracket?' do
      it 'returns true for left_array_bracket tokens' do
        expect(left_array_bracket_token.left_array_bracket?).to be true
      end

      it 'returns false for non left_array_bracket tokens' do
        expect(left_ref_bracket_token.left_array_bracket?).to be false
        expect(right_array_bracket_token.left_array_bracket?).to be false
      end
    end

    describe '#left_ref_bracket?' do
      it 'returns true for left_ref_bracket tokens' do
        expect(left_ref_bracket_token.left_ref_bracket?).to be true
      end

      it 'returns false for non left_ref_bracket tokens' do
        expect(left_array_bracket_token.left_ref_bracket?).to be false
        expect(right_ref_bracket_token.left_ref_bracket?).to be false
      end
    end

    describe '#left_bracket?' do
      it 'returns true for all left_bracket tokens' do
        expect(left_ref_bracket_token.left_bracket?).to be true
        expect(left_array_bracket_token.left_bracket?).to be true
      end

      it 'returns false for non left_bracket tokens' do
        expect(right_ref_bracket_token.left_bracket?).to be false
        expect(right_array_bracket_token.left_bracket?).to be false
      end
    end

    describe '#right_bracket?' do
      it 'returns true for all right_bracket tokens' do
        expect(right_ref_bracket_token.right_bracket?).to be true
        expect(right_array_bracket_token.right_bracket?).to be true
      end

      it 'returns false for non right_bracket tokens' do
        expect(left_ref_bracket_token.right_bracket?).to be false
        expect(left_array_bracket_token.right_bracket?).to be false
      end
    end

    describe '#left_brace?' do
      it 'returns true for right_bracket tokens' do
        expect(right_ref_bracket_token.right_bracket?).to be true
        expect(right_array_bracket_token.right_bracket?).to be true
      end

      it 'returns false for non right_bracket tokens' do
        expect(left_ref_bracket_token.right_bracket?).to be false
        expect(left_array_bracket_token.right_bracket?).to be false
      end
    end

    describe '#comma?' do
      it 'returns true for comma tokens' do
        expect(comma_token.comma?).to be true
      end

      it 'returns false for non comma tokens' do
        expect(semicolon_token.comma?).to be false
        expect(right_ref_bracket_token.comma?).to be false
      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.rescue_modifier?).to be true
      end

      it 'returns false for non rescue modifier tokens' do
        expect(first_token.rescue_modifier?).to be false
        expect(end_token.rescue_modifier?).to be false
      end
    end

    describe '#end?' do
      it 'returns true for end tokens' do
        expect(end_token.end?).to be true
      end

      it 'returns false for non end tokens' do
        expect(semicolon_token.end?).to be false
        expect(comment_token.end?).to be false
      end
    end

    describe '#equals_sign?' do
      it 'returns true for equals sign tokens' do
        expect(equals_token.equal_sign?).to be true
      end

      it 'returns false for non equals sign tokens' do
        expect(semicolon_token.equal_sign?).to be false
        expect(comma_token.equal_sign?).to be false
      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.left_brace?).to be true
        end

        it 'returns false for non left hash brace tokens' do
          expect(left_block_brace_token.left_brace?).to be false
          expect(right_hash_brace_token.left_brace?).to be false
        end
      end

      describe '#left_curly_brace?' do
        it 'returns true for left block brace tokens' do
          expect(left_block_brace_token.left_curly_brace?).to be true
        end

        it 'returns false for non left block brace tokens' do
          expect(left_hash_brace_token.left_curly_brace?).to be false
          expect(right_block_brace_token.left_curly_brace?).to be false
        end
      end

      describe '#right_curly_brace?' do
        it 'returns true for all right brace tokens' do
          expect(right_hash_brace_token.right_curly_brace?).to be true
          expect(right_block_brace_token.right_curly_brace?).to be true
        end

        it 'returns false for non right brace tokens' do
          expect(left_hash_brace_token.right_curly_brace?).to be false
          expect(left_parens_token.right_curly_brace?).to be false
        end
      end

      describe '#left_parens?' do
        it 'returns true for left parens tokens' do
          expect(left_parens_token.left_parens?).to be true
        end

        it 'returns false for non left parens tokens' do
          expect(left_hash_brace_token.left_parens?).to be false
          expect(right_parens_token.left_parens?).to be false
        end
      end

      describe '#right_parens?' do
        it 'returns true for right parens tokens' do
          expect(right_parens_token.right_parens?).to be true
        end

        it 'returns false for non right parens tokens' do
          expect(right_hash_brace_token.right_parens?).to be false
          expect(left_parens_token.right_parens?).to be false
        end
      end
    end
  end
end