forked from asmarques/chai-bignumber
-
Notifications
You must be signed in to change notification settings - Fork 21
/
chai-bn.js
183 lines (167 loc) · 5.87 KB
/
chai-bn.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
module.exports = function (BN) {
const isEqualTo = BN.prototype.eq;
const isGreaterThan = BN.prototype.gt;
const isGreaterThanOrEqualTo = BN.prototype.gte;
const isLessThan = BN.prototype.lt;
const isLessThanOrEqualTo = BN.prototype.lte;
const isNegative = BN.prototype.isNeg;
const isZero = BN.prototype.isZero;
return function (chai, utils) {
const flag = utils.flag;
// The 'bignumber' property sets the 'bignumber' flag, enabling the custom overrides
chai.Assertion.addProperty('bignumber', function () {
utils.flag(this, 'bignumber', true);
});
// BN objects created using different (compatible) instances of BN can be used via BN.isBN()
const isBN = function (object) {
return object instanceof BN || BN.isBN(object);
};
const convert = function (value) {
if (isBN(value)) {
return value;
} else if (typeof value === 'string') {
return new BN(value);
// BN also supports conversion from e.g. JavaScript numbers, but only for small values. We disable that entirely
} else {
new chai.Assertion(value).assert(false,
'expected #{act} to be an instance of BN or string');
}
};
// Overwrites the assertion performed by multiple methods (which should be aliases) with a new function. Prior to
// calling said function, we assert that the actual value is a BN, and attempt to convert all other arguments to BN.
const overwriteMethods = function (messageIndex, methodNames, newAssertion) {
function overwriteMethod (originalAssertion) {
return function () {
if (utils.flag(this, 'bignumber')) {
const actual = convert(this._obj);
const args = [actual].concat(
[].slice
.call(arguments)
.slice(0, messageIndex)
.map(convert))
.concat(arguments[messageIndex]);
newAssertion.apply(this, args);
} else {
originalAssertion.apply(this, arguments);
}
};
}
methodNames.forEach(methodName =>
chai.Assertion.overwriteMethod(methodName, overwriteMethod)
);
};
// Overwrites the assertion performed by multiple properties (which should be aliases) with a new function. Prior to
// calling said function, we assert that the actual value is a BN.
const overwriteProperties = function (propertyNames, newAssertion) {
function overwriteProperty (originalAssertion) {
return function () {
if (utils.flag(this, 'bignumber')) {
const actual = convert(this._obj);
newAssertion.apply(this, [actual]);
} else {
originalAssertion.call(this);
}
};
}
propertyNames.forEach(propertyName =>
chai.Assertion.overwriteProperty(propertyName, overwriteProperty)
);
};
// BN.eq
overwriteMethods(1, ['equal', 'equals', 'eq'], function (actual, expected, msg) {
if (msg) {
flag(this, 'message', msg);
}
this.assert(
isEqualTo.bind(expected)(actual),
'expected #{act} to equal #{exp}',
'expected #{act} to be different from #{exp}',
expected.toString(),
actual.toString()
);
});
// BN.gt
overwriteMethods(1, ['above', 'gt', 'greaterThan'], function (actual, expected, msg) {
if (msg) {
flag(this, 'message', msg);
}
this.assert(
isGreaterThan.bind(actual)(expected),
'expected #{act} to be greater than #{exp}',
'expected #{act} to be less than or equal to #{exp}',
expected.toString(),
actual.toString()
);
});
// BN.gte
overwriteMethods(1, ['least', 'gte'], function (actual, expected, msg) {
if (msg) {
flag(this, 'message', msg);
}
this.assert(
isGreaterThanOrEqualTo.bind(actual)(expected),
'expected #{act} to be greater than or equal to #{exp}',
'expected #{act} to be less than #{exp}',
expected.toString(),
actual.toString()
);
});
// BN.lt
overwriteMethods(1, ['below', 'lt', 'lessThan'], function (actual, expected, msg) {
if (msg) {
flag(this, 'message', msg);
}
this.assert(
isLessThan.bind(actual)(expected),
'expected #{act} to be less than #{exp}',
'expected #{act} to be greater than or equal to #{exp}',
expected.toString(),
actual.toString()
);
});
// BN.lte
overwriteMethods(1, ['most', 'lte'], function (actual, expected, msg) {
if (msg) {
flag(this, 'message', msg);
}
this.assert(
isLessThanOrEqualTo.bind(actual)(expected),
'expected #{act} to be less than or equal to #{exp}',
'expected #{act} to be greater than #{exp}',
expected.toString(),
actual.toString()
);
});
// Equality with tolerance, using gte and lte
overwriteMethods(2, ['closeTo'], function (actual, expected, delta, msg) {
if (msg) {
flag(this, 'message', msg);
}
this.assert(
isGreaterThanOrEqualTo.bind(actual)(expected.sub(delta)) && isLessThanOrEqualTo.bind(actual)(expected.add(delta)),
`expected #{act} to be within '${delta}' of #{exp}`,
`expected #{act} to be further than '${delta}' from #{exp}`,
expected.toString(),
actual.toString()
);
});
// BN.isNeg
overwriteProperties(['negative'], function (value) {
this.assert(
isNegative.bind(value)(),
'expected #{this} to be negative',
'expected #{this} to not be negative',
value.toString()
);
});
// BN.isZero
overwriteProperties(['zero'], function (value) {
this.assert(
isZero.bind(value)(),
'expected #{this} to be zero',
'expected #{this} to not be zero',
value.toString()
);
});
};
};