Skip to content

Commit

Permalink
EDT-2912 Alter expectThrow to be able to take in a revert reason and …
Browse files Browse the repository at this point in the history
…test that exact string is what we receive back from the node and truffle. Use with direct call on delegate test to check for expected revert reason.
  • Loading branch information
AC0DEM0NK3Y committed Apr 8, 2019
1 parent 0d95695 commit 8ce5aa4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion test/ERC1155Mintable.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global artifacts, contract, it, assert */
/* eslint-disable prefer-reflect */

const expectThrow = require('./helpers/expectThrow');
import expectThrow from './helpers/expectThrow';

const ERC1155Mintable = artifacts.require('ERC1155Mintable.sol');
const ERC1155MockReceiver = artifacts.require('ERC1155MockReceiver.sol');
Expand Down
4 changes: 2 additions & 2 deletions test/ERC1155ProxyTest.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global artifacts, contract, it, assert */
/* eslint-disable prefer-reflect */

const expectThrow = require('./helpers/expectThrow');
import expectThrow from './helpers/expectThrow';

const BigNumber = require('bignumber.js');

Expand Down Expand Up @@ -280,6 +280,6 @@ contract('ERC1155ProxyTest - tests sending 1155 items to an ERC1538 supported pr
});

it('attempt direct call to a delegate', async () => {
await expectThrow(receiverDelegateERC1155.onERC1155Received(zeroAddress, zeroAddress, 0, 0, web3.utils.fromAscii('')));
await expectThrow(receiverDelegateERC1155.onERC1155Received(zeroAddress, zeroAddress, 0, 0, web3.utils.fromAscii('')), "Direct call: onERC1155Received");
});
});
39 changes: 28 additions & 11 deletions test/helpers/expectThrow.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
module.exports = async promise => {
export default async (promise, expectedRevertMessage="") => {
try {
await promise;
} catch (error) {
// TODO: Check jump destination to destinguish between a throw
// and an actual invalid jump.
const invalidOpcode = error.message.search('invalid opcode') >= 0;
// TODO: When we contract A calls contract B, and B throws, instead
// of an 'invalid jump', we get an 'out of gas' error. How do
// we distinguish this from an actual out of gas event? (The
// testrpc log actually show an 'invalid jump' event.)
const outOfGas = error.message.search('out of gas') >= 0;
const revert = error.message.search('revert') >= 0;
let invalidOpcode = false;
let outOfGas = false;
let revert = false;
if (expectedRevertMessage !== "") {
// Check end of error string, it's where the revert reason is output.
let expectedLen = expectedRevertMessage.length;
if (expectedLen < error.message.length) {
// Rather annoyingly, truffle adds a period if it can decode the revert reason.
if (error.message.search('-- Reason given:') >= 0) {
revert = expectedRevertMessage === error.message.substring(error.message.length-expectedLen-1, error.message.length-1);
} else {
revert = expectedRevertMessage === error.message.substring(error.message.length-expectedLen, error.message.length);
}
}
} else {
// TODO: Check jump destination to destinguish between a throw
// and an actual invalid jump.
invalidOpcode = error.message.search('invalid opcode') >= 0;
// TODO: When we contract A calls contract B, and B throws, instead
// of an 'invalid jump', we get an 'out of gas' error. How do
// we distinguish this from an actual out of gas event? (The
// testrpc log actually show an 'invalid jump' event.)
outOfGas = error.message.search('out of gas') >= 0;
revert = error.message.search('revert') >= 0;
}

assert(
invalidOpcode || outOfGas || revert,
'Expected throw, got \'' + error + '\' instead',
'Expected throw with revert string \'' + expectedRevertMessage + '\', got \'' + error + '\' instead',
);
return;
}
Expand Down

0 comments on commit 8ce5aa4

Please sign in to comment.