forked from tlrobinson/narwhal-jsc
/
interpreter-tests.js
52 lines (36 loc) · 1.39 KB
/
interpreter-tests.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
var ASSERT = require("test/assert");
var Context = require("interpreter").Context;
exports.testContextGlobalAssignment = function() {
var c = new Context();
ASSERT.isTrue(typeof global.foo === "undefined");
ASSERT.isTrue(!c.global.foo);
var testObj = {};
c.global.foo = testObj;
ASSERT.isTrue(typeof global.foo === "undefined");
ASSERT.eq(testObj, c.global.foo);
ASSERT.eq(testObj, c.eval("foo"));
ASSERT.eq(testObj, (new c.Function("return foo;"))());
}
exports.testContextEval = function() {
var c = new Context();
ASSERT.isTrue(typeof global.foo === "undefined");
ASSERT.isTrue(!c.global.foo);
c.eval("foo = 1234;");
ASSERT.isTrue(typeof global.foo === "undefined");
ASSERT.eq(1234, c.global.foo);
ASSERT.eq(1234, c.eval("foo"));
ASSERT.eq(1234, (new c.Function("return foo;"))());
}
exports.testContextFunction = function() {
var c = new Context();
ASSERT.isTrue(typeof global.foo === "undefined");
ASSERT.isTrue(!c.global.foo);
var testObj = {};
(new c.Function("bar", "foo = bar;"))(testObj);
ASSERT.isTrue(typeof global.foo === "undefined");
ASSERT.eq(testObj, c.global.foo);
ASSERT.eq(testObj, c.eval("foo"));
ASSERT.eq(testObj, (new c.Function("return foo;"))());
}
if (require.main == module)
require("os").exit(require("test/runner").run(exports));