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/math/ABSTest.ts
-rw-r--r--
2066
 1import { assert } from "chai";
 2import { it, describe } from "../../testutils/TestUtils";
 3import { stub } from "sinon";
 4import { F7ExceptionName } from "../../../../main/js/errors/F7ExceptionName";
 5import { NAException } from "../../../../main/js/errors/NAException";
 6import { ValueException } from "../../../../main/js/errors/ValueException";
 7import { ABS } from "../../../../main/js/formulas/math/ABS";
 8import { Grid } from "../../../../main/js/models/common/Grid";
 9import { CollateralLookupFunction, LookupFunction } from "../../../../main/js/models/common/Types";
10import { CommonModels } from "../../testutils/CommonModels";
11
12describe("ABS", function () {
13  it("should work with numbers", function () {
14    assert.equal(ABS.SELF.run(null, 10), 10);
15    assert.equal(ABS.SELF.run(null, -10), 10);
16    assert.equal(ABS.SELF.run(null, -0), 0);
17  });
18
19  it("should work with strings", function () {
20    assert.equal(ABS.SELF.run(null, "10"), 10);
21  });
22
23  it("should do pass-through errors", function () {
24    assert.deepEqual(ABS.SELF.run(null, new ValueException()), new ValueException());
25  });
26
27  it("should use lookup", function () {
28    const lookup = stub();
29    const collateralLookup = stub();
30    const F = new ABS(lookup as LookupFunction, collateralLookup as CollateralLookupFunction);
31    collateralLookup.withArgs(CommonModels.A1, CommonModels.M22_RANGE).returns(-2);
32    assert.deepEqual(F.run(CommonModels.A1, CommonModels.M22_RANGE), 2);
33    assert.isTrue(collateralLookup.calledWith(CommonModels.A1, CommonModels.M22_RANGE));
34    assert.equal(collateralLookup.callCount, 1);
35    assert.isTrue(lookup.notCalled);
36  });
37
38  it("should handle grids", function () {
39    const one = Grid.builder().add(0, 0, -4).add(0, 1, "A").build();
40    assert.deepEqual(ABS.SELF.run(null, one), 4);
41  });
42
43  it("should return error when argument lengths are wrong", function () {
44    assert.deepEqual((ABS.SELF.run(null) as NAException).name, F7ExceptionName.NA);
45    assert.deepEqual((ABS.SELF.run(null, "A", "B") as NAException).name, F7ExceptionName.NA);
46  });
47});