Initial Commit
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
import { describe, expect, it } from "@suaveplan/testing/runner";
|
||||
import { compareValues } from "./operators-internal.js";
|
||||
|
||||
describe("compareValues — equality + numeric", () => {
|
||||
it("equals / notEquals (loose)", { timeout: 5000 }, () => {
|
||||
expect(compareValues(1, "equals", 1)).toBe(true);
|
||||
expect(compareValues(1, "notEquals", 2)).toBe(true);
|
||||
});
|
||||
|
||||
it("strictEquals / strictNotEquals", { timeout: 5000 }, () => {
|
||||
expect(compareValues("1", "strictEquals", "1")).toBe(true);
|
||||
expect(compareValues(1, "strictNotEquals", 2)).toBe(true);
|
||||
});
|
||||
|
||||
it("greater / less", { timeout: 5000 }, () => {
|
||||
expect(compareValues(5, "greaterThan", 1)).toBe(true);
|
||||
expect(compareValues(5, "greaterThanOrEqual", 5)).toBe(true);
|
||||
expect(compareValues(1, "lessThan", 5)).toBe(true);
|
||||
expect(compareValues(5, "lessThanOrEqual", 5)).toBe(true);
|
||||
});
|
||||
|
||||
it("between / notBetween inclusive", { timeout: 5000 }, () => {
|
||||
expect(compareValues(5, "between", [1, 10])).toBe(true);
|
||||
expect(compareValues(1, "between", [1, 10])).toBe(true);
|
||||
expect(compareValues(10, "between", [1, 10])).toBe(true);
|
||||
expect(compareValues(11, "between", [1, 10])).toBe(false);
|
||||
expect(compareValues(11, "notBetween", [1, 10])).toBe(true);
|
||||
expect(compareValues(5, "notBetween", [1, 10])).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("compareValues — collections", () => {
|
||||
it("in / notIn against array", { timeout: 5000 }, () => {
|
||||
expect(compareValues(2, "in", [1, 2, 3])).toBe(true);
|
||||
expect(compareValues(9, "notIn", [1, 2, 3])).toBe(true);
|
||||
expect(compareValues(2, "in", "not-array")).toBe(false);
|
||||
expect(compareValues(9, "notIn", "not-array")).toBe(false);
|
||||
});
|
||||
|
||||
it("contains over array / string", { timeout: 5000 }, () => {
|
||||
expect(compareValues([1, 2, 3], "contains", 2)).toBe(true);
|
||||
expect(compareValues("abc", "contains", "b")).toBe(true);
|
||||
expect(compareValues(42, "contains", 2)).toBe(false);
|
||||
});
|
||||
|
||||
it("notContains over array / string / other", { timeout: 5000 }, () => {
|
||||
expect(compareValues([1, 2, 3], "notContains", 9)).toBe(true);
|
||||
expect(compareValues("abc", "notContains", "z")).toBe(true);
|
||||
expect(compareValues(42, "notContains", 2)).toBe(true);
|
||||
});
|
||||
|
||||
it("containsAll / containsAny / overlaps", { timeout: 5000 }, () => {
|
||||
expect(compareValues([1, 2, 3], "containsAll", [1, 2])).toBe(true);
|
||||
expect(compareValues([1, 2, 3], "containsAll", [1, 9])).toBe(false);
|
||||
expect(compareValues([1, 2, 3], "containsAny", [9, 2])).toBe(true);
|
||||
expect(compareValues([1, 2, 3], "containsAny", [9, 8])).toBe(false);
|
||||
expect(compareValues([1, 2, 3], "overlaps", [3, 4])).toBe(true);
|
||||
expect(compareValues([1, 2, 3], "overlaps", [9])).toBe(false);
|
||||
expect(compareValues("not-array", "containsAll", [1])).toBe(false);
|
||||
expect(compareValues("not-array", "containsAny", [1])).toBe(false);
|
||||
expect(compareValues("not-array", "overlaps", [1])).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("compareValues — existence + type", () => {
|
||||
it("exists / notExists", { timeout: 5000 }, () => {
|
||||
expect(compareValues(0, "exists", true)).toBe(true);
|
||||
expect(compareValues(undefined, "exists", true)).toBe(false);
|
||||
expect(compareValues(undefined, "notExists", true)).toBe(true);
|
||||
expect(compareValues(0, "notExists", true)).toBe(false);
|
||||
});
|
||||
|
||||
it("isNull / isNotNull", { timeout: 5000 }, () => {
|
||||
expect(compareValues(null, "isNull", true)).toBe(true);
|
||||
expect(compareValues(0, "isNull", true)).toBe(false);
|
||||
expect(compareValues(0, "isNotNull", true)).toBe(true);
|
||||
expect(compareValues(null, "isNotNull", true)).toBe(false);
|
||||
});
|
||||
|
||||
it("isEmpty / isNotEmpty", { timeout: 5000 }, () => {
|
||||
expect(compareValues(null, "isEmpty", true)).toBe(true);
|
||||
expect(compareValues(undefined, "isEmpty", true)).toBe(true);
|
||||
expect(compareValues("", "isEmpty", true)).toBe(true);
|
||||
expect(compareValues([], "isEmpty", true)).toBe(true);
|
||||
expect(compareValues({}, "isEmpty", true)).toBe(true);
|
||||
expect(compareValues(0, "isEmpty", true)).toBe(false);
|
||||
expect(compareValues(null, "isNotEmpty", true)).toBe(false);
|
||||
expect(compareValues("abc", "isNotEmpty", true)).toBe(true);
|
||||
expect(compareValues([1], "isNotEmpty", true)).toBe(true);
|
||||
expect(compareValues({ a: 1 }, "isNotEmpty", true)).toBe(true);
|
||||
expect(compareValues(0, "isNotEmpty", true)).toBe(true);
|
||||
});
|
||||
|
||||
it("isType / isNotType", { timeout: 5000 }, () => {
|
||||
expect(compareValues("a", "isType", "string")).toBe(true);
|
||||
expect(compareValues(1, "isType", "string")).toBe(false);
|
||||
expect(compareValues(1, "isNotType", "string")).toBe(true);
|
||||
expect(compareValues("a", "isNotType", "string")).toBe(false);
|
||||
});
|
||||
|
||||
it("hasProperty checks own keys", { timeout: 5000 }, () => {
|
||||
expect(compareValues({ a: 1 }, "hasProperty", "a")).toBe(true);
|
||||
expect(compareValues({ a: 1 }, "hasProperty", "b")).toBe(false);
|
||||
expect(compareValues(null, "hasProperty", "a")).toBe(false);
|
||||
expect(compareValues("not-an-obj", "hasProperty", "length")).toBe(
|
||||
false
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("compareValues — length", () => {
|
||||
it("lengthEquals / GreaterThan / LessThan for strings", {
|
||||
timeout: 5000,
|
||||
}, () => {
|
||||
expect(compareValues("abc", "lengthEquals", 3)).toBe(true);
|
||||
expect(compareValues("abcd", "lengthGreaterThan", 3)).toBe(true);
|
||||
expect(compareValues("ab", "lengthLessThan", 3)).toBe(true);
|
||||
});
|
||||
|
||||
it("length on arrays / objects / non-countables", { timeout: 5000 }, () => {
|
||||
expect(compareValues([1, 2], "lengthEquals", 2)).toBe(true);
|
||||
expect(compareValues({ a: 1, b: 2 }, "lengthEquals", 2)).toBe(true);
|
||||
expect(compareValues(42, "lengthEquals", 2)).toBe(false);
|
||||
expect(compareValues(null, "lengthLessThan", 5)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("compareValues — string", () => {
|
||||
it("startsWith / endsWith / matches / notMatches", {
|
||||
timeout: 5000,
|
||||
}, () => {
|
||||
expect(compareValues("abcd", "startsWith", "ab")).toBe(true);
|
||||
expect(compareValues("abcd", "endsWith", "cd")).toBe(true);
|
||||
expect(compareValues("abcd", "matches", "^a.*d$")).toBe(true);
|
||||
expect(compareValues("abcd", "matches", /^a.*d$/)).toBe(true);
|
||||
expect(compareValues(1, "matches", "^.*$")).toBe(false);
|
||||
expect(compareValues(1, "notMatches", "^.*$")).toBe(true);
|
||||
expect(compareValues("abcd", "notMatches", "^z")).toBe(true);
|
||||
expect(compareValues("abcd", "notMatches", /^z/)).toBe(true);
|
||||
expect(compareValues(42, "startsWith", "4")).toBe(false);
|
||||
expect(compareValues(42, "endsWith", "2")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("compareValues — unknown", () => {
|
||||
it("raises RULES.OPERATOR_NOT_FOUND", { timeout: 5000 }, () => {
|
||||
expect(() => compareValues(1, "bogus" as never, 1)).toThrow(
|
||||
/RULES\.OPERATOR_NOT_FOUND/
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user