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/info/TYPETest.ts
-rw-r--r--
2755
 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 { TYPE } from "../../../../main/js/formulas/info/TYPE";
 9import { Grid } from "../../../../main/js/models/common/Grid";
10import {
11  CollateralLookupFunction,
12  Computed,
13  LookupFunction,
14  Primitive,
15} from "../../../../main/js/models/common/Types";
16import { CommonModels } from "../../testutils/CommonModels";
17
18describe("TYPE", function () {
19  it("should work with numbers", function () {
20    assert.deepEqual(TYPE.SELF.run(null, 10), 1);
21  });
22
23  it("should work with strings", function () {
24    assert.deepEqual(TYPE.SELF.run(null, "String"), 2);
25  });
26
27  it("should work with booleans", function () {
28    assert.deepEqual(TYPE.SELF.run(null, true), 4);
29    assert.deepEqual(TYPE.SELF.run(null, false), 4);
30  });
31
32  it("should work with errors", function () {
33    assert.deepEqual(TYPE.SELF.run(null, new NAException()), 16);
34    assert.deepEqual(TYPE.SELF.run(null, new DivException()), 16);
35  });
36
37  it("should work with blank/null/empty", function () {
38    assert.deepEqual(TYPE.SELF.run(null, null), 1);
39  });
40
41  it("should work with lookup", function () {
42    const lookup = stub();
43    const collateralLookup = stub();
44    const F = new TYPE(lookup as LookupFunction, collateralLookup as CollateralLookupFunction);
45    collateralLookup.withArgs(CommonModels.A1, CommonModels.M22_RANGE).returns(10);
46    assert.deepEqual(F.run(CommonModels.A1, CommonModels.M22_RANGE), 1);
47    assert.isTrue(collateralLookup.calledWith(CommonModels.A1, CommonModels.M22_RANGE));
48    assert.equal(collateralLookup.callCount, 1);
49    assert.isTrue(lookup.notCalled);
50  });
51
52  it("should work with grid", function () {
53    assert.deepEqual(TYPE.SELF.run(null, Grid.from<Primitive>([["Nope"]])), 2);
54    assert.deepEqual(TYPE.SELF.run(null, Grid.from<Primitive>([[22]])), 1);
55    assert.deepEqual(TYPE.SELF.run(null, Grid.from<Primitive>([[true]])), 4);
56    assert.deepEqual(TYPE.SELF.run(null, Grid.from<Computed>([[new DivException()]])), 16);
57    const multiGrid = Grid.from<Primitive>([[true], ["Other"]]);
58    assert.deepEqual(TYPE.SELF.run(null, multiGrid), 64);
59  });
60
61  it("should return error when arguments are not of the correct length", function () {
62    assert.deepEqual((TYPE.SELF.run(null) as F7Exception).name, F7ExceptionName.NA);
63    assert.deepEqual((TYPE.SELF.run(null, "A", "B") as F7Exception).name, F7ExceptionName.NA);
64  });
65});