-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterprete.test.ts
138 lines (124 loc) · 3.49 KB
/
interprete.test.ts
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
import { interpreter } from "..";
describe("interpreter", () => {
it("should interpreter global object", () => {
const input = "console.log(1,2,3,4);console.log(5,6,7,8)";
const output = interpreter(input);
expect(output).toStrictEqual([
[1, 2, 3, 4],
[5, 6, 7, 8],
]);
});
it("should interperter global function", () => {
const input = "console.log(parseInt('1'))";
const output = interpreter(input);
expect(output).toStrictEqual([[1]]);
});
it("should interpreter function declarations", () => {
const input = `function Fibonacci(n) {
if(n === 1) {
return 1;
}
if(n === 2) {
return 2;
}
return Fibonacci(n-1)+Fibonacci(n-2)
}
console.log(Fibonacci(3))`;
const output = interpreter(input);
expect(output).toStrictEqual([[3]]);
});
it("should interpreter nested if statement", () => {
const input = `function Fibonacci(n) {
if(true){
if(n === 1) {
return 1;
}
}
if(n === 2) {
return 2;
}
return Fibonacci(n-1)+Fibonacci(n-2)
}
console.log(Fibonacci(5))`;
const output = interpreter(input);
expect(output).toStrictEqual([[8]]);
});
it("should interpreter identifiers", () => {
const input = `var a = 1; console.log(a)`;
const output = interpreter(input);
expect(output).toStrictEqual([[1]]);
});
it("should interpreter if else statement", () => {
const input = `var a = 1;
if(a === 2){
console.log(3);
}else {
console.log('right');
}`;
const output = interpreter(input);
expect(output).toStrictEqual([["right"]]);
});
it("should interpreter while statement", () => {
const input = `var a = 10;
while(a !== 1){
console.log(a);
a--;
}`;
const output = interpreter(input);
expect(output).toStrictEqual([
[10],
[9],
[8],
[7],
[6],
[5],
[4],
[3],
[2],
]);
});
it("should interpreter break statement", () => {
const input = `var a = 10;
while(true){
console.log(a);
a--;
if(a === 5){
break;
}
}`;
const output = interpreter(input);
expect(output).toStrictEqual([[10], [9], [8], [7], [6]]);
});
it("should interpreter for statement", () => {
const input = `for(var a = 1; a < 10; a++){
console.log(a);
}`;
const output = interpreter(input);
expect(output).toStrictEqual([[1], [2], [3], [4], [5], [6], [7], [8], [9]]);
});
it("should interpreter for break statement", () => {
const input = `for(var a = 1; a < 10; a++){
console.log(a);
if(a === 5){
break;
}
}`;
const output = interpreter(input);
expect(output).toStrictEqual([[1], [2], [3], [4], [5]]);
});
it("should interpreter math function", () => {
const input = "console.log(Math.max(1,2))";
const output = interpreter(input);
expect(output).toStrictEqual([[2]]);
});
it("should interpreter array", () => {
const input = `var arr = Array.from('1235'); console.log(arr);`;
const output = interpreter(input);
expect(output).toStrictEqual([[["1", "2", "3", "5"]]]);
});
it("should interpreter identify", () => {
const input = `var a = 1; a = 2;console.log(a);`;
const output = interpreter(input);
expect(output).toStrictEqual([[2]]);
});
});