-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmerge-key.test.ts
More file actions
60 lines (57 loc) · 1.71 KB
/
merge-key.test.ts
File metadata and controls
60 lines (57 loc) · 1.71 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
import { describe, expect, it } from "./suite.ts";
import * as ps from "../mod.ts";
import { lookup$ } from "../psmap.ts";
// https://yaml.org/type/merge.html
describe("merge keys", () => {
it("mix in all the properties of a map", async () => {
let interp = ps.createPlatformScript();
let program = ps.parse(`
<<:
one: 1
two: 2
`);
let map = await interp.eval(program) as ps.PSMap;
expect(lookup$("one", map)).toEqual(ps.number(1));
expect(lookup$("two", map)).toEqual(ps.number(2));
});
it("mix in all the maps in a seq", async () => {
let interp = ps.createPlatformScript();
let program = ps.parse(`
<<:
-
one: 1
two: 2
-
three: 3
four: 4
`);
let map = await interp.eval(program) as ps.PSMap;
expect(lookup$("one", map)).toEqual(ps.number(1));
expect(lookup$("two", map)).toEqual(ps.number(2));
expect(lookup$("three", map)).toEqual(ps.number(3));
expect(lookup$("four", map)).toEqual(ps.number(4));
});
it("throw an error if the mapping points to a non-collection", async () => {
// TODO: this is a type error when we implement type system.
let program = ps.parse(`<<: not a map`);
let interp = ps.createPlatformScript();
try {
await interp.eval(program);
throw new Error(
"expected mapping a non-collection to fail, but it did not",
);
} catch (error) {
expect(error.message).toMatch(/merge key/);
}
});
it("can invoke a function", async () => {
let program = ps.parse(`
$let:
id(x): $x
$do: {<<: { $id: { hello: world } } }
`);
let interp = ps.createPlatformScript();
let map = await interp.eval(program) as ps.PSMap;
expect(lookup$("hello", map)).toEqual(ps.string("world"));
});
});