-
Notifications
You must be signed in to change notification settings - Fork 81
/
parse.test.js
88 lines (71 loc) · 2.34 KB
/
parse.test.js
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
import { parse } from '../src/parse';
describe(parse, () => {
it.skip('should return a token with the type of NumericLiteral for number tokens', () => {
const tokens = [{ type: 'Number', value: 2 }];
const ast = { type: 'NumericLiteral', value: 2 };
expect(parse(tokens)).toEqual(ast);
});
// Exercise 3 Begin
it.skip('should return a token with the type of StringLiteral for string tokens', () => {
const tokens = [{ type: 'String', value: 'hello' }];
const ast = { type: 'StringLiteral', value: 'hello' };
expect(parse(tokens)).toEqual(ast);
});
it.skip('should return a token with the type of Identifier for name tokens', () => {
const tokens = [{ type: 'Name', value: 'x' }];
const ast = { type: 'Identifier', name: 'x' };
expect(parse(tokens)).toEqual(ast);
});
// Exercise 3 End
it.skip('should return an AST for a basic data structure', () => {
const tokens = [
{ type: 'Parenthesis', value: '(' },
{ type: 'Name', value: 'add' },
{ type: 'Number', value: 2 },
{ type: 'Number', value: 3 },
{ type: 'Parenthesis', value: ')' },
];
const ast = {
type: 'CallExpression',
name: 'add',
arguments: [
{ type: 'NumericLiteral', value: 2 },
{ type: 'NumericLiteral', value: 3 },
],
};
const result = parse(tokens);
expect(result).toEqual(ast);
});
it.skip('should return an AST for a nested data structure', () => {
const tokens = [
{ type: 'Parenthesis', value: '(' },
{ type: 'Name', value: 'add' },
{ type: 'Number', value: 2 },
{ type: 'Number', value: 3 },
{ type: 'Parenthesis', value: '(' },
{ type: 'Name', value: 'subtract' },
{ type: 'Number', value: 4 },
{ type: 'Number', value: 2 },
{ type: 'Parenthesis', value: ')' },
{ type: 'Parenthesis', value: ')' },
];
const ast = {
type: 'CallExpression',
name: 'add',
arguments: [
{ type: 'NumericLiteral', value: 2 },
{ type: 'NumericLiteral', value: 3 },
{
type: 'CallExpression',
name: 'subtract',
arguments: [
{ type: 'NumericLiteral', value: 4 },
{ type: 'NumericLiteral', value: 2 },
],
},
],
};
const result = parse(tokens);
expect(result).toEqual(ast);
});
});