-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode_spec.rb
383 lines (323 loc) · 7.75 KB
/
node_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
378
379
380
381
382
383
# frozen_string_literal: true
require 'spec_helper'
require 'tempfile'
describe Imagen::Node::Base do
let(:ast) { Imagen::AST::Parser.parse_file('spec/fixtures/class.rb') }
it 'stores filename, first_line and last_line' do
node = described_class.new.build_from_ast(ast)
expect(node.first_line).to eq(3)
expect(node.last_line).to eq(18)
expect(node.file_path).to eq('spec/fixtures/class.rb')
end
it 'has access to source code' do
input = <<-STR
if 2 + 2 == 5
puts 'wow'
end
STR
node = described_class.new.build_from_ast(
Imagen::AST::Parser.parse(input)
)
# TODO: do we need the trailing newline?
expect("#{node.source}\n").to eq <<-STR
if 2 + 2 == 5
puts 'wow'
end
STR
expect(node.source_lines_with_numbers).to eq(
[
[1, ' if 2 + 2 == 5'],
[2, " puts 'wow'"],
[3, ' end']
]
)
end
end
describe Imagen::Node::Root do
let(:input) do
<<-STR
if 2 + 2 == 5
puts 'wow'
end
STR
end
describe '#human_name' do
let(:node) do
described_class.new.build_from_ast(Imagen::AST::Parser.parse(input))
end
it 'has human name' do
expect(node.human_name).to eq('root')
end
end
it 'builds from file' do
node = described_class.new.build_from_file('spec/fixtures/class.rb')
expect(node).to be_a(Imagen::Node::Root)
expect(node.children[0].source_lines.length).to eq(17)
end
it 'prints syntax errors' do
Tempfile.open do |f|
f.write("do puts 'incomplete' end")
f.flush
ret = nil
expect { ret = described_class.new.build_from_file(f.path) }
.to output(/unexpected token kDO/).to_stderr
expect(ret).to be_a(Imagen::Node::Root)
end
end
it 'does not fail on invalid utf8 sequences' do
Tempfile.open do |f|
f.write('"\x87"')
f.flush
node = Imagen::Node::CMethod.new.build_from_ast(
Imagen::AST::Parser.parse_file(f)
)
expect(node.ast_node.children.first).to eq("\x87")
end
end
it 'reads files as UTF-8' do
tf = Tempfile.new('ascii_file', encoding: Encoding::BINARY)
tf.write("\x27\xE2\x8C\x9A\xEF\xB8\x8F\x27") # (clock emoji in quotes)
tf.close
node = Imagen::Node::CMethod.new.build_from_ast(
Imagen::AST::Parser.parse_file(tf.path)
)
expect(node.ast_node.children.first).to eq('⌚️')
tf.unlink
end
end
describe Imagen::Node::Module do
let(:input) do
<<-STR
module Foo
if 2 + 2 == 5
puts 'wow'
end
end
STR
end
let(:node) do
described_class.new.build_from_ast(Imagen::AST::Parser.parse(input))
end
it 'has human name' do
expect(node.human_name).to eq('module')
end
describe '#empty_def?' do
let(:module_node) do
root = Imagen::Node::Root.new.build_from_ast(Imagen::AST::Parser.parse(input))
root.children.first
end
context 'with an empty method def' do
let(:input) do
<<-STR
module Foo
end
STR
end
it 'returns true for empty class methods' do
expect(module_node).to be_empty_def
end
end
context 'with a non-empty method def' do
let(:input) do
<<-STR
module Foo
attr_reader :bar
end
STR
end
it 'returns false for non-empty class methods' do
expect(module_node).not_to be_empty_def
end
end
end
end
describe Imagen::Node::Class do
let(:input) do
<<-STR
class Bar
if 2 + 2 == 5
puts 'wow'
end
end
STR
end
let(:node) do
described_class.new.build_from_ast(Imagen::AST::Parser.parse(input))
end
it 'has human name' do
expect(node.human_name).to eq('class')
end
describe '#empty_def?' do
let(:class_node) do
root = Imagen::Node::Root.new.build_from_ast(Imagen::AST::Parser.parse(input))
root.children.first
end
context 'with an empty method def' do
let(:input) do
<<-STR
class Foo
end
STR
end
it 'returns true for empty class methods' do
expect(class_node).to be_empty_def
end
end
context 'with a non-empty method def' do
let(:input) do
<<-STR
class Foo
attr_reader :bar
end
STR
end
it 'returns false for non-empty class methods' do
expect(class_node).not_to be_empty_def
end
end
end
end
describe Imagen::Node::CMethod do
let(:input) do
<<-STR
class Tom
def self.taylor
puts 'wow'
end
end
STR
end
let(:node) do
Imagen::Node::CMethod.new.build_from_ast(Imagen::AST::Parser.parse(input))
end
it 'has human name' do
expect(node.human_name).to eq('class method')
end
describe '#empty_def?' do
let(:cmethod_node) do
root = Imagen::Node::Root.new.build_from_ast(Imagen::AST::Parser.parse(input))
root.children.first.children.first
end
context 'with an empty method def' do
let(:input) do
<<-STR
class Tom
def self.taylor
end
end
STR
end
it 'returns true for empty class methods' do
expect(cmethod_node).to be_empty_def
end
end
context 'with a non-empty method def' do
let(:input) do
<<-STR
class Tom
def self.taylor
1 + 1
end
end
STR
end
it 'returns false for non-empty class methods' do
expect(cmethod_node).not_to be_empty_def
end
end
end
end
describe Imagen::Node::IMethod do
let(:input) do
<<-STR
class Hello
def darkness
puts 'my old friend'
end
end
STR
end
let(:node) do
described_class.new.build_from_ast(Imagen::AST::Parser.parse(input))
end
it 'has human name' do
expect(node.human_name).to eq('instance method')
end
describe '#empty_def?' do
let(:imethod_node) do
root = Imagen::Node::Root.new.build_from_ast(Imagen::AST::Parser.parse(input))
root.children.first.children.first
end
context 'with an empty method def' do
let(:input) do
<<-STR
class Tom
def taylor
end
end
STR
end
it 'returns true for empty class methods' do
expect(imethod_node).to be_empty_def
end
end
context 'with a non-empty method def' do
let(:input) do
<<-STR
class Tom
def taylor
1 + 1
end
end
STR
end
it 'returns false for non-empty class methods' do
expect(imethod_node).not_to be_empty_def
end
end
end
end
describe Imagen::Node::Block do
let(:input) do
<<-STR
(0..1).each do
puts 'my old friend, block'
end
STR
end
let(:node) do
described_class.new.build_from_ast(Imagen::AST::Parser.parse(input))
end
it 'has a human name' do
expect(node.human_name).to eq('block')
end
describe '#empty_def?' do
let(:block_node) do
root = Imagen::Node::Root.new.build_from_ast(Imagen::AST::Parser.parse(input))
root.children.first
end
context 'with an empty method def' do
let(:input) do
<<-STR
(0..1).each do
end
STR
end
it 'returns true for empty class methods' do
expect(block_node).to be_empty_def
end
end
context 'with a non-empty method def' do
let(:input) do
<<-STR
(0..1).each do
puts 'foo'
end
STR
end
it 'returns false for non-empty class methods' do
expect(block_node).not_to be_empty_def
end
end
end
end