diff options
author | Alex Browne <stephenalexbrowne@gmail.com> | 2018-06-06 07:20:38 +0800 |
---|---|---|
committer | Alex Browne <stephenalexbrowne@gmail.com> | 2018-06-07 03:40:31 +0800 |
commit | d6d7f4e875b161aa7284467a61f67989f76ec89e (patch) | |
tree | d0287504809489cec96a9673ffac41429cf14cd7 /packages/contracts | |
parent | 63caddea62453863de84a4b53e14fe3e61d3008f (diff) | |
download | dexon-sol-tools-d6d7f4e875b161aa7284467a61f67989f76ec89e.tar.gz dexon-sol-tools-d6d7f4e875b161aa7284467a61f67989f76ec89e.tar.zst dexon-sol-tools-d6d7f4e875b161aa7284467a61f67989f76ec89e.zip |
Update more things to work with both Geth and Ganache
Diffstat (limited to 'packages/contracts')
-rw-r--r-- | packages/contracts/src/utils/assertions.ts | 36 | ||||
-rw-r--r-- | packages/contracts/test/libraries/lib_bytes.ts | 29 | ||||
-rw-r--r-- | packages/contracts/test/unlimited_allowance_token.ts | 15 |
3 files changed, 42 insertions, 38 deletions
diff --git a/packages/contracts/src/utils/assertions.ts b/packages/contracts/src/utils/assertions.ts index 1ea071d01..c08bc7271 100644 --- a/packages/contracts/src/utils/assertions.ts +++ b/packages/contracts/src/utils/assertions.ts @@ -5,38 +5,30 @@ import { constants } from './constants'; const expect = chai.expect; -// throws if the given promise does not reject with one of two expected error -// messages. -export function expectRevertOrAlwaysFailingTransaction<T>(p: Promise<T>): PromiseLike<void> { +function _expectEitherError<T>(p: Promise<T>, error1: string, error2: string): PromiseLike<void> { return expect(p) .to.be.rejected() .then(e => { expect(e).to.satisfy( - (err: Error) => - _.includes(err.message, constants.REVERT) || _.includes(err.message, 'always failing transaction'), + (err: Error) => _.includes(err.message, error1) || _.includes(err.message, error2), + `expected promise to reject with error message that includes "${error1}" or "${error2}", but got: ` + + `"${e.message}"\n`, ); }); } export function expectInsufficientFunds<T>(p: Promise<T>): PromiseLike<void> { - return expect(p) - .to.be.rejected() - .then(e => { - expect(e).to.satisfy( - (err: Error) => - _.includes(err.message, 'insufficient funds') || - _.includes(err.message, "sender doesn't have enough funds"), - ); - }); + return _expectEitherError(p, 'insufficient funds', "sender doesn't have enough funds"); +} + +export function expectRevertOrOtherError<T>(p: Promise<T>, otherError: string): PromiseLike<void> { + return _expectEitherError(p, constants.REVERT, otherError); +} + +export function expectRevertOrAlwaysFailingTransaction<T>(p: Promise<T>): PromiseLike<void> { + return expectRevertOrOtherError(p, 'always failing transaction'); } export function expectRevertOrContractCallFailed<T>(p: Promise<T>): PromiseLike<void> { - return expect(p) - .to.be.rejected() - .then(e => { - expect(e).to.satisfy( - (err: Error) => - _.includes(err.message, constants.REVERT) || _.includes(err.message, 'Contract call failed'), - ); - }); + return expectRevertOrOtherError<T>(p, 'Contract call failed'); } diff --git a/packages/contracts/test/libraries/lib_bytes.ts b/packages/contracts/test/libraries/lib_bytes.ts index 26802a60d..f5435a81e 100644 --- a/packages/contracts/test/libraries/lib_bytes.ts +++ b/packages/contracts/test/libraries/lib_bytes.ts @@ -10,7 +10,7 @@ import * as Web3 from 'web3'; import { TestLibBytesContract } from '../../src/contract_wrappers/generated/test_lib_bytes'; import { artifacts } from '../../src/utils/artifacts'; -import { expectRevertOrAlwaysFailingTransaction } from '../../src/utils/assertions'; +import { expectRevertOrAlwaysFailingTransaction, expectRevertOrOtherError } from '../../src/utils/assertions'; import { chaiSetup } from '../../src/utils/chai_setup'; import { constants } from '../../src/utils/constants'; import { provider, txDefaults, web3Wrapper } from '../../src/utils/web3_wrapper'; @@ -64,7 +64,8 @@ describe('LibBytes', () => { describe('popByte', () => { it('should revert if length is 0', async () => { - return expect(libBytes.publicPopByte.callAsync(constants.NULL_BYTES)).to.be.rejectedWith( + return expectRevertOrOtherError( + libBytes.publicPopByte.callAsync(constants.NULL_BYTES), constants.LIB_BYTES_GT_ZERO_LENGTH_REQUIRED, ); }); @@ -80,7 +81,8 @@ describe('LibBytes', () => { describe('popAddress', () => { it('should revert if length is less than 20', async () => { - return expect(libBytes.publicPopAddress.callAsync(byteArrayShorterThan20Bytes)).to.be.rejectedWith( + return expectRevertOrOtherError( + libBytes.publicPopAddress.callAsync(byteArrayShorterThan20Bytes), constants.LIB_BYTES_GTE_20_LENGTH_REQUIRED, ); }); @@ -165,7 +167,8 @@ describe('LibBytes', () => { it('should fail if the byte array is too short to hold an address)', async () => { const shortByteArray = '0xabcdef'; const offset = new BigNumber(0); - return expect(libBytes.publicReadAddress.callAsync(shortByteArray, offset)).to.be.rejectedWith( + return expectRevertOrOtherError( + libBytes.publicReadAddress.callAsync(shortByteArray, offset), constants.LIB_BYTES_GTE_20_LENGTH_REQUIRED, ); }); @@ -173,7 +176,8 @@ describe('LibBytes', () => { it('should fail if the length between the offset and end of the byte array is too short to hold an address)', async () => { const byteArray = ethUtil.addHexPrefix(testAddress); const badOffset = new BigNumber(ethUtil.toBuffer(byteArray).byteLength); - return expect(libBytes.publicReadAddress.callAsync(byteArray, badOffset)).to.be.rejectedWith( + return expectRevertOrOtherError( + libBytes.publicReadAddress.callAsync(byteArray, badOffset), constants.LIB_BYTES_GTE_20_LENGTH_REQUIRED, ); }); @@ -209,14 +213,16 @@ describe('LibBytes', () => { it('should fail if the byte array is too short to hold a bytes32)', async () => { const offset = new BigNumber(0); - return expect(libBytes.publicReadBytes32.callAsync(byteArrayShorterThan32Bytes, offset)).to.be.rejectedWith( + return expectRevertOrOtherError( + libBytes.publicReadBytes32.callAsync(byteArrayShorterThan32Bytes, offset), constants.LIB_BYTES_GTE_32_LENGTH_REQUIRED, ); }); it('should fail if the length between the offset and end of the byte array is too short to hold a bytes32)', async () => { const badOffset = new BigNumber(ethUtil.toBuffer(testBytes32).byteLength); - return expect(libBytes.publicReadBytes32.callAsync(testBytes32, badOffset)).to.be.rejectedWith( + return expectRevertOrOtherError( + libBytes.publicReadBytes32.callAsync(testBytes32, badOffset), constants.LIB_BYTES_GTE_32_LENGTH_REQUIRED, ); }); @@ -256,7 +262,8 @@ describe('LibBytes', () => { it('should fail if the byte array is too short to hold a uint256)', async () => { const offset = new BigNumber(0); - return expect(libBytes.publicReadUint256.callAsync(byteArrayShorterThan32Bytes, offset)).to.be.rejectedWith( + return expectRevertOrOtherError( + libBytes.publicReadUint256.callAsync(byteArrayShorterThan32Bytes, offset), constants.LIB_BYTES_GTE_32_LENGTH_REQUIRED, ); }); @@ -266,7 +273,8 @@ describe('LibBytes', () => { const testUint256AsBuffer = ethUtil.toBuffer(formattedTestUint256); const byteArray = ethUtil.bufferToHex(testUint256AsBuffer); const badOffset = new BigNumber(testUint256AsBuffer.byteLength); - return expect(libBytes.publicReadUint256.callAsync(byteArray, badOffset)).to.be.rejectedWith( + return expectRevertOrOtherError( + libBytes.publicReadUint256.callAsync(byteArray, badOffset), constants.LIB_BYTES_GTE_32_LENGTH_REQUIRED, ); }); @@ -287,7 +295,8 @@ describe('LibBytes', () => { // AssertionError: expected promise to be rejected with an error including 'revert' but it was fulfilled with '0x08c379a0' it('should revert if byte array has a length < 4', async () => { const byteArrayLessThan4Bytes = '0x010101'; - return expect(libBytes.publicReadFirst4.callAsync(byteArrayLessThan4Bytes)).to.be.rejectedWith( + return expectRevertOrOtherError( + libBytes.publicReadFirst4.callAsync(byteArrayLessThan4Bytes), constants.LIB_BYTES_GTE_4_LENGTH_REQUIRED, ); }); diff --git a/packages/contracts/test/unlimited_allowance_token.ts b/packages/contracts/test/unlimited_allowance_token.ts index ea20df040..a66d0055b 100644 --- a/packages/contracts/test/unlimited_allowance_token.ts +++ b/packages/contracts/test/unlimited_allowance_token.ts @@ -7,7 +7,7 @@ import * as Web3 from 'web3'; import { DummyERC20TokenContract } from '../src/contract_wrappers/generated/dummy_e_r_c20_token'; import { artifacts } from '../src/utils/artifacts'; -import { expectRevertOrAlwaysFailingTransaction } from '../src/utils/assertions'; +import { expectRevertOrAlwaysFailingTransaction, expectRevertOrOtherError } from '../src/utils/assertions'; import { chaiSetup } from '../src/utils/chai_setup'; import { constants } from '../src/utils/constants'; import { provider, txDefaults, web3Wrapper } from '../src/utils/web3_wrapper'; @@ -56,7 +56,8 @@ describe('UnlimitedAllowanceToken', () => { it('should throw if owner has insufficient balance', async () => { const ownerBalance = await token.balanceOf.callAsync(owner); const amountToTransfer = ownerBalance.plus(1); - return expect(token.transfer.callAsync(spender, amountToTransfer, { from: owner })).to.be.rejectedWith( + return expectRevertOrOtherError( + token.transfer.callAsync(spender, amountToTransfer, { from: owner }), constants.ERC20_INSUFFICIENT_BALANCE, ); }); @@ -94,11 +95,12 @@ describe('UnlimitedAllowanceToken', () => { await token.approve.sendTransactionAsync(spender, amountToTransfer, { from: owner }), constants.AWAIT_TRANSACTION_MINED_MS, ); - return expect( + return expectRevertOrOtherError( token.transferFrom.callAsync(owner, spender, amountToTransfer, { from: spender, }), - ).to.be.rejectedWith(constants.ERC20_INSUFFICIENT_BALANCE); + constants.ERC20_INSUFFICIENT_BALANCE, + ); }); it('should throw if spender has insufficient allowance', async () => { @@ -109,11 +111,12 @@ describe('UnlimitedAllowanceToken', () => { const isSpenderAllowanceInsufficient = spenderAllowance.cmp(amountToTransfer) < 0; expect(isSpenderAllowanceInsufficient).to.be.true(); - return expect( + return expectRevertOrOtherError( token.transferFrom.callAsync(owner, spender, amountToTransfer, { from: spender, }), - ).to.be.rejectedWith(constants.ERC20_INSUFFICIENT_ALLOWANCE); + constants.ERC20_INSUFFICIENT_ALLOWANCE, + ); }); it('should return true on a 0 value transfer', async () => { |