From 035aebf7aabf4faecfad4e76d17c28b8ec61b8b6 Mon Sep 17 00:00:00 2001 From: MrClottom Date: Mon, 18 Jan 2021 16:33:38 +0100 Subject: [PATCH 1/3] Fix expectRevert accepting partial matches --- src/expectRevert.js | 29 ++++++++++++++++++++++------- test/src/expectRevert.test.js | 4 ++++ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/expectRevert.js b/src/expectRevert.js index 8080105..82e54cb 100644 --- a/src/expectRevert.js +++ b/src/expectRevert.js @@ -6,17 +6,32 @@ const semver = require('semver'); const checkedProviders = new WeakSet(); +function getVMException (errorMessage) { + const VMExceptionPattern = /Returned error: VM Exception while processing transaction: (.*?)\.?$/; + const [, VMException] = errorMessage.match(VMExceptionPattern); + return VMException || null; +} + +function getRevertString (VMException) { + const match = VMException.match(/revert (?:(?:(.*) -- Reason given: \1)|(.*))/); + 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; diff --git a/test/src/expectRevert.test.js b/test/src/expectRevert.test.js index 1f41d3c..5629a0d 100644 --- a/test/src/expectRevert.test.js +++ b/test/src/expectRevert.test.js @@ -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'); }); From 15fe453849af90bda87b049ba926adc7e61b46f7 Mon Sep 17 00:00:00 2001 From: MrClottom Date: Mon, 18 Jan 2021 20:16:37 +0100 Subject: [PATCH 2/3] Add alternative error message handling --- src/expectRevert.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/expectRevert.js b/src/expectRevert.js index 82e54cb..c13582b 100644 --- a/src/expectRevert.js +++ b/src/expectRevert.js @@ -7,13 +7,20 @@ 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 [, VMException] = errorMessage.match(VMExceptionPattern); + 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; } From 2d4ad7474e9774fba505739319b73cec3b3a4fe0 Mon Sep 17 00:00:00 2001 From: Philippe Dumonet Date: Sat, 20 Mar 2021 20:14:14 +0100 Subject: [PATCH 3/3] Add function based event validation --- src/expectEvent.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/expectEvent.js b/src/expectEvent.js index bd3f120..3eb6f9c 100644 --- a/src/expectEvent.js +++ b/src/expectEvent.js @@ -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)) {