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/IFNATest.ts
-rw-r--r--
2523
 1import { assert } from "chai";
 2import { it, describe } from "../../testutils/TestUtils";
 3import { stub } from "sinon";
 4import { DivException } from "../../../../main/js/errors/DivException";
 5import { F7ExceptionName } from "../../../../main/js/errors/F7ExceptionName";
 6import { NAException } from "../../../../main/js/errors/NAException";
 7import { IFNA } from "../../../../main/js/formulas/logic/IFNA";
 8import { Grid } from "../../../../main/js/models/common/Grid";
 9import {
10  CollateralLookupFunction,
11  LookupFunction,
12  Primitive,
13} from "../../../../main/js/models/common/Types";
14import { CommonModels } from "../../testutils/CommonModels";
15
16describe("IFNA", function () {
17  it("should work with plain types", function () {
18    assert.deepEqual(IFNA.SELF.run(null, true, false), true);
19    assert.deepEqual(IFNA.SELF.run(null, false, true), false);
20    assert.deepEqual(IFNA.SELF.run(null, 10, "A"), 10);
21    assert.deepEqual(IFNA.SELF.run(null, "TRUE", "A"), "TRUE");
22    assert.deepEqual(IFNA.SELF.run(null, "", "A"), "");
23  });
24
25  it("should work with error", function () {
26    assert.deepEqual(IFNA.SELF.run(null, new DivException(), 1), new DivException());
27    assert.deepEqual(IFNA.SELF.run(null, new NAException(), 1), 1);
28  });
29
30  it("should use lookup if error", function () {
31    const lookup = stub();
32    const collateralLookup = stub();
33    const F = new IFNA(lookup as LookupFunction, collateralLookup as CollateralLookupFunction);
34    collateralLookup.withArgs(CommonModels.A1, new NAException()).returns(new NAException());
35    collateralLookup.withArgs(CommonModels.A1, CommonModels.M22_RANGE).returns(1000);
36    assert.deepEqual(F.run(CommonModels.A1, new NAException(), CommonModels.M22_RANGE), 1000);
37    assert.isTrue(collateralLookup.calledWith(CommonModels.A1, new NAException()));
38    assert.isTrue(collateralLookup.calledWith(CommonModels.A1, CommonModels.M22_RANGE));
39    assert.equal(collateralLookup.callCount, 2);
40    assert.isTrue(lookup.notCalled);
41  });
42
43  it("should handle grids", function () {
44    const one = Grid.from<Primitive>([[44], ["A"]]);
45    assert.deepEqual(IFNA.SELF.run(null, new NAException(), one), one);
46    assert.deepEqual(IFNA.SELF.run(null, one, false), one);
47  });
48
49  it("should return error when argument lengths are wrong", function () {
50    assert.deepEqual((IFNA.SELF.run(null, "A") as NAException).name, F7ExceptionName.NA);
51    assert.deepEqual(
52      (IFNA.SELF.run(null, "A", "B", "C", "D") as NAException).name,
53      F7ExceptionName.NA
54    );
55  });
56});