Skip to content

Commit 2625328

Browse files
authored
Docs for resetGasMetering and expectPartialRevert cheatcodes (#1265)
1 parent bdcb98d commit 2625328

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@
418418
- [`startBroadcast`](./cheatcodes/start-broadcast.md)
419419
- [`stopBroadcast`](./cheatcodes/stop-broadcast.md)
420420
- [`pauseGasMetering`](./cheatcodes/pause-gas-metering.md)
421+
- [`resetGasMetering`](./cheatcodes/reset-gas-metering.md)
421422
- [`resumeGasMetering`](./cheatcodes/resume-gas-metering.md)
422423
- [`txGasPrice`](./cheatcodes/tx-gas-price.md)
423424
- [`startStateDiffRecording`](./cheatcodes/start-state-diff-recording.md)

src/cheatcodes/environment.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
- [`startBroadcast`](./start-broadcast.md)
2929
- [`stopBroadcast`](./stop-broadcast.md)
3030
- [`pauseGasMetering`](./pause-gas-metering.md)
31+
- [`resetGasMetering`](./reset-gas-metering.md)
3132
- [`resumeGasMetering`](./resume-gas-metering.md)
3233
- [`txGasPrice`](./tx-gas-price.md)
3334
- [`startStateDiffRecording`](./start-state-diff-recording.md)

src/cheatcodes/expect-revert.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ function expectRevert(bytes4 message) external;
1414
function expectRevert(bytes calldata message) external;
1515
```
1616

17+
```solidity
18+
function expectPartialRevert(bytes4 message) external;
19+
```
20+
1721
### Description
1822

1923
If the **next call** does not revert with the expected data `message`, then `expectRevert` will.
@@ -22,12 +26,34 @@ After calling `expectRevert`, calls to other cheatcodes before the reverting cal
2226

2327
This means, for example, we can call [`prank`](./prank.md) immediately before the reverting call.
2428

25-
There are 3 signatures:
29+
There are 3 signatures for `expectRevert`:
2630

2731
- **Without parameters**: Asserts that the next call reverts, regardless of the message.
28-
- **With `bytes4`**: Asserts that the next call reverts with the specified 4 bytes.
32+
- **With `bytes4`**: Asserts that the next call reverts with the specified 4 bytes and exact match of revert data.
2933
- **With `bytes`**: Asserts that the next call reverts with the specified bytes.
3034

35+
and one signature for `expectPartialRevert`:
36+
- **`bytes4`**: Asserts that the next call reverts and the specified 4 bytes match the first 4 bytes of revert data.
37+
38+
> ℹ️ **Note:**
39+
>
40+
> Custom errors can have arguments that sometimes are difficult to calculate in a testing environment or they may be unrelated to the test at hand (e.g. a value computed in the internal function of a third-party contract). In such cases, `expectPartialRevert` can be used to ignore arguments and match only on the selector of custom error. For example, testing a function that reverts with `WrongNumber(uint256 number)` custom error:
41+
> ```solidity
42+
> function count() public {
43+
> revert WrongNumber(0);
44+
> }
45+
> ```
46+
> should pass when using `expectPartialRevert`:
47+
> ```solidity
48+
> vm.expectPartialRevert(Counter.WrongNumber.selector);
49+
> counter.count();
50+
> ```
51+
> but fails if exact match expected:
52+
> ```solidity
53+
> vm.expectRevert(Counter.WrongNumber.selector);
54+
> counter.count();
55+
> ```
56+
3157
> ⚠️ **Gotcha: Usage with low-level calls**
3258
>
3359
> Normally, a call that succeeds returns a status of `true` (along with any return data) and a call that reverts returns `false`.
@@ -104,6 +130,12 @@ function testMultipleExpectReverts() public {
104130
}
105131
```
106132

133+
To use `expectPartialRevert` with a custom [error type][error-type], use its selector.
134+
135+
```solidity
136+
vm.expectRevert(CustomError.selector);
137+
```
138+
107139
### SEE ALSO
108140

109141
Forge Standard Library

src/cheatcodes/reset-gas-metering.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## `resetGasMetering`
2+
3+
### Signature
4+
5+
```solidity
6+
function resetGasMetering() external;
7+
```
8+
9+
### Description
10+
11+
Resets gas metering to the gas limit of current execution frame (i.e. `gasleft()` will be restored to initial value).

0 commit comments

Comments
 (0)