name:
src/test/js/formulas/logic/IFERRORTest.ts
-rw-r--r--
2491
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 { IFERROR } from "../../../../main/js/formulas/logic/IFERROR";
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("IFERROR", function () {
17 it("should work with plain types", function () {
18 assert.deepEqual(IFERROR.SELF.run(null, true, false), true);
19 assert.deepEqual(IFERROR.SELF.run(null, false, true), false);
20 assert.deepEqual(IFERROR.SELF.run(null, 10, "A"), 10);
21 assert.deepEqual(IFERROR.SELF.run(null, "TRUE", "A"), "TRUE");
22 assert.deepEqual(IFERROR.SELF.run(null, "", "A"), "");
23 });
24
25 it("should work with error", function () {
26 assert.deepEqual(IFERROR.SELF.run(null, new DivException(), 10.1), 10.1);
27 });
28
29 it("should use lookup if error", function () {
30 const lookup = stub();
31 const collateralLookup = stub();
32 const F = new IFERROR(lookup as LookupFunction, collateralLookup as CollateralLookupFunction);
33 collateralLookup.withArgs(CommonModels.A1, new DivException()).returns(new DivException());
34 collateralLookup.withArgs(CommonModels.A1, CommonModels.M22_RANGE).returns(1000);
35 assert.deepEqual(F.run(CommonModels.A1, new DivException(), CommonModels.M22_RANGE), 1000);
36 assert.isTrue(collateralLookup.calledWith(CommonModels.A1, new DivException()));
37 assert.isTrue(collateralLookup.calledWith(CommonModels.A1, CommonModels.M22_RANGE));
38 assert.equal(collateralLookup.callCount, 2);
39 assert.isTrue(lookup.notCalled);
40 });
41
42 it("should handle grids", function () {
43 const one = Grid.from<Primitive>([[44], ["A"]]);
44 assert.deepEqual(IFERROR.SELF.run(null, new DivException(), one), one);
45 assert.deepEqual(IFERROR.SELF.run(null, one, false), one);
46 });
47
48 it("should return error when argument lengths are wrong", function () {
49 assert.deepEqual((IFERROR.SELF.run(null, "A") as NAException).name, F7ExceptionName.NA);
50 assert.deepEqual(
51 (IFERROR.SELF.run(null, "A", "B", "C", "D") as NAException).name,
52 F7ExceptionName.NA
53 );
54 });
55});