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/EQTest.ts
-rw-r--r--
2666
 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 { EQ } from "../../../../main/js/formulas/logic/EQ";
 9import { Grid } from "../../../../main/js/models/common/Grid";
10import { CollateralLookupFunction, LookupFunction } from "../../../../main/js/models/common/Types";
11import { CommonModels } from "../../testutils/CommonModels";
12
13describe("EQ", function () {
14  it("should do same type equality", function () {
15    assert.equal(EQ.SELF.run(null, 10.0, 10.0), true);
16    assert.equal(EQ.SELF.run(null, 10.0, 0.0), false);
17    assert.equal(EQ.SELF.run(null, "Same", "Same"), true);
18    assert.equal(EQ.SELF.run(null, "Same", "Different"), false);
19    assert.equal(EQ.SELF.run(null, true, true), true);
20    assert.equal(EQ.SELF.run(null, true, false), false);
21    assert.equal(EQ.SELF.run(null, false, false), true);
22  });
23
24  it("should do pass-through errors", function () {
25    assert.equal(
26      (EQ.SELF.run(null, 4.4444, new ValueException()) as F7Exception).name,
27      F7ExceptionName.VALUE
28    );
29  });
30
31  it("should use lookup", function () {
32    const lookup = stub();
33    const collateralLookup = stub();
34    const F = new EQ(lookup as LookupFunction, collateralLookup as CollateralLookupFunction);
35    collateralLookup.withArgs(CommonModels.A1, CommonModels.M22_RANGE).returns("A");
36    collateralLookup.withArgs(CommonModels.A1, CommonModels.G19_RANGE).returns("B");
37    assert.deepEqual(F.run(CommonModels.A1, CommonModels.M22_RANGE, CommonModels.G19_RANGE), false);
38    assert.isTrue(collateralLookup.calledWith(CommonModels.A1, CommonModels.M22_RANGE));
39    assert.isTrue(collateralLookup.calledWith(CommonModels.A1, CommonModels.G19_RANGE));
40    assert.equal(collateralLookup.callCount, 2);
41    assert.isTrue(lookup.notCalled);
42  });
43
44  it("should handle grids", function () {
45    const one = Grid.builder().add(0, 0, 44.0).add(0, 1, "A").build();
46    const two = Grid.builder().add(0, 0, 44.0).add(0, 1, "B").build();
47    assert.deepEqual(EQ.SELF.run(null, one, two), true);
48  });
49
50  it("should return error when argument lengths are wrong", function () {
51    assert.deepEqual((EQ.SELF.run(null, "Too few") as NAException).name, F7ExceptionName.NA);
52    assert.deepEqual(
53      (EQ.SELF.run(null, "A", "B", "Too many") as NAException).name,
54      F7ExceptionName.NA
55    );
56  });
57});