Files
openzeppelin-contracts/test/token/ERC20/behaviors/ERC20Burnable.behavior.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

109 lines
3.5 KiB
JavaScript

const { BN, constants, expectEvent, shouldFail } = require('openzeppelin-test-helpers');
const { ZERO_ADDRESS } = constants;
function shouldBehaveLikeERC20Burnable (owner, initialBalance, [burner]) {
describe('burn', function () {
describe('when the given amount is not greater than balance of the sender', function () {
context('for a zero amount', function () {
shouldBurn(new BN(0));
});
context('for a non-zero amount', function () {
shouldBurn(new BN(100));
});
function shouldBurn (amount) {
beforeEach(async function () {
({ logs: this.logs } = await this.token.burn(amount, { from: owner }));
});
it('burns the requested amount', async function () {
(await this.token.balanceOf(owner)).should.be.bignumber.equal(initialBalance.sub(amount));
});
it('emits a transfer event', async function () {
expectEvent.inLogs(this.logs, 'Transfer', {
from: owner,
to: ZERO_ADDRESS,
value: amount,
});
});
}
});
describe('when the given amount is greater than the balance of the sender', function () {
const amount = initialBalance.addn(1);
it('reverts', async function () {
await shouldFail.reverting.withMessage(this.token.burn(amount, { from: owner }),
'SafeMath: subtraction overflow'
);
});
});
});
describe('burnFrom', function () {
describe('on success', function () {
context('for a zero amount', function () {
shouldBurnFrom(new BN(0));
});
context('for a non-zero amount', function () {
shouldBurnFrom(new BN(100));
});
function shouldBurnFrom (amount) {
const originalAllowance = amount.muln(3);
beforeEach(async function () {
await this.token.approve(burner, originalAllowance, { from: owner });
const { logs } = await this.token.burnFrom(owner, amount, { from: burner });
this.logs = logs;
});
it('burns the requested amount', async function () {
(await this.token.balanceOf(owner)).should.be.bignumber.equal(initialBalance.sub(amount));
});
it('decrements allowance', async function () {
(await this.token.allowance(owner, burner)).should.be.bignumber.equal(originalAllowance.sub(amount));
});
it('emits a transfer event', async function () {
expectEvent.inLogs(this.logs, 'Transfer', {
from: owner,
to: ZERO_ADDRESS,
value: amount,
});
});
}
});
describe('when the given amount is greater than the balance of the sender', function () {
const amount = initialBalance.addn(1);
it('reverts', async function () {
await this.token.approve(burner, amount, { from: owner });
await shouldFail.reverting.withMessage(this.token.burnFrom(owner, amount, { from: burner }),
'SafeMath: subtraction overflow'
);
});
});
describe('when the given amount is greater than the allowance', function () {
const allowance = new BN(100);
it('reverts', async function () {
await this.token.approve(burner, allowance, { from: owner });
await shouldFail.reverting.withMessage(this.token.burnFrom(owner, allowance.addn(1), { from: burner }),
'SafeMath: subtraction overflow'
);
});
});
});
}
module.exports = {
shouldBehaveLikeERC20Burnable,
};