|
| 1 | +import { describe, expect, it } from "./suite.ts"; |
| 2 | + |
| 3 | +import * as ps from "../mod.ts"; |
| 4 | +import { lookup$ } from "../psmap.ts"; |
| 5 | + |
| 6 | +// https://yaml.org/type/merge.html |
| 7 | + |
| 8 | +describe("merge keys", () => { |
| 9 | + it("mix in all the properties of a map", async () => { |
| 10 | + let interp = ps.createPlatformScript(); |
| 11 | + let program = ps.parse(` |
| 12 | +<<: |
| 13 | + one: 1 |
| 14 | + two: 2 |
| 15 | +`); |
| 16 | + let map = await interp.eval(program) as ps.PSMap; |
| 17 | + expect(lookup$("one", map)).toEqual(ps.number(1)); |
| 18 | + expect(lookup$("two", map)).toEqual(ps.number(2)); |
| 19 | + }); |
| 20 | + it("mix in all the maps in a seq", async () => { |
| 21 | + let interp = ps.createPlatformScript(); |
| 22 | + let program = ps.parse(` |
| 23 | +<<: |
| 24 | + - |
| 25 | + one: 1 |
| 26 | + two: 2 |
| 27 | + - |
| 28 | + three: 3 |
| 29 | + four: 4 |
| 30 | +`); |
| 31 | + let map = await interp.eval(program) as ps.PSMap; |
| 32 | + expect(lookup$("one", map)).toEqual(ps.number(1)); |
| 33 | + expect(lookup$("two", map)).toEqual(ps.number(2)); |
| 34 | + expect(lookup$("three", map)).toEqual(ps.number(3)); |
| 35 | + expect(lookup$("four", map)).toEqual(ps.number(4)); |
| 36 | + }); |
| 37 | + it("throw an error if the mapping points to a non-collection", async () => { |
| 38 | + // TODO: this is a type error when we implement type system. |
| 39 | + let program = ps.parse(`<<: not a map`); |
| 40 | + let interp = ps.createPlatformScript(); |
| 41 | + try { |
| 42 | + await interp.eval(program); |
| 43 | + throw new Error( |
| 44 | + "expected mapping a non-collection to fail, but it did not", |
| 45 | + ); |
| 46 | + } catch (error) { |
| 47 | + expect(error.message).toMatch(/merge key/); |
| 48 | + } |
| 49 | + }); |
| 50 | + it("can invoke a function", async () => { |
| 51 | + let program = ps.parse(` |
| 52 | +$let: |
| 53 | + id(x): $x |
| 54 | +$do: {<<: { $id: { hello: world } } } |
| 55 | +`); |
| 56 | + let interp = ps.createPlatformScript(); |
| 57 | + let map = await interp.eval(program) as ps.PSMap; |
| 58 | + expect(lookup$("hello", map)).toEqual(ps.string("world")); |
| 59 | + }); |
| 60 | + |
| 61 | +}); |
0 commit comments