diff --git a/contracts/mocks/EventEmitter.sol b/contracts/mocks/EventEmitter.sol index 257b23651..74e479b5c 100644 --- a/contracts/mocks/EventEmitter.sol +++ b/contracts/mocks/EventEmitter.sol @@ -11,6 +11,12 @@ contract EventEmitter { event String(string value); event LongUintBooleanString(uint256 uintValue, bool booleanValue, string stringValue); + constructor (uint256 uintValue, bool booleanValue, string stringValue) public { + emit LongUint(uintValue); + emit Boolean(booleanValue); + emit String(stringValue); + } + function emitArgumentless() public { emit Argumentless(); } diff --git a/package-lock.json b/package-lock.json index 9e2fe893b..a5076897f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1970,6 +1970,15 @@ "type-detect": "^4.0.0" } }, + "chai-as-promised": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/chai-as-promised/-/chai-as-promised-7.1.1.tgz", + "integrity": "sha512-azL6xMoi+uxu6z4rhWQ1jbdUhOMhis2PvscD/xjLqNMkv3BPPp2JyyuTHOrf9BOosGpNQ11v6BKv/g57RXbiaA==", + "dev": true, + "requires": { + "check-error": "^1.0.2" + } + }, "chai-bignumber": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/chai-bignumber/-/chai-bignumber-2.0.2.tgz", diff --git a/package.json b/package.json index f97f1050a..b12d55e84 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "homepage": "https://github.com/OpenZeppelin/zeppelin-solidity", "devDependencies": { "chai": "^4.1.2", + "chai-as-promised": "^7.1.1", "chai-bignumber": "^2.0.2", "coveralls": "^3.0.1", "dotenv": "^4.0.0", diff --git a/test/helpers/decodeLogs.js b/test/helpers/decodeLogs.js index 1f1bc946c..2b671c92b 100644 --- a/test/helpers/decodeLogs.js +++ b/test/helpers/decodeLogs.js @@ -1,8 +1,8 @@ const SolidityEvent = require('web3/lib/web3/event.js'); -function decodeLogs (logs, contract, address) { +function decodeLogs (logs, events, address) { return logs.map(log => { - const event = new SolidityEvent(null, contract.events[log.topics[0]], address); + const event = new SolidityEvent(null, events[log.topics[0]], address); return event.decode(log); }); } diff --git a/test/helpers/expectEvent.js b/test/helpers/expectEvent.js index b857b6636..87dfeb542 100644 --- a/test/helpers/expectEvent.js +++ b/test/helpers/expectEvent.js @@ -1,3 +1,5 @@ +const { decodeLogs } = require('./decodeLogs'); + const BigNumber = web3.BigNumber; const should = require('chai') .use(require('chai-bignumber')(BigNumber)) @@ -21,6 +23,13 @@ async function inTransaction (tx, eventName, eventArgs = {}) { return inLogs(logs, 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); + + return inLogs(logs, eventName, eventArgs); +} + function contains (args, key, value) { if (isBigNumber(args[key])) { args[key].should.be.bignumber.equal(value); @@ -38,4 +47,5 @@ function isBigNumber (object) { module.exports = { inLogs, inTransaction, + inConstruction, }; diff --git a/test/helpers/test/expectEvent.test.js b/test/helpers/test/expectEvent.test.js index 8d162faec..3d58385eb 100644 --- a/test/helpers/test/expectEvent.test.js +++ b/test/helpers/test/expectEvent.test.js @@ -4,11 +4,70 @@ const EventEmitter = artifacts.require('EventEmitter'); const BigNumber = web3.BigNumber; const should = require('chai') .use(require('chai-bignumber')(BigNumber)) + .use(require('chai-as-promised')) .should(); describe('expectEvent', function () { beforeEach(async function () { - this.emitter = await EventEmitter.new(); + this.constructionValues = { + uint: 42, + boolean: true, + string: 'OpenZeppelin', + }; + + this.emitter = await EventEmitter.new( + this.constructionValues.uint, + this.constructionValues.boolean, + this.constructionValues.string + ); + }); + + describe('inConstructor', function () { + context('long uint value', function () { + it('accepts emitted events with correct number', async function () { + await expectEvent.inConstruction(this.emitter, 'LongUint', + { value: this.constructionValues.uint } + ); + }); + + it('throws if an incorrect value is passed', async function () { + return expectEvent.inConstruction(this.emitter, 'LongUint', + { value: 23 } + ).should.be.rejected; + }); + }); + + context('boolean value', function () { + it('accepts emitted events with correct value', async function () { + await expectEvent.inConstruction(this.emitter, 'Boolean', + { value: this.constructionValues.boolean } + ); + }); + + it('throws if an incorrect value is passed', async function () { + return expectEvent.inConstruction(this.emitter, 'Boolean', + { value: !this.constructionValues.boolean } + ).should.be.rejected; + }); + }); + + context('string value', function () { + it('accepts emitted events with correct string', async function () { + await expectEvent.inConstruction(this.emitter, 'String', + { value: this.constructionValues.string } + ); + }); + + it('throws if an incorrect string is passed', async function () { + return expectEvent.inConstruction(this.emitter, 'String', + { value: 'ClosedZeppelin' } + ).should.be.rejected; + }); + }); + + it('throws if an unemitted event is requested', async function () { + return expectEvent.inConstruction(this.emitter, 'UnemittedEvent').should.be.rejected; + }); }); describe('inLogs', function () {