chai-bn
Advanced tools
Comparing version 0.1.0-alpha.2 to 0.1.0-beta.0
@@ -11,2 +11,3 @@ module.exports = function (BN) { | ||
return function (chai, utils) { | ||
// The 'bignumber' property sets the 'bignumber' flag, enabling the custom overrides | ||
chai.Assertion.addProperty('bignumber', function () { | ||
@@ -16,2 +17,3 @@ utils.flag(this, 'bignumber', true); | ||
// BN objects created using different (compatible) instances of BN can be used via BN.isBN() | ||
const isBN = function (object) { | ||
@@ -22,6 +24,7 @@ return object instanceof BN || BN.isBN(object); | ||
const convert = function (value) { | ||
if (typeof value === 'string') { | ||
if (isBN(value)) { | ||
return value; | ||
} else if (typeof value === 'string') { | ||
return new BN(value); | ||
} else if (isBN(value)) { | ||
return value; | ||
// BN also supports conversion from e.g. JavaScript numbers, but only for small values. We disable that entirely | ||
} else { | ||
@@ -33,7 +36,7 @@ new chai.Assertion(value).assert(false, | ||
const assertBN = function (value) { | ||
const assertIsBN = function (value) { | ||
if (!isBN(value)) { | ||
new chai.Assertion(value).assert( | ||
false, | ||
'expected #{value} to be an instance of BN' | ||
'expected #{act} to be an instance of BN' | ||
); | ||
@@ -43,14 +46,16 @@ } | ||
const overwriteMethods = function (names, fn) { | ||
function overwriteMethod (original) { | ||
// 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 the expected value. | ||
const overwriteMethods = function (methodNames, newAssertion) { | ||
function overwriteMethod (originalAssertion) { | ||
return function (value) { | ||
if (utils.flag(this, 'bignumber')) { | ||
const actual = this._obj; | ||
assertBN(actual); | ||
assertIsBN(actual); | ||
const expected = convert(value); | ||
fn.apply(this, [expected, actual]); | ||
newAssertion.apply(this, [expected, actual]); | ||
} else { | ||
original.apply(this, arguments); | ||
originalAssertion.apply(this, arguments); | ||
} | ||
@@ -60,17 +65,19 @@ }; | ||
names.forEach(name => | ||
chai.Assertion.overwriteMethod(name, overwriteMethod) | ||
methodNames.forEach(methodName => | ||
chai.Assertion.overwriteMethod(methodName, overwriteMethod) | ||
); | ||
}; | ||
const overwriteProperties = function (names, fn) { | ||
function overwriteProperty (original) { | ||
// 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 = this._obj; | ||
assertBN(actual); | ||
assertIsBN(actual); | ||
fn.apply(this, [actual]); | ||
newAssertion.apply(this, [actual]); | ||
} else { | ||
original.call(this); | ||
originalAssertion.call(this); | ||
} | ||
@@ -80,4 +87,4 @@ }; | ||
names.forEach(name => | ||
chai.Assertion.overwriteProperty(name, overwriteProperty) | ||
propertyNames.forEach(propertyName => | ||
chai.Assertion.overwriteProperty(propertyName, overwriteProperty) | ||
); | ||
@@ -84,0 +91,0 @@ }; |
{ | ||
"name": "chai-bn", | ||
"version": "0.1.0-alpha.2", | ||
"version": "0.1.0-beta.0", | ||
"description": "Chai assertions for comparing arbitrary-precision integers using the bignumber.js library", | ||
@@ -5,0 +5,0 @@ "main": "chai-bn.js", |
@@ -6,3 +6,3 @@ # chai-bn | ||
Chai assertions for comparing arbitrary-precision integers using the [bn.js](https://github.com/indutny/bn.js) library. Forked from [chai-bignumber](https://github.com/asmarques/chai-bignumber), which uses the [bignumber.js](https://github.com/MikeMcl/bignumber.js) library. | ||
[`Chai`](https://www.chaijs.com/) assertions for comparing arbitrary-precision integers using the [bn.js](https://github.com/indutny/bn.js) library. Forked from [chai-bignumber](https://github.com/asmarques/chai-bignumber), which uses the [bignumber.js](https://github.com/MikeMcl/bignumber.js) library. | ||
@@ -27,3 +27,3 @@ ## Installation | ||
The following assertion methods are provided and will override the existing builtin assertions if the `bignumber` property is explicitly set as part of the assertion chain: | ||
The following assertion methods are provided and will override the existing builtin assertions if the `bignumber` property is set as part of the assertion chain: | ||
- equal/equals/eq | ||
@@ -39,4 +39,6 @@ - above/gt/greaterThan | ||
Actual values (i.e. the values being asserted) must be instances of `BN`. Expected values (i.e. the values the actual value is expected to match) may be instances of either `BN` or `string` which can be converted into a valid number. Only BDD style (`expect` or `should`) assertions are supported. | ||
Actual values (i.e. the values being asserted) **must** be instances of `BN`. Expected values (i.e. the values the actual value is expected to match) may be instances of either `BN` or `string` which can be converted into a valid number. This is a key difference with [chai-bignumber](https://github.com/asmarques/chai-bignumber), which automatically converts JavaScript numbers and strings to `BigNumber` instances for both actual and expected values. | ||
Only BDD style (`expect` or `should`) assertions are supported. | ||
## Examples | ||
@@ -50,5 +52,5 @@ | ||
actual.should.be.bignumber.equal(expected); | ||
expect(actual).to.be.bignumber.at.most(expected); | ||
(new BN('1000')).should.be.bignumber.lessThan('2000'); | ||
actual.should.be.a.bignumber.that.equals(expected); | ||
expect(actual).to.be.a.bignumber.that.is.at.most(expected); | ||
(new BN('1000')).should.be.a.bignumber.that.is.lessThan('2000'); | ||
``` | ||
@@ -59,8 +61,10 @@ | ||
```javascript | ||
(new BN('-100')).should.be.negative; | ||
expect(new BN('1').sub(new BN('1'))).to.be.zero; | ||
(new BN('-100')).should.be.a.bignumber.that.is.negative; | ||
expect(new BN('1').sub(new BN('1'))).to.be.a.bignumber.that.is.zero; | ||
``` | ||
Some `Chai` properties (e.g. the `that.is` chain) have no effect other than increasing readability, and can be dropped if less verbosity is desired. | ||
## License | ||
[MIT](LICENSE) |
9575
143
66