name:
src/test/js/formulas/math/ODDTest.ts
-rw-r--r--
2942
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 { ODD } from "../../../../main/js/formulas/math/ODD";
8import { Grid } from "../../../../main/js/models/common/Grid";
9import { CollateralLookupFunction, LookupFunction } from "../../../../main/js/models/common/Types";
10import { CommonModels } from "../../testutils/CommonModels";
11
12describe("ODD", function () {
13 it("should work with positive numbers", function () {
14 assert.deepEqual(ODD.SELF.run(null, 2), 3);
15 assert.deepEqual(ODD.SELF.run(null, 1), 1);
16 assert.deepEqual(ODD.SELF.run(null, 1.1), 3);
17 assert.deepEqual(ODD.SELF.run(null, 2.1), 3);
18 assert.deepEqual(ODD.SELF.run(null, 3), 3);
19 assert.deepEqual(ODD.SELF.run(null, 0), 1);
20 assert.deepEqual(ODD.SELF.run(null, -0), 1);
21 assert.deepEqual(ODD.SELF.run(null, 0.1298738912), 1);
22 assert.deepEqual(ODD.SELF.run(null, 1.1298738912), 3);
23 assert.deepEqual(ODD.SELF.run(null, 2.1298738912), 3);
24 assert.deepEqual(ODD.SELF.run(null, true), 1);
25 assert.deepEqual(ODD.SELF.run(null, false), 1);
26 });
27
28 it("should work with negative numbers", function () {
29 assert.deepEqual(ODD.SELF.run(null, -2), -3);
30 assert.deepEqual(ODD.SELF.run(null, -1), -1);
31 assert.deepEqual(ODD.SELF.run(null, -1.1), -3);
32 assert.deepEqual(ODD.SELF.run(null, -2.1), -3);
33 assert.deepEqual(ODD.SELF.run(null, -3), -3);
34 assert.deepEqual(ODD.SELF.run(null, 0), 1);
35 });
36
37 it("should work with strings", function () {
38 assert.deepEqual(ODD.SELF.run(null, "2"), 3);
39 });
40
41 it("should do pass-through errors", function () {
42 assert.deepEqual(ODD.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 ODD(lookup as LookupFunction, collateralLookup as CollateralLookupFunction);
49 collateralLookup.withArgs(CommonModels.A1, CommonModels.M22_RANGE).returns(0.1298738912);
50 assert.deepEqual(F.run(CommonModels.A1, CommonModels.M22_RANGE), 1);
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, 0.1298738).add(0, 1, "A").build();
58 assert.deepEqual(ODD.SELF.run(null, one), 1);
59 });
60
61 it("should return error when argument lengths are wrong", function () {
62 assert.deepEqual((ODD.SELF.run(null) as NAException).name, F7ExceptionName.NA);
63 assert.deepEqual((ODD.SELF.run(null, "A", "B") as NAException).name, F7ExceptionName.NA);
64 });
65});