name:
src/test/js/formulas/math/EVENTest.ts
-rw-r--r--
2979
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 { EVEN } from "../../../../main/js/formulas/math/EVEN";
8import { Grid } from "../../../../main/js/models/common/Grid";
9import { CollateralLookupFunction, LookupFunction } from "../../../../main/js/models/common/Types";
10import { CommonModels } from "../../testutils/CommonModels";
11
12describe("EVEN", function () {
13 it("should work with positive numbers", function () {
14 assert.deepEqual(EVEN.SELF.run(null, 2), 2);
15 assert.deepEqual(EVEN.SELF.run(null, 1), 2);
16 assert.deepEqual(EVEN.SELF.run(null, 1.1), 2);
17 assert.deepEqual(EVEN.SELF.run(null, 2.1), 4);
18 assert.deepEqual(EVEN.SELF.run(null, 3), 4);
19 assert.deepEqual(EVEN.SELF.run(null, 0), 0);
20 assert.deepEqual(EVEN.SELF.run(null, -0), 0);
21 assert.deepEqual(EVEN.SELF.run(null, 0.1298738912), 2);
22 assert.deepEqual(EVEN.SELF.run(null, 1.1298738912), 2);
23 assert.deepEqual(EVEN.SELF.run(null, 2.1298738912), 4);
24 assert.deepEqual(EVEN.SELF.run(null, true), 2);
25 assert.deepEqual(EVEN.SELF.run(null, false), 0);
26 });
27
28 it("should work with negative numbers", function () {
29 assert.deepEqual(EVEN.SELF.run(null, -2.0), -2.0);
30 assert.deepEqual(EVEN.SELF.run(null, -1.0), -2.0);
31 assert.deepEqual(EVEN.SELF.run(null, -1.1), -2.0);
32 assert.deepEqual(EVEN.SELF.run(null, -2.1), -4.0);
33 assert.deepEqual(EVEN.SELF.run(null, -3.0), -4.0);
34 assert.deepEqual(EVEN.SELF.run(null, 0.0), 0.0);
35 });
36
37 it("should work with strings", function () {
38 assert.deepEqual(EVEN.SELF.run(null, "9.9"), 10.0);
39 });
40
41 it("should do pass-through errors", function () {
42 assert.deepEqual(EVEN.SELF.run(null, new ValueException()), new ValueException());
43 });
44
45 it("should use lookup", function () {
46 const lookup = stub();
47 const collateralLookup = stub();
48 const F = new EVEN(lookup as LookupFunction, collateralLookup as CollateralLookupFunction);
49 collateralLookup.withArgs(CommonModels.A1, CommonModels.M22_RANGE).returns(1.1);
50 assert.deepEqual(F.run(CommonModels.A1, CommonModels.M22_RANGE), 2);
51 assert.isTrue(collateralLookup.calledWith(CommonModels.A1, CommonModels.M22_RANGE));
52 assert.equal(collateralLookup.callCount, 1);
53 assert.isTrue(lookup.notCalled);
54 });
55
56 it("should handle grids", function () {
57 const one = Grid.builder().add(0, 0, 1.1).add(0, 1, "A").build();
58 assert.deepEqual(EVEN.SELF.run(null, one), 2);
59 });
60
61 it("should return error when argument lengths are wrong", function () {
62 assert.deepEqual((EVEN.SELF.run(null) as NAException).name, F7ExceptionName.NA);
63 assert.deepEqual((EVEN.SELF.run(null, "A", "B") as NAException).name, F7ExceptionName.NA);
64 });
65});