Skip to content

Commit ecd808c

Browse files
authored
Add test for modifiers with post-conditions (#835)
1 parent 54bfdbd commit ecd808c

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
pragma solidity ^0.7.0;
2+
3+
contract Test {
4+
bool precondition = true;
5+
bool postcondition = true;
6+
7+
modifier m {
8+
require(precondition);
9+
_;
10+
require(postcondition);
11+
}
12+
13+
function flip_precondition() public {
14+
precondition = !precondition;
15+
}
16+
17+
function flip_postcondition() public {
18+
postcondition = !postcondition;
19+
}
20+
21+
function a() m public {
22+
uint x = 5;
23+
}
24+
}

test/units/modifiers.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,44 @@ describe('modifiers', () => {
166166
});
167167
});
168168

169+
it('should cover when there are post-conditions', async function() {
170+
const contract = await util.bootstrapCoverage('modifiers/postconditions', api);
171+
coverage.addContract(contract.instrumented, util.filePath);
172+
173+
// Both true
174+
await contract.instance.a();
175+
176+
// Precondition false
177+
await contract.instance.flip_precondition();
178+
try {
179+
await contract.instance.a();
180+
} catch(e) { /*ignore*/ }
181+
182+
// Reset precondition to true, set postcondition false
183+
await contract.instance.flip_precondition();
184+
await contract.instance.flip_postcondition();
185+
186+
// Postcondition false
187+
try {
188+
await contract.instance.a();
189+
} catch(e) { /*ignore*/ }
190+
191+
const mapping = coverage.generate(contract.data, util.pathPrefix);
192+
193+
assert.deepEqual(mapping[util.filePath].l, {
194+
8:5, 9:3, 10:3, 14:2, 18:1, 22:3
195+
});
196+
assert.deepEqual(mapping[util.filePath].b, {
197+
1:[3,2], 2:[1,2], 3:[3,2],
198+
});
199+
assert.deepEqual(mapping[util.filePath].s, {
200+
1:5, 2:3, 3:3
201+
});
202+
assert.deepEqual(mapping[util.filePath].f, {
203+
1:5, 2:2, 3:1, 4:3
204+
});
205+
});
206+
169207
// Case: when first modifier always suceeds but a subsequent modifier succeeds and fails,
170208
// there should be a missing `else` branch on first modifier
171209
it('should not be influenced by revert from a subsequent modifier', async function() {

0 commit comments

Comments
 (0)