-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_parser.py
617 lines (502 loc) · 16.8 KB
/
test_parser.py
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
from __future__ import annotations
from pathlib import Path
import textwrap
import pytest
from codebased.parser import parse_objects
@pytest.mark.parametrize("file_type", ["ts", "js", "jsx", "tsx"])
def test_javascript_top_level_variable_declarations(file_type):
source = textwrap.dedent(
"""
let stringData = "Hello, world!";
export const numberData = 123;
const booleanData = true;
export const nullData = null;
export let undefinedData = undefined;
export var objectData = { id: 1, name: 'John', age: 30 };
var arrayData = [
{ id: 1, name: 'John', age: 30 },
{ id: 2, name: 'Jane', age: 25 },
{ id: 3, name: 'Bob', age: 35 },
];
export const hidePII = (datum) => {
return {id: datum.id};
};
function maskPII(datum) {
return {
id: datum.id,
name: datum.name.replace(/./g, '*'),
age: string(datum.age).replace(/./g, '*'),
};
}
export const sanitizedData = hidePII(objectData);
"""
).encode()
file_name = f'src/constants.{file_type}'
objects = parse_objects(
Path(file_name),
source
)
assert len(objects) == 11
file_o, string_o, number_o, boolean_o, null_o, undefined_o = objects[:6]
object_o, array_o, hide_pii_o, mask_pii_o, sanitized_o = objects[6:]
assert file_o.name == file_name
assert file_o.kind == 'file'
assert string_o.name == 'stringData'
assert string_o.kind == 'definition.constant'
assert number_o.name == 'numberData'
assert number_o.kind == 'definition.constant'
assert boolean_o.name == 'booleanData'
assert boolean_o.kind == 'definition.constant'
assert null_o.name == 'nullData'
assert null_o.kind == 'definition.constant'
assert undefined_o.name == 'undefinedData'
assert undefined_o.kind == 'definition.constant'
assert object_o.name == 'objectData'
assert object_o.kind == 'definition.constant'
assert array_o.name == 'arrayData'
assert array_o.kind == 'definition.constant'
assert hide_pii_o.name == 'hidePII'
assert hide_pii_o.kind == 'definition.function'
assert mask_pii_o.name == 'maskPII'
assert mask_pii_o.kind == 'definition.function'
assert sanitized_o.name == 'sanitizedData'
assert sanitized_o.kind == 'definition.constant'
def test_parse_cxx_header_file():
file_name = 'src/shapes.h'
source = textwrap.dedent(
"""
#ifndef SHAPES_H
#define SHAPES_H
#include <iostream>
struct Point {
double x;
double y;
};
class Shape {
public:
Shape();
virtual ~Shape();
virtual double area() = 0;
};
class Circle : public Shape {
public:
Circle(double radius);
double area() override;
private:
double radius_;
};
class Rectangle : public Shape {
public:
Rectangle(double width, double height);
double area() override;
private:
double width_;
double height_;
};
#endif
"""
).encode()
source_lines = source.splitlines()
objects = parse_objects(
Path(file_name),
source
)
assert len(objects) == 8
file, point, shape, shape_area, circle, circle_area, rectangle, rectangle_area = objects
assert file.name == file_name
assert file.kind == 'file'
assert file.language == 'cpp'
assert file.context_before == []
assert file.context_after == []
ifndef_start, ifndef_end = source_lines.index(b'#ifndef SHAPES_H'), source_lines.index(b'#endif')
assert point.name == 'Point'
assert point.kind == 'definition.struct'
assert point.context_before == [ifndef_start]
assert point.context_after == [ifndef_end]
assert shape.name == 'Shape'
assert shape.kind == 'definition.class'
assert shape.context_before == [ifndef_start]
assert shape.context_after == [ifndef_end]
shape_start = shape.coordinates[0][0]
shape_end = shape.coordinates[1][0]
assert shape_area.name == 'area'
assert shape_area.kind == 'definition.method'
assert shape_area.context_before == [ifndef_start, shape_start]
assert shape_area.context_after == [ifndef_end, shape_end]
assert circle.name == 'Circle'
assert circle.kind == 'definition.class'
assert circle.context_before == [ifndef_start]
assert circle.context_after == [ifndef_end]
circle_start = circle.coordinates[0][0]
circle_end = circle.coordinates[1][0]
assert circle_area.name == 'area'
assert circle_area.kind == 'definition.method'
assert circle_area.context_before == [ifndef_start, circle_start]
assert circle_area.context_after == [ifndef_end, circle_end]
assert rectangle.name == 'Rectangle'
assert rectangle.kind == 'definition.class'
assert rectangle.context_before == [ifndef_start]
assert rectangle.context_after == [ifndef_end]
rectangle_start = rectangle.coordinates[0][0]
rectangle_end = rectangle.coordinates[1][0]
assert rectangle_area.name == 'area'
assert rectangle_area.kind == 'definition.method'
assert rectangle_area.context_before == [ifndef_start, rectangle_start]
assert rectangle_area.context_after == [ifndef_end, rectangle_end]
def test_parse_c_header_file():
# TODO: Properly parse C function declarations.
# Note: Definitions are correctly parsed.
file_name = 'src/shapes.h'
source = textwrap.dedent(
"""
#ifndef SHAPES_H
#define SHAPES_H
#include <stdio.h>
typedef struct {
double x;
double y;
} Point;
typedef struct Shape Shape;
typedef double (*AreaFunc)(const Shape*);
struct Shape {
AreaFunc area;
};
typedef struct {
Shape base;
double radius;
} Circle;
typedef struct {
Shape base;
double width;
double height;
} Rectangle;
double circle_area(const Shape* shape);
double rectangle_area(const Shape* shape);
Circle* create_circle(double radius);
Rectangle* create_rectangle(double width, double height);
void destroy_shape(Shape* shape);
#endif
"""
).encode()
source_lines = source.splitlines()
objects = parse_objects(
Path(file_name),
source
)
assert len(objects) == 6
file, point, shape_fwd, shape, circle, rectangle = objects
assert file.name == file_name
assert file.kind == 'file'
# This is not ideal, but it's fine.
assert file.language == 'cpp'
assert file.context_before == []
assert file.context_after == []
ifndef_start, ifndef_end = source_lines.index(b'#ifndef SHAPES_H'), source_lines.index(b'#endif')
assert point.name == 'Point'
assert point.kind == 'definition.type'
assert point.context_before == [ifndef_start]
assert point.context_after == [ifndef_end]
assert shape_fwd.name == 'Shape'
assert shape_fwd.kind == 'definition.type'
assert shape_fwd.context_before == [ifndef_start]
assert shape_fwd.context_after == [ifndef_end]
assert shape.name == 'Shape'
assert shape.kind == 'definition.struct'
assert shape.context_before == [ifndef_start]
assert shape.context_after == [ifndef_end]
assert circle.name == 'Circle'
assert circle.kind == 'definition.type'
assert circle.context_before == [ifndef_start]
assert circle.context_after == [ifndef_end]
assert rectangle.name == 'Rectangle'
assert rectangle.kind == 'definition.type'
assert rectangle.context_before == [ifndef_start]
assert rectangle.context_after == [ifndef_end]
def test_parse_rust():
file_name = 'src/main.rs'
source = textwrap.dedent(
"""
#[derive(Debug)]
pub struct Point {
x: f64,
y: f64,
}
impl Point {
pub fn new(x: f64, y: f64) -> Self {
Self { x, y }
}
}
fn main() {
let p = Point::new(1.0, 2.0);
println!("Hello, world!");
}
"""
).encode()
source_lines = source.splitlines()
objects = parse_objects(
Path(file_name),
source
)
assert len(objects) == 5
file, point, impl, function, main = objects
assert file.name == file_name
assert file.kind == 'file'
assert file.language == 'rust'
assert file.context_before == []
assert file.context_after == []
assert point.name == 'Point'
assert point.kind == 'definition.struct'
assert impl.name == 'Point'
assert impl.kind == 'definition.struct.impl'
assert function.name == 'new'
assert function.kind == 'definition.function'
assert function.context_before == [impl.coordinates[0][0]]
assert function.context_after == [impl.coordinates[1][0]]
assert main.name == 'main'
assert main.kind == 'definition.function'
def test_parse_python():
file_name = 'src/main.py'
source = textwrap.dedent(
"""
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
ORIGIN = Point(0, 0)
def main():
p = Point(1, 2)
print("Hello, world!")
"""
).encode()
objects = parse_objects(
Path(file_name),
source
)
assert len(objects) == 5
file, class_, __init__, origin, main = objects
assert file.name == file_name
assert file.kind == 'file'
assert file.language == 'python'
assert file.context_before == []
assert file.context_after == []
assert class_.name == 'Point'
assert class_.kind == 'definition.class'
assert class_.context_before == []
assert class_.context_after == []
assert __init__.name == '__init__'
assert __init__.kind == 'definition.function'
assert __init__.context_before == [class_.coordinates[0][0]]
assert __init__.context_after == []
assert origin.name == 'ORIGIN'
assert origin.kind == 'definition.constant'
assert origin.context_before == []
assert origin.context_after == []
assert main.name == 'main'
assert main.kind == 'definition.function'
assert main.context_before == []
assert main.context_after == []
def test_parse_c_sharp():
# I know literally nothing about this language.
# If you're reading this because I made a mistake, please let me know.
file_name = 'src/Main.cs'
source = textwrap.dedent(
"""
public class Point {
public double X { get; set; }
public double Y { get; set; }
}
public static void Main() {
var p = new Point { X = 1, Y = 2 };
Console.WriteLine("Hello, world!");
}
"""
).encode()
source_lines = source.splitlines()
objects = parse_objects(
Path(file_name),
source
)
assert len(objects) == 2
file, point = objects
assert file.name == file_name
assert file.kind == 'file'
assert file.language == 'csharp'
assert file.context_before == []
assert file.context_after == []
assert point.name == 'Point'
assert point.kind == 'definition.class'
assert point.context_before == []
def test_go():
file_name = 'src/main.go'
source = textwrap.dedent(
"""
package main
import "fmt"
type Point struct {
X float64
Y float64
}
func (*Point) Area() float64 {
return 0
}
func main() {
p := Point{X: 1, Y: 2}
fmt.Println("Hello, world!")
}
"""
).encode()
objects = parse_objects(
Path(file_name),
source
)
file, point, area, main = objects
assert file.name == file_name
assert file.kind == 'file'
assert file.language == 'go'
assert file.context_before == []
assert file.context_after == []
assert point.name == 'Point'
assert point.kind == 'definition.type'
assert point.context_before == []
assert point.context_after == []
assert area.name == 'Area'
assert area.kind == 'definition.method'
assert main.name == 'main'
assert main.kind == 'definition.function'
assert main.context_before == []
assert main.context_after == []
def test_java():
file_name = 'src/Main.java'
source = textwrap.dedent(
"""
public class Point {
public double x;
public double y;
public double area() {
return 0;
}
}
public class Main {
public static void main(String[] args) {
Point p = new Point();
System.out.println("Hello, world!");
}
}
"""
).encode()
objects = parse_objects(
Path(file_name),
source
)
assert len(objects) == 5
file, point, area, main_class, main = objects
assert file.name == file_name
assert file.kind == 'file'
assert file.language == 'java'
assert file.context_before == []
assert file.context_after == []
assert point.name == 'Point'
assert point.kind == 'definition.class'
assert point.context_before == []
assert point.context_after == []
assert area.name == 'area'
assert area.kind == 'definition.method'
assert area.context_before == [point.coordinates[0][0]]
assert area.context_after == [point.coordinates[1][0]]
assert main_class.name == 'Main'
assert main_class.kind == 'definition.class'
assert main_class.context_before == []
assert main_class.context_after == []
assert main.name == 'main'
assert main.kind == 'definition.method'
assert main.context_before == [main_class.coordinates[0][0]]
assert main.context_after == [main_class.coordinates[1][0]]
def test_ruby():
file_name = 'src/main.rb'
source = textwrap.dedent(
"""
class Point
attr_accessor :x, :y
def area
0
end
end
def main
p = Point.new
puts "Hello, world!"
end
"""
).encode()
objects = parse_objects(
Path(file_name),
source
)
assert len(objects) == 4
file, point, area, main = objects
assert file.name == file_name
assert file.kind == 'file'
assert file.language == 'ruby'
assert file.context_before == []
assert file.context_after == []
assert point.name == 'Point'
assert point.kind == 'definition.class'
assert point.context_before == []
assert point.context_after == []
assert area.name == 'area'
assert area.kind == 'definition.method'
assert area.context_before == [point.coordinates[0][0]]
assert area.context_after == [point.coordinates[1][0]]
assert main.name == 'main'
# In Ruby, all "functions" are methods.
assert main.kind == 'definition.method'
assert main.context_before == []
assert main.context_after == []
def test_php():
file_name = 'src/main.php'
source = textwrap.dedent(
"""
<?php
class Point {
public double $x;
public double $y;
public function area(): float {
return 0;
}
}
function main() {
$p = new Point();
echo "Hello, world!";
}
"""
).encode()
objects = parse_objects(
Path(file_name),
source
)
assert len(objects) == 6
file, point, x, y, area, main = objects
assert file.name == file_name
assert file.kind == 'file'
assert file.language == 'php'
assert file.context_before == []
assert file.context_after == []
assert point.name == 'Point'
assert point.kind == 'definition.class'
assert point.context_before == []
assert point.context_after == []
assert x.name == 'x'
assert x.kind == 'definition.field'
assert x.context_before == [point.coordinates[0][0]]
assert x.context_after == [point.coordinates[1][0]]
assert y.name == 'y'
assert y.kind == 'definition.field'
assert y.context_before == [point.coordinates[0][0]]
assert y.context_after == [point.coordinates[1][0]]
assert area.name == 'area'
assert area.kind == 'definition.method'
assert area.context_before == [point.coordinates[0][0]]
assert area.context_after == [point.coordinates[1][0]]
assert main.name == 'main'
assert main.kind == 'definition.function'
assert main.context_before == []
assert main.context_after == []