|
| 1 | +import {Runtime} from "@observablehq/runtime"; |
| 2 | +import {valueof} from "./valueof.js"; |
| 3 | +import assert from "assert"; |
| 4 | + |
| 5 | +it("module.variable(…, {shadow}) can define a shadow input", async () => { |
| 6 | + const runtime = new Runtime(); |
| 7 | + const module = runtime.module(); |
| 8 | + |
| 9 | + module.define("val", [], 1000); |
| 10 | + const a = module.variable(true, {shadow: {val: 100}}).define("a", ["val"], (val) => val); |
| 11 | + |
| 12 | + assert.deepStrictEqual(await valueof(a), {value: 100}); |
| 13 | +}); |
| 14 | + |
| 15 | +it("module.variable(…, {shadow}) can define shadow inputs that differ between variables", async () => { |
| 16 | + const runtime = new Runtime(); |
| 17 | + const module = runtime.module(); |
| 18 | + |
| 19 | + module.define("val", [], 1000); |
| 20 | + const a = module.variable(true, {shadow: {val: 100}}).define("a", ["val"], (val) => val); |
| 21 | + const b = module.variable(true, {shadow: {val: 200}}).define("b", ["val"], (val) => val); |
| 22 | + assert.deepStrictEqual(await valueof(a), {value: 100}); |
| 23 | + assert.deepStrictEqual(await valueof(b), {value: 200}); |
| 24 | +}); |
| 25 | + |
| 26 | +it("module.variable(…, {shadow}) variables that are downstream will also use the shadow input", async () => { |
| 27 | + const runtime = new Runtime(); |
| 28 | + const module = runtime.module(); |
| 29 | + |
| 30 | + module.define("val", [], 1000); |
| 31 | + const a = module.variable(true, {shadow: {val: 100}}).define("a", ["val"], (val) => val); |
| 32 | + const b = module.variable(true, {shadow: {val: 200}}).define("b", ["val"], (val) => val); |
| 33 | + const c = module.variable(true).define("c", ["a", "b"], (a, b) => `${a}, ${b}`); |
| 34 | + |
| 35 | + assert.deepStrictEqual(await valueof(a), {value: 100}); |
| 36 | + assert.deepStrictEqual(await valueof(b), {value: 200}); |
| 37 | + assert.deepStrictEqual(await valueof(c), { value: "100, 200" }); |
| 38 | +}); |
| 39 | + |
| 40 | +it("module.variable(…, {shadow}) variables a->b->c that shadow the same inputs will each use their own shadows", async () => { |
| 41 | + const runtime = new Runtime(); |
| 42 | + const main = runtime.module(); |
| 43 | + |
| 44 | + const a = main |
| 45 | + .variable(true, {shadow: {val: 100}}) |
| 46 | + .define("a", ["val"], (val) => val); |
| 47 | + const b = main |
| 48 | + .variable(true, {shadow: {val: 200}}) |
| 49 | + .define("b", ["val", "a"], (val, a) => `${val}, ${a}`); |
| 50 | + const c = main |
| 51 | + .variable(true, {shadow: {val: 300}}) |
| 52 | + .define("c", ["val", "b", "a"], (val, b, a) => `${val}, (${b}), ${a}`); |
| 53 | + |
| 54 | + assert.deepStrictEqual(await valueof(a), {value: 100}); |
| 55 | + assert.deepStrictEqual(await valueof(b), {value: "200, 100"}); |
| 56 | + assert.deepStrictEqual(await valueof(c), {value: "300, (200, 100), 100"}); |
| 57 | +}); |
| 58 | + |
| 59 | +it("module.variable(…, {shadow}) can shadow a non-existent variable", async () => { |
| 60 | + const runtime = new Runtime(); |
| 61 | + const main = runtime.module(); |
| 62 | + |
| 63 | + const a = main |
| 64 | + .variable(true, {shadow: {val: 100}}) |
| 65 | + .define("a", ["val"], (val) => val); |
| 66 | + |
| 67 | + assert.deepStrictEqual(await valueof(a), {value: 100}); |
| 68 | +}); |
0 commit comments