-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy pathtest.js
311 lines (288 loc) · 8.71 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
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
var Interpreter = require('../interpreter.js');
var expect = require('chai').expect;
var setup_code = "function Writer() {\
this.output = [];\
this.log = function(text) {\
this.output.push(text);\
};\
};\
var c = new Writer();";
function getOutput(code) {
var test = new Interpreter(setup_code + code);
var state = null;
var count = 0;
while(test.step()) {
//console.log(count , test.stateStack[0]);
if(test.stateStack.length > 0) {
state = test.stateStack[0];
}
count ++;
}
var results = test.extract(state.scope.properties.c.properties.output)
return results;
}
describe("try/catch/finally", () => {
it("Should print the results of the try block before the finally block", () => {
var test_code = '\
try {\
c.log("tried");\
} finally {\
c.log("finally");\
}';
var results = getOutput(test_code);
expect(results).to.deep.equal(["tried","finally"]);
});
it("Should have an uncaught exception", () => {
var test_code = '\
try {\
c.log("tried");\
throw "oops";\
} finally {\
c.log("finally");\
}';
expect( () => {
var results = getOutput(test_code);
}).to.throw(Error);
});
it("Should print the results of the try block before the finally block, and not the catch branch", () => {
var test_code = '\
try {\
c.log("tried");\
} catch (e) {\
c.log("caught");\
} finally {\
c.log("finally");\
}';
var results = getOutput(test_code);
expect(results).to.deep.equal(["tried","finally"]);
});
it("Should print the results of the caught block before the finally block, and not the rest of the try branch", () => {
var test_code = '\
try {\
c.log("tried");\
throw false;\
c.log("tried again");\
} catch (e) {\
c.log("caught");\
} finally {\
c.log("finally");\
}';
var results = getOutput(test_code);
expect(results).to.deep.equal(["tried","caught","finally"]);
});
it("Should handle the exception in the outer try catch exception, and finish inner finally", () => {
var test_code = '\
try {\
try {\
throw "oops";\
}\
finally {\
c.log("finally");\
}\
}\
catch (ex) {\
c.log("outer");\
c.log(ex);\
}';
var results = getOutput(test_code);
expect(results).to.deep.equal(["finally","outer","oops"]);
});
it("Should handle inner try catch, and not call outer catch", () => {
var test_code = '\
try {\
try {\
throw "oops";\
}\
catch (ex) {\
c.log("inner");\
c.log(ex);\
}\
finally {\
c.log("finally");\
}\
}\
catch (ex) {\
c.log("outer");\
c.log(ex);\
}';
var results = getOutput(test_code);
expect(results).to.deep.equal(["inner","oops","finally"]);
});
it("Should handle rethrowing the error, and catching in the outer", () => {
var test_code = '\
try {\
try {\
throw "oops";\
}\
catch (ex) {\
c.log("inner");\
c.log(ex);\
throw ex;\
}\
finally {\
c.log("finally");\
}\
}\
catch (ex) {\
c.log("outer");\
c.log(ex);\
}';
var results = getOutput(test_code);
expect(results).to.deep.equal(["inner","oops","finally","outer","oops"]);
});
it("Should not catch if returning early in a finally block", () => {
var test_code = '\
function test() {\
try {\
try {\
throw "oops";\
}\
catch (ex) {\
c.log("inner");\
c.log(ex);\
throw ex;\
}\
finally {\
c.log("finally");\
return;\
}\
}\
catch (ex) {\
c.log("outer");\
}\
}\
test();';
var test = new Interpreter(setup_code + test_code);
var state = null;
var count = 0;
expect(() => {
while(test.step()) {
//console.log(count , test.stateStack[0]);
if(test.stateStack.length > 0) {
state = test.stateStack[0];
}
count++;
}
}).to.throw(Error);
var results = test.extract(test.getScope().properties.c.properties.output);
expect(results).to.deep.equal(['inner','oops','finally']);
});
it("It should not leave undone finally statements for uncaught exceptions", () => {
var test_code = '\
function test() {\
try {\
c.log("try");\
throw "oops";\
c.log("shouldn\'t be here");\
} finally {\
c.log("finally");\
}\
c.log("got here?");\
return 4;\
}\
var hmm = test();';
var test = new Interpreter(setup_code + test_code);
var state = null;
var count = 0;
expect(() => {
while(test.step()) {
//console.log(count , test.stateStack[0]);
if(test.stateStack.length > 0) {
state = test.stateStack[0];
}
count++;
}
}).to.throw(Error);
var results = test.extract(test.stateStack[test.stateStack.length-1].scope.properties.c.properties.output);
expect(results).to.deep.equal(['try','finally']);
})
})
describe('return', () => {
it("Should return a simple value", () => {
var test_code = '\
function id(x) {\
return x;\
}\
c.log(id(10));';
var results = getOutput(test_code);
expect(results).to.deep.equal([10]);
});
it("Should return early", () => {
var test_code = '\
function test(bool) {\
if(bool) {\
c.log(bool);\
return bool;\
c.log("nope");\
}else{\
c.log("negative");\
return bool;\
}\
}\
c.log(test(true));';
var results = getOutput(test_code);
expect(results).to.deep.equal([true, true]);
});
it("Should respect finally", () => {
var test_code = '\
function example() {\
try {\
return true;\
}\
finally {\
return false;\
}\
}\
c.log(example());';
var results = getOutput(test_code);
expect(results).to.deep.equal([false]);
});
it("Should respect catch and finally", () => {
var test_code = '\
function example() {\
try {\
throw "oops";\
} catch (e) {\
return e;\
} finally {\
return false;\
}\
}\
c.log(example());';
var results = getOutput(test_code);
expect(results).to.deep.equal([false]);
});
it("Should handle continue statements", () => {
var test_code = '\
for(var i = 0; i < 2; i++) {\
try {\
c.log(i);\
continue;\
} finally {\
c.log("end");\
}\
}';
var results = getOutput(test_code);
expect(results).to.deep.equal([0,'end', 1, 'end'])
})
xit("should handle the scopes created by catch", () => {
var test_code = 'function capturedFoo() {return foo};\
foo = "prior to throw";\
try {\
throw "Error";\
}\
catch (foo) {\
var foo = "initializer in catch";\
}';
var test = new Interpreter(setup_code + test_code);
var state = null;
while(test.step()) {
//console.log(count , test.stateStack[0]);
if(test.stateStack.length > 0) {
state = test.stateStack[0];
}
}
var results = test.extractScopeValues(state.scope);
expect(results.properties.foo).to.equal('prior to throw');
})
});