Skip to content

Commit

Permalink
Docs for resetGasMetering and expectPartialRevert cheatcodes (#1265)
Browse files Browse the repository at this point in the history
  • Loading branch information
grandizzy committed Aug 28, 2024
1 parent bdcb98d commit 2625328
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@
- [`startBroadcast`](./cheatcodes/start-broadcast.md)
- [`stopBroadcast`](./cheatcodes/stop-broadcast.md)
- [`pauseGasMetering`](./cheatcodes/pause-gas-metering.md)
- [`resetGasMetering`](./cheatcodes/reset-gas-metering.md)
- [`resumeGasMetering`](./cheatcodes/resume-gas-metering.md)
- [`txGasPrice`](./cheatcodes/tx-gas-price.md)
- [`startStateDiffRecording`](./cheatcodes/start-state-diff-recording.md)
Expand Down
1 change: 1 addition & 0 deletions src/cheatcodes/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- [`startBroadcast`](./start-broadcast.md)
- [`stopBroadcast`](./stop-broadcast.md)
- [`pauseGasMetering`](./pause-gas-metering.md)
- [`resetGasMetering`](./reset-gas-metering.md)
- [`resumeGasMetering`](./resume-gas-metering.md)
- [`txGasPrice`](./tx-gas-price.md)
- [`startStateDiffRecording`](./start-state-diff-recording.md)
Expand Down
36 changes: 34 additions & 2 deletions src/cheatcodes/expect-revert.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ function expectRevert(bytes4 message) external;
function expectRevert(bytes calldata message) external;
```

```solidity
function expectPartialRevert(bytes4 message) external;
```

### Description

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

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

There are 3 signatures:
There are 3 signatures for `expectRevert`:

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

and one signature for `expectPartialRevert`:
- **`bytes4`**: Asserts that the next call reverts and the specified 4 bytes match the first 4 bytes of revert data.

> ℹ️ **Note:**
>
> 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:
> ```solidity
> function count() public {
> revert WrongNumber(0);
> }
> ```
> should pass when using `expectPartialRevert`:
> ```solidity
> vm.expectPartialRevert(Counter.WrongNumber.selector);
> counter.count();
> ```
> but fails if exact match expected:
> ```solidity
> vm.expectRevert(Counter.WrongNumber.selector);
> counter.count();
> ```
> ⚠️ **Gotcha: Usage with low-level calls**
>
> Normally, a call that succeeds returns a status of `true` (along with any return data) and a call that reverts returns `false`.
Expand Down Expand Up @@ -104,6 +130,12 @@ function testMultipleExpectReverts() public {
}
```

To use `expectPartialRevert` with a custom [error type][error-type], use its selector.

```solidity
vm.expectRevert(CustomError.selector);
```

### SEE ALSO

Forge Standard Library
Expand Down
11 changes: 11 additions & 0 deletions src/cheatcodes/reset-gas-metering.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## `resetGasMetering`

### Signature

```solidity
function resetGasMetering() external;
```

### Description

Resets gas metering to the gas limit of current execution frame (i.e. `gasleft()` will be restored to initial value).

0 comments on commit 2625328

Please sign in to comment.