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"],
|
2024-05-20 20:39:12 -04:00
|
|
|
// floored division
|
|
|
|
["1 // 5", "0"],
|
|
|
|
["6 // 5", "1"],
|
|
|
|
["-1 // 5", "-1"],
|
|
|
|
["-6 // 5", "-2"],
|
|
|
|
["1 // -5", "-1"],
|
|
|
|
["6 // -5", "-2"],
|
|
|
|
["-1 // -5", "0"],
|
|
|
|
["-6 // -5", "1"],
|
|
|
|
// floored modulo
|
|
|
|
["1 % 5", "1"],
|
|
|
|
["6 % 5", "1"],
|
|
|
|
["-6 % 5", "4"],
|
|
|
|
["-1 % 5", "4"],
|
|
|
|
["1 % -5", "-4"],
|
|
|
|
["6 % -5", "-4"],
|
|
|
|
["-6 % -5", "-1"],
|
|
|
|
["-1 % -5", "-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
|
|
|
|
});
|