Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Added mint and burn tests for zero amounts.
  • Loading branch information
nventuro committed Aug 22, 2018
commit ccce43b7f2d09ecd2c59d95daa932f564ba97963
102 changes: 60 additions & 42 deletions test/token/ERC20/BurnableToken.behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,36 @@ require('chai')
function shouldBehaveLikeBurnableToken (owner, initialBalance, [burner]) {
describe('burn', function () {
describe('when the given amount is not greater than balance of the sender', function () {
const amount = 100;

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 - amount);
context('for a zero amount', function () {
shouldBurn(0);
});

it('emits a burn event', async function () {
const event = expectEvent.inLogs(this.logs, 'Burn');
event.args.burner.should.eq(owner);
event.args.value.should.be.bignumber.equal(amount);
context('for a non-zero amount', function () {
shouldBurn(100);
});

it('emits a transfer event', async function () {
const event = expectEvent.inLogs(this.logs, 'Transfer');
event.args.from.should.eq(owner);
event.args.to.should.eq(ZERO_ADDRESS);
event.args.value.should.be.bignumber.equal(amount);
});
function shouldBurn (amount) {
beforeEach(async function () {
({ logs: this.logs } = await this.token.burn(amount, { from: owner }));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this this.logs works? amazing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, I copied it over from the Burnable tests

});

it('burns the requested amount', async function () {
(await this.token.balanceOf(owner)).should.be.bignumber.equal(initialBalance - amount);
});

it('emits a burn event', async function () {
const event = expectEvent.inLogs(this.logs, 'Burn');
event.args.burner.should.eq(owner);
event.args.value.should.be.bignumber.equal(amount);
});

it('emits a transfer event', async function () {
const event = expectEvent.inLogs(this.logs, 'Transfer');
event.args.from.should.eq(owner);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these equals checks should be in the inLogs final argument. and, barring that, should be equals()

Copy link
Contributor Author

@nventuro nventuro Aug 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come #1219 didn't cover that? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #1231. Regarding the log tests, I'd rather we fixed all of those at once in #1232

event.args.to.should.eq(ZERO_ADDRESS);
event.args.value.should.be.bignumber.equal(amount);
});
}
});

describe('when the given amount is greater than the balance of the sender', function () {
Expand All @@ -46,34 +54,44 @@ function shouldBehaveLikeBurnableToken (owner, initialBalance, [burner]) {

describe('burnFrom', function () {
describe('on success', function () {
const amount = 100;

beforeEach(async function () {
await this.token.approve(burner, 300, { 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 - amount);
context('for a zero amount', function () {
shouldBurnFrom(0);
});

it('decrements allowance', async function () {
(await this.token.allowance(owner, burner)).should.be.bignumber.equal(200);
context('for a non-zero amount', function () {
shouldBurnFrom(100);
});

it('emits a burn event', async function () {
const event = expectEvent.inLogs(this.logs, 'Burn');
event.args.burner.should.eq(owner);
event.args.value.should.be.bignumber.equal(amount);
});

it('emits a transfer event', async function () {
const event = expectEvent.inLogs(this.logs, 'Transfer');
event.args.from.should.eq(owner);
event.args.to.should.eq(ZERO_ADDRESS);
event.args.value.should.be.bignumber.equal(amount);
});
function shouldBurnFrom (amount) {
const originalAllowance = amount * 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 - amount);
});

it('decrements allowance', async function () {
(await this.token.allowance(owner, burner)).should.be.bignumber.equal(originalAllowance - amount);
});

it('emits a burn event', async function () {
const event = expectEvent.inLogs(this.logs, 'Burn');
event.args.burner.should.eq(owner);
event.args.value.should.be.bignumber.equal(amount);
});

it('emits a transfer event', async function () {
const event = expectEvent.inLogs(this.logs, 'Transfer');
event.args.from.should.eq(owner);
event.args.to.should.eq(ZERO_ADDRESS);
event.args.value.should.be.bignumber.equal(amount);
});
}
});

describe('when the given amount is greater than the balance of the sender', function () {
Expand Down
42 changes: 26 additions & 16 deletions test/token/ERC20/MintableToken.behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,26 +93,36 @@ function shouldBehaveLikeMintableToken (owner, minter, [anyone]) {
const from = minter;

describe('when the token minting is not finished', function () {
it('mints the requested amount', async function () {
await this.token.mint(owner, amount, { from });
context('for a zero amount', function () {
shouldMint(0);
});

(await this.token.balanceOf(owner)).should.be.bignumber.equal(amount);
context('for a non-zero amount', function () {
shouldMint(amount);
});

it('emits a mint and a transfer event', async function () {
const { logs } = await this.token.mint(owner, amount, { from });
function shouldMint (amount) {
beforeEach(async function () {
({ logs: this.logs } = await this.token.mint(anyone, amount, { from }));
});

const mintEvent = expectEvent.inLogs(logs, 'Mint', {
to: owner,
it('mints the requested amount', async function () {
(await this.token.balanceOf(anyone)).should.be.bignumber.equal(amount);
});
mintEvent.args.amount.should.be.bignumber.equal(amount);

const transferEvent = expectEvent.inLogs(logs, 'Transfer', {
from: ZERO_ADDRESS,
to: owner,
it('emits a mint and a transfer event', async function () {
const mintEvent = expectEvent.inLogs(this.logs, 'Mint', {
to: anyone,
});
mintEvent.args.amount.should.be.bignumber.equal(amount);

const transferEvent = expectEvent.inLogs(this.logs, 'Transfer', {
from: ZERO_ADDRESS,
to: anyone,
});
transferEvent.args.value.should.be.bignumber.equal(amount);
});
transferEvent.args.value.should.be.bignumber.equal(amount);
});
}
});

describe('when the token minting is finished', function () {
Expand All @@ -121,7 +131,7 @@ function shouldBehaveLikeMintableToken (owner, minter, [anyone]) {
});

it('reverts', async function () {
await assertRevert(this.token.mint(owner, amount, { from }));
await assertRevert(this.token.mint(anyone, amount, { from }));
});
});
});
Expand All @@ -131,7 +141,7 @@ function shouldBehaveLikeMintableToken (owner, minter, [anyone]) {

describe('when the token minting is not finished', function () {
it('reverts', async function () {
await assertRevert(this.token.mint(owner, amount, { from }));
await assertRevert(this.token.mint(anyone, amount, { from }));
});
});

Expand All @@ -141,7 +151,7 @@ function shouldBehaveLikeMintableToken (owner, minter, [anyone]) {
});

it('reverts', async function () {
await assertRevert(this.token.mint(owner, amount, { from }));
await assertRevert(this.token.mint(anyone, amount, { from }));
});
});
});
Expand Down