Skip to content

Commit

Permalink
Add BigNumber support to expectEvent/inLogs (#1026) (#1338)
Browse files Browse the repository at this point in the history
* Add BigNumber support to expectEvent/inLogs (#1026)

* switched direct logs array check to expectEvent method in AllowanceCrowdsale.test.js

* Refactor expectEvent.inLogs function to use simple value number check

* Introduced should.be.bignumber method to compare BigNumber values

* Destructure transaction object to extract logs field
  • Loading branch information
jbogacz authored and nventuro committed Sep 26, 2018
1 parent ae109f6 commit 947de54
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
17 changes: 10 additions & 7 deletions test/crowdsale/AllowanceCrowdsale.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const expectEvent = require('../helpers/expectEvent');
const { ether } = require('../helpers/ether');
const { assertRevert } = require('../helpers/assertRevert');
const { ethGetBalance } = require('../helpers/web3');

const BigNumber = web3.BigNumber;

const should = require('chai')
require('chai')
.use(require('chai-bignumber')(BigNumber))
.should();

Expand Down Expand Up @@ -41,12 +42,14 @@ contract('AllowanceCrowdsale', function ([_, investor, wallet, purchaser, tokenW
describe('high-level purchase', function () {
it('should log purchase', async function () {
const { logs } = await this.crowdsale.sendTransaction({ value: value, from: investor });
const event = logs.find(e => e.event === 'TokensPurchased');
should.exist(event);
event.args.purchaser.should.equal(investor);
event.args.beneficiary.should.equal(investor);
event.args.value.should.be.bignumber.equal(value);
event.args.amount.should.be.bignumber.equal(expectedTokenAmount);
expectEvent.inLogs(
logs,
'TokensPurchased',
{
purchaser: investor,
beneficiary: investor,
value: value,
amount: expectedTokenAmount });
});

it('should assign tokens to sender', async function () {
Expand Down
32 changes: 20 additions & 12 deletions test/helpers/expectEvent.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
const should = require('chai').should();
const BigNumber = web3.BigNumber;
const should = require('chai')
.use(require('chai-bignumber')(BigNumber))
.should();

function inLogs (logs, eventName, eventArgs = {}) {
const event = logs.find(function (e) {
if (e.event === eventName) {
let matches = true;

for (const [k, v] of Object.entries(eventArgs)) {
if (e.args[k] !== v) {
matches = false;
}
}

if (matches) {
return true;
contains(e.args, k, v);
}
return true;
}
});

should.exist(event);

return event;
}

Expand All @@ -27,6 +21,20 @@ async function inTransaction (tx, eventName, eventArgs = {}) {
return inLogs(logs, eventName, eventArgs);
}

function contains (args, key, value) {
if (isBigNumber(args[key])) {
args[key].should.be.bignumber.equal(value);
} else {
args[key].should.be.equal(value);
}
}

function isBigNumber (object) {
return object.isBigNumber ||
object instanceof BigNumber ||
(object.constructor && object.constructor.name === 'BigNumber');
}

module.exports = {
inLogs,
inTransaction,
Expand Down

0 comments on commit 947de54

Please sign in to comment.