-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
137 lines (102 loc) Β· 2.44 KB
/
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
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
var Jenny = require("./Jenny");
var assert = require("assert");
var jennyParser = require("./parser/jennyParser");
/**
* Testing here ----------------------------
*/
var jenny = new Jenny();
assert.strictEqual(jenny.eval(1), 1);
assert.strictEqual(jenny.eval(12), 12);
assert.strictEqual(jenny.eval(["+", ["+", ["+", 2, 2], 3], 2]), 9);
assert.strictEqual(jenny.eval(["+", 1, 2]), 3);
//Testing Minus operator
assert.strictEqual(jenny.eval(jennyParser.parse(`(- 5 5)`)), 0);
assert.strictEqual(jenny.eval('"HelloWorld"'), 'HelloWorld');
// Testing assignment
assert.strictEqual(jenny.eval(jennyParser.parse(`(var x 10)`)), 10);
// Testing lookup to variable name
assert.strictEqual(jenny.eval("x"), 10);
//Testing assignment of booleans
assert.strictEqual(jenny.eval(jennyParser.parse(`(var isUser true)`)), true);
// Testing nested evaluation
assert.strictEqual(jenny.eval(jennyParser.parse(`(var z (+ 25 25))`)), 50);
// Testing scopes and blocks.
assert.strictEqual(jenny.eval(jennyParser.parse(
`(begin
(var x 10)
(var y 10)
(* x y)
)`
)), 100);
// Testing nested blocks
assert.strictEqual(jenny.eval(jennyParser.parse(`
(begin
(var x 10)
(var y 10)
(begin
(var z (+ x y))
)
)
`)), 20);
// Testing setting value
assert.strictEqual(jenny.eval(
["begin",
["var", "x", 10],
["var", "y", 100],
["set", "x", 200],
["begin",
["var", "z", 300],
["set", "x", 600] // overriding parent scope property x
],
"x"
]
), 600);
assert.strictEqual(jenny.eval([
"begin",
["var", "x", 100],
["var", "z", 200],
["if", ["=", "x", 100],
["set", "x", 200],
["set", "x", 300],
],
"x"
]), 200);
/**
* Testing While loop
*/
assert.strictEqual(jenny.eval(jennyParser.parse(`
(begin
(var x 1)
(while (< x 100)
(set x (+ x 1))
)
)
`)), 100);
assert.strictEqual(jenny.eval(jennyParser.parse(`
(begin
(var age 20)
(set age 30)
age
)`)), 30);
assert.strictEqual(jenny.eval(jennyParser.parse(
`(
if (= 1 1)
1
2
)
`
)), 1)
jenny.eval(jennyParser.parse(`
(
print (+ 2 2)
)
`));
/**
* Testing User defined function
*/
//assert.strictEqual(, 4)
//assert.strictEqual(jenny.eval(100), 100)
/**
* IF this is printed then all tests passed
*/
console.log("ALL TESTS PASSED β
")