-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodule.test.ts
More file actions
160 lines (142 loc) · 4.84 KB
/
module.test.ts
File metadata and controls
160 lines (142 loc) · 4.84 KB
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
import type { PSMap, PSModule } from "../types.ts";
import type { Task } from "../deps.ts";
import { describe, expect, it, useStaticFileServer } from "./suite.ts";
import { load, moduleEval, number, parse, ps2js, string } from "../mod.ts";
import { run } from "../deps.ts";
import { lookup, lookup$ } from "../psmap.ts";
describe("a PlatformScript module", () => {
it("can be loaded from an absolute url", async () => {
let mod = await loadmod("1.yaml");
expect(mod.value).toEqual(number(1));
});
it("has a list of exported symbols", async () => {
let mod = await loadmod("nodeps.yaml");
let symbols = mod.value as PSMap;
expect(lookup$("five", symbols).value).toEqual(5);
expect(lookup$("str", symbols).value).toEqual("world");
expect(lookup$("id", symbols).type).toEqual("fn");
});
it("can load other modules from a relative url", async () => {
let mod = await loadmod("imports-relative.yaml");
expect(lookup$("myfive", mod.value).value).toEqual(5);
expect(lookup$("hello", mod.value).value).toEqual("hello world");
});
it("does not import symbols that are not expliticly mapped", async () => {
let mod = await loadmod("imports-relative.yaml");
expect(lookup("str", mod.value).type).toEqual("nothing");
});
it("throws an error if the symbol does not exist in the source module", async () => {
try {
await loadmod("no-such-symbol.yaml");
throw new Error("importing a symbol that does not exist should fail");
} catch (error) {
expect(error.message).toMatch(/does not have a member/);
}
});
it("can evalute single value expression at the module level", async () => {
let mod = await loadmod("do.yaml");
expect(ps2js(mod.value)).toEqual("hello from module");
});
it("will be treated as the body of a 'do' expression if it is a bare list", async () => {
let mod = await loadmod("list.yaml");
expect(ps2js(mod.value)).toEqual([3, 2, 1, "end-of-list"]);
});
it("can import from multiple different modules", async () => {
let mod = await loadmod("multi-dep.yaml");
expect(ps2js(mod.value)).toEqual([5, "hello world"]);
});
it("supports loading both from the network and from local file system", async () => {
await run(function* () {
let { hostname, port } = yield* useStaticFileServer("test/modules");
let mod = yield* load({
location: `http://${hostname}:${port}/multi-dep.yaml`,
});
expect(ps2js(mod.value)).toEqual([5, "hello world"]);
});
});
it("supports splat to alias an entire module", async () => {
await run(function* () {
let source = parse(`
$import:
nodeps<<, id: nodeps.yaml
$id: $nodeps
`);
let mod = yield* moduleEval({
source,
location: new URL(`modules/virtual-module.yaml`, import.meta.url),
});
expect(lookup$("five", mod.value)).toEqual(number(5));
expect(lookup$("str", mod.value)).toEqual(string("world"));
expect(lookup$("id", mod.value).type).toEqual("fn");
});
});
it.ignore("can re-alias names to local names", () => {});
it.ignore("is an error to alias a property in a non-map module", () => {});
// it("can handle circular module references");
// it("can be specified using WASM");
it("supports evaluating a module directly without loading it from a url", async () => {
await run(function* () {
let source = parse(`
$import:
five: nodeps.yaml
myfive: $five
main: $myfive
`);
let mod = yield* moduleEval({
source,
location: new URL(`modules/virtual-module.yaml`, import.meta.url),
});
expect(lookup$("main", mod.value)).toEqual(number(5));
});
});
it("can import a module manually via location", async () => {
let mod = await runmod(`
$import:
five:
location: nodeps.yaml
main: $five
`);
expect(lookup$("main", mod.value)).toEqual(number(5));
});
it("can import an module as quoted platformscript", async () => {
let mod = await runmod(`
$import:
nodeps<<:
location: nodeps.yaml
quote: true
self(x): $x
id: { $self: $nodeps.id }
`);
let id = lookup$("id", mod.value);
expect(id.type).toEqual("quote");
expect(lookup$("(x)=>", id.value)).toEqual(string("$x"));
});
it("can import a single value as quoted platformscript", async () => {
let mod = await runmod(`
$import:
id:
location: nodeps.yaml
quote: true
id: $id
`);
let id = lookup$("id", mod.value);
expect(id.type).toEqual("quote");
expect(lookup$("(x)=>", id.value)).toEqual(string("$x"));
});
});
function runmod(modstring: string): Task<PSModule> {
let source = parse(modstring);
return run(() =>
moduleEval({
source,
location: new URL(`modules/virtual-module.yaml`, import.meta.url),
})
);
}
function loadmod(url: string): Task<PSModule> {
return run(() =>
load({
location: new URL(`modules/${url}`, import.meta.url),
})
);
}