mirror of
https://github.com/Horizontal42/summarum.git
synced 2026-11-10 20:21:58 +03:00
Merge 5172fd99db into 37cfd2144b
This commit is contained in:
commit
96bede3676
1 changed files with 36 additions and 0 deletions
|
|
@ -342,14 +342,50 @@ describe("extension API", () => {
|
||||||
// the original registration must survive the failed collision
|
// the original registration must survive the failed collision
|
||||||
expect(calc("2 horses in meters")).toBe("4.8 m");
|
expect(calc("2 horses in meters")).toBe("4.8 m");
|
||||||
});
|
});
|
||||||
|
it("addUnit throws on unknown baseUnitId", () => {
|
||||||
|
expect(() =>
|
||||||
|
eng.addUnit({ id: "unicorn", phrases: "uni", baseUnitId: "unknown", ratio: 1 })
|
||||||
|
).toThrow(/unknown baseUnitId unknown/);
|
||||||
|
});
|
||||||
it("addFunction (zum from Sample.js)", () => {
|
it("addFunction (zum from Sample.js)", () => {
|
||||||
eng.addFunction({ id: "zum", phrases: "zum" }, (values) => ({ double: values[0].double + values[1].double }));
|
eng.addFunction({ id: "zum", phrases: "zum" }, (values) => ({ double: values[0].double + values[1].double }));
|
||||||
expect(calc("zum(2;3)")).toBe("5");
|
expect(calc("zum(2;3)")).toBe("5");
|
||||||
});
|
});
|
||||||
|
it("addFunction converts percent to ExtensionValue", () => {
|
||||||
|
let capturedPercent: number | undefined;
|
||||||
|
eng.addFunction({ id: "test_percent", phrases: "test_percent" }, (values) => {
|
||||||
|
capturedPercent = values[0].double;
|
||||||
|
return { double: values[0].double };
|
||||||
|
});
|
||||||
|
expect(calc("test_percent(50%)")).toBe("0.5");
|
||||||
|
expect(capturedPercent).toBe(0.5);
|
||||||
|
});
|
||||||
|
it("addFunction converts date to ExtensionValue", () => {
|
||||||
|
let capturedDate: number | undefined;
|
||||||
|
eng.addFunction({ id: "test_date", phrases: "test_date" }, (values) => {
|
||||||
|
capturedDate = values[0].double;
|
||||||
|
return { double: values[0].double };
|
||||||
|
});
|
||||||
|
eng.evaluateDocument("test_date(2024-01-01)");
|
||||||
|
expect(capturedDate).toBe(new Date("2024-01-01T00:00:00Z").getTime());
|
||||||
|
});
|
||||||
it("setVariable", () => {
|
it("setVariable", () => {
|
||||||
eng.setVariable("myvar", { double: 5, unitId: "USD" });
|
eng.setVariable("myvar", { double: 5, unitId: "USD" });
|
||||||
expect(calc("myvar * 2")).toBe("$10");
|
expect(calc("myvar * 2")).toBe("$10");
|
||||||
});
|
});
|
||||||
|
it("setVariable with number without unitId", () => {
|
||||||
|
eng.setVariable("numvar", 42);
|
||||||
|
expect(calc("numvar * 2")).toBe("84");
|
||||||
|
});
|
||||||
|
it("evaluateExpression returns the last value", () => {
|
||||||
|
const val = eng.evaluateExpression("10 + 20");
|
||||||
|
expect(val).not.toBeNull();
|
||||||
|
if (val && val.kind === "quantity") {
|
||||||
|
expect(val.value.toNumber()).toBe(30);
|
||||||
|
} else {
|
||||||
|
throw new Error("expected a quantity");
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
function calcWithResolver(
|
function calcWithResolver(
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue