From 9acba7431ffaf53ee303a7a7334ba0d7b4ef5087 Mon Sep 17 00:00:00 2001 From: Tangent Wantwight Date: Mon, 20 May 2024 20:53:06 -0400 Subject: [PATCH] More expr test cases --- src/lib/expr.test.ts | 47 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/src/lib/expr.test.ts b/src/lib/expr.test.ts index fbb0edb..9095d40 100644 --- a/src/lib/expr.test.ts +++ b/src/lib/expr.test.ts @@ -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"); + }); + }); });