mirror of
https://github.com/Horizontal42/summarum.git
synced 2026-11-11 01:02:01 +03:00
test: missing error tests for evaluation errors
Add explicit unit tests that construct invalid AST nodes to verify that the core `evaluate()` function throws `EvalError` and `XRefError` exceptions correctly when hitting evaluation issues like unresolved variables or broken cross-references. Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com>
This commit is contained in:
parent
c958f30662
commit
fe7001d715
1 changed files with 25 additions and 3 deletions
|
|
@ -1,8 +1,9 @@
|
||||||
import { describe, it, expect, beforeAll } from "vitest";
|
import { describe, it, expect, beforeAll } from "vitest";
|
||||||
import { SumEngine } from "./index";
|
import { SumEngine } from "./index";
|
||||||
import { qty } from "./types";
|
import { qty, EvalError, XRefError, Decimal } from "./types";
|
||||||
import type { XRefResolution } from "./evaluator";
|
import type { XRefResolution, EvalCtx } from "./evaluator";
|
||||||
import { formatValue } from "./index";
|
import { evaluate } from "./evaluator";
|
||||||
|
import { formatValue } from "./formatter";
|
||||||
|
|
||||||
let eng: SumEngine;
|
let eng: SumEngine;
|
||||||
|
|
||||||
|
|
@ -423,3 +424,24 @@ describe("cross-sheet references", () => {
|
||||||
expect(r[0].assign).toBe("rent");
|
expect(r[0].assign).toBe("rent");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("evaluation errors", () => {
|
||||||
|
it("throws EvalError for unknown variable", () => {
|
||||||
|
const ctx = {
|
||||||
|
vars: new Map(),
|
||||||
|
reg: eng.reg,
|
||||||
|
historicalRates: {},
|
||||||
|
} as unknown as EvalCtx;
|
||||||
|
expect(() => evaluate({ k: "var", name: "unknown" }, ctx)).toThrowError(EvalError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("throws XRefError for unresolved xref", () => {
|
||||||
|
const ctx = {
|
||||||
|
vars: new Map(),
|
||||||
|
reg: eng.reg,
|
||||||
|
historicalRates: {},
|
||||||
|
resolveXRef: (sheet: string, key: string) => ({ ok: false, reason: "xref error" }),
|
||||||
|
} as unknown as EvalCtx;
|
||||||
|
expect(() => evaluate({ k: "xref", sheet: "Sheet", key: "key" }, ctx)).toThrowError(XRefError);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue