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/execution/CodeExecutorGeneralDivisionTest.ts
-rw-r--r--
2878
 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 { CommonModels } from "../testutils/CommonModels";
 7import { run, runWithLookups } from "../testutils/TestUtils";
 8
 9describe("CodeExecutor - General Division Test", function () {
10  it("should work with numbers", function () {
11    assert.deepEqual(run("10 / 5"), 2.0);
12    assert.deepEqual(run("1 / 1"), 1.0);
13    assert.deepEqual(run("10 / -10"), -1.0);
14    assert.deepEqual(run("-10 / -2"), 5.0);
15    assert.deepEqual(run("1e10 / 1.1"), 9.09090909090909e9);
16  });
17
18  it("should work with booleans", function () {
19    assert.deepEqual(run("9 / TRUE"), 9.0);
20    assert.deepEqual((run("9 / FALSE") as F7Exception).name, F7ExceptionName.DIV);
21  });
22
23  it("should work with strings", function () {
24    assert.deepEqual(run('12 / "3"'), 4.0);
25    assert.deepEqual(run('12 / "3e1"'), 0.4);
26    assert.deepEqual(run('12 / "2"'), 6.0);
27  });
28
29  it("should work with arrays", function () {
30    assert.deepEqual(run("12 / {2}"), 6.0);
31    assert.deepEqual(run("12 / {2, 44}"), 6.0);
32    assert.deepEqual(run('12 / -{2, "Ignore me."}'), -6.0);
33  });
34
35  it("should return errors when strings are not numbers", function () {
36    assert.deepEqual((run('9 / "No good."') as F7Exception).name, F7ExceptionName.VALUE);
37  });
38
39  it("should work with blanks", function () {
40    const lookup = stub();
41    const collateralLookup = stub();
42    collateralLookup.withArgs(CommonModels.A1, 2).returns(2);
43    collateralLookup.withArgs(CommonModels.A1, CommonModels.M44_RANGE).returns(null);
44    assert.deepEqual(
45      (runWithLookups("2 / M44", lookup, collateralLookup) as F7Exception).name,
46      F7ExceptionName.DIV
47    );
48    assert.isTrue(collateralLookup.calledWith(CommonModels.A1, 2));
49    assert.isTrue(collateralLookup.calledWith(CommonModels.A1, CommonModels.M44_RANGE));
50    assert.equal(collateralLookup.callCount, 2);
51    assert.isTrue(lookup.notCalled);
52  });
53
54  it("should return errors when error literals are present", function () {
55    assert.deepEqual((run("3 / #NULL!") as F7Exception).name, F7ExceptionName.NULL);
56    assert.deepEqual((run("3 / #DIV/0!") as F7Exception).name, F7ExceptionName.DIV);
57    assert.deepEqual((run("3 / #VALUE!") as F7Exception).name, F7ExceptionName.VALUE);
58    assert.deepEqual((run("3 / #REF!") as F7Exception).name, F7ExceptionName.REF);
59    assert.deepEqual((run("3 / #NAME?") as F7Exception).name, F7ExceptionName.NAME);
60    assert.deepEqual((run("3 / #NUM!") as F7Exception).name, F7ExceptionName.NUM);
61    assert.deepEqual((run("3 / #N/A") as F7Exception).name, F7ExceptionName.NA);
62    assert.deepEqual((run("3 / #ERROR!") as F7Exception).name, F7ExceptionName.PARSE);
63  });
64});