-
Notifications
You must be signed in to change notification settings - Fork 37
/
chai-bignumber.js
183 lines (165 loc) · 5.68 KB
/
chai-bignumber.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 (BigNumber) {
BigNumber = BigNumber || require('bignumber.js');
var round = BigNumber.prototype.round || BigNumber.prototype.decimalPlaces;
var isEqualTo = BigNumber.prototype.isEqualTo || BigNumber.prototype.equals;
var isGreaterThan = BigNumber.prototype.isGreaterThan || BigNumber.prototype.greaterThan;
var isGreaterThanOrEqualTo = BigNumber.prototype.isGreaterThanOrEqualTo || BigNumber.prototype.greaterThanOrEqualTo;
var isLessThan = BigNumber.prototype.isLessThan || BigNumber.prototype.lessThan;
var isLessThanOrEqualTo = BigNumber.prototype.isLessThanOrEqualTo || BigNumber.prototype.lessThanOrEqualTo;
return function (chai, utils) {
chai.Assertion.addProperty('bignumber', function () {
utils.flag(this, 'bignumber', true);
});
var isBigNumber = function (object) {
return object.isBigNumber ||
object instanceof BigNumber ||
(object.constructor && object.constructor.name === 'BigNumber');
};
var convert = function (value, dp, rm) {
var number;
if (typeof value === 'string' || typeof value === 'number') {
number = new BigNumber(value);
} else if (isBigNumber(value)) {
number = value;
} else {
new chai.Assertion(value).assert(false,
'expected #{act} to be an instance of string, number or BigNumber');
}
if (parseInt(dp) === dp) {
if (rm === undefined) {
rm = BigNumber.ROUND_HALF_UP;
}
number = round.bind(number)(dp, rm);
}
return number;
};
var overwriteMethods = function (names, fn) {
function overwriteMethod(original) {
return function (value, dp, rm) {
if (utils.flag(this, 'bignumber')) {
var expected = convert(value, dp, rm);
var actual = convert(this._obj, dp, rm);
fn.apply(this, [expected, actual]);
} else {
original.apply(this, arguments);
}
};
}
for (var i = 0; i < names.length; i++) {
chai.Assertion.overwriteMethod(names[i], overwriteMethod);
}
};
// BigNumber.isEqualTo
overwriteMethods(['equal', 'equals', 'eq'], function (expected, actual) {
this.assert(
isEqualTo.bind(expected)(actual),
'expected #{act} to equal #{exp}',
'expected #{act} to be different from #{exp}',
expected.toString(),
actual.toString()
);
});
// BigNumber.isGreaterThan
overwriteMethods(['above', 'gt', 'greaterThan'], function (expected, actual) {
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()
);
});
// BigNumber.isGreaterThanOrEqualTo
overwriteMethods(['least', 'gte', 'greaterThanOrEqual'], function (expected, actual) {
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()
);
});
// BigNumber.isLessThan
overwriteMethods(['below', 'lt', 'lessThan'], function (expected, actual) {
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()
);
});
// BigNumber.isLessThanOrEqualTo
overwriteMethods(['most', 'lte', 'lessThanOrEqual'], function (expected, actual) {
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()
);
});
// BigNumber.isFinite
chai.Assertion.addProperty('finite', function () {
var value = convert(this._obj);
this.assert(
value.isFinite(),
'expected #{this} to be finite',
'expected #{this} to not be finite',
value.toString()
);
});
// BigNumber.isInteger
chai.Assertion.addProperty('integer', function () {
var value = convert(this._obj);
this.assert(
value.isInteger(),
'expected #{this} to be an integer',
'expected #{this} to not be an integer',
value.toString()
);
});
// BigNumber.isNegative
chai.Assertion.addProperty('negative', function () {
var value = convert(this._obj);
this.assert(
value.isNegative(),
'expected #{this} to be negative',
'expected #{this} to not be negative',
value.toString()
);
});
// BigNumber.isZero
chai.Assertion.addProperty('zero', function () {
var value = convert(this._obj);
this.assert(
value.isZero(),
'expected #{this} to be zero',
'expected #{this} to not be zero',
value.toString()
);
});
// BigNumber.oneOf
chai.Assertion.overwriteMethod('oneOf', function (original) {
return function (list) {
if (utils.flag(this, 'bignumber')) {
var value = convert(this._obj);
var found = false;
for (var i = 0; i < list.length; i++) {
if (value.isEqualTo(list[i])) {
found = true;
}
}
this.assert(
found,
'expected #{this} to be one of #{exp}',
'expected #{this} to not be one of #{exp}',
list
);
} else {
original.apply(this, arguments);
}
};
});
};
};