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