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