summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2025-07-13 21:04:42 -0300
committerEuAndreh <eu@euandre.org>2025-07-13 21:04:42 -0300
commit400f763c28416aaa2f142d2ff8418d78d4e050c1 (patch)
tree32c42033b7114c440e06cb484146a1fcc9058bee
parentsrc/paca.mjs (rangeStateStep): Fix typo in SyntaxError message (diff)
downloadpaca-400f763c28416aaa2f142d2ff8418d78d4e050c1.tar.gz
paca-400f763c28416aaa2f142d2ff8418d78d4e050c1.tar.xz
tests/paca.mjs (test_rangeStateStep): Finish test cases for rangeStateStep
-rw-r--r--tests/paca.mjs139
1 files changed, 139 insertions, 0 deletions
diff --git a/tests/paca.mjs b/tests/paca.mjs
index 15e15ec..739fe80 100644
--- a/tests/paca.mjs
+++ b/tests/paca.mjs
@@ -2,6 +2,7 @@ import { explode, reduced, reductions, runTests } from "sjs";
import {
SyntaxError,
+ ValueError,
ConcatStep,
numFromDigits,
rangeStateStep,
@@ -73,6 +74,144 @@ const test_rangeStateStep = t => {
t.assertEq(error.message, "missing comma in range operator");
t.assertEq(error instanceof SyntaxError, true);
});
+
+ t.testing("error when range decreases", () => {
+ const { value: { error }} = rangeStateStep(
+ {
+ context: {
+ from: [ "3" ],
+ to: [ "1" ],
+ where: "to",
+ },
+ },
+ "}",
+ null,
+ null,
+ );
+ t.assertEq(error.message, "bad range values: {3,1}");
+ t.assertEq(error instanceof ValueError, true);
+ });
+
+ t.testing("OK when \"to\" is unbounded", () => {
+ const given = rangeStateStep(
+ {
+ out: [ 1, 2, 3 ],
+ context: {
+ from: [ "1", "0" ],
+ to: [],
+ where: "to",
+ },
+ },
+ "}",
+ );
+ const expected = {
+ out: [ 1, 2, 3, {
+ operator: "range",
+ from: 10,
+ to: -1,
+ }],
+ state: "accepting",
+ context: null,
+ };
+ t.assertEq(given, expected);
+ });
+
+ t.testing("error when we have more than 1 comma", () => {
+ const { value: { error }} = rangeStateStep(
+ { context: { where: "to" } },
+ ",",
+ null,
+ null,
+ );
+ t.assertEq(
+ error.message,
+ "extraneous comma in range expression",
+ );
+ t.assertEq(error instanceof SyntaxError, true);
+ });
+
+ t.testing("a comma change the `where` - `from` => `to`", () => {
+ const given = rangeStateStep(
+ {
+ out: 123,
+ state: 321,
+ context: {
+ x: 1,
+ y: 2,
+ where: "ignored"
+ },
+ },
+ ",",
+ null,
+ null
+ );
+ const expected = {
+ out: 123,
+ state: 321,
+ context: {
+ x: 1,
+ y: 2,
+ where: "to",
+ },
+ };
+ t.assertEq(given, expected);
+ });
+
+ t.testing("error on non-numeric component chars", () => {
+ const { value: { error }} = rangeStateStep({}, "a", null, null);
+ t.assertEq(error.message, "bad char in range expression: a");
+ t.assertEq(error instanceof SyntaxError, true);
+ });
+
+ t.testing("digit gets added to the key that `where` indicates", () => {
+ const given1 = rangeStateStep(
+ {
+ out: 1,
+ state: 2,
+ context: {
+ a: "bc",
+ from: [ "1", "3" ],
+ to: [],
+ where: "from",
+ },
+ },
+ "7",
+ null,
+ null,
+ );
+ const expected1 = {
+ out: 1,
+ state: 2,
+ context: {
+ a: "bc",
+ from: [ "1", "3", "7" ],
+ to: [],
+ where: "from",
+ },
+ };
+ t.assertEq(given1, expected1);
+
+ const given2 = rangeStateStep(
+ {
+ context: {
+ to: [],
+ where: "to",
+ },
+ },
+ "0",
+ null,
+ null,
+ );
+ const expected2 = {
+ out: undefined,
+ state: undefined,
+ context: {
+ to: [ "0" ],
+ where: "to" ,
+ },
+ };
+ t.assertEq(given2, expected2);
+ });
};
const test_TRANSITION_FNS = t => {