name:
src/test/js/formulas/math/DIVIDETest.ts
-rw-r--r--
3261
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 { DIVIDE } from "../../../../main/js/formulas/math/DIVIDE";
9import { Grid } from "../../../../main/js/models/common/Grid";
10import { CollateralLookupFunction, LookupFunction } from "../../../../main/js/models/common/Types";
11import { CommonModels } from "../../testutils/CommonModels";
12
13describe("DIVIDE", function () {
14 it("should do normal operations", function () {
15 assert.deepEqual(DIVIDE.SELF.run(null, 10, 10), 1);
16 assert.deepEqual(DIVIDE.SELF.run(null, 10, 2), 5);
17 assert.deepEqual(DIVIDE.SELF.run(null, 10, 3), 3.3333333333333335);
18 assert.deepEqual(DIVIDE.SELF.run(null, 0, 1628736813.2), 0);
19 assert.deepEqual(DIVIDE.SELF.run(null, 218637221.22, 2876.111), 76018.35298429025);
20 });
21
22 it("should do string conversion", function () {
23 assert.deepEqual(DIVIDE.SELF.run(null, "10", "10"), 1);
24 assert.deepEqual(DIVIDE.SELF.run(null, "10", "2"), 5);
25 assert.deepEqual(DIVIDE.SELF.run(null, "10", "3"), 3.3333333333333335);
26 });
27
28 it("should do pass-through errors", function () {
29 assert.deepEqual(
30 (DIVIDE.SELF.run(null, 10, new ValueException()) as F7Exception).name,
31 F7ExceptionName.VALUE
32 );
33 assert.deepEqual(
34 (DIVIDE.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 DIVIDE(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), 3);
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, 3).add(0, 1, "B").build();
55 assert.deepEqual(DIVIDE.SELF.run(null, one, two), 2);
56 });
57
58 it("should throw error when dividing by zero", function () {
59 assert.deepEqual((DIVIDE.SELF.run(null, 328467, 0) as F7Exception).name, F7ExceptionName.DIV);
60 assert.deepEqual((DIVIDE.SELF.run(null, 0, 0) as F7Exception).name, F7ExceptionName.DIV);
61 });
62
63 it("should return error when argument lengths are wrong", function () {
64 assert.deepEqual((DIVIDE.SELF.run(null, "Too few") as NAException).name, F7ExceptionName.NA);
65 assert.deepEqual(
66 (DIVIDE.SELF.run(null, "A", "B", "Too many") as NAException).name,
67 F7ExceptionName.NA
68 );
69 });
70});