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