Skip to content

Commit

Permalink
Fix handling BigNumbers array as argument of the event log (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-flexidao authored and marekkirejczyk committed Dec 13, 2019
1 parent db85028 commit 64fc4ae
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
8 changes: 7 additions & 1 deletion lib/matchers/matchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,13 @@ const solidity = (chai: any, utils: any) => {
expectedArgs.length,
actualArgs.length);
for (let index = 0; index < expectedArgs.length; index++) {
new chai.Assertion(expectedArgs[index]).equal(actualArgs[index]);
if (expectedArgs[index].length !== undefined && typeof expectedArgs[index] !== 'string') {
for (let j = 0; j < expectedArgs[index].length; j++) {
new chai.Assertion(expectedArgs[index][j]).equal(actualArgs[index][j]);
}
} else {
new chai.Assertion((expectedArgs[index])).equal((actualArgs[index]));
}
}
};

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"test:nobuild": "export NODE_ENV=test && mocha",
"test": "yarn test:buildonly && yarn test:nobuild",
"lint": "eslint '{lib,test}/**/*.ts'",
"lint:fix": "eslint --fix '{lib,test}/**/*.ts'",
"build": "tsc -p tsconfig.build.json",
"clean": "rimraf ./dist ./test/compiler/build ./test/example/build ./test/matchers/build"
},
Expand Down
13 changes: 12 additions & 1 deletion test/matchers/contracts/events.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ contract Events {

event One(uint value, string msg, bytes32 encoded);
event Two(uint indexed value, string msg);
event Arrays(uint256[3] value, bytes32[2] encoded);

function emitOne() public {
function emitOne() public {
emit One(1, "One", 0x00cFBbaF7DDB3a1476767101c12a0162e241fbAD2a0162e2410cFBbaF7162123);
}

Expand All @@ -19,6 +20,16 @@ contract Events {
emit Two(2, "Two");
}

function emitArrays() public {
emit Arrays([
uint256(1),
uint256(2),
uint256(3)],
[
bytes32(0x00cFBbaF7DDB3a1476767101c12a0162e241fbAD2a0162e2410cFBbaF7162123),
bytes32(0x00cFBbaF7DDB3a1476767101c12a0162e241fbAD2a0162e2410cFBbaF7162124)]);
}

function doNotEmit() pure public {

}
Expand Down
47 changes: 46 additions & 1 deletion test/matchers/events.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {expect, AssertionError} from 'chai';
import {createMockProvider, deployContract, getWallets} from '../../lib';
import Events from './build/Events.json';
import {Contract} from 'ethers';
import {Contract, utils} from 'ethers';

describe('INTEGRATION: Events', () => {
const provider = createMockProvider();
Expand Down Expand Up @@ -104,4 +104,49 @@ describe('INTEGRATION: Events', () => {
'to equal \'0x00cfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf7162123\''
);
});

it('Event with array of BigNumbers and bytes32 types', async () => {
await expect(events.emitArrays()).to.emit(events, 'Arrays')
.withArgs(
[1, 2, 3],
['0x00cfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf7162123',
'0x00cfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf7162124']);
});

it('Event with array of BigNumbers providing bignumbers to the matcher', async () => {
await expect(events.emitArrays()).to.emit(events, 'Arrays')
.withArgs(
[utils.bigNumberify(1), 2, utils.bigNumberify(3)],
['0x00cfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf7162123',
'0x00cfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf7162124']);
});

it('Event with one different arg within array (bytes32)', async () => {
await expect(
expect(events.emitArrays()).to.emit(events, 'Arrays')
.withArgs(
[utils.bigNumberify(1), 2, utils.bigNumberify(3)],
['0x00cfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf7162121',
'0x00cfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf7162124'])
).to.be.eventually.rejectedWith(
AssertionError,
'expected \'0x00cfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf7162121\' ' +
'to equal \'0x00cfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf7162123\''
);
});

it('Event with one different arg within array (BigNumber)', async () => {
await expect(
expect(events.emitArrays()).to.emit(events, 'Arrays')
.withArgs(
[0, 2, 3],
['0x00cfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf7162123',
'0x00cfbbaf7ddb3a1476767101c12a0162e241fbad2a0162e2410cfbbaf7162124'])
).to.be.eventually.rejectedWith(
AssertionError,
// eslint-disable-next-line no-useless-escape
'Expected \"0\" ' +
'to be equal 1'
);
});
});

0 comments on commit 64fc4ae

Please sign in to comment.