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/logic/NETest.ts
-rw-r--r--
4503
 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 { NE } from "../../../../main/js/formulas/logic/NE";
 9import { Grid } from "../../../../main/js/models/common/Grid";
10import {
11  CollateralLookupFunction,
12  LookupFunction,
13  Primitive,
14} from "../../../../main/js/models/common/Types";
15import { CommonModels } from "../../testutils/CommonModels";
16
17describe("NE", function () {
18  it("should compare strings", function () {
19    assert.deepEqual(NE.SELF.run(null, "Hello", "Diff"), true);
20    assert.deepEqual(NE.SELF.run(null, "a", "a"), false);
21    assert.deepEqual(NE.SELF.run(null, "a", "aa"), true);
22    assert.deepEqual(NE.SELF.run(null, "aa", "a"), true);
23    assert.deepEqual(NE.SELF.run(null, "a", "A"), false);
24    assert.deepEqual(NE.SELF.run(null, "A", "a"), false);
25    assert.deepEqual(NE.SELF.run(null, "A", "A"), false);
26    assert.deepEqual(NE.SELF.run(null, "Aa", "A"), true);
27    assert.deepEqual(NE.SELF.run(null, "AA", "a"), true);
28    assert.deepEqual(NE.SELF.run(null, "aA", "a"), true);
29    assert.deepEqual(NE.SELF.run(null, "aA", "A"), true);
30    assert.deepEqual(NE.SELF.run(null, "押", "し"), true);
31    assert.deepEqual(NE.SELF.run(null, "し", "押"), true);
32    assert.deepEqual(NE.SELF.run(null, "String", 129321321.0), true);
33    assert.deepEqual(NE.SELF.run(null, 129321321.0, "String"), true);
34  });
35
36  it("should compare numbers", function () {
37    assert.deepEqual(NE.SELF.run(null, 1.0, 1.0), false);
38    assert.deepEqual(NE.SELF.run(null, 1.0, 0.0), true);
39    assert.deepEqual(NE.SELF.run(null, 0.0, 1.0), true);
40    assert.deepEqual(NE.SELF.run(null, 0.0, 0.0), false);
41  });
42
43  it("should compare booleans", function () {
44    assert.deepEqual(NE.SELF.run(null, true, false), true);
45    assert.deepEqual(NE.SELF.run(null, false, true), true);
46    assert.deepEqual(NE.SELF.run(null, true, true), false);
47    assert.deepEqual(NE.SELF.run(null, false, false), false);
48    assert.deepEqual(NE.SELF.run(null, true, "String"), true);
49    assert.deepEqual(NE.SELF.run(null, "String", true), true);
50  });
51
52  it("should do pass-through errors", function () {
53    assert.equal(
54      (NE.SELF.run(null, 4.4444, new ValueException()) as F7Exception).name,
55      F7ExceptionName.VALUE
56    );
57  });
58
59  it("should compare across types using type precedence", function () {
60    assert.deepEqual(NE.SELF.run(null, "a", 0.0), true);
61    assert.deepEqual(NE.SELF.run(null, 0.0, "a"), true);
62    assert.deepEqual(NE.SELF.run(null, true, 0.0), true);
63    assert.deepEqual(NE.SELF.run(null, false, 0.0), true);
64    assert.deepEqual(NE.SELF.run(null, 0.0, true), true);
65    assert.deepEqual(NE.SELF.run(null, 0.0, false), true);
66    assert.deepEqual(NE.SELF.run(null, true, "a"), true);
67    assert.deepEqual(NE.SELF.run(null, false, "a"), true);
68    assert.deepEqual(NE.SELF.run(null, "a", true), true);
69    assert.deepEqual(NE.SELF.run(null, "a", false), true);
70  });
71
72  it("should use lookup", function () {
73    const lookup = stub();
74    const collateralLookup = stub();
75    const F = new NE(lookup as LookupFunction, collateralLookup as CollateralLookupFunction);
76    collateralLookup.withArgs(CommonModels.A1, CommonModels.M22_RANGE).returns(2);
77    collateralLookup.withArgs(CommonModels.A1, CommonModels.G19_RANGE).returns(4);
78    assert.deepEqual(F.run(CommonModels.A1, CommonModels.M22_RANGE, CommonModels.G19_RANGE), true);
79    assert.isTrue(collateralLookup.calledWith(CommonModels.A1, CommonModels.M22_RANGE));
80    assert.isTrue(collateralLookup.calledWith(CommonModels.A1, CommonModels.G19_RANGE));
81    assert.equal(collateralLookup.callCount, 2);
82    assert.isTrue(lookup.notCalled);
83  });
84
85  it("should handle grids", function () {
86    const one = Grid.from<Primitive>([[4], ["A"]]);
87    const two = Grid.from<Primitive>([[10], ["B"]]);
88    assert.deepEqual(NE.SELF.run(null, one, two), true);
89  });
90
91  it("should return error when argument lenNEhs are wrong", function () {
92    assert.deepEqual((NE.SELF.run(null, "Too few") as NAException).name, F7ExceptionName.NA);
93    assert.deepEqual(
94      (NE.SELF.run(null, "A", "B", "Too many") as NAException).name,
95      F7ExceptionName.NA
96    );
97  });
98});