diff --git a/chai-bignumber.js b/chai-bignumber.js index dfd2e97..a1d1204 100644 --- a/chai-bignumber.js +++ b/chai-bignumber.js @@ -6,6 +6,7 @@ module.exports = function (BigNumber) { var isGreaterThanOrEqualTo = BigNumber.prototype.isGreaterThanOrEqualTo || BigNumber.prototype.greaterThanOrEqualTo; var isLessThan = BigNumber.prototype.isLessThan || BigNumber.prototype.lessThan; var isLessThanOrEqualTo = BigNumber.prototype.isLessThanOrEqualTo || BigNumber.prototype.lessThanOrEqualTo; + var oneOf = BigNumber.prototype.oneOf return function (chai, utils) { chai.Assertion.addProperty('bignumber', function () { @@ -155,5 +156,29 @@ module.exports = function (BigNumber) { 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); + } + }; + }); + }; }; diff --git a/test/chai-bignumber.test.js b/test/chai-bignumber.test.js index 6a7f6f6..a585640 100644 --- a/test/chai-bignumber.test.js +++ b/test/chai-bignumber.test.js @@ -723,4 +723,38 @@ describe('chai-bignumber', function () { } }); }); + + describe('oneOf', function () { + it('should find number in array', function () { + var tests = [ + [new BigNumber(0), [new BigNumber(0), null, undefined]], + [1, [1, new BigNumber(2), 5]], + ["3", [new BigNumber(3), 6, 7, "8"]], + [new BigNumber(4), [4, "10", new BigNumber(6)]], + [new BigNumber(5), ["5", new BigNumber(10)]], + ]; + + for (var i = 0; i < tests.length; i++) { + var a = tests[i][0]; + var l = tests[i][1]; + a.should.be.bignumber.oneOf(l); + expect(a).to.be.bignumber.oneOf(l); + } + }); + + it('should not find number in array', function () { + var tests = [ + [new BigNumber(0), []], + [10, [1, new BigNumber(2), 5]], + ["3", [new BigNumber(30), null, undefined]], + ]; + + for (var i = 0; i < tests.length; i++) { + var a = tests[i][0]; + var l = tests[i][1]; + a.should.not.be.bignumber.oneOf(l); + expect(a).to.not.be.bignumber.oneOf(l); + } + }); + }); });