const fs = require("fs"); const path = require("path"); import { JsonLogic } from "../json-logic"; describe("LoggerUtil testing", () => { describe("applies() tests", () => { it("tests.json", () => { const jsonLogic = new JsonLogic(); const body = fs.readFileSync( path.resolve(__dirname, "tests.json"), "utf8" ); let tests = JSON.parse(body); tests = tests.filter((test: any) => { return typeof test !== "string"; }); for (let test of tests) { const rule = test[0]; const data = test[1]; const expected = test[2]; const result = jsonLogic.apply(rule, data); expect(result).toEqual(expected); } }); it("rule_like() tests", () => { const jsonLogic = new JsonLogic(); const body = fs.readFileSync( path.resolve(__dirname, "rule_like.json"), "utf8" ); let tests = JSON.parse(body); tests = tests.filter((test: any) => { return typeof test !== "string"; }); for (let test of tests) { const rule = test[0]; const data = test[1]; const expected = test[2]; const result = jsonLogic.rule_like(rule, data); expect(result).toEqual(expected); } }); it("Expanding functionality with method", () => { const jsonLogic = new JsonLogic(); const a = { count: 0, increment: function() { return (this.count += 1); }, add: function(b: number) { return (this.count += b); } }; expect( jsonLogic.apply({ method: [{ var: "a" }, "increment"] }, { a: a }) ).toEqual(1); // Look up "a" in data, and run the increment method on it with no args. expect(a.count).toEqual(1); // Happy state change expect( jsonLogic.apply({ method: [{ var: "a" }, "add", [41]] }, { a: a }) ).toEqual(42); expect(a.count).toEqual(42); }); it("Control structures don't eval depth-first", () => { const jsonLogic = new JsonLogic(); let conditions: [] = []; let consequents: [] = []; jsonLogic.add_operation("push.if", (v: any) => { conditions.push(v as never); return v; }); jsonLogic.add_operation("push.then", (v: any) => { consequents.push(v as never); return v; }); jsonLogic.add_operation("push.else", (v: any) => { consequents.push(v as never); return v; }); jsonLogic.apply({ if: [ { "push.if": [true] }, { "push.then": ["first"] }, { "push.if": [false] }, { "push.then": ["second"] }, { "push.else": ["third"] } ] }); expect(conditions).toEqual([true]); expect(consequents).toEqual(["first"]); conditions = []; consequents = []; jsonLogic.apply({ if: [ { "push.if": [false] }, { "push.then": ["first"] }, { "push.if": [true] }, { "push.then": ["second"] }, { "push.else": ["third"] } ] }); expect(conditions).toEqual([false, true]); expect(consequents).toEqual(["second"]); conditions = []; consequents = []; jsonLogic.apply({ if: [ { "push.if": [false] }, { "push.then": ["first"] }, { "push.if": [false] }, { "push.then": ["second"] }, { "push.else": ["third"] } ] }); expect(conditions).toEqual([false, false]); expect(consequents).toEqual(["third"]); jsonLogic.add_operation("push", (arg: any) => { i.push(arg as never); return arg; }); let i: [] = []; i = []; jsonLogic.apply({ and: [{ push: [false] }, { push: [false] }] }); expect(i).toEqual([false]); i = []; jsonLogic.apply({ and: [{ push: [false] }, { push: [true] }] }); expect(i).toEqual([false]); i = []; jsonLogic.apply({ and: [{ push: [true] }, { push: [false] }] }); expect(i).toEqual([true, false]); i = []; jsonLogic.apply({ and: [{ push: [true] }, { push: [true] }] }); expect(i).toEqual([true, true]); i = []; jsonLogic.apply({ or: [{ push: [false] }, { push: [false] }] }); expect(i).toEqual([false, false]); i = []; jsonLogic.apply({ or: [{ push: [false] }, { push: [true] }] }); expect(i).toEqual([false, true]); i = []; jsonLogic.apply({ or: [{ push: [true] }, { push: [false] }] }); expect(i).toEqual([true]); i = []; jsonLogic.apply({ or: [{ push: [true] }, { push: [true] }] }); expect(i).toEqual([true]); }); }); });