From 58a357cce5d5a9c9d5430e5975a3c4e634e0ef3c Mon Sep 17 00:00:00 2001 From: Sam Nolan Date: Fri, 29 Apr 2022 14:10:41 +0000 Subject: [PATCH] Add basic partial test --- .../squiggle-lang/__tests__/TS/JS_test.ts | 13 ++++++++- .../squiggle-lang/__tests__/TS/TestHelpers.ts | 28 +++++++++++++++---- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/packages/squiggle-lang/__tests__/TS/JS_test.ts b/packages/squiggle-lang/__tests__/TS/JS_test.ts index 33aae7db..e522eb95 100644 --- a/packages/squiggle-lang/__tests__/TS/JS_test.ts +++ b/packages/squiggle-lang/__tests__/TS/JS_test.ts @@ -1,5 +1,5 @@ import { Distribution, resultMap } from "../../src/js/index"; -import { testRun } from "./TestHelpers"; +import { testRun, testRunPartial } from "./TestHelpers"; function Ok(x: b) { return { tag: "Ok", value: x }; @@ -57,6 +57,17 @@ describe("Record", () => { }); }); +describe("Partials", () => { + test("Can pass variables between partials and cells", () => { + let bindings = testRunPartial(`x = 5`); + let bindings2 = testRunPartial(`y = x + 2`, bindings); + expect(testRun(`y + 3`, bindings2)).toEqual({ + tag: "number", + value: 10, + }); + }); +}); + describe("Distribution", () => { //It's important that sampleCount is less than 9. If it's more, than that will create randomness //Also, note, the value should be created using makeSampleSetDist() later on. diff --git a/packages/squiggle-lang/__tests__/TS/TestHelpers.ts b/packages/squiggle-lang/__tests__/TS/TestHelpers.ts index 8ec0c3e1..7d51c98e 100644 --- a/packages/squiggle-lang/__tests__/TS/TestHelpers.ts +++ b/packages/squiggle-lang/__tests__/TS/TestHelpers.ts @@ -1,14 +1,16 @@ import { run, - // Distribution, + runPartial, + bindings, squiggleExpression, errorValueToString, - // errorValue, - // result, } from "../../src/js/index"; -export function testRun(x: string): squiggleExpression { - let squiggleResult = run(x, {}, { sampleCount: 1000, xyPointLength: 100 }); +export function testRun(x: string, bindings = {}): squiggleExpression { + let squiggleResult = run(x, bindings, { + sampleCount: 1000, + xyPointLength: 100, + }); // return squiggleResult.value if (squiggleResult.tag === "Ok") { return squiggleResult.value; @@ -21,6 +23,22 @@ export function testRun(x: string): squiggleExpression { } } +export function testRunPartial(x: string, bindings: bindings = {}): bindings { + let squiggleResult = runPartial(x, bindings, { + sampleCount: 1000, + xyPointLength: 100, + }); + if (squiggleResult.tag === "Ok") { + return squiggleResult.value; + } else { + throw new Error( + `Expected squiggle expression to evaluate but got error: ${errorValueToString( + squiggleResult.value + )}` + ); + } +} + export function failDefault() { expect("be reached").toBe("codepath should never"); }