Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mock_call with dynamic return data #2861

Open
cptartur opened this issue Jan 20, 2025 · 5 comments · May be fixed by #2904
Open

mock_call with dynamic return data #2861

cptartur opened this issue Jan 20, 2025 · 5 comments · May be fixed by #2904
Assignees
Labels
feature New feature request/description new snforge

Comments

@cptartur
Copy link
Member

Which component is your feature related to?

Forge

Feature Request

mock_call cheatcode accepts static ret_data. It could be useful if it was possible to define a function in in that can receive the calldata and dynamically return different ret_data.

@cptartur cptartur added feature New feature request/description snforge labels Jan 20, 2025
@cptartur cptartur moved this to New in Starknet foundry Jan 20, 2025
@github-actions github-actions bot added the new label Jan 20, 2025
@PoulavBhowmick03
Copy link

Can i take this one up? Would love to work on it

@ptisserand
Copy link
Contributor

Hi, here is a proposal for mock callback interface:

trait MockCallback<T, R> {
    fn execute(ref self: T, input: Span<felt252>) -> R;
}

What do you think about adding also selector to execute parameters ?

@cptartur
Copy link
Member Author

Hi, here is a proposal for mock callback interface:

trait MockCallback<T, R> {
fn execute(ref self: T, input: Span) -> R;
}
What do you think about adding also selector to execute parameters ?

@ptisserand this is nice but I wonder if we shouldn't reduce the scope of this issue and simply extend mock_call (by introducing a new variant) so it allows mocking for specific input data. Something along the lines of

fn mock_call( contract_address: ContractAddress, function_selector: felt252, calldata: Span<felt252>, ret_data: T, n_times: u32)

This should cover most user cases and be more in-line with other cheatcodes (also much easier to implement).

ccing @kkawula @franciszekjob @ddoktorski

@ptisserand
Copy link
Contributor

ptisserand commented Jan 31, 2025

@cptartur what do you think to use calldata: Option<Span<felt252>> instead ?

My idea is when calldata is None, we have the same behavior as current implementation.
So user can have specific returned value for given calldata and also a default returned value.

Or we can have a more explicit enum like:

enum MockCallData {
    Any,
    Value: Span<felt252>,
}

ccing @kkawula @franciszekjob @ddoktorski

@ptisserand
Copy link
Contributor

Here is a test example with MockCallData enum:

#[test]
fn mock_call_simple() {
    let calldata = array![420];

    let contract = declare("MockChecker").unwrap().contract_class();
    let (contract_address, _) = contract.deploy(@calldata).unwrap();

    let dispatcher = IMockCheckerDispatcher { contract_address };

    let specific_mock_ret_data = 421;
    let default_mock_ret_data = 404;
    let expected_calldata = MockCallData::Values([].span());
    start_mock_call(contract_address, selector!("get_thing"), expected_calldata, specific_mock_ret_data);
    start_mock_call(contract_address, selector!("get_thing"), MockCallData::Any, default_mock_ret_data);
    let thing = dispatcher.get_thing();
    assert(thing == specific_mock_ret_data, 'Incorrect thing');

    stop_mock_call(contract_address, selector!("get_thing"), expected_calldata);
    let thing = dispatcher.get_thing();
    assert(thing == default_mock_ret_data, 'Incorrect thing');

    stop_mock_call(contract_address, selector!("get_thing"), MockCallData::Any);
    let thing = dispatcher.get_thing();
    assert(thing == 420, 'Incorrect thing');
}

@ptisserand ptisserand linked a pull request Jan 31, 2025 that will close this issue
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature request/description new snforge
Projects
Status: New
Development

Successfully merging a pull request may close this issue.

3 participants