2024-05-17 20:26:54 -04:00
|
|
|
import { AsText, TextPiece } from "../words";
|
|
|
|
import { Expr } from "./expr";
|
|
|
|
|
|
|
|
describe("expr", () => {
|
2024-05-18 21:24:19 -04:00
|
|
|
describe("Expr does math", () => {
|
|
|
|
test.each([
|
|
|
|
["1", "1"],
|
2024-05-18 21:53:23 -04:00
|
|
|
["-1", "-1"],
|
2024-05-18 21:24:19 -04:00
|
|
|
["1 + 2", "3"],
|
|
|
|
["1 - 2", "-1"],
|
|
|
|
["1 * 2", "2"],
|
|
|
|
["1 / 2", "0.5"],
|
|
|
|
["1 // 2", "0"],
|
2024-05-18 23:22:50 -04:00
|
|
|
["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"],
|
2024-05-18 21:24:19 -04:00
|
|
|
// 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();
|
|
|
|
});
|
2024-05-17 20:26:54 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: handle expr prefix
|
|
|
|
});
|