Add basic partial test

This commit is contained in:
Sam Nolan 2022-04-29 14:10:41 +00:00
parent 8f879b7168
commit 58a357cce5
2 changed files with 35 additions and 6 deletions

View File

@ -1,5 +1,5 @@
import { Distribution, resultMap } from "../../src/js/index";
import { testRun } from "./TestHelpers";
import { testRun, testRunPartial } from "./TestHelpers";
function Ok<b>(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.

View File

@ -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");
}