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/ANDTest.ts
-rw-r--r--
3202
 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 { AND } from "../../../../main/js/formulas/logic/AND";
 9import { Grid } from "../../../../main/js/models/common/Grid";
10import { CollateralLookupFunction, LookupFunction } from "../../../../main/js/models/common/Types";
11import { Converters } from "../../../../main/js/utils/Converters";
12import { CommonModels } from "../../testutils/CommonModels";
13
14describe("AND", function () {
15  it("should work with numbers", function () {
16    assert.equal(AND.SELF.run(null, 10, 10), true);
17    assert.equal(AND.SELF.run(null, 10, 0), false);
18    assert.equal(AND.SELF.run(null, 1, 1, 1, 1, 1, 1), true);
19  });
20
21  it("should work with strings", function () {
22    assert.equal(AND.SELF.run(null, 10, "TRUE", true), true);
23    assert.equal(AND.SELF.run(null, 10, "true", true), true);
24    assert.equal(AND.SELF.run(null, 10, "FALSE", true), false);
25    assert.equal(AND.SELF.run(null, 10, "false", true), false);
26  });
27
28  it("should work with booleans", function () {
29    assert.equal(AND.SELF.run(null, true, true, true), true);
30    assert.equal(AND.SELF.run(null, true, true, false), false);
31    assert.equal(AND.SELF.run(null, false, false, false), false);
32  });
33
34  it("should work with null/blank/empty", function () {
35    assert.equal(
36      Converters.castAsF7Exception(AND.SELF.run(null, null, null)).name,
37      F7ExceptionName.VALUE
38    );
39    assert.equal(AND.SELF.run(null, null, null, true), true);
40  });
41
42  it("should do pass-through errors", function () {
43    assert.equal(
44      (AND.SELF.run(null, 4.4444, new ValueException()) as F7Exception).name,
45      F7ExceptionName.VALUE
46    );
47  });
48
49  it("should use lookup", function () {
50    const lookup = stub();
51    const collateralLookup = stub();
52    const F = new AND(lookup as LookupFunction, collateralLookup as CollateralLookupFunction);
53    lookup.withArgs(CommonModels.M22_RANGE).returns(true);
54    lookup.withArgs(CommonModels.G19_RANGE).returns(false);
55    assert.deepEqual(F.run(CommonModels.A1, CommonModels.M22_RANGE, CommonModels.G19_RANGE), false);
56    assert.isTrue(lookup.calledWith(CommonModels.M22_RANGE));
57    assert.isTrue(lookup.calledWith(CommonModels.G19_RANGE));
58    assert.equal(lookup.callCount, 2);
59    assert.isTrue(collateralLookup.notCalled);
60  });
61
62  it("should handle grids", function () {
63    const one = Grid.builder().add(0, 0, 44.0).add(0, 1, true).build();
64    const two = Grid.builder().add(0, 0, 44.0).add(0, 1, true).build();
65    const three = Grid.builder().add(0, 0, 44.0).add(0, 1, false).build();
66    assert.deepEqual(AND.SELF.run(null, one, two), true);
67    assert.deepEqual(AND.SELF.run(null, two, three), false);
68  });
69
70  it("should return error when argument lengths are wrong", function () {
71    assert.deepEqual((AND.SELF.run(null) as NAException).name, F7ExceptionName.NA);
72  });
73});