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/engineering/DELTATest.ts
-rw-r--r--
2683
 1import { assert } from "chai";
 2import { it, describe } from "../../testutils/TestUtils";
 3import { stub } from "sinon";
 4import { F7ExceptionName } from "../../../../main/js/errors/F7ExceptionName";
 5import { NAException } from "../../../../main/js/errors/NAException";
 6import { DELTA } from "../../../../main/js/formulas/engineering/DELTA";
 7import { Grid } from "../../../../main/js/models/common/Grid";
 8import { CollateralLookupFunction, LookupFunction } from "../../../../main/js/models/common/Types";
 9import { CommonModels } from "../../testutils/CommonModels";
10import { assertF7ExceptionByName } from "../../testutils/TestUtils";
11
12describe("DELTA", function () {
13  it("should work with plain numbers", function () {
14    assert.deepEqual(DELTA.SELF.run(null, 1, 2), 0);
15    assert.deepEqual(DELTA.SELF.run(null, 2, 2), 1);
16    assert.deepEqual(DELTA.SELF.run(null, 8762, 8762), 1);
17  });
18
19  it("should work with string representations of numbers", function () {
20    assert.deepEqual(DELTA.SELF.run(null, 1, "2"), 0);
21    assert.deepEqual(DELTA.SELF.run(null, 2, "2"), 1);
22  });
23
24  it("should work with booleans", function () {
25    assert.deepEqual(DELTA.SELF.run(null, true, true), 1);
26    assert.deepEqual(DELTA.SELF.run(null, false, false), 1);
27    assert.deepEqual(DELTA.SELF.run(null, false, true), 0);
28  });
29
30  it("should return VALUE error when called with strings that are not numbers", function () {
31    assertF7ExceptionByName(
32      DELTA.SELF.run(null, 1, "You have no good car ideas."),
33      F7ExceptionName.VALUE
34    );
35  });
36
37  it("should use lookup", function () {
38    const lookup = stub();
39    const collateralLookup = stub();
40    const F = new DELTA(lookup as LookupFunction, collateralLookup as CollateralLookupFunction);
41    collateralLookup.withArgs(CommonModels.A1, CommonModels.M22_RANGE).returns(8762);
42    collateralLookup.withArgs(CommonModels.A1, CommonModels.G19_RANGE).returns(8762);
43    assert.deepEqual(F.run(CommonModels.A1, CommonModels.M22_RANGE, CommonModels.G19_RANGE), 1);
44    assert.isTrue(collateralLookup.calledWith(CommonModels.A1, CommonModels.M22_RANGE));
45    assert.isTrue(collateralLookup.calledWith(CommonModels.A1, CommonModels.G19_RANGE));
46    assert.equal(collateralLookup.callCount, 2);
47    assert.isTrue(lookup.notCalled);
48  });
49
50  it("should handle grids", function () {
51    assert.deepEqual(DELTA.SELF.run(null, Grid.from([[8762], [10]]), 8762), 1);
52  });
53
54  it("should return error when argument lengths are wrong", function () {
55    assert.deepEqual((DELTA.SELF.run(null) as NAException).name, F7ExceptionName.NA);
56    assert.deepEqual((DELTA.SELF.run(null, "A", "B", "C") as NAException).name, F7ExceptionName.NA);
57  });
58});