Skip to content

Commit 947de54

Browse files
jbogacznventuro
authored andcommitted
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
1 parent ae109f6 commit 947de54

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

test/crowdsale/AllowanceCrowdsale.test.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
const expectEvent = require('../helpers/expectEvent');
12
const { ether } = require('../helpers/ether');
23
const { assertRevert } = require('../helpers/assertRevert');
34
const { ethGetBalance } = require('../helpers/web3');
45

56
const BigNumber = web3.BigNumber;
67

7-
const should = require('chai')
8+
require('chai')
89
.use(require('chai-bignumber')(BigNumber))
910
.should();
1011

@@ -41,12 +42,14 @@ contract('AllowanceCrowdsale', function ([_, investor, wallet, purchaser, tokenW
4142
describe('high-level purchase', function () {
4243
it('should log purchase', async function () {
4344
const { logs } = await this.crowdsale.sendTransaction({ value: value, from: investor });
44-
const event = logs.find(e => e.event === 'TokensPurchased');
45-
should.exist(event);
46-
event.args.purchaser.should.equal(investor);
47-
event.args.beneficiary.should.equal(investor);
48-
event.args.value.should.be.bignumber.equal(value);
49-
event.args.amount.should.be.bignumber.equal(expectedTokenAmount);
45+
expectEvent.inLogs(
46+
logs,
47+
'TokensPurchased',
48+
{
49+
purchaser: investor,
50+
beneficiary: investor,
51+
value: value,
52+
amount: expectedTokenAmount });
5053
});
5154

5255
it('should assign tokens to sender', async function () {

test/helpers/expectEvent.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
1-
const should = require('chai').should();
1+
const BigNumber = web3.BigNumber;
2+
const should = require('chai')
3+
.use(require('chai-bignumber')(BigNumber))
4+
.should();
25

36
function inLogs (logs, eventName, eventArgs = {}) {
47
const event = logs.find(function (e) {
58
if (e.event === eventName) {
6-
let matches = true;
7-
89
for (const [k, v] of Object.entries(eventArgs)) {
9-
if (e.args[k] !== v) {
10-
matches = false;
11-
}
12-
}
13-
14-
if (matches) {
15-
return true;
10+
contains(e.args, k, v);
1611
}
12+
return true;
1713
}
1814
});
19-
2015
should.exist(event);
21-
2216
return event;
2317
}
2418

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

24+
function contains (args, key, value) {
25+
if (isBigNumber(args[key])) {
26+
args[key].should.be.bignumber.equal(value);
27+
} else {
28+
args[key].should.be.equal(value);
29+
}
30+
}
31+
32+
function isBigNumber (object) {
33+
return object.isBigNumber ||
34+
object instanceof BigNumber ||
35+
(object.constructor && object.constructor.name === 'BigNumber');
36+
}
37+
3038
module.exports = {
3139
inLogs,
3240
inTransaction,

0 commit comments

Comments
 (0)