-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cheatcodes): Add
vm.mockCalls
to mock different return data fo…
…r multiple calls (#9024) * Refactor vm.mockCall to be based on mutable VecDeque * Add vm.mockCalls cheatcode * Refactor mock_call to be wrapper for mock_calls * Add a test to vm.mockCalls * Add test for vm.mockCalls with msg.value * Fix fmt & clippy following vm.mockCalls implementation * Fix Solidity fmt in testdata/default/cheats/MockCalls.t.sol * Add test in MockCalls.t.sol to check last mocked data persists * Remove allow(clippy::ptr_arg) from mock_call & mock_calls * Apply suggestions from code review --------- Co-authored-by: zerosnacks <[email protected]> Co-authored-by: DaniPopes <[email protected]>
- Loading branch information
1 parent
22a72d5
commit d7d9b40
Showing
6 changed files
with
169 additions
and
18 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
pragma solidity 0.8.18; | ||
|
||
import "ds-test/test.sol"; | ||
import "cheats/Vm.sol"; | ||
|
||
contract MockCallsTest is DSTest { | ||
Vm constant vm = Vm(HEVM_ADDRESS); | ||
|
||
function testMockCallsLastShouldPersist() public { | ||
address mockUser = vm.addr(vm.randomUint()); | ||
address mockErc20 = vm.addr(vm.randomUint()); | ||
bytes memory data = abi.encodeWithSignature("balanceOf(address)", mockUser); | ||
bytes[] memory mocks = new bytes[](2); | ||
mocks[0] = abi.encode(2 ether); | ||
mocks[1] = abi.encode(7.219 ether); | ||
vm.mockCalls(mockErc20, data, mocks); | ||
(, bytes memory ret1) = mockErc20.call(data); | ||
assertEq(abi.decode(ret1, (uint256)), 2 ether); | ||
(, bytes memory ret2) = mockErc20.call(data); | ||
assertEq(abi.decode(ret2, (uint256)), 7.219 ether); | ||
(, bytes memory ret3) = mockErc20.call(data); | ||
assertEq(abi.decode(ret3, (uint256)), 7.219 ether); | ||
} | ||
|
||
function testMockCallsWithValue() public { | ||
address mockUser = vm.addr(vm.randomUint()); | ||
address mockErc20 = vm.addr(vm.randomUint()); | ||
bytes memory data = abi.encodeWithSignature("balanceOf(address)", mockUser); | ||
bytes[] memory mocks = new bytes[](3); | ||
mocks[0] = abi.encode(2 ether); | ||
mocks[1] = abi.encode(1 ether); | ||
mocks[2] = abi.encode(6.423 ether); | ||
vm.mockCalls(mockErc20, 1 ether, data, mocks); | ||
(, bytes memory ret1) = mockErc20.call{value: 1 ether}(data); | ||
assertEq(abi.decode(ret1, (uint256)), 2 ether); | ||
(, bytes memory ret2) = mockErc20.call{value: 1 ether}(data); | ||
assertEq(abi.decode(ret2, (uint256)), 1 ether); | ||
(, bytes memory ret3) = mockErc20.call{value: 1 ether}(data); | ||
assertEq(abi.decode(ret3, (uint256)), 6.423 ether); | ||
} | ||
|
||
function testMockCalls() public { | ||
address mockUser = vm.addr(vm.randomUint()); | ||
address mockErc20 = vm.addr(vm.randomUint()); | ||
bytes memory data = abi.encodeWithSignature("balanceOf(address)", mockUser); | ||
bytes[] memory mocks = new bytes[](3); | ||
mocks[0] = abi.encode(2 ether); | ||
mocks[1] = abi.encode(1 ether); | ||
mocks[2] = abi.encode(6.423 ether); | ||
vm.mockCalls(mockErc20, data, mocks); | ||
(, bytes memory ret1) = mockErc20.call(data); | ||
assertEq(abi.decode(ret1, (uint256)), 2 ether); | ||
(, bytes memory ret2) = mockErc20.call(data); | ||
assertEq(abi.decode(ret2, (uint256)), 1 ether); | ||
(, bytes memory ret3) = mockErc20.call(data); | ||
assertEq(abi.decode(ret3, (uint256)), 6.423 ether); | ||
} | ||
} |