-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename shouldFail to expectRevert (#39)
* Rename shouldFail to expectFailure. * Fix exports. * Refactor expectRevert and change API. * Update src/expectRevert.js Co-Authored-By: Francisco Giordano <[email protected]> * Update CHANGELOG.md Co-Authored-By: Francisco Giordano <[email protected]> * Update CHANGELOG.md Co-Authored-By: Francisco Giordano <[email protected]> * Update src/expectRevert.js Co-Authored-By: Francisco Giordano <[email protected]> * Update src/expectRevert.js Co-Authored-By: Francisco Giordano <[email protected]> * Fix output message. * Add assertFailure helper. * Update tests and integration tests to use the new API. * update documentation for expectRevert * Update README.md
- Loading branch information
Showing
16 changed files
with
318 additions
and
277 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
pragma solidity ^0.4.24; | ||
|
||
contract Reverter { | ||
uint256[] private array; | ||
|
||
function dontRevert() public pure { | ||
} | ||
|
||
function revertFromRevert() public pure { | ||
revert(); | ||
} | ||
|
||
function revertFromRevertWithReason() public pure { | ||
revert("Call to revert"); | ||
} | ||
|
||
function revertFromRequire() public pure { | ||
require(false); | ||
} | ||
|
||
function revertFromRequireWithReason() public pure { | ||
require(false, "Failed requirement"); | ||
} | ||
|
||
function revertFromThrow() public pure { | ||
throw; | ||
} | ||
|
||
function revertFromAssert() public pure { | ||
assert(false); | ||
} | ||
|
||
function revertFromOutOfGas() public { | ||
for (uint256 i = 0; i < 2**200; ++i) { | ||
array.push(i); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const { web3 } = require('./setup'); | ||
|
||
const { expect } = require('chai'); | ||
const colors = require('ansi-colors'); | ||
const semver = require('semver'); | ||
|
||
async function expectException (promise, expectedError) { | ||
try { | ||
await promise; | ||
} catch (error) { | ||
expect(error.message).to.include(expectedError, `Wrong failure type, expected '${expectedError}'`); | ||
return; | ||
} | ||
|
||
throw Error('Expected failure not received'); | ||
} | ||
|
||
const expectRevert = async function (promise, expectedError) { | ||
if (!expectedError) { | ||
promise.catch(() => { }); | ||
throw Error('No revert reason specified: call expectRevert with the reason string, or use expectRevert.unspecified \ | ||
if your \'require\' statement doesn\'t have one.'); | ||
} | ||
|
||
// Find out if current version of ganache-core supports revert reason i.e >= 2.2.0. | ||
// https://github.com/trufflesuite/ganache-core/releases/tag/v2.2.0 | ||
const nodeInfo = await web3.eth.getNodeInfo(); | ||
const matches = /TestRPC\/v([0-9.]+)\/ethereum-js/.exec(nodeInfo); | ||
|
||
const warn = function (msg) { | ||
console.log(`${colors.white.bgBlack('openzeppelin-test-helpers')} ${colors.black.bgYellow('WARN')} \ | ||
expectRevert: ` + msg); | ||
}; | ||
|
||
if (matches === null || !(1 in matches)) { | ||
// warn users and skip reason check. | ||
warn('revert reason checking only supported on Ganache v2.2.0 or newer.'); | ||
expectedError = 'revert'; | ||
} else if (!semver.gte(matches[1], '2.2.0')) { | ||
// warn users and skip reason check. | ||
warn(`current version of Ganache (v${matches[1]}) doesn't return revert reason. Use v2.2.0 or newer.`); | ||
expectedError = 'revert'; | ||
} | ||
|
||
await expectException(promise, expectedError); | ||
}; | ||
|
||
expectRevert.assertion = (promise) => expectException(promise, 'invalid opcode'); | ||
expectRevert.outOfGas = (promise) => expectException(promise, 'out of gas'); | ||
expectRevert.unspecified = (promise) => expectException(promise, 'revert'); | ||
|
||
module.exports = expectRevert; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const { expect } = require('chai'); | ||
|
||
async function assertFailure (promise) { | ||
try { | ||
await promise; | ||
} catch (error) { | ||
return; | ||
} | ||
expect.fail(); | ||
} | ||
|
||
module.exports = assertFailure; |
Oops, something went wrong.