-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
<!-- Reference any GitHub issues resolved by this PR --> Closes # ## Introduced changes <!-- A brief description of the changes --> - Add `start_mock_call` cheatcode (with tests and docs) - Add `stop_mock_call` cheatcode (with tests and docs) ## Breaking changes <!-- List of all breaking changes, if applicable --> ## Checklist <!-- Make sure all of these are complete --> - [x] Linked relevant issue - [x] Updated relevant documentation - [x] Added relevant tests - [x] Performed self-review of the code - [x] Added changes to `CHANGELOG.md` --------- Co-authored-by: Marcin Warchoł <[email protected]> Co-authored-by: Maksymilian Demitraszek <[email protected]> Co-authored-by: Piotr Magiera <[email protected]>
- Loading branch information
1 parent
1a6bddc
commit 1981e86
Showing
18 changed files
with
1,172 additions
and
3 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use crate::CheatnetState; | ||
use starknet_api::core::{ContractAddress, EntryPointSelector}; | ||
use starknet_api::hash::StarkFelt; | ||
use std::collections::HashMap; | ||
|
||
impl CheatnetState { | ||
pub fn start_mock_call( | ||
&mut self, | ||
contract_address: ContractAddress, | ||
function_name: EntryPointSelector, | ||
ret_data: Vec<StarkFelt>, | ||
) { | ||
let contract_mocked_functions = self | ||
.cheatcode_state | ||
.mocked_functions | ||
.entry(contract_address) | ||
.or_insert_with(HashMap::new); | ||
|
||
contract_mocked_functions.insert(function_name, ret_data); | ||
} | ||
|
||
pub fn stop_mock_call( | ||
&mut self, | ||
contract_address: ContractAddress, | ||
function_name: EntryPointSelector, | ||
) { | ||
if let std::collections::hash_map::Entry::Occupied(mut e) = self | ||
.cheatcode_state | ||
.mocked_functions | ||
.entry(contract_address) | ||
{ | ||
let contract_mocked_functions = e.get_mut(); | ||
contract_mocked_functions.remove(&function_name); | ||
} | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
crates/forge/tests/data/contracts/constructor_mock_checker.cairo
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,34 @@ | ||
use starknet::ContractAddress; | ||
|
||
#[starknet::interface] | ||
trait IConstructorMockChecker<TContractState> { | ||
fn get_stored_thing(ref self: TContractState) -> felt252; | ||
fn get_constant_thing(ref self: TContractState) -> felt252; | ||
} | ||
|
||
#[starknet::contract] | ||
mod ConstructorMockChecker { | ||
use super::IConstructorMockChecker; | ||
|
||
#[storage] | ||
struct Storage { | ||
stored_thing: felt252, | ||
} | ||
|
||
#[constructor] | ||
fn constructor(ref self: ContractState) { | ||
let const_thing = self.get_constant_thing(); | ||
self.stored_thing.write(const_thing); | ||
} | ||
|
||
#[external(v0)] | ||
impl IConstructorMockCheckerImpl of super::IConstructorMockChecker<ContractState> { | ||
fn get_constant_thing(ref self: ContractState) -> felt252 { | ||
13 | ||
} | ||
|
||
fn get_stored_thing(ref self: ContractState) -> felt252 { | ||
self.stored_thing.read() | ||
} | ||
} | ||
} |
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,54 @@ | ||
#[derive(Serde, Drop)] | ||
struct StructThing { | ||
item_one: felt252, | ||
item_two: felt252, | ||
} | ||
|
||
#[starknet::interface] | ||
trait IMockChecker<TContractState> { | ||
fn get_thing(ref self: TContractState) -> felt252; | ||
fn get_thing_wrapper(ref self: TContractState) -> felt252; | ||
fn get_constant_thing(ref self: TContractState) -> felt252; | ||
fn get_struct_thing(ref self: TContractState) -> StructThing; | ||
fn get_arr_thing(ref self: TContractState) -> Array<StructThing>; | ||
} | ||
|
||
#[starknet::contract] | ||
mod MockChecker { | ||
use super::IMockChecker; | ||
use super::StructThing; | ||
use array::ArrayTrait; | ||
|
||
#[storage] | ||
struct Storage { | ||
stored_thing: felt252 | ||
} | ||
|
||
#[constructor] | ||
fn constructor(ref self: ContractState, arg1: felt252) { | ||
self.stored_thing.write(arg1) | ||
} | ||
|
||
#[external(v0)] | ||
impl IMockCheckerImpl of super::IMockChecker<ContractState> { | ||
fn get_thing(ref self: ContractState) -> felt252 { | ||
self.stored_thing.read() | ||
} | ||
|
||
fn get_thing_wrapper(ref self: ContractState) -> felt252 { | ||
self.get_thing() | ||
} | ||
|
||
fn get_constant_thing(ref self: ContractState) -> felt252 { | ||
13 | ||
} | ||
|
||
fn get_struct_thing(ref self: ContractState) -> StructThing { | ||
StructThing {item_one: 12, item_two: 21} | ||
} | ||
|
||
fn get_arr_thing(ref self: ContractState) -> Array<StructThing> { | ||
array![StructThing {item_one: 12, item_two: 21}] | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
crates/forge/tests/data/contracts/mock_checker_library_call.cairo
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,28 @@ | ||
use starknet::ClassHash; | ||
|
||
#[starknet::interface] | ||
trait IMockChecker<TContractState> { | ||
fn get_constant_thing(ref self: TContractState) -> felt252; | ||
} | ||
|
||
#[starknet::interface] | ||
trait IMockCheckerLibCall<TContractState> { | ||
fn get_constant_thing_with_lib_call(ref self: TContractState, class_hash: ClassHash) -> felt252; | ||
} | ||
|
||
#[starknet::contract] | ||
mod MockCheckerLibCall { | ||
use super::{IMockCheckerDispatcherTrait, IMockCheckerLibraryDispatcher}; | ||
use starknet::ClassHash; | ||
|
||
#[storage] | ||
struct Storage {} | ||
|
||
#[external(v0)] | ||
impl IMockCheckerLibCall of super::IMockCheckerLibCall<ContractState> { | ||
fn get_constant_thing_with_lib_call(ref self: ContractState, class_hash: ClassHash) -> felt252 { | ||
let mock_checker = IMockCheckerLibraryDispatcher { class_hash }; | ||
mock_checker.get_constant_thing() | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
crates/forge/tests/data/contracts/mock_checker_proxy.cairo
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,49 @@ | ||
use starknet::ContractAddress; | ||
|
||
#[starknet::interface] | ||
trait IMockChecker<TContractState> { | ||
fn get_thing(ref self: TContractState) -> felt252; | ||
} | ||
|
||
|
||
#[starknet::interface] | ||
trait IMockCheckerProxy<TContractState> { | ||
fn get_thing_from_contract(ref self: TContractState, address: ContractAddress) -> felt252; | ||
fn get_thing_from_contract_and_emit_event(ref self: TContractState, address: ContractAddress) -> felt252; | ||
} | ||
|
||
#[starknet::contract] | ||
mod MockCheckerProxy { | ||
use starknet::ContractAddress; | ||
use super::IMockCheckerDispatcherTrait; | ||
use super::IMockCheckerDispatcher; | ||
|
||
#[storage] | ||
struct Storage {} | ||
|
||
#[event] | ||
#[derive(Drop, starknet::Event)] | ||
enum Event { | ||
ThingEmitted: ThingEmitted | ||
} | ||
|
||
#[derive(Drop, starknet::Event)] | ||
struct ThingEmitted { | ||
thing: felt252 | ||
} | ||
|
||
#[external(v0)] | ||
impl IMockCheckerProxy of super::IMockCheckerProxy<ContractState> { | ||
fn get_thing_from_contract(ref self: ContractState, address: ContractAddress) -> felt252 { | ||
let dispatcher = IMockCheckerDispatcher { contract_address: address }; | ||
dispatcher.get_thing() | ||
} | ||
|
||
fn get_thing_from_contract_and_emit_event(ref self: ContractState, address: ContractAddress) -> felt252 { | ||
let dispatcher = IMockCheckerDispatcher { contract_address: address }; | ||
let thing = dispatcher.get_thing(); | ||
self.emit(Event::ThingEmitted(ThingEmitted { thing })); | ||
thing | ||
} | ||
} | ||
} |
Oops, something went wrong.