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