Skip to content

Commit

Permalink
Fix numeric predication comparisons when no right-hand values are fou…
Browse files Browse the repository at this point in the history
  • Loading branch information
stwlam committed Jul 31, 2023
1 parent 8dd79fd commit ab6b324
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/module/system/predication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ class PredicatePF2e extends Array<PredicateStatement> {
const maybeNumber = Number(operand);
if (!Number.isNaN(maybeNumber)) return [maybeNumber];
const pattern = new RegExp(String.raw`^${operand}:([^:]+)$`);
return domainArray.map((s) => Number(pattern.exec(s)?.[1] || NaN)).filter((v) => !Number.isNaN(v));
const values = domainArray
.map((s) => Number(pattern.exec(s)?.[1] || NaN))
.filter((v) => !Number.isNaN(v));
return values.length > 0 ? values : [NaN];
};
const leftValues = getValues(left);
const rightValues = getValues(right);
Expand Down
31 changes: 29 additions & 2 deletions tests/module/system/predication.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ describe("Predication with string atomics returns correct results", () => {
});

describe("Predication with numeric-comparison atomics returns correct results", () => {
test("simple greater-than", () => {
const predicate = new PredicatePF2e({ gt: ["foo", 2] });
expect(predicate.test(["foo:1"])).toEqual(false);
expect(predicate.test(["foo:2"])).toEqual(false);
expect(predicate.test(["foo:3"])).toEqual(true);
expect(predicate.test(["foo"])).toEqual(false);
expect(predicate.test([])).toEqual(false);
});

test("simple less-than", () => {
const predicate = new PredicatePF2e({ lt: ["foo", 2] });
expect(predicate.test(["foo:1"])).toEqual(true);
expect(predicate.test(["foo:2"])).toEqual(false);
expect(predicate.test(["foo:3"])).toEqual(false);
expect(predicate.test(["foo"])).toEqual(false);
expect(predicate.test([])).toEqual(false);
});

test("greater-than, less-than", () => {
const predicate = new PredicatePF2e({ gt: ["foo", 2] }, { lt: ["bar", 2] });
expect(predicate.test(["foo:1", "bar:3"])).toEqual(false);
Expand Down Expand Up @@ -57,6 +75,15 @@ describe("Predication with numeric-comparison atomics returns correct results",
expect(predicate.test(["self:level:1", "target:level:2"])).toEqual(true);
expect(predicate.test(["self:level:2", "target:level:1"])).toEqual(false);
});

test("less-than-or-equal-to without matching value pair", () => {
const predicate = new PredicatePF2e({ lte: ["self:level", "target:level"] });
expect(predicate.test([])).toEqual(false);
expect(predicate.test(["self:level:1"])).toEqual(false);
expect(predicate.test(["self:level:1", "foo:2"])).toEqual(false);
expect(predicate.test(["self:level:1", "target:lebel:2"])).toEqual(false);
expect(predicate.test(["self:level:1", "target:level:2"])).toEqual(true);
});
});

describe("Predication with conjunction and negation returns correct results", () => {
Expand Down Expand Up @@ -209,7 +236,7 @@ describe("Predication with exclusive disjunction returns correct results", () =>
});

describe("Predication with material conditional and negation return correct results", () => {
test("material conditional with the `all` quantifier", () => {
test("simple material conditional", () => {
const predicate = new PredicatePF2e({ if: "foo", then: "bar" });
expect(predicate.test(["foo"])).toEqual(false);
expect(predicate.test(["foo", "bar"])).toEqual(true);
Expand All @@ -218,7 +245,7 @@ describe("Predication with material conditional and negation return correct resu
expect(predicate.test([])).toEqual(true);
});

test("material conditional and negation with the `all` quantifier", () => {
test("material conditional and negation", () => {
const predicate = new PredicatePF2e({ if: "foo", then: { not: "bar" } });
expect(predicate.test(["foo"])).toEqual(true);
expect(predicate.test(["foo", "bar"])).toEqual(false);
Expand Down

0 comments on commit ab6b324

Please sign in to comment.