/
value-test.js
117 lines (106 loc) · 3.66 KB
/
value-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
import {Runtime} from "../../src/";
import tape from "../tape";
tape("module.value(name) returns a promise to the variable’s next value", async test => {
const runtime = new Runtime();
const module = runtime.module();
module.variable(true).define("foo", [], () => 42);
test.deepEqual(await module.value("foo"), 42);
});
tape("module.value(name) implicitly makes the variable reachable", async test => {
const runtime = new Runtime();
const module = runtime.module();
module.define("foo", [], () => 42);
test.deepEqual(await module.value("foo"), 42);
});
tape("module.value(name) supports errors", async test => {
const runtime = new Runtime();
const module = runtime.module();
module.define("foo", [], () => { throw new Error(42); });
try {
await module.value("foo");
test.fail();
} catch (error) {
test.deepEqual(error.message, "42");
}
});
tape("module.value(name) supports generators", async test => {
const runtime = new Runtime();
const module = runtime.module();
module.define("foo", [], function*() { yield 1; yield 2; yield 3; });
test.deepEqual(await module.value("foo"), 1);
test.deepEqual(await module.value("foo"), 2);
test.deepEqual(await module.value("foo"), 3);
test.deepEqual(await module.value("foo"), 3);
});
tape("module.value(name) supports generators that throw", async test => {
const runtime = new Runtime();
const module = runtime.module();
module.define("foo", [], function*() { yield 1; throw new Error("fooed"); });
module.define("bar", ["foo"], foo => foo);
const [foo1, bar1] = await Promise.all([module.value("foo"), module.value("bar")]);
test.deepEqual(foo1, 1);
test.deepEqual(bar1, 1);
try {
await module.value("foo");
test.fail();
} catch (error) {
test.deepEqual(error.message, "fooed");
}
try {
await module.value("bar");
test.fail();
} catch (error) {
test.deepEqual(error.message, "fooed");
}
});
tape("module.value(name) supports async generators", async test => {
const runtime = new Runtime();
const module = runtime.module();
module.define("foo", [], async function*() { yield 1; yield 2; yield 3; });
test.deepEqual(await module.value("foo"), 1);
test.deepEqual(await module.value("foo"), 2);
test.deepEqual(await module.value("foo"), 3);
test.deepEqual(await module.value("foo"), 3);
});
tape("module.value(name) supports promises", async test => {
const runtime = new Runtime();
const module = runtime.module();
module.define("foo", [], async () => { return await 42; });
test.deepEqual(await module.value("foo"), 42);
});
tape("module.value(name) supports constants", async test => {
const runtime = new Runtime();
const module = runtime.module();
module.define("foo", [], 42);
test.deepEqual(await module.value("foo"), 42);
});
tape("module.value(name) supports missing variables", async test => {
const runtime = new Runtime();
const module = runtime.module();
try {
await module.value("bar");
test.fail();
} catch (error) {
test.deepEqual(error.message, "bar is not defined");
}
});
tape("module.value(name) returns a promise on error", async test => {
const runtime = new Runtime();
const module = runtime.module();
const promise = module.value("bar");
try {
await promise;
test.fail();
} catch (error) {
test.deepEqual(error.message, "bar is not defined");
}
});
tape("module.value(name) does not force recomputation", async test => {
let foo = 0;
const runtime = new Runtime();
const module = runtime.module();
module.define("foo", [], () => ++foo);
test.deepEqual(await module.value("foo"), 1);
test.deepEqual(await module.value("foo"), 1);
test.deepEqual(await module.value("foo"), 1);
});