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/parser/TO_PERCENTTest.ts
-rw-r--r--
2980
 1import { assert } from "chai";
 2import { it, describe } from "../../testutils/TestUtils";
 3import { stub } from "sinon";
 4import { DivException } from "../../../../main/js/errors/DivException";
 5import { F7Exception } from "../../../../main/js/errors/F7Exception";
 6import { F7ExceptionName } from "../../../../main/js/errors/F7ExceptionName";
 7import { NAException } from "../../../../main/js/errors/NAException";
 8import { TO_PERCENT } from "../../../../main/js/formulas/parser/TO_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("TO_PERCENT", function () {
14  it("should work with numbers", function () {
15    assert.deepEqual(TO_PERCENT.SELF.run(null, 10), 10);
16  });
17
18  it("should work with strings", function () {
19    assert.deepEqual(TO_PERCENT.SELF.run(null, "99"), 99);
20  });
21
22  it("should work with booleans", function () {
23    assert.deepEqual(TO_PERCENT.SELF.run(null, true), 1);
24    assert.deepEqual(TO_PERCENT.SELF.run(null, false), 0);
25  });
26
27  it("should work with errors", function () {
28    assert.deepEqual(
29      (TO_PERCENT.SELF.run(null, new NAException()) as F7Exception).name,
30      F7ExceptionName.NA
31    );
32    assert.deepEqual(
33      (TO_PERCENT.SELF.run(null, new DivException()) as F7Exception).name,
34      F7ExceptionName.DIV
35    );
36  });
37
38  it("should work with blank/null/empty", function () {
39    assert.deepEqual(TO_PERCENT.SELF.run(null, null), 0);
40  });
41
42  it("should work with lookup", function () {
43    const lookup = stub();
44    const collateralLookup = stub();
45    const F = new TO_PERCENT(
46      lookup as LookupFunction,
47      collateralLookup as CollateralLookupFunction
48    );
49    collateralLookup.withArgs(CommonModels.A1, CommonModels.M22_RANGE).returns(9.14101);
50    assert.deepEqual(F.run(CommonModels.A1, CommonModels.M22_RANGE), 9.14101);
51    assert.isTrue(collateralLookup.calledWith(CommonModels.A1, CommonModels.M22_RANGE));
52    assert.equal(collateralLookup.callCount, 1);
53    assert.isTrue(lookup.notCalled);
54  });
55
56  it("should work with grid", function () {
57    assert.deepEqual(TO_PERCENT.SELF.run(null, Grid.builder().add(0, 0, "1").build()), 1);
58    assert.deepEqual(TO_PERCENT.SELF.run(null, Grid.builder().add(0, 0, 22).build()), 22);
59    assert.deepEqual(TO_PERCENT.SELF.run(null, Grid.builder().add(0, 0, true).build()), 1);
60    assert.deepEqual(
61      (
62        TO_PERCENT.SELF.run(
63          null,
64          Grid.builder().add(0, 0, new DivException()).build()
65        ) as F7Exception
66      ).name,
67      F7ExceptionName.DIV
68    );
69  });
70
71  it("should return error when arguments are not of the correct length", function () {
72    assert.deepEqual((TO_PERCENT.SELF.run(null) as F7Exception).name, F7ExceptionName.NA);
73    assert.deepEqual((TO_PERCENT.SELF.run(null, "A", "B") as F7Exception).name, F7ExceptionName.NA);
74  });
75});