-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathsetupTests.ts
127 lines (111 loc) · 4.54 KB
/
setupTests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { BigNumberish } from 'ethers';
import { AsyncFunc } from 'mocha';
import chai, { expect } from 'chai';
import { NAry } from '@balancer-labs/v3-helpers/src/models/types/types';
import { ZERO_ADDRESS } from '@balancer-labs/v3-helpers/src/constants';
import { bn, fp, isBn } from '@balancer-labs/v3-helpers/src/numbers';
import { expectEqualWithError, expectLessThanOrEqualWithError } from '@balancer-labs/v3-helpers/src/test/relativeError';
import { sharedBeforeEach } from './sharedBeforeEach';
/* eslint-disable @typescript-eslint/ban-ts-comment */
/* eslint-disable @typescript-eslint/no-explicit-any */
declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Chai {
interface Assertion {
zero: void;
zeros: void;
zeroAddress: void;
equalFp(value: BigNumberish): void;
lteWithError(value: NAry<BigNumberish>, error: BigNumberish): void;
equalWithError(value: NAry<BigNumberish>, error: BigNumberish): void;
almostEqual(value: NAry<BigNumberish>, error?: BigNumberish, message?: string): void;
almostEqualFp(value: NAry<BigNumberish>, error?: BigNumberish): void;
}
}
function sharedBeforeEach(fn: AsyncFunc): void;
function sharedBeforeEach(name: string, fn: AsyncFunc): void;
}
global.sharedBeforeEach = (nameOrFn: string | AsyncFunc, maybeFn?: AsyncFunc): void => {
sharedBeforeEach(nameOrFn, maybeFn);
};
chai.use(function (chai, utils) {
const { Assertion } = chai;
Assertion.addProperty('zero', function () {
new Assertion(this._obj).to.be.equal(bn(0));
});
Assertion.addProperty('zeros', function () {
const obj = this._obj;
const expectedValue = Array(obj.length).fill(bn(0));
new Assertion(obj).to.be.deep.equal(expectedValue);
});
Assertion.addProperty('zeroAddress', function () {
new Assertion(this._obj).to.be.equal(ZERO_ADDRESS);
});
Assertion.addMethod('equalFp', function (expectedValue: BigNumberish) {
expect(this._obj).to.be.equal(fp(expectedValue));
});
Assertion.addMethod('equalWithError', function (expectedValue: NAry<BigNumberish>, error: BigNumberish) {
if (Array.isArray(expectedValue)) {
const actual: BigNumberish[] = this._obj;
actual.forEach((actual, i) => expectEqualWithError(actual, expectedValue[i], error));
} else {
expectEqualWithError(this._obj, expectedValue, error);
}
});
Assertion.addMethod('lteWithError', function (expectedValue: NAry<BigNumberish>, error: BigNumberish) {
if (Array.isArray(expectedValue)) {
const actual: BigNumberish[] = this._obj;
actual.forEach((actual, i) => expectLessThanOrEqualWithError(actual, expectedValue[i], error));
} else {
expectLessThanOrEqualWithError(this._obj, expectedValue, error);
}
});
Assertion.addMethod(
'almostEqual',
function (expectedValue: NAry<BigNumberish>, error?: BigNumberish, message?: string) {
if (Array.isArray(expectedValue)) {
const actuals: BigNumberish[] = this._obj;
actuals.forEach((actual, i) => expectEqualWithError(actual, expectedValue[i], error, message));
} else {
expectEqualWithError(this._obj, expectedValue, error, message);
}
}
);
Assertion.addMethod(
'almostEqualFp',
function (expectedValue: NAry<BigNumberish>, error?: BigNumberish, message?: string) {
if (Array.isArray(expectedValue)) {
const actuals: BigNumberish[] = this._obj;
actuals.forEach((actual, i) => expectEqualWithError(actual, fp(expectedValue[i]), error, message));
} else {
expectEqualWithError(this._obj, fp(expectedValue), error, message);
}
}
);
['eq', 'equal', 'equals'].forEach((fn: string) => {
Assertion.overwriteMethod(fn, function (_super) {
return function (this: any, expected: any) {
const actual = utils.flag(this, 'object');
if (
utils.flag(this, 'deep') &&
Array.isArray(actual) &&
Array.isArray(expected) &&
actual.length === expected.length &&
(actual.some((v) => isBn(v)) || expected.some((v) => isBn(v)))
) {
const equal = actual.every((value: any, i: number) => bn(value) == expected[i]);
this.assert(
equal,
`Expected "[${expected}]" to be deeply equal [${actual}]`,
`Expected "[${expected}]" NOT to be deeply equal [${actual}]`,
expected,
actual
);
} else {
// eslint-disable-next-line prefer-rest-params
_super.apply(this, arguments);
}
};
});
});
});