Skip to content

Commit

Permalink
Improve ERC20s tests coverage (OpenZeppelin#712)
Browse files Browse the repository at this point in the history
* Improve StandardToken tests coverage
* Improve BasicToken test coverage
* Improve MintableToken test coverage
* Improve BurnableToken test coverage
* Improve PausableToken tests coverage
  • Loading branch information
facuspagnuolo committed Feb 8, 2018
1 parent 889d090 commit cc718bf
Show file tree
Hide file tree
Showing 5 changed files with 898 additions and 187 deletions.
89 changes: 69 additions & 20 deletions test/token/BasicToken.test.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,82 @@
import assertRevert from '../helpers/assertRevert';
const BasicToken = artifacts.require('BasicTokenMock');

var BasicTokenMock = artifacts.require('BasicTokenMock');
contract('StandardToken', function ([_, owner, recipient, anotherAccount]) {
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';

contract('BasicToken', function (accounts) {
it('should return the correct totalSupply after construction', async function () {
let token = await BasicTokenMock.new(accounts[0], 100);
let totalSupply = await token.totalSupply();
beforeEach(async function () {
this.token = await BasicToken.new(owner, 100);
});

describe('total supply', function () {
it('returns the total amount of tokens', async function () {
const totalSupply = await this.token.totalSupply();

assert.equal(totalSupply, 100);
assert.equal(totalSupply, 100);
});
});

it('should return correct balances after transfer', async function () {
let token = await BasicTokenMock.new(accounts[0], 100);
await token.transfer(accounts[1], 100);
describe('balanceOf', function () {
describe('when the requested account has no tokens', function () {
it('returns zero', async function () {
const balance = await this.token.balanceOf(anotherAccount);

let firstAccountBalance = await token.balanceOf(accounts[0]);
assert.equal(firstAccountBalance, 0);
assert.equal(balance, 0);
});
});

let secondAccountBalance = await token.balanceOf(accounts[1]);
assert.equal(secondAccountBalance, 100);
});
describe('when the requested account has some tokens', function () {
it('returns the total amount of tokens', async function () {
const balance = await this.token.balanceOf(owner);

it('should throw an error when trying to transfer more than balance', async function () {
let token = await BasicTokenMock.new(accounts[0], 100);
await assertRevert(token.transfer(accounts[1], 101));
assert.equal(balance, 100);
});
});
});

it('should throw an error when trying to transfer to 0x0', async function () {
let token = await BasicTokenMock.new(accounts[0], 100);
await assertRevert(token.transfer(0x0, 100));
describe('transfer', function () {
describe('when the recipient is not the zero address', function () {
const to = recipient;

describe('when the sender does not have enough balance', function () {
const amount = 101;

it('reverts', async function () {
await assertRevert(this.token.transfer(to, amount, { from: owner }));
});
});

describe('when the sender has enough balance', function () {
const amount = 100;

it('transfers the requested amount', async function () {
await this.token.transfer(to, amount, { from: owner });

const senderBalance = await this.token.balanceOf(owner);
assert.equal(senderBalance, 0);

const recipientBalance = await this.token.balanceOf(to);
assert.equal(recipientBalance, amount);
});

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

assert.equal(logs.length, 1);
assert.equal(logs[0].event, 'Transfer');
assert.equal(logs[0].args.from, owner);
assert.equal(logs[0].args.to, to);
assert(logs[0].args.value.eq(amount));
});
});
});

describe('when the recipient is the zero address', function () {
const to = ZERO_ADDRESS;

it('reverts', async function () {
await assertRevert(this.token.transfer(to, 100, { from: owner }));
});
});
});
});
54 changes: 28 additions & 26 deletions test/token/BurnableToken.test.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@

const EVMRevert = require('../helpers/EVMRevert.js');
import assertRevert from '../helpers/assertRevert';
const BurnableTokenMock = artifacts.require('BurnableTokenMock');
const BigNumber = web3.BigNumber;

require('chai')
.use(require('chai-as-promised'))
.use(require('chai-bignumber')(BigNumber))
.should();
contract('BurnableToken', function ([owner]) {
beforeEach(async function () {
this.token = await BurnableTokenMock.new(owner, 1000);
});

const expect = require('chai').expect;
describe('burn', function () {
const from = owner;

contract('BurnableToken', function (accounts) {
let token;
let expectedTokenSupply = new BigNumber(999);
describe('when the given amount is not greater than balance of the sender', function () {
const amount = 100;

beforeEach(async function () {
token = await BurnableTokenMock.new(accounts[0], 1000);
});
it('burns the requested amount', async function () {
await this.token.burn(amount, { from });

it('owner should be able to burn tokens', async function () {
const { logs } = await token.burn(1, { from: accounts[0] });
const balance = await this.token.balanceOf(from);
assert.equal(balance, 900);
});

const balance = await token.balanceOf(accounts[0]);
balance.should.be.bignumber.equal(expectedTokenSupply);
it('emits a burn event', async function () {
const { logs } = await this.token.burn(amount, { from });

const totalSupply = await token.totalSupply();
totalSupply.should.be.bignumber.equal(expectedTokenSupply);
assert.equal(logs.length, 1);
assert.equal(logs[0].event, 'Burn');
assert.equal(logs[0].args.burner, owner);
assert.equal(logs[0].args.value, amount);
});
});

const event = logs.find(e => e.event === 'Burn');
expect(event).to.exist;
});
describe('when the given amount is greater than the balance of the sender', function () {
const amount = 1001;

it('cannot burn more tokens than your balance', async function () {
await token.burn(2000, { from: accounts[0] })
.should.be.rejectedWith(EVMRevert);
it('reverts', async function () {
await assertRevert(this.token.burn(amount, { from }));
});
});
});
});
151 changes: 122 additions & 29 deletions test/token/MintableToken.test.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,137 @@
import assertRevert from '../helpers/assertRevert';
const MintableToken = artifacts.require('MintableToken');

import expectThrow from '../helpers/expectThrow';
var MintableToken = artifacts.require('MintableToken');

contract('Mintable', function (accounts) {
let token;

contract('Mintable', function ([owner, anotherAccount]) {
beforeEach(async function () {
token = await MintableToken.new();
this.token = await MintableToken.new({ from: owner });
});

it('should start with a totalSupply of 0', async function () {
let totalSupply = await token.totalSupply();
describe('minting finished', function () {
describe('when the token is not finished', function () {
it('returns false', async function () {
const mintingFinished = await this.token.mintingFinished();
assert.equal(mintingFinished, false);
});
});

describe('when the token is finished', function () {
beforeEach(async function () {
await this.token.finishMinting({ from: owner });
});

assert.equal(totalSupply, 0);
it('returns true', async function () {
const mintingFinished = await this.token.mintingFinished.call();
assert.equal(mintingFinished, true);
});
});
});

it('should return mintingFinished false after construction', async function () {
let mintingFinished = await token.mintingFinished();
describe('finish minting', function () {
describe('when the sender is the token owner', function () {
const from = owner;

assert.equal(mintingFinished, false);
});
describe('when the token was not finished', function () {
it('finishes token minting', async function () {
await this.token.finishMinting({ from });

const mintingFinished = await this.token.mintingFinished();
assert.equal(mintingFinished, true);
});

it('emits a mint finished event', async function () {
const { logs } = await this.token.finishMinting({ from });

it('should mint a given amount of tokens to a given address', async function () {
const result = await token.mint(accounts[0], 100);
assert.equal(result.logs[0].event, 'Mint');
assert.equal(result.logs[0].args.to.valueOf(), accounts[0]);
assert.equal(result.logs[0].args.amount.valueOf(), 100);
assert.equal(result.logs[1].event, 'Transfer');
assert.equal(result.logs[1].args.from.valueOf(), 0x0);
assert.equal(logs.length, 1);
assert.equal(logs[0].event, 'MintFinished');
});
});

let balance0 = await token.balanceOf(accounts[0]);
assert(balance0, 100);
describe('when the token was already finished', function () {
beforeEach(async function () {
await this.token.finishMinting({ from });
});

let totalSupply = await token.totalSupply();
assert(totalSupply, 100);
it('reverts', async function () {
await assertRevert(this.token.finishMinting({ from }));
});
});
});

describe('when the sender is not the token owner', function () {
const from = anotherAccount;

describe('when the token was not finished', function () {
it('reverts', async function () {
await assertRevert(this.token.finishMinting({ from }));
});
});

describe('when the token was already finished', function () {
beforeEach(async function () {
await this.token.finishMinting({ from: owner });
});

it('reverts', async function () {
await assertRevert(this.token.finishMinting({ from }));
});
});
});
});

it('should fail to mint after call to finishMinting', async function () {
await token.finishMinting();
assert.equal(await token.mintingFinished(), true);
await expectThrow(token.mint(accounts[0], 100));
describe('mint', function () {
const amount = 100;

describe('when the sender is the token owner', function () {
const from = owner;

describe('when the token was not finished', function () {
it('mints the requested amount', async function () {
await this.token.mint(owner, amount, { from });

const balance = await this.token.balanceOf(owner);
assert.equal(balance, amount);
});

it('emits a mint finished event', async function () {
const { logs } = await this.token.mint(owner, amount, { from });

assert.equal(logs.length, 2);
assert.equal(logs[0].event, 'Mint');
assert.equal(logs[0].args.to, owner);
assert.equal(logs[0].args.amount, amount);
assert.equal(logs[1].event, 'Transfer');
});
});

describe('when the token minting is finished', function () {
beforeEach(async function () {
await this.token.finishMinting({ from });
});

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

describe('when the sender is not the token owner', function () {
const from = anotherAccount;

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

describe('when the token was already finished', function () {
beforeEach(async function () {
await this.token.finishMinting({ from: owner });
});

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

0 comments on commit cc718bf

Please sign in to comment.