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/math/ADDTest.ts
-rw-r--r--
2695
 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 { ValueException } from "../../../../main/js/errors/ValueException";
 7import { ADD } from "../../../../main/js/formulas/math/ADD";
 8import { Grid } from "../../../../main/js/models/common/Grid";
 9import { CollateralLookupFunction, LookupFunction } from "../../../../main/js/models/common/Types";
10import { CommonModels } from "../../testutils/CommonModels";
11
12describe("ADD", function () {
13  it("should do normal operations", function () {
14    assert.equal(ADD.SELF.run(null, 10, 10), 20);
15    assert.equal(ADD.SELF.run(null, 10, 2), 12);
16    assert.equal(ADD.SELF.run(null, 0, 1628736813.2), 1628736813.2);
17    assert.equal(ADD.SELF.run(null, 218637221.22, 2876.111), 218640097.331);
18  });
19
20  it("should do string conversion", function () {
21    assert.equal(ADD.SELF.run(null, "10", "10"), 20);
22    assert.equal(ADD.SELF.run(null, "10", "2"), 12);
23    assert.equal(ADD.SELF.run(null, "10", "3"), 13);
24  });
25
26  it("should do pass-through errors", function () {
27    assert.deepEqual(ADD.SELF.run(null, 10, new ValueException()), new ValueException());
28    assert.deepEqual(ADD.SELF.run(null, new ValueException(), 10), new ValueException());
29  });
30
31  it("should use lookup", function () {
32    const lookup = stub();
33    const collateralLookup = stub();
34    const F = new ADD(lookup as LookupFunction, collateralLookup as CollateralLookupFunction);
35    collateralLookup.withArgs(CommonModels.A1, CommonModels.M22_RANGE).returns(2);
36    collateralLookup.withArgs(CommonModels.A1, CommonModels.G19_RANGE).returns(4);
37    assert.deepEqual(F.run(CommonModels.A1, CommonModels.M22_RANGE, CommonModels.G19_RANGE), 6);
38    assert.isTrue(collateralLookup.calledWith(CommonModels.A1, CommonModels.M22_RANGE));
39    assert.isTrue(collateralLookup.calledWith(CommonModels.A1, CommonModels.G19_RANGE));
40    assert.equal(collateralLookup.callCount, 2);
41    assert.isTrue(lookup.notCalled);
42  });
43
44  it("should handle grids", function () {
45    const one = Grid.builder().add(0, 0, 4).add(0, 1, "A").build();
46    const two = Grid.builder().add(0, 0, 10).add(0, 1, "B").build();
47    assert.deepEqual(ADD.SELF.run(null, one, two), 14);
48  });
49
50  it("should return error when argument lengths are wrong", function () {
51    assert.deepEqual((ADD.SELF.run(null, "Too few") as NAException).name, F7ExceptionName.NA);
52    assert.deepEqual(
53      (ADD.SELF.run(null, "A", "B", "Too many") as NAException).name,
54      F7ExceptionName.NA
55    );
56  });
57});