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_TEXTTest.ts
-rw-r--r--
2919
 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_TEXT } from "../../../../main/js/formulas/parser/TO_TEXT";
 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_TEXT", function () {
14  it("should work with numbers", function () {
15    assert.deepEqual(TO_TEXT.SELF.run(null, 10), "10");
16  });
17
18  it("should work with strings", function () {
19    assert.deepEqual(TO_TEXT.SELF.run(null, "String"), "String");
20  });
21
22  it("should work with booleans", function () {
23    assert.deepEqual(TO_TEXT.SELF.run(null, true), "TRUE");
24    assert.deepEqual(TO_TEXT.SELF.run(null, false), "FALSE");
25  });
26
27  it("should work with errors", function () {
28    assert.deepEqual(
29      (TO_TEXT.SELF.run(null, new NAException()) as F7Exception).name,
30      F7ExceptionName.NA
31    );
32    assert.deepEqual(
33      (TO_TEXT.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_TEXT.SELF.run(null, null), "");
40  });
41
42  it("should work with lookup", function () {
43    const lookup = stub();
44    const collateralLookup = stub();
45    const F = new TO_TEXT(lookup as LookupFunction, collateralLookup as CollateralLookupFunction);
46    collateralLookup.withArgs(CommonModels.A1, CommonModels.M22_RANGE).returns(9.14101);
47    assert.deepEqual(F.run(CommonModels.A1, CommonModels.M22_RANGE), "9.14101");
48    assert.isTrue(collateralLookup.calledWith(CommonModels.A1, CommonModels.M22_RANGE));
49    assert.equal(collateralLookup.callCount, 1);
50    assert.isTrue(lookup.notCalled);
51  });
52
53  it("should work with grid", function () {
54    assert.deepEqual(TO_TEXT.SELF.run(null, Grid.builder().add(0, 0, "String").build()), "String");
55    assert.deepEqual(TO_TEXT.SELF.run(null, Grid.builder().add(0, 0, 22).build()), "22");
56    assert.deepEqual(TO_TEXT.SELF.run(null, Grid.builder().add(0, 0, true).build()), "TRUE");
57    assert.deepEqual(
58      (TO_TEXT.SELF.run(null, Grid.builder().add(0, 0, new DivException()).build()) as F7Exception)
59        .name,
60      F7ExceptionName.DIV
61    );
62  });
63
64  it("should return error when arguments are not of the correct length", function () {
65    assert.deepEqual((TO_TEXT.SELF.run(null) as F7Exception).name, F7ExceptionName.NA);
66    assert.deepEqual((TO_TEXT.SELF.run(null, "A", "B") as F7Exception).name, F7ExceptionName.NA);
67  });
68});