name:
src/test/js/formulas/logic/XORTest.ts
-rw-r--r--
3464
1import { assert } from "chai";
2import { it, describe } from "../../testutils/TestUtils";
3import { stub } from "sinon";
4import { DivException } from "../../../../main/js/errors/DivException";
5import { F7Exception } from "../../../../main/js/errors/F7Exception";
6import { F7ExceptionName } from "../../../../main/js/errors/F7ExceptionName";
7import { NAException } from "../../../../main/js/errors/NAException";
8import { XOR } from "../../../../main/js/formulas/logic/XOR";
9import { Grid } from "../../../../main/js/models/common/Grid";
10import { CollateralLookupFunction, LookupFunction } from "../../../../main/js/models/common/Types";
11import { Converters } from "../../../../main/js/utils/Converters";
12import { CommonModels } from "../../testutils/CommonModels";
13
14describe("XOR", function () {
15 it("should work with numbers", function () {
16 assert.equal(XOR.SELF.run(null, 10, 10), false);
17 assert.equal(XOR.SELF.run(null, 10, 0), true);
18 assert.equal(XOR.SELF.run(null, 1, 1, 1, 1, 1, 1, 0), false);
19 assert.equal(XOR.SELF.run(null, 0, 0, 0, 1, 0), true);
20 assert.equal(XOR.SELF.run(null, 0, 0, 0, 1, 1), false);
21 assert.equal(XOR.SELF.run(null, 0, 0, 0, 0, 0), false);
22 });
23
24 it("should work with strings", function () {
25 assert.equal(XOR.SELF.run(null, "TRUE", true), false);
26 assert.equal(XOR.SELF.run(null, "true", true), false);
27 assert.equal(XOR.SELF.run(null, "FALSE", true), true);
28 assert.equal(XOR.SELF.run(null, "false", false), false);
29 });
30
31 it("should work with booleans", function () {
32 assert.equal(XOR.SELF.run(null, true, true, true), true);
33 assert.equal(XOR.SELF.run(null, true, true, false), false);
34 assert.equal(XOR.SELF.run(null, false, false, false), false);
35 assert.equal(XOR.SELF.run(null, false, false, true), true);
36 });
37
38 it("should work with null/blank/empty", function () {
39 assert.equal(
40 Converters.castAsF7Exception(XOR.SELF.run(null, null, null)).name,
41 F7ExceptionName.VALUE
42 );
43 assert.equal(XOR.SELF.run(null, null, null, false), false);
44 assert.equal(XOR.SELF.run(null, null, null, true), true);
45 });
46
47 it("should do pass-through errors", function () {
48 assert.equal(
49 (XOR.SELF.run(null, 4, new DivException()) as F7Exception).name,
50 F7ExceptionName.DIV
51 );
52 });
53
54 it("should use lookup", function () {
55 const lookup = stub();
56 const collateralLookup = stub();
57 const F = new XOR(lookup as LookupFunction, collateralLookup as CollateralLookupFunction);
58 lookup.withArgs(CommonModels.M22_RANGE).returns(true);
59 lookup.withArgs(CommonModels.G19_RANGE).returns(true);
60 assert.deepEqual(F.run(CommonModels.A1, CommonModels.M22_RANGE, CommonModels.G19_RANGE), false);
61 assert.isTrue(lookup.calledWith(CommonModels.M22_RANGE));
62 assert.isTrue(lookup.calledWith(CommonModels.G19_RANGE));
63 assert.equal(lookup.callCount, 2);
64 assert.isTrue(collateralLookup.notCalled);
65 });
66
67 it("should handle grids", function () {
68 const one = Grid.from([[false], [true]]);
69 const two = Grid.from([[false], [false]]);
70 const three = Grid.from([[false], [false]]);
71 assert.deepEqual(XOR.SELF.run(null, one, two), true);
72 assert.deepEqual(XOR.SELF.run(null, two, three), false);
73 assert.deepEqual(XOR.SELF.run(null, three), false);
74 });
75
76 it("should return error when argument lengths are wrong", function () {
77 assert.deepEqual((XOR.SELF.run(null) as NAException).name, F7ExceptionName.NA);
78 });
79});