Files
openzeppelin-contracts/test/token/ERC20/SafeERC20.test.js
Balaji Pachai 3682c6575c Added message string for require() (#1704)
* Error handling in ERC20 and ERC721

* Added message string for require.

* Fixed solhint errors.

* Updated PR as per issue #1709

* changes as per #1709 and openzeppelin forum.

* Changes in require statement

* Changes in require statement

* build pipeline fix

* Changes as per @nventuro's comment.

* Update revert reason strings.

* Fianal update of revert reason strings.

* WIP: Updating reason strings in test cases

* WIP: Added changes to ERC20 and ERC721

* Fixes linting errors in *.tes.js files

* Achieved 100% code coverage

* Updated the test cases with shouldFail.reverting.withMessage()

* Fix package-lock.

* address review comments

* fix linter issues

* fix remaining revert reasons
2019-04-24 11:17:08 -03:00

134 lines
4.3 KiB
JavaScript

const { shouldFail } = require('openzeppelin-test-helpers');
const ERC20ReturnFalseMock = artifacts.require('ERC20ReturnFalseMock');
const ERC20ReturnTrueMock = artifacts.require('ERC20ReturnTrueMock');
const ERC20NoReturnMock = artifacts.require('ERC20NoReturnMock');
const SafeERC20Wrapper = artifacts.require('SafeERC20Wrapper');
contract('SafeERC20', function ([_, hasNoCode]) {
describe('with address that has no contract code', function () {
beforeEach(async function () {
this.wrapper = await SafeERC20Wrapper.new(hasNoCode);
});
shouldRevertOnAllCalls('SafeERC20: call to non-contract');
});
describe('with token that returns false on all calls', function () {
beforeEach(async function () {
this.wrapper = await SafeERC20Wrapper.new((await ERC20ReturnFalseMock.new()).address);
});
shouldRevertOnAllCalls('SafeERC20: ERC20 operation did not succeed');
});
describe('with token that returns true on all calls', function () {
beforeEach(async function () {
this.wrapper = await SafeERC20Wrapper.new((await ERC20ReturnTrueMock.new()).address);
});
shouldOnlyRevertOnErrors();
});
describe('with token that returns no boolean values', function () {
beforeEach(async function () {
this.wrapper = await SafeERC20Wrapper.new((await ERC20NoReturnMock.new()).address);
});
shouldOnlyRevertOnErrors();
});
});
function shouldRevertOnAllCalls (reason) {
it('reverts on transfer', async function () {
await shouldFail.reverting.withMessage(this.wrapper.transfer(), reason);
});
it('reverts on transferFrom', async function () {
await shouldFail.reverting.withMessage(this.wrapper.transferFrom(), reason);
});
it('reverts on approve', async function () {
await shouldFail.reverting.withMessage(this.wrapper.approve(0), reason);
});
it('reverts on increaseAllowance', async function () {
// [TODO] make sure it's reverting for the right reason
await shouldFail.reverting(this.wrapper.increaseAllowance(0));
});
it('reverts on decreaseAllowance', async function () {
// [TODO] make sure it's reverting for the right reason
await shouldFail.reverting(this.wrapper.decreaseAllowance(0));
});
}
function shouldOnlyRevertOnErrors () {
it('doesn\'t revert on transfer', async function () {
await this.wrapper.transfer();
});
it('doesn\'t revert on transferFrom', async function () {
await this.wrapper.transferFrom();
});
describe('approvals', function () {
context('with zero allowance', function () {
beforeEach(async function () {
await this.wrapper.setAllowance(0);
});
it('doesn\'t revert when approving a non-zero allowance', async function () {
await this.wrapper.approve(100);
});
it('doesn\'t revert when approving a zero allowance', async function () {
await this.wrapper.approve(0);
});
it('doesn\'t revert when increasing the allowance', async function () {
await this.wrapper.increaseAllowance(10);
});
it('reverts when decreasing the allowance', async function () {
await shouldFail.reverting.withMessage(
this.wrapper.decreaseAllowance(10),
'SafeMath: subtraction overflow'
);
});
});
context('with non-zero allowance', function () {
beforeEach(async function () {
await this.wrapper.setAllowance(100);
});
it('reverts when approving a non-zero allowance', async function () {
await shouldFail.reverting.withMessage(
this.wrapper.approve(20),
'SafeERC20: approve from non-zero to non-zero allowance'
);
});
it('doesn\'t revert when approving a zero allowance', async function () {
await this.wrapper.approve(0);
});
it('doesn\'t revert when increasing the allowance', async function () {
await this.wrapper.increaseAllowance(10);
});
it('doesn\'t revert when decreasing the allowance to a positive value', async function () {
await this.wrapper.decreaseAllowance(50);
});
it('reverts when decreasing the allowance to a negative value', async function () {
await shouldFail.reverting.withMessage(
this.wrapper.decreaseAllowance(200),
'SafeMath: subtraction overflow'
);
});
});
});
}