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/ROUNDUPTest.ts
-rw-r--r--
4583
 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 { ROUNDUP } from "../../../../main/js/formulas/math/ROUNDUP";
 8import { Grid } from "../../../../main/js/models/common/Grid";
 9import { CollateralLookupFunction, LookupFunction } from "../../../../main/js/models/common/Types";
10import { CommonModels } from "../../testutils/CommonModels";
11
12describe("ROUNDUP", function () {
13  it("should work with whole numbers with places defaulting to 0", function () {
14    assert.deepEqual(ROUNDUP.SELF.run(null, 10), 10);
15    assert.deepEqual(ROUNDUP.SELF.run(null, -10), -10);
16    assert.deepEqual(ROUNDUP.SELF.run(null, 0), 0);
17  });
18
19  it("should round down decimals to full with places defaulting to 0", function () {
20    assert.deepEqual(ROUNDUP.SELF.run(null, 3.218639128), 4);
21    assert.deepEqual(ROUNDUP.SELF.run(null, 7.99), 8);
22    assert.deepEqual(ROUNDUP.SELF.run(null, 7.0000000001), 8);
23    assert.deepEqual(ROUNDUP.SELF.run(null, -4.444444), -5);
24  });
25
26  it("should round up to place", function () {
27    assert.deepEqual(ROUNDUP.SELF.run(null, 3.218639128, 4), 3.2187);
28    assert.deepEqual(ROUNDUP.SELF.run(null, 3.218639128, 5), 3.21864);
29    assert.deepEqual(ROUNDUP.SELF.run(null, 9.12, 0), 10);
30    assert.deepEqual(ROUNDUP.SELF.run(null, 9.12, 1), 9.2);
31    assert.deepEqual(ROUNDUP.SELF.run(null, 9.12, 2), 9.12);
32    assert.deepEqual(ROUNDUP.SELF.run(null, 9.12, 3), 9.12);
33    assert.deepEqual(ROUNDUP.SELF.run(null, 9.12, 8), 9.12);
34  });
35
36  it("should round up to the negative place", function () {
37    assert.deepEqual(ROUNDUP.SELF.run(null, 3341.218639128, 0), 3342);
38    assert.deepEqual(ROUNDUP.SELF.run(null, 3341.218639128, -1), 3350);
39    assert.deepEqual(ROUNDUP.SELF.run(null, 3341.218639128, -2), 3400);
40    assert.deepEqual(ROUNDUP.SELF.run(null, 3341.218639128, -3), 4000);
41    assert.deepEqual(ROUNDUP.SELF.run(null, 3341.218639128, -4), 10000);
42    assert.deepEqual(ROUNDUP.SELF.run(null, -3341.839749239, 0), -3342);
43    assert.deepEqual(ROUNDUP.SELF.run(null, -3341.839749239, -1), -3350);
44    assert.deepEqual(ROUNDUP.SELF.run(null, -3341.839749239, -2), -3400);
45    assert.deepEqual(ROUNDUP.SELF.run(null, -3341.839749239, -3), -4000);
46    assert.deepEqual(ROUNDUP.SELF.run(null, -3341.839749239, -4), -10000);
47  });
48
49  it("should round to non-integer places", function () {
50    assert.deepEqual(ROUNDUP.SELF.run(null, 3341.218639128, -4.99), 10000.0);
51    assert.deepEqual(ROUNDUP.SELF.run(null, 3341.218639128, -3.999999), 4000.0);
52    assert.deepEqual(ROUNDUP.SELF.run(null, 3341.218639128, -2.1), 3400.0);
53    assert.deepEqual(ROUNDUP.SELF.run(null, 3341.218639128, -1.1982371982), 3350.0);
54    assert.deepEqual(ROUNDUP.SELF.run(null, 3341.218639128, 0.0), 3342.0);
55    assert.deepEqual(ROUNDUP.SELF.run(null, 3341.218639128, 0.999), 3342.0);
56    assert.deepEqual(ROUNDUP.SELF.run(null, 3341.218639128, -0.999), 3342.0);
57    assert.deepEqual(ROUNDUP.SELF.run(null, 3341.218639128, 1.111), 3341.3);
58    assert.deepEqual(ROUNDUP.SELF.run(null, 3341.218639128, -1.111), 3350.0);
59  });
60
61  it("should work with strings", function () {
62    assert.deepEqual(ROUNDUP.SELF.run(null, "9"), 9);
63  });
64
65  it("should do pass-through errors", function () {
66    assert.deepEqual(ROUNDUP.SELF.run(null, new ValueException()), new ValueException());
67  });
68
69  it("should use lookup", function () {
70    const lookup = stub();
71    const collateralLookup = stub();
72    const F = new ROUNDUP(lookup as LookupFunction, collateralLookup as CollateralLookupFunction);
73    collateralLookup.withArgs(CommonModels.A1, CommonModels.M22_RANGE).returns(1.1);
74    assert.deepEqual(F.run(CommonModels.A1, CommonModels.M22_RANGE), 2);
75    assert.isTrue(collateralLookup.calledWith(CommonModels.A1, CommonModels.M22_RANGE));
76    assert.equal(collateralLookup.callCount, 1);
77    assert.isTrue(lookup.notCalled);
78  });
79
80  it("should handle grids", function () {
81    const one = Grid.builder().add(0, 0, 1.1).add(0, 1, "A").build();
82    assert.deepEqual(ROUNDUP.SELF.run(null, one), 2);
83  });
84
85  it("should return error when argument lengths are wrong", function () {
86    assert.deepEqual((ROUNDUP.SELF.run(null) as NAException).name, F7ExceptionName.NA);
87    assert.deepEqual(
88      (ROUNDUP.SELF.run(null, "A", "B", "C") as NAException).name,
89      F7ExceptionName.NA
90    );
91  });
92});