More expr test cases

This commit is contained in:
Tangent Wantwight 2024-05-20 20:53:06 -04:00
parent 7e21e71a07
commit 9acba7431f

View file

@ -2,7 +2,7 @@ import { AsText, TextPiece } from "../words";
import { Expr } from "./expr";
describe("expr", () => {
describe("Expr does math", () => {
describe("does math", () => {
test.each([
["1", "1"],
["-1", "-1"],
@ -28,8 +28,6 @@ describe("expr", () => {
["6 % -5", "-4"],
["-6 % -5", "-1"],
["-1 % -5", "-1"],
// TODO: operator precedence
// TODO: parentheses
])("%s", (expression, result) => {
const actualResult = Expr({}, [{ text: expression }]);
expect("error" in actualResult).toBeFalsy();
@ -37,13 +35,46 @@ describe("expr", () => {
});
});
// TODO; error reporting
describe("Expr rejects invalid expressions", () => {
test.each([["1 $ 2"], ["1 1 + 2"], ["$ 1"]])("%s", (expression) => {
describe("handles operator precedence", () => {
test.each([
["1 - 2 + 1", "0"],
["1 + 2 * 3", "7"],
["1 / 2 + 3", "3.5"],
])("%s", (expression, result) => {
const actualResult = Expr({}, [{ text: expression }]);
expect("error" in actualResult).toBeTruthy();
expect("error" in actualResult).toBeFalsy();
expect(AsText(actualResult as TextPiece)).toEqual(result);
});
});
// TODO: handle expr prefix
// TODO: parentheses
// TODO; error reporting
describe("rejects invalid expressions", () => {
test.each([[""], ["1 $ 2"], ["1 1 + 2"], ["1 + + 2"], ["$ 1"]])(
"%s",
(expression) => {
const actualResult = Expr({}, [{ text: expression }]);
expect("error" in actualResult).toBeTruthy();
}
);
});
// TODO: operators should only be accepted as bare words
describe("ignores an expr prefix", () => {
test.each([
[["1", "+", "2"]],
[["expr", "1", "+", "2"]],
[["1 + 2"]],
[["expr", "1 + 2"]],
])("%s", (argv) => {
const actualResult = Expr(
{},
argv.map((text) => ({ bare: text }))
);
expect("error" in actualResult).toBeFalsy();
expect(AsText(actualResult as TextPiece)).toEqual("3");
});
});
});