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