From cfc868bf4dc371b299801dd24917a695387546e2 Mon Sep 17 00:00:00 2001 From: Jacob Evans Date: Thu, 18 Jan 2018 18:17:22 +1100 Subject: Reject negative amounts in isValidBaseUnitAmount --- packages/assert/src/index.ts | 2 ++ packages/assert/test/assert_test.ts | 10 ++++++++++ 2 files changed, 12 insertions(+) (limited to 'packages/assert') diff --git a/packages/assert/src/index.ts b/packages/assert/src/index.ts index 56f663780..015ffe579 100644 --- a/packages/assert/src/index.ts +++ b/packages/assert/src/index.ts @@ -12,6 +12,8 @@ export const assert = { }, isValidBaseUnitAmount(variableName: string, value: BigNumber) { assert.isBigNumber(variableName, value); + const isNegative = value.lessThan(0); + this.assert(!isNegative, `${variableName} should not be a negative number, found value: ${value.toNumber()}` ); const hasDecimals = value.decimalPlaces() !== 0; this.assert( !hasDecimals, diff --git a/packages/assert/test/assert_test.ts b/packages/assert/test/assert_test.ts index ff337196d..732e99b3e 100644 --- a/packages/assert/test/assert_test.ts +++ b/packages/assert/test/assert_test.ts @@ -22,6 +22,16 @@ describe('Assertions', () => { invalidInputs.forEach(input => expect(assert.isBigNumber.bind(assert, variableName, input)).to.throw()); }); }); + describe('#isValidBaseUnitAmount', () => { + it('should not throw for valid input', () => { + const validInputs = [new BigNumber(23), new BigNumber('45000000')]; + validInputs.forEach(input => expect(assert.isValidBaseUnitAmount.bind(assert, variableName, input)).to.not.throw()); + }); + it('should throw for invalid input', () => { + const invalidInputs = [0, undefined, new BigNumber(3.145), 3.145, new BigNumber(-400)]; + invalidInputs.forEach(input => expect(assert.isValidBaseUnitAmount.bind(assert, variableName, input)).to.throw()); + }); + }); describe('#isString', () => { it('should not throw for valid input', () => { const validInputs = ['hello', 'goodbye']; -- cgit v1.2.3