fix more tests for new TS classes

This commit is contained in:
Vyacheslav Matyukhin 2022-09-01 14:36:27 +04:00
parent eda1a45fda
commit 48fb634140
No known key found for this signature in database
GPG Key ID: 3D2A774C5489F96C
6 changed files with 130 additions and 160 deletions

View File

@ -1,5 +1,4 @@
import { SqProject, SqValue } from "../../src/js"; import { run, SqProject, SqValue, SqValueTag } from "../../src/js";
import { SqNumberValue } from "../../src/js/SqValue";
import { testRun } from "./TestHelpers"; import { testRun } from "./TestHelpers";
function Ok<b>(x: b) { function Ok<b>(x: b) {
@ -9,157 +8,121 @@ function Ok<b>(x: b) {
describe("Simple calculations and results", () => { describe("Simple calculations and results", () => {
test("mean(normal(5,2))", () => { test("mean(normal(5,2))", () => {
const result = testRun("mean(normal(5,2))"); // FIXME const result = testRun("mean(normal(5,2))"); // FIXME
expect(result.value).toEqual(5); expect(result.toString()).toEqual("5");
}); });
test("10+10", () => { test("10+10", () => {
let result = testRun("10 + 10") as SqNumberValue; let result = testRun("10 + 10");
expect(result.value).toEqual(20); expect(result.toString()).toEqual("20");
});
});
describe("Log function", () => {
test("log(1) = 0", () => {
let foo = testRun("log(1)");
expect(foo.toString()).toEqual("0");
}); });
}); });
// describe("Log function", () => {
// test("log(1) = 0", () => {
// let foo = testRun("log(1)");
// expect(foo).toEqual({ tag: "number", value: 0 });
// });
// });
// describe("Array", () => { describe("Array", () => {
// test("nested Array", () => { test("nested Array", () => {
// expect(testRun("[[1]]")).toEqual({ expect(testRun("[[ 1 ]]").toString()).toEqual("[[1]]");
// tag: "array", });
// value: [ });
// {
// tag: "array",
// value: [
// {
// tag: "number",
// value: 1,
// },
// ],
// },
// ],
// });
// });
// });
// describe("Record", () => { describe("Record", () => {
// test("Return record", () => { test("Return record", () => {
// expect(testRun("{a: 1}")).toEqual({ expect(testRun("{a:1}").toString()).toEqual("{a: 1}");
// tag: "record", });
// value: { });
// a: {
// tag: "number",
// value: 1,
// },
// },
// });
// });
// });
// describe("Partials", () => { describe("Continues", () => {
// test("Can pass variables between partials and cells", () => { test("Bindings from continues are accessible", () => {
// const project = Project.create(); const project = SqProject.create();
// project.setSource("p1", "x = 5"); project.setSource("p1", "x = 5");
// project.setSource("p2", "y = x + 2"); project.setSource("p2", "y = x + 2");
// project.setSource("main", "y + 3"); project.setSource("main", "y + 3");
// project.run("main"); project.setContinues("main", ["p2"]);
// const result = project.getResult("main"); project.setContinues("p2", ["p1"]);
// expect(result.tag).toEqual("Ok"); project.run("main");
// expect(result.value).toBeInstanceOf(SquiggleValue); const result = project.getResult("main");
// expect(result.value).toHaveProperty("tag", "number"); expect(result.tag).toEqual("Ok");
// failDefault(); // FIXME expect(result.value.toString()).toEqual("10");
// // let bindings = testRunPartial(`x = 5`); });
// // let bindings2 = testRunPartial(`y = x + 2`, bindings); test("Can merge bindings from three partials", () => {
// // expect(testRun(`y + 3`, bindings2)).toEqual({ const project = SqProject.create();
// // tag: "number", project.setSource("p1", "x = 1");
// // value: 10, project.setSource("p2", "y = 2");
// // }); project.setSource("p3", "z = 3");
// }); project.setSource("main", "x + y + z");
// test("Can merge bindings from three partials", () => { project.setContinues("main", ["p1", "p2", "p3"]);
// let bindings1 = testRunPartial(`x = 1`); project.run("main");
// let bindings2 = testRunPartial(`y = 2`); const result = project.getResult("main");
// let bindings3 = testRunPartial(`z = 3`); expect(result.tag).toEqual("Ok");
// expect( expect(result.value.toString()).toEqual("6");
// testRun(`x + y + z`, mergeBindings([bindings1, bindings2, bindings3])) });
// ).toEqual({ });
// tag: "number",
// value: 6,
// });
// });
// });
// describe("JS Imports", () => { describe("Distribution", () => {
// test("Can pass parameters into partials and cells", () => { //It's important that sampleCount is less than 9. If it's more, than that will create randomness
// let bindings = testRunPartial(`y = $x + 2`, defaultBindings, { x: 1 }); //Also, note, the value should be created using makeSampleSetDist() later on.
// let bindings2 = testRunPartial(`z = y + $a`, bindings, { a: 3 }); let env = { sampleCount: 8, xyPointLength: 100 };
// expect(testRun(`z`, bindings2)).toEqual({ let dist1Samples = [3, 4, 5, 6, 6, 7, 10, 15, 30];
// tag: "number", let dist1SampleCount = dist1Samples.length;
// value: 6,
// });
// });
// test("Complicated deep parameters", () => {
// expect(
// testRun(`$x.y[0][0].w + $x.z + $u.v`, defaultBindings, {
// x: { y: [[{ w: 1 }]], z: 2 },
// u: { v: 3 },
// })
// ).toEqual({
// tag: "number",
// value: 6,
// });
// });
// });
// describe("Distribution", () => { const buildDist = (samples: number[]) => {
// //It's important that sampleCount is less than 9. If it's more, than that will create randomness const src = `SampleSet.fromList([${samples.join(",")}])`;
// //Also, note, the value should be created using makeSampleSetDist() later on. const { result } = run(src, {
// let env = { sampleCount: 8, xyPointLength: 100 }; environment: env,
// let dist1Samples = [3, 4, 5, 6, 6, 7, 10, 15, 30]; });
// let dist1SampleCount = dist1Samples.length; if (result.tag !== "Ok") {
// let dist = new Distribution( throw new Error(
// { tag: "SampleSet", value: [3, 4, 5, 6, 6, 7, 10, 15, 30] }, `Failed to build SampleSet: from ${src}: ${result.value}`
// env );
// ); }
// let dist2 = new Distribution( const dist = result.value;
// { tag: "SampleSet", value: [20, 22, 24, 29, 30, 35, 38, 44, 52] }, if (dist.tag !== SqValueTag.Distribution) {
// env throw new Error("Expected Distribution");
// ); }
return dist.value;
};
// test("mean", () => { const dist = buildDist(dist1Samples);
// expect(dist.mean().value).toBeCloseTo(9.5555555); const dist2 = buildDist([20, 22, 24, 29, 30, 35, 38, 44, 52]);
// });
// test("pdf", () => { test("mean", () => {
// expect(dist.pdf(5.0).value).toBeCloseTo(0.10499097598222966, 1); expect(dist.mean(env).value).toBeCloseTo(9.5555555);
// }); });
// test("cdf", () => { test("pdf", () => {
// expect(dist.cdf(5.0).value).toBeCloseTo( expect(dist.pdf(env, 5.0).value).toBeCloseTo(0.10499097598222966, 1);
// dist1Samples.filter((x) => x <= 5).length / dist1SampleCount, });
// 1 test("cdf", () => {
// ); expect(dist.cdf(env, 5.0).value).toBeCloseTo(
// }); dist1Samples.filter((x) => x <= 5).length / dist1SampleCount,
// test("inv", () => { 1
// expect(dist.inv(0.5).value).toBeCloseTo(6); );
// }); });
// test("toPointSet", () => { test("inv", () => {
// expect( expect(dist.inv(env, 0.5).value).toBeCloseTo(6);
// resultMap(dist.toPointSet(), (r: Distribution) => r.toString()) });
// ).toEqual(Ok("Point Set Distribution")); // test("toPointSet", () => {
// }); // expect(
// test("toSparkline", () => { // resultMap(dist.toPointSet(), (r: Distribution) => r.toString())
// expect(dist.toSparkline(20).value).toEqual("▁▁▃▇█▇▄▂▂▂▁▁▁▁▁▂▂▁▁▁"); // ).toEqual(Ok("Point Set Distribution"));
// }); // });
// test("algebraicAdd", () => { // test("toSparkline", () => {
// expect( // expect(dist.toSparkline(20).value).toEqual("▁▁▃▇█▇▄▂▂▂▁▁▁▁▁▂▂▁▁▁");
// resultMap(dist.algebraicAdd(dist2), (r: Distribution) => // });
// r.toSparkline(20) // test("algebraicAdd", () => {
// ).value // expect(
// ).toEqual(Ok("▁▁▂▄▆████▇▆▄▄▃▃▃▂▁▁▁")); // resultMap(dist.algebraicAdd(dist2), (r: Distribution) =>
// }); // r.toSparkline(20)
// test("pointwiseAdd", () => { // ).value
// expect( // ).toEqual(Ok("▁▁▂▄▆████▇▆▄▄▃▃▃▂▁▁▁"));
// resultMap(dist.pointwiseAdd(dist2), (r: Distribution) => // });
// r.toSparkline(20) // test("pointwiseAdd", () => {
// ).value // expect(
// ).toEqual(Ok("▁▂██▃▃▃▃▄▅▄▃▃▂▂▂▁▁▁▁")); // resultMap(dist.pointwiseAdd(dist2), (r: Distribution) =>
// }); // r.toSparkline(20)
// }); // ).value
// ).toEqual(Ok("▁▂██▃▃▃▃▄▅▄▃▃▂▂▂▁▁▁▁"));
// });
});

View File

@ -1,4 +1,3 @@
// import { errorValueToString } from "../../src/js/index";
import * as fc from "fast-check"; import * as fc from "fast-check";
import { testRun } from "./TestHelpers"; import { testRun } from "./TestHelpers";

View File

@ -36,9 +36,9 @@ describe("Squiggle's parser is whitespace insensitive", () => {
whitespaceGen(), whitespaceGen(),
whitespaceGen(), whitespaceGen(),
(a, b, c, d, e, f, g, h) => { (a, b, c, d, e, f, g, h) => {
expect(testRun(squiggleString(a, b, c, d, e, f, g, h))).toEqual( expect(
squiggleOutput testRun(squiggleString(a, b, c, d, e, f, g, h))
); ).toEqualSqValue(squiggleOutput);
} }
) )
); );

View File

@ -1,4 +1,3 @@
// import { errorValueToString } from "../../src/js/index";
import { testRun, expectErrorToBeBounded, SqValueTag } from "./TestHelpers"; import { testRun, expectErrorToBeBounded, SqValueTag } from "./TestHelpers";
import * as fc from "fast-check"; import * as fc from "fast-check";

View File

@ -1,16 +1,21 @@
import { run, SqValueTag } from "../../src/js"; import { run, SqValueTag } from "../../src/js";
export { SqValueTag }; export { SqValueTag };
expect.extend({
toEqualSqValue(x, y) {
// hack via https://github.com/facebook/jest/issues/10329#issuecomment-820656061
const { getMatchers } = require("expect/build/jestMatchersObject");
return getMatchers().toEqual.call(this, x.toString(), y.toString());
},
});
export function testRun(x: string) { export function testRun(x: string) {
const { result, bindings } = run(x); // FIXME - set environment const { result, bindings } = run(x, {
// x, environment: {
// bindings, sampleCount: 1000,
// { xyPointLength: 100,
// sampleCount: 1000, },
// xyPointLength: 100, });
// },
// imports
// );
if (result.tag === "Ok") { if (result.tag === "Ok") {
return result.value; return result.value;

View File

@ -32,6 +32,10 @@ export abstract class SqAbstractValue {
} }
return value; return value;
}; };
toString() {
return RSValue.toString(this._value);
}
} }
export class SqArrayValue extends SqAbstractValue { export class SqArrayValue extends SqAbstractValue {