prototype-3x5/src/lib/expr.test.ts

38 lines
1.1 KiB
TypeScript
Raw Normal View History

import { AsText, TextPiece } from "../words";
import { Expr } from "./expr";
describe("expr", () => {
describe("Expr does math", () => {
test.each([
["1", "1"],
2024-05-18 21:53:23 -04:00
["-1", "-1"],
["1 + 2", "3"],
["1 - 2", "-1"],
["1 * 2", "2"],
["1 / 2", "0.5"],
["1 // 2", "0"],
["1 % 2", "1"],
["3 % 2", "1"],
["-1 % 2", "1"],
// TODO: might change this! negative dividend is weird no matter what, but positive modulo is arguably the better call?
["1 % -2", "-1"],
// TODO: operator precedence
// TODO: parentheses
])("%s", (expression, result) => {
const actualResult = Expr({}, [{ text: expression }]);
expect("error" in actualResult).toBeFalsy();
expect(AsText(actualResult as TextPiece)).toEqual(result);
});
});
// TODO; error reporting
describe("Expr rejects invalid expressions", () => {
test.each([["1 $ 2"], ["1 1 + 2"], ["$ 1"]])("%s", (expression) => {
const actualResult = Expr({}, [{ text: expression }]);
expect("error" in actualResult).toBeTruthy();
});
});
// TODO: handle expr prefix
});