Skip to content

Commit 9f82290

Browse files
authored
First test helpers tests (#1369)
* Removed unused advanceToBlock. * Added advanceBlock tests. * Fixed advanceToBlock tests. * Added single argument tests. * Finished inLogs tests. * Fixed linter errors. * Fixed linter errors.
1 parent eb92fd1 commit 9f82290

File tree

4 files changed

+313
-12
lines changed

4 files changed

+313
-12
lines changed

contracts/mocks/EventEmitter.sol

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
pragma solidity ^0.4.24;
2+
3+
contract EventEmitter {
4+
event Argumentless();
5+
event ShortUint(uint8 value);
6+
event ShortInt(int8 value);
7+
event LongUint(uint256 value);
8+
event LongInt(int256 value);
9+
event Address(address value);
10+
event Boolean(bool value);
11+
event String(string value);
12+
event LongUintBooleanString(
13+
uint256 uintValue,
14+
bool booleanValue,
15+
string stringValue
16+
);
17+
18+
function emitArgumentless() public {
19+
emit Argumentless();
20+
}
21+
22+
function emitShortUint(uint8 value) public {
23+
emit ShortUint(value);
24+
}
25+
26+
function emitShortInt(int8 value) public {
27+
emit ShortInt(value);
28+
}
29+
30+
function emitLongUint(uint256 value) public {
31+
emit LongUint(value);
32+
}
33+
34+
function emitLongInt(int256 value) public {
35+
emit LongInt(value);
36+
}
37+
38+
function emitAddress(address value) public {
39+
emit Address(value);
40+
}
41+
42+
function emitBoolean(bool value) public {
43+
emit Boolean(value);
44+
}
45+
46+
function emitString(string value) public {
47+
emit String(value);
48+
}
49+
50+
function emitLongUintBooleanString(
51+
uint256 uintValue,
52+
bool booleanValue,
53+
string stringValue)
54+
public {
55+
emit LongUintBooleanString(uintValue, booleanValue, stringValue);
56+
}
57+
58+
function emitLongUintAndBoolean(uint256 uintValue, bool boolValue) public {
59+
emit LongUint(uintValue);
60+
emit Boolean(boolValue);
61+
}
62+
}

test/helpers/advanceToBlock.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,6 @@ function advanceBlock () {
1010
});
1111
}
1212

13-
// Advances the block number so that the last mined block is `number`.
14-
async function advanceToBlock (number) {
15-
if (web3.eth.blockNumber > number) {
16-
throw Error(`block number ${number} is in the past (current is ${web3.eth.blockNumber})`);
17-
}
18-
19-
while (web3.eth.blockNumber < number) {
20-
await advanceBlock();
21-
}
22-
}
23-
2413
module.exports = {
2514
advanceBlock,
26-
advanceToBlock,
2715
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const advanceToBlock = require('../advanceToBlock');
2+
3+
const BigNumber = web3.BigNumber;
4+
require('chai')
5+
.use(require('chai-bignumber')(BigNumber))
6+
.should();
7+
8+
describe('advanceToBlock', function () {
9+
beforeEach(function () {
10+
this.startingBlock = web3.eth.blockNumber;
11+
});
12+
13+
describe('advanceBlock', function () {
14+
it('increases the block number by one', async function () {
15+
await advanceToBlock.advanceBlock();
16+
web3.eth.blockNumber.should.be.bignumber.equal(this.startingBlock + 1);
17+
});
18+
});
19+
});

test/helpers/test/expectEvent.test.js

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
const expectEvent = require('../expectEvent');
2+
const EventEmitter = artifacts.require('EventEmitter');
3+
4+
const BigNumber = web3.BigNumber;
5+
const should = require('chai')
6+
.use(require('chai-bignumber')(BigNumber))
7+
.should();
8+
9+
describe('expectEvent', function () {
10+
beforeEach(async function () {
11+
this.emitter = await EventEmitter.new();
12+
});
13+
14+
describe('inLogs', function () {
15+
describe('with no arguments', function () {
16+
beforeEach(async function () {
17+
({ logs: this.logs } = await this.emitter.emitArgumentless());
18+
});
19+
20+
it('accepts emitted events', function () {
21+
expectEvent.inLogs(this.logs, 'Argumentless');
22+
});
23+
24+
it('throws if an unemitted event is requested', function () {
25+
should.Throw(() => expectEvent.inLogs(this.logs, 'UnemittedEvent'));
26+
});
27+
});
28+
29+
describe('with single argument', function () {
30+
context('short uint value', function () {
31+
beforeEach(async function () {
32+
this.value = 42;
33+
({ logs: this.logs } = await this.emitter.emitShortUint(this.value));
34+
});
35+
36+
it('accepts emitted events with correct JavaScript number', function () {
37+
expectEvent.inLogs(this.logs, 'ShortUint', { value: this.value });
38+
});
39+
40+
it('accepts emitted events with correct BigNumber', function () {
41+
expectEvent.inLogs(this.logs, 'ShortUint', { value: new BigNumber(this.value) });
42+
});
43+
44+
it('throws if an unemitted event is requested', function () {
45+
should.Throw(() => expectEvent.inLogs(this.logs, 'UnemittedEvent', { value: this.value }));
46+
});
47+
48+
it('throws if an incorrect value is passed', function () {
49+
should.Throw(() => expectEvent.inLogs(this.logs, 'ShortUint', { value: 23 }));
50+
});
51+
});
52+
53+
context('short int value', function () {
54+
beforeEach(async function () {
55+
this.value = -42;
56+
({ logs: this.logs } = await this.emitter.emitShortInt(this.value));
57+
});
58+
59+
it('accepts emitted events with correct JavaScript number', function () {
60+
expectEvent.inLogs(this.logs, 'ShortInt', { value: this.value });
61+
});
62+
63+
it('accepts emitted events with correct BigNumber', function () {
64+
expectEvent.inLogs(this.logs, 'ShortInt', { value: new BigNumber(this.value) });
65+
});
66+
67+
it('throws if an unemitted event is requested', function () {
68+
should.Throw(() => expectEvent.inLogs(this.logs, 'UnemittedEvent', { value: this.value }));
69+
});
70+
71+
it('throws if an incorrect value is passed', function () {
72+
should.Throw(() => expectEvent.inLogs(this.logs, 'ShortInt', { value: -23 }));
73+
});
74+
});
75+
76+
context('long uint value', function () {
77+
beforeEach(async function () {
78+
this.bigNumValue = new BigNumber('123456789012345678901234567890');
79+
({ logs: this.logs } = await this.emitter.emitLongUint(this.bigNumValue));
80+
});
81+
82+
it('accepts emitted events with correct BigNumber', function () {
83+
expectEvent.inLogs(this.logs, 'LongUint', { value: this.bigNumValue });
84+
});
85+
86+
it('throws if an unemitted event is requested', function () {
87+
should.Throw(() => expectEvent.inLogs(this.logs, 'UnemittedEvent', { value: this.bigNumValue }));
88+
});
89+
90+
it('throws if an incorrect value is passed', function () {
91+
should.Throw(() => expectEvent.inLogs(this.logs, 'LongUint', { value: 2300 }));
92+
});
93+
});
94+
95+
context('long int value', function () {
96+
beforeEach(async function () {
97+
this.bigNumValue = new BigNumber('-123456789012345678901234567890');
98+
({ logs: this.logs } = await this.emitter.emitLongInt(this.bigNumValue));
99+
});
100+
101+
it('accepts emitted events with correct BigNumber', function () {
102+
expectEvent.inLogs(this.logs, 'LongInt', { value: this.bigNumValue });
103+
});
104+
105+
it('throws if an unemitted event is requested', function () {
106+
should.Throw(() => expectEvent.inLogs(this.logs, 'UnemittedEvent', { value: this.bigNumValue }));
107+
});
108+
109+
it('throws if an incorrect value is passed', function () {
110+
should.Throw(() => expectEvent.inLogs(this.logs, 'LongInt', { value: -2300 }));
111+
});
112+
});
113+
114+
context('address value', function () {
115+
beforeEach(async function () {
116+
this.value = '0x811412068e9fbf25dc300a29e5e316f7122b282c';
117+
({ logs: this.logs } = await this.emitter.emitAddress(this.value));
118+
});
119+
120+
it('accepts emitted events with correct address', function () {
121+
expectEvent.inLogs(this.logs, 'Address', { value: this.value });
122+
});
123+
124+
it('throws if an unemitted event is requested', function () {
125+
should.Throw(() => expectEvent.inLogs(this.logs, 'UnemittedEvent', { value: this.value }));
126+
});
127+
128+
it('throws if an incorrect value is passed', function () {
129+
should.Throw(() =>
130+
expectEvent.inLogs(this.logs, 'Address', { value: '0x21d04e022e0b52b5d5bcf90b7f1aabf406be002d' })
131+
);
132+
});
133+
});
134+
135+
context('boolean value', function () {
136+
beforeEach(async function () {
137+
this.value = true;
138+
({ logs: this.logs } = await this.emitter.emitBoolean(this.value));
139+
});
140+
141+
it('accepts emitted events with correct address', function () {
142+
expectEvent.inLogs(this.logs, 'Boolean', { value: this.value });
143+
});
144+
145+
it('throws if an unemitted event is requested', function () {
146+
should.Throw(() => expectEvent.inLogs(this.logs, 'UnemittedEvent', { value: this.value }));
147+
});
148+
149+
it('throws if an incorrect value is passed', function () {
150+
should.Throw(() => expectEvent.inLogs(this.logs, 'Boolean', { value: false }));
151+
});
152+
});
153+
154+
context('string value', function () {
155+
beforeEach(async function () {
156+
this.value = 'OpenZeppelin';
157+
({ logs: this.logs } = await this.emitter.emitString(this.value));
158+
});
159+
160+
it('accepts emitted events with correct string', function () {
161+
expectEvent.inLogs(this.logs, 'String', { value: this.value });
162+
});
163+
164+
it('throws if an unemitted event is requested', function () {
165+
should.Throw(() => expectEvent.inLogs(this.logs, 'UnemittedEvent', { value: this.value }));
166+
});
167+
168+
it('throws if an incorrect value is passed', function () {
169+
should.Throw(() => expectEvent.inLogs(this.logs, 'String', { value: 'ClosedZeppelin' }));
170+
});
171+
});
172+
});
173+
174+
describe('with multiple arguments', function () {
175+
beforeEach(async function () {
176+
this.uintValue = new BigNumber('123456789012345678901234567890');
177+
this.booleanValue = true;
178+
this.stringValue = 'OpenZeppelin';
179+
({ logs: this.logs } =
180+
await this.emitter.emitLongUintBooleanString(this.uintValue, this.booleanValue, this.stringValue));
181+
});
182+
183+
it('accepts correct values', function () {
184+
expectEvent.inLogs(this.logs, 'LongUintBooleanString', {
185+
uintValue: this.uintValue, booleanValue: this.booleanValue, stringValue: this.stringValue,
186+
});
187+
});
188+
189+
it('throws with correct values assigned to wrong arguments', function () {
190+
should.Throw(() => expectEvent.inLogs(this.logs, 'LongUintBooleanString', {
191+
uintValue: this.booleanValue, booleanValue: this.uintValue, stringValue: this.stringValue,
192+
}));
193+
});
194+
195+
it('throws when any of the values is incorrect', function () {
196+
should.Throw(() => expectEvent.inLogs(this.logs, 'LongUintBooleanString', {
197+
uintValue: 23, booleanValue: this.booleanValue, stringValue: this.stringValue,
198+
}));
199+
200+
should.Throw(() => expectEvent.inLogs(this.logs, 'LongUintBooleanString', {
201+
uintValue: this.uintValue, booleanValue: false, stringValue: this.stringValue,
202+
}));
203+
204+
should.Throw(() => expectEvent.inLogs(this.logs, 'LongUintBooleanString', {
205+
uintValue: this.uintValue, booleanValue: this.booleanValue, stringValue: 'ClosedZeppelin',
206+
}));
207+
});
208+
});
209+
210+
describe('with multiple events', function () {
211+
beforeEach(async function () {
212+
this.uintValue = 42;
213+
this.booleanValue = true;
214+
({ logs: this.logs } = await this.emitter.emitLongUintAndBoolean(this.uintValue, this.booleanValue));
215+
});
216+
217+
it('accepts all emitted events with correct values', function () {
218+
expectEvent.inLogs(this.logs, 'LongUint', { value: this.uintValue });
219+
expectEvent.inLogs(this.logs, 'Boolean', { value: this.booleanValue });
220+
});
221+
222+
it('throws if an unemitted event is requested', function () {
223+
should.Throw(() => expectEvent.inLogs(this.logs, 'UnemittedEvent', { value: this.uintValue }));
224+
});
225+
226+
it('throws if incorrect values are passed', function () {
227+
should.Throw(() => expectEvent.inLogs(this.logs, 'LongUint', { value: 23 }));
228+
should.Throw(() => expectEvent.inLogs(this.logs, 'Boolean', { value: false }));
229+
});
230+
});
231+
});
232+
});

0 commit comments

Comments
 (0)