Add BigNumber support to expectEvent/inLogs (#1026) (#1338)

* 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
pull/1343/head
Jakub Bogacz 6 years ago committed by Nicolás Venturo
parent ae109f69cc
commit 947de54cee
  1. 17
      test/crowdsale/AllowanceCrowdsale.test.js
  2. 32
      test/helpers/expectEvent.js

@ -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();
@ -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 () {

@ -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;
}
@ -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,

Loading…
Cancel
Save