name:
src/test/js/formulas/logic/IFTest.ts
-rw-r--r--
3810
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 { NumException } from "../../../../main/js/errors/NumException";
9import { IF } from "../../../../main/js/formulas/logic/IF";
10import { Grid } from "../../../../main/js/models/common/Grid";
11import {
12 CollateralLookupFunction,
13 LookupFunction,
14 Primitive,
15} from "../../../../main/js/models/common/Types";
16import { CommonModels } from "../../testutils/CommonModels";
17
18describe("IF", function () {
19 it("should work with simple comparisons", function () {
20 assert.deepEqual(IF.SELF.run(null, true, true, false), true);
21 assert.deepEqual(IF.SELF.run(null, false, true, false), false);
22 assert.deepEqual(IF.SELF.run(null, 10.0, "A", "B"), "A");
23 assert.deepEqual(IF.SELF.run(null, -1716.1, "A", "B"), "A");
24 assert.deepEqual(IF.SELF.run(null, "TRUE", "A", "B"), "A");
25 assert.deepEqual(IF.SELF.run(null, "", "A", "B"), "B");
26 });
27
28 it("should work with errors as two return arguments", function () {
29 assert.deepEqual(IF.SELF.run(null, true, 10.1, new DivException()), 10.1);
30 assert.deepEqual(
31 (IF.SELF.run(null, true, new NumException(), new DivException()) as F7Exception).name,
32 F7ExceptionName.NUM
33 );
34 assert.deepEqual(IF.SELF.run(null, false, new DivException(), 10.11), 10.11);
35 assert.deepEqual(
36 (IF.SELF.run(null, true, new DivException(), 10.11) as F7Exception).name,
37 F7ExceptionName.DIV
38 );
39 });
40
41 it("should use lookup for first value if true", function () {
42 const lookup = stub();
43 const collateralLookup = stub();
44 const F = new IF(lookup as LookupFunction, collateralLookup as CollateralLookupFunction);
45 collateralLookup.withArgs(CommonModels.A1, true).returns(true);
46 collateralLookup.withArgs(CommonModels.A1, CommonModels.M22_RANGE).returns(1000);
47 assert.deepEqual(
48 F.run(CommonModels.A1, true, CommonModels.M22_RANGE, CommonModels.G19_RANGE),
49 1000
50 );
51 assert.isTrue(collateralLookup.calledWith(CommonModels.A1, CommonModels.M22_RANGE));
52 assert.isTrue(collateralLookup.calledWith(CommonModels.A1, true));
53 assert.equal(collateralLookup.callCount, 2);
54 assert.isTrue(lookup.notCalled);
55 });
56
57 it("should use lookup for second value if false", function () {
58 const lookup = stub();
59 const collateralLookup = stub();
60 const F = new IF(lookup as LookupFunction, collateralLookup as CollateralLookupFunction);
61 collateralLookup.withArgs(CommonModels.A1, false).returns(false);
62 collateralLookup.withArgs(CommonModels.A1, CommonModels.G19_RANGE).returns(1000);
63 assert.deepEqual(
64 F.run(CommonModels.A1, false, CommonModels.M22_RANGE, CommonModels.G19_RANGE),
65 1000
66 );
67 assert.isTrue(collateralLookup.calledWith(CommonModels.A1, CommonModels.G19_RANGE));
68 assert.isTrue(collateralLookup.calledWith(CommonModels.A1, false));
69 assert.equal(collateralLookup.callCount, 2);
70 assert.isTrue(lookup.notCalled);
71 });
72
73 it("should handle grids", function () {
74 const one = Grid.from<Primitive>([[44], ["A"]]);
75 const two = Grid.from<Primitive>([[44], ["B"]]);
76 assert.deepEqual(IF.SELF.run(null, true, one, two), 44);
77 });
78
79 it("should return error when argument lengths are wrong", function () {
80 assert.deepEqual((IF.SELF.run(null, "A", "B") as NAException).name, F7ExceptionName.NA);
81 assert.deepEqual(
82 (IF.SELF.run(null, "A", "B", "C", "D") as NAException).name,
83 F7ExceptionName.NA
84 );
85 });
86});