-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mockFunction cheatcode and tests
- Loading branch information
Showing
8 changed files
with
151 additions
and
0 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
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mod assume; | ||
mod expect; | ||
mod gas_metering; | ||
mod mock; | ||
mod random; |
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,94 @@ | ||
//! Contains various tests for `mock*` cheatcodes. | ||
|
||
use foundry_test_utils::{forgetest_init, str}; | ||
|
||
// Kontrol `mockFunction` cheatcode tests ported. | ||
forgetest_init!(test_mock_function_kontrol, |prj, cmd| { | ||
prj.wipe_contracts(); | ||
prj.insert_ds_test(); | ||
prj.insert_vm(); | ||
prj.clear(); | ||
|
||
prj.add_source( | ||
"MockFunction.t.sol", | ||
r#"pragma solidity 0.8.24; | ||
import {Vm} from "./Vm.sol"; | ||
import {DSTest} from "./test.sol"; | ||
contract MockFunctionContract { | ||
uint256 public a; | ||
function mocked_function() public { | ||
a = 321; | ||
} | ||
function mocked_args_function(uint256 x) public { | ||
a = 321 + x; | ||
} | ||
} | ||
contract ModelMockFunctionContract { | ||
uint256 public a; | ||
function mocked_function() public { | ||
a = 123; | ||
} | ||
function mocked_args_function(uint256 x) public { | ||
a = 123 + x; | ||
} | ||
} | ||
contract MockFunctionTest is DSTest { | ||
MockFunctionContract my_contract; | ||
ModelMockFunctionContract model_contract; | ||
Vm vm = Vm(HEVM_ADDRESS); | ||
function setUp() public { | ||
my_contract = new MockFunctionContract(); | ||
model_contract = new ModelMockFunctionContract(); | ||
} | ||
function test_mock_function() public { | ||
vm.mockFunction( | ||
address(my_contract), | ||
address(model_contract), | ||
abi.encodeWithSelector(MockFunctionContract.mocked_function.selector) | ||
); | ||
my_contract.mocked_function(); | ||
assertEq(my_contract.a(), 123); | ||
} | ||
function test_mock_function_concrete_args() public { | ||
vm.mockFunction( | ||
address(my_contract), | ||
address(model_contract), | ||
abi.encodeWithSelector(MockFunctionContract.mocked_args_function.selector, 456) | ||
); | ||
my_contract.mocked_args_function(456); | ||
assertEq(my_contract.a(), 123 + 456); | ||
my_contract.mocked_args_function(567); | ||
assertEq(my_contract.a(), 321 + 567); | ||
} | ||
function test_mock_function_all_args() public { | ||
vm.mockFunction( | ||
address(my_contract), | ||
address(model_contract), | ||
abi.encodeWithSelector(MockFunctionContract.mocked_args_function.selector) | ||
); | ||
my_contract.mocked_args_function(678); | ||
assertEq(my_contract.a(), 123 + 678); | ||
my_contract.mocked_args_function(789); | ||
assertEq(my_contract.a(), 123 + 789); | ||
} | ||
} | ||
"#, | ||
) | ||
.unwrap(); | ||
|
||
cmd.args(["test"]).assert_success().stdout_eq(str![[r#" | ||
... | ||
[PASS] test_mock_function() ([GAS]) | ||
[PASS] test_mock_function_all_args() ([GAS]) | ||
[PASS] test_mock_function_concrete_args() ([GAS]) | ||
... | ||
"#]]); | ||
}); |
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.