Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix expectRevert accepting partial matches #152

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/expectEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,10 @@ function decodeLogs (logs, emitter, eventName) {

function contains (args, key, value) {
expect(key in args).to.equal(true, `Event argument '${key}' not found`);

if (value === null) {

if (typeof value === 'function') {
value(args[key]);
} else if (value === null) {
expect(args[key]).to.equal(null,
`expected event argument '${key}' to be null but got ${args[key]}`);
} else if (isBN(args[key]) || isBN(value)) {
Expand Down
36 changes: 29 additions & 7 deletions src/expectRevert.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,39 @@ const semver = require('semver');

const checkedProviders = new WeakSet();

function getVMException (errorMessage) {
switch (errorMessage) {
case 'Returned error: Transaction reverted without a reason':
return 'revert';
}
const VMExceptionPattern = /Returned error: VM Exception while processing transaction: (.*?)\.?$/;
const match = errorMessage.match(VMExceptionPattern);
if (!match) throw new Error(`Unrecognized VM error string "${errorMessage}"`);
const [, VMException] = match;
return VMException || null;
}

function getRevertString (VMException) {
const match = VMException.match(/revert (?:(?:(.*) -- Reason given: \1)|(.*))/);
if (!match) throw new Error(`Unrecognized revert error string "${VMException}"`);
const [, newForm, oldForm] = match;
return newForm || oldForm || null;
}

async function expectException (promise, expectedError) {
try {
await promise;
} catch (error) {
if (error.message.indexOf(expectedError) === -1) {
// When the exception was a revert, the resulting string will include only
// the revert reason, otherwise it will be the type of exception (e.g. 'invalid opcode')
const actualError = error.message.replace(
/Returned error: VM Exception while processing transaction: (revert )?/,
'',
);
const VMException = getVMException(error.message);

if (expectedError === 'revert') {
// eslint-disable-next-line no-unused-expressions
expect(VMException.startsWith('revert'), 'Wrong kind of exception received').to.be.true;
} else {
const actualError = VMException.startsWith('revert')
? getRevertString(VMException)
: VMException;

expect(actualError).to.equal(expectedError, 'Wrong kind of exception received');
}
return;
Expand Down
4 changes: 4 additions & 0 deletions test/src/expectRevert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ describe('expectRevert', function () {
await assertFailure(expectRevert(this.reverter.revertFromRequireWithReason(), 'Wrong reason'));
});

it('rejects a failed requirement with partial error message match', async function () {
await assertFailure(expectRevert(this.reverter.revertFromRequireWithReason(), 'VM'));
});

it('accepts a failed requirement with correct expected reason', async function () {
await expectRevert(this.reverter.revertFromRequireWithReason(), 'Failed requirement');
});
Expand Down