From 0cdbbd553a2afec6539280da1798933c77c2b692 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Venturo?= Date: Thu, 22 Nov 2018 16:17:25 -0300 Subject: [PATCH] Changed inTransaction, removed decodeLogs. --- package-lock.json | 7 +++--- package.json | 3 +++ test/helpers/decodeLogs.js | 12 ----------- test/helpers/expectEvent.js | 25 ++++++++++++++------- test/helpers/test/expectEvent.test.js | 31 +++++++++++++++++++++------ 5 files changed, 47 insertions(+), 31 deletions(-) delete mode 100644 test/helpers/decodeLogs.js diff --git a/package-lock.json b/package-lock.json index a5076897f..de5100da3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6894,10 +6894,9 @@ } }, "lodash": { - "version": "4.17.10", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.10.tgz", - "integrity": "sha512-UejweD1pDoXu+AD825lWwp4ZGtSwgnpZxb3JDViD7StjQz+Nb/6l093lx4OQ0foGWNRoc19mWy7BzL+UAK2iVg==", - "dev": true + "version": "4.17.11", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.11.tgz", + "integrity": "sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==" }, "lodash.assign": { "version": "4.2.0", diff --git a/package.json b/package.json index b12d55e84..e008072b5 100644 --- a/package.json +++ b/package.json @@ -56,5 +56,8 @@ "truffle": "^4.1.13", "truffle-hdwallet-provider": "0.0.5", "web3-utils": "^1.0.0-beta.34" + }, + "dependencies": { + "lodash": "^4.17.11" } } diff --git a/test/helpers/decodeLogs.js b/test/helpers/decodeLogs.js deleted file mode 100644 index 2b671c92b..000000000 --- a/test/helpers/decodeLogs.js +++ /dev/null @@ -1,12 +0,0 @@ -const SolidityEvent = require('web3/lib/web3/event.js'); - -function decodeLogs (logs, events, address) { - return logs.map(log => { - const event = new SolidityEvent(null, events[log.topics[0]], address); - return event.decode(log); - }); -} - -module.exports = { - decodeLogs, -}; diff --git a/test/helpers/expectEvent.js b/test/helpers/expectEvent.js index 87dfeb542..eb12bfd24 100644 --- a/test/helpers/expectEvent.js +++ b/test/helpers/expectEvent.js @@ -1,4 +1,5 @@ -const { decodeLogs } = require('./decodeLogs'); +const SolidityEvent = require('web3/lib/web3/event.js'); +const _ = require('lodash'); const BigNumber = web3.BigNumber; const should = require('chai') @@ -18,14 +19,13 @@ function inLogs (logs, eventName, eventArgs = {}) { return event; } -async function inTransaction (tx, eventName, eventArgs = {}) { - const { logs } = await tx; - return inLogs(logs, eventName, eventArgs); +async function inConstruction (contract, eventName, eventArgs = {}) { + return inTransaction(contract.transactionHash, contract.constructor, eventName, eventArgs); } -async function inConstruction (contract, eventName, eventArgs = {}) { - const receipt = await web3.eth.getTransactionReceipt(contract.transactionHash); - const logs = decodeLogs(receipt.logs, contract.constructor.events, contract.address); +async function inTransaction (txHash, emitter, eventName, eventArgs = {}) { + const receipt = await web3.eth.getTransactionReceipt(txHash); + const logs = decodeLogs(receipt.logs, emitter.events); return inLogs(logs, eventName, eventArgs); } @@ -44,8 +44,17 @@ function isBigNumber (object) { (object.constructor && object.constructor.name === 'BigNumber'); } +function decodeLogs (logs, events) { + return _.flatten(logs.map(log => + log.topics.filter(topic => topic in events).map(topic => { + const event = new SolidityEvent(null, events[topic], 0); + return event.decode(log); + }) + )); +} + module.exports = { inLogs, - inTransaction, inConstruction, + inTransaction, }; diff --git a/test/helpers/test/expectEvent.test.js b/test/helpers/test/expectEvent.test.js index 3d58385eb..df5de3ceb 100644 --- a/test/helpers/test/expectEvent.test.js +++ b/test/helpers/test/expectEvent.test.js @@ -7,7 +7,7 @@ const should = require('chai') .use(require('chai-as-promised')) .should(); -describe('expectEvent', function () { +describe.only('expectEvent', function () { beforeEach(async function () { this.constructionValues = { uint: 42, @@ -23,15 +23,15 @@ describe('expectEvent', function () { }); describe('inConstructor', function () { - context('long uint value', function () { + context('short uint value', function () { it('accepts emitted events with correct number', async function () { - await expectEvent.inConstruction(this.emitter, 'LongUint', + await expectEvent.inConstruction(this.emitter, 'ShortUint', { value: this.constructionValues.uint } ); }); it('throws if an incorrect value is passed', async function () { - return expectEvent.inConstruction(this.emitter, 'LongUint', + return expectEvent.inConstruction(this.emitter, 'ShortUint', { value: 23 } ).should.be.rejected; }); @@ -290,9 +290,26 @@ describe('expectEvent', function () { }); describe('inTransaction', function () { - it('processes the logs inside a transaction', async function () { - const tx = await this.emitter.emitShortUint(5); - expectEvent.inTransaction(tx, 'ShortInt', { value: 5 }); + describe('when emitting from called contract', function () { + context('short uint value', function () { + beforeEach(async function () { + this.value = 42; + const receipt = await this.emitter.emitShortUint(this.value); + this.txHash = receipt.tx; + }); + + it('accepts emitted events with correct number', async function () { + await expectEvent.inTransaction(this.txHash, EventEmitter, 'ShortUint', { value: this.value }); + }); + + it('throws if an unemitted event is requested', function () { + return expectEvent.inTransaction(this.txHash, EventEmitter, 'UnemittedEvent', { value: this.value }).should.be.rejected; + }); + + it('throws if an incorrect value is passed', function () { + return expectEvent.inTransaction(this.txHash, EventEmitter, 'ShortUint', { value: 23 }).should.be.rejected; + }); + }); }); }); });