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