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/CodeExecutorGeneralAdditionTest.ts
-rw-r--r--
3138
 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 Addition Test", function () {
10  it("should work with numbers", function () {
11    assert.deepEqual(run("1"), 1.0);
12    assert.deepEqual(run("10 + 10"), 20.0);
13    assert.deepEqual(run("0 + 0"), 0.0);
14    assert.deepEqual(run("10 + -10"), 0.0);
15    assert.deepEqual(run("-10 + -10"), -20.0);
16    assert.deepEqual(run("1e10 + 1.1"), 1.00000000011e10);
17  });
18
19  it("should work with booleans", function () {
20    assert.deepEqual(run("9 + TRUE"), 10.0);
21    assert.deepEqual(run("9 + FALSE"), 9.0);
22    assert.deepEqual(run("9 + -TRUE"), 8.0);
23    assert.deepEqual(run("9 + -FALSE"), 9.0);
24  });
25
26  it("should work with strings", function () {
27    assert.deepEqual(run('9 + "10"'), 19.0);
28    assert.deepEqual(run('9 + "1e10"'), 1.0000000009e10);
29    assert.deepEqual(run('9 + "0"'), 9.0);
30  });
31
32  it("should work with arrays", function () {
33    assert.deepEqual(run("2 + {1}"), 3.0);
34    assert.deepEqual(run("2 + {1, 44}"), 3.0);
35    assert.deepEqual(run('2 + -{1, "Ignore me."}'), 1.0);
36  });
37
38  it("should work with blanks", function () {
39    const lookup = stub();
40    const collateralLookup = stub();
41    collateralLookup.withArgs(CommonModels.A1, 2).returns(2);
42    collateralLookup.withArgs(CommonModels.A1, CommonModels.M44_RANGE).returns(null);
43    assert.deepEqual(runWithLookups("2 + M44", lookup, collateralLookup), 2);
44    assert.isTrue(collateralLookup.calledWith(CommonModels.A1, 2));
45    assert.isTrue(collateralLookup.calledWith(CommonModels.A1, CommonModels.M44_RANGE));
46    assert.equal(collateralLookup.callCount, 2);
47    assert.isTrue(lookup.notCalled);
48  });
49
50  it("should return errors when strings are not numbers", function () {
51    assert.deepEqual((run('9 + "No good."') as F7Exception).name, F7ExceptionName.VALUE);
52    // TODO/HACK: This is commented out because javascript parses numbers in a weird way. Still thinking this one through.
53    //assert.deepEqual((run("9 + \"10    10\"") as F7Exception).name, F7ExceptionName.VALUE);
54  });
55
56  it("should return errors when error literals are present", function () {
57    assert.deepEqual((run("3 + #NULL!") as F7Exception).name, F7ExceptionName.NULL);
58    assert.deepEqual((run("3 + #DIV/0!") as F7Exception).name, F7ExceptionName.DIV);
59    assert.deepEqual((run("3 + #VALUE!") as F7Exception).name, F7ExceptionName.VALUE);
60    assert.deepEqual((run("3 + #REF!") as F7Exception).name, F7ExceptionName.REF);
61    assert.deepEqual((run("3 + #NAME?") as F7Exception).name, F7ExceptionName.NAME);
62    assert.deepEqual((run("3 + #NUM!") as F7Exception).name, F7ExceptionName.NUM);
63    assert.deepEqual((run("3 + #N/A") as F7Exception).name, F7ExceptionName.NA);
64    assert.deepEqual((run("3 + #ERROR!") as F7Exception).name, F7ExceptionName.PARSE);
65  });
66});