forked from foundry-rs/foundry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpectRevert.t.sol
More file actions
442 lines (356 loc) · 12.5 KB
/
Copy pathExpectRevert.t.sol
File metadata and controls
442 lines (356 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity ^0.8.18;
import "ds-test/test.sol";
import "cheats/Vm.sol";
contract Reverter {
error CustomError();
function revertWithMessage(string memory message) public pure {
revert(message);
}
function doNotRevert() public pure {}
function panic() public pure returns (uint256) {
return uint256(100) - uint256(101);
}
function revertWithCustomError() public pure {
revert CustomError();
}
function nestedRevert(Reverter inner, string memory message) public pure {
inner.revertWithMessage(message);
}
function callThenRevert(Dummy dummy, string memory message) public pure {
dummy.callMe();
revert(message);
}
function callThenNoRevert(Dummy dummy) public pure {
dummy.callMe();
}
function revertWithoutReason() public pure {
revert();
}
}
contract ConstructorReverter {
constructor(string memory message) {
revert(message);
}
}
/// Used to ensure that the dummy data from `vm.expectRevert`
/// is large enough to decode big structs.
///
/// The struct is based on issue #2454
struct LargeDummyStruct {
address a;
uint256 b;
bool c;
address d;
address e;
string f;
address[8] g;
address h;
uint256 i;
}
contract Dummy {
function callMe() public pure returns (string memory) {
return "thanks for calling";
}
function largeReturnType() public pure returns (LargeDummyStruct memory) {
revert("reverted with large return type");
}
}
contract ExpectRevertTest is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);
function shouldRevert() internal {
revert();
}
function testExpectRevertString() public {
Reverter reverter = new Reverter();
vm.expectRevert("revert");
reverter.revertWithMessage("revert");
}
function testShouldFailIfExpectRevertWrongString() public {
Reverter reverter = new Reverter();
vm.expectRevert("my not so cool error", 0);
reverter.revertWithMessage("my cool error");
}
function testExpectRevertConstructor() public {
vm.expectRevert("constructor revert");
new ConstructorReverter("constructor revert");
}
function testExpectRevertBuiltin() public {
Reverter reverter = new Reverter();
vm.expectRevert(abi.encodeWithSignature("Panic(uint256)", 0x11));
reverter.panic();
}
function testExpectRevertCustomError() public {
Reverter reverter = new Reverter();
vm.expectRevert(abi.encodePacked(Reverter.CustomError.selector));
reverter.revertWithCustomError();
}
function testExpectRevertNested() public {
Reverter reverter = new Reverter();
Reverter inner = new Reverter();
vm.expectRevert("nested revert");
reverter.nestedRevert(inner, "nested revert");
}
function testExpectRevertCallsThenReverts() public {
Reverter reverter = new Reverter();
Dummy dummy = new Dummy();
vm.expectRevert("called a function and then reverted");
reverter.callThenRevert(dummy, "called a function and then reverted");
}
function testDummyReturnDataForBigType() public {
Dummy dummy = new Dummy();
vm.expectRevert("reverted with large return type");
dummy.largeReturnType();
}
function testExpectRevertNoReason() public {
Reverter reverter = new Reverter();
vm.expectRevert(bytes(""));
reverter.revertWithoutReason();
}
function testExpectRevertAnyRevert() public {
vm.expectRevert();
new ConstructorReverter("hello this is a revert message");
Reverter reverter = new Reverter();
vm.expectRevert();
reverter.revertWithMessage("this is also a revert message");
vm.expectRevert();
reverter.panic();
vm.expectRevert();
reverter.revertWithCustomError();
Reverter reverter2 = new Reverter();
vm.expectRevert();
reverter.nestedRevert(reverter2, "this too is a revert message");
Dummy dummy = new Dummy();
vm.expectRevert();
reverter.callThenRevert(dummy, "revert message 4 i ran out of synonims for also");
vm.expectRevert();
reverter.revertWithoutReason();
}
function testexpectCheatcodeRevert() public {
vm._expectCheatcodeRevert('JSON value at ".a" is not an object');
vm.parseJsonKeys('{"a": "b"}', ".a");
}
}
contract AContract {
BContract bContract;
CContract cContract;
constructor(BContract _bContract, CContract _cContract) {
bContract = _bContract;
cContract = _cContract;
}
function callAndRevert() public pure {
require(1 > 2, "Reverted by AContract");
}
function callAndRevertInBContract() public {
bContract.callAndRevert();
}
function callAndRevertInCContract() public {
cContract.callAndRevert();
}
function callAndRevertInCContractThroughBContract() public {
bContract.callAndRevertInCContract();
}
function createDContract() public {
new DContract();
}
function createDContractThroughBContract() public {
bContract.createDContract();
}
function createDContractThroughCContract() public {
cContract.createDContract();
}
function doNotRevert() public {}
}
contract BContract {
CContract cContract;
constructor(CContract _cContract) {
cContract = _cContract;
}
function callAndRevert() public pure {
require(1 > 2, "Reverted by BContract");
}
function callAndRevertInCContract() public {
this.doNotRevert();
cContract.doNotRevert();
cContract.callAndRevert();
}
function createDContract() public {
this.doNotRevert();
cContract.doNotRevert();
new DContract();
}
function createDContractThroughCContract() public {
this.doNotRevert();
cContract.doNotRevert();
cContract.createDContract();
}
function doNotRevert() public {}
}
contract CContract {
error CContractError(string reason);
function callAndRevert() public pure {
revert CContractError("Reverted by CContract");
}
function createDContract() public {
new DContract();
}
function doNotRevert() public {}
}
contract DContract {
constructor() {
require(1 > 2, "Reverted by DContract");
}
}
contract ExpectRevertWithReverterTest is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);
error CContractError(string reason);
AContract aContract;
BContract bContract;
CContract cContract;
function setUp() public {
cContract = new CContract();
bContract = new BContract(cContract);
aContract = new AContract(bContract, cContract);
}
function testExpectRevertsWithReverter() public {
// Test expect revert with reverter at first call.
vm.expectRevert(address(aContract));
aContract.callAndRevert();
// Test expect revert with reverter at second subcall.
vm.expectRevert(address(bContract));
aContract.callAndRevertInBContract();
// Test expect revert with partial data match and reverter at third subcall.
vm.expectPartialRevert(CContractError.selector, address(cContract));
aContract.callAndRevertInCContractThroughBContract();
// Test expect revert with exact data match and reverter at second subcall.
vm.expectRevert(abi.encodeWithSelector(CContractError.selector, "Reverted by CContract"), address(cContract));
aContract.callAndRevertInCContract();
}
function testExpectRevertsWithReverterInConstructor() public {
// Test expect revert with reverter when constructor reverts.
vm.expectRevert(abi.encodePacked("Reverted by DContract"), address(cContract));
cContract.createDContract();
vm.expectRevert(address(bContract));
bContract.createDContract();
vm.expectRevert(address(cContract));
bContract.createDContractThroughCContract();
vm.expectRevert(address(aContract));
aContract.createDContract();
vm.expectRevert(address(bContract));
aContract.createDContractThroughBContract();
vm.expectRevert(address(cContract));
aContract.createDContractThroughCContract();
}
}
contract ExpectRevertCount is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);
function testRevertCountAny() public {
uint64 count = 3;
Reverter reverter = new Reverter();
vm.expectRevert(count);
reverter.revertWithMessage("revert");
reverter.revertWithMessage("revert2");
reverter.revertWithMessage("revert3");
vm.expectRevert("revert");
reverter.revertWithMessage("revert");
}
function testNoRevert() public {
uint64 count = 0;
Reverter reverter = new Reverter();
vm.expectRevert(count);
reverter.doNotRevert();
}
function testRevertCountSpecific() public {
uint64 count = 2;
Reverter reverter = new Reverter();
vm.expectRevert("revert", count);
reverter.revertWithMessage("revert");
reverter.revertWithMessage("revert");
}
function testNoRevertSpecific() public {
uint64 count = 0;
Reverter reverter = new Reverter();
vm.expectRevert("revert", count);
reverter.doNotRevert();
}
function testNoRevertSpecificButDiffRevert() public {
uint64 count = 0;
Reverter reverter = new Reverter();
vm.expectRevert("revert", count);
reverter.revertWithMessage("revert2");
}
function testRevertCountWithConstructor() public {
uint64 count = 1;
vm.expectRevert("constructor revert", count);
new ConstructorReverter("constructor revert");
}
function testNoRevertWithConstructor() public {
uint64 count = 0;
vm.expectRevert("constructor revert", count);
new CContract();
}
function testRevertCountNestedSpecific() public {
uint64 count = 2;
Reverter reverter = new Reverter();
Reverter inner = new Reverter();
vm.expectRevert("nested revert", count);
reverter.revertWithMessage("nested revert");
reverter.nestedRevert(inner, "nested revert");
vm.expectRevert("nested revert", count);
reverter.nestedRevert(inner, "nested revert");
reverter.nestedRevert(inner, "nested revert");
}
function testRevertCountCallsThenReverts() public {
uint64 count = 2;
Reverter reverter = new Reverter();
Dummy dummy = new Dummy();
vm.expectRevert("called a function and then reverted", count);
reverter.callThenRevert(dummy, "called a function and then reverted");
reverter.callThenRevert(dummy, "called a function and then reverted");
}
function testNoRevertCall() public {
uint64 count = 0;
Reverter reverter = new Reverter();
Dummy dummy = new Dummy();
vm.expectRevert("called a function and then reverted", count);
reverter.callThenNoRevert(dummy);
}
}
contract ExpectRevertCountWithReverter is DSTest {
Vm constant vm = Vm(HEVM_ADDRESS);
function testRevertCountWithReverter() public {
uint64 count = 2;
Reverter reverter = new Reverter();
vm.expectRevert(address(reverter), count);
reverter.revertWithMessage("revert");
reverter.revertWithMessage("revert");
}
function testNoRevertWithReverter() public {
uint64 count = 0;
Reverter reverter = new Reverter();
vm.expectRevert(address(reverter), count);
reverter.doNotRevert();
}
function testNoRevertWithWrongReverter() public {
uint64 count = 0;
Reverter reverter = new Reverter();
Reverter reverter2 = new Reverter();
vm.expectRevert(address(reverter), count);
reverter2.revertWithMessage("revert"); // revert from wrong reverter
}
function testReverterCountWithData() public {
uint64 count = 2;
Reverter reverter = new Reverter();
vm.expectRevert("revert", address(reverter), count);
reverter.revertWithMessage("revert");
reverter.revertWithMessage("revert");
}
function testNoReverterCountWithData() public {
uint64 count = 0;
Reverter reverter = new Reverter();
vm.expectRevert("revert", address(reverter), count);
reverter.doNotRevert();
vm.expectRevert("revert", address(reverter), count);
reverter.revertWithMessage("revert2");
}
}