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