-
Notifications
You must be signed in to change notification settings - Fork 208
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
feat: mock_call with dynamic return data #2904
base: master
Are you sure you want to change the base?
Conversation
0a08540
to
d75e50b
Compare
Add `mock_call_when`, `start_mock_call_when` and `stop_mock_call_when`
…:TargetCalls is 0
89e963b
to
02a7dcd
Compare
let key_zero = (call.entry_point_selector, Felt::zero()); | ||
|
||
match contract_functions.get(&key) { | ||
Some(CheatStatus::Cheated(_, CheatSpan::TargetCalls(0))) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After calling get_mocked_function_cheat_status
, we call .decrement_cheat_span
on the returned value. It changes the CheatSpan to Uncheated
after it decreases to 0
.
I don't think there can be a case where TargetCalls
is equal to 0 unless explicitly set to this value somewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume this logic match arm was added to handle the case where entrypoint for specific calldata is no longer cheated but for any calldata still is, but it should already be handled by _
case anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Turns out technically it could be set to 0
but it shouldn't be allowed.
Created #2927
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure to understand what should I do here: remove my workaround?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't have to match for the case where cheat span is CheatSpan::TargetCalls(0)
. Technically, user could have somehow created it like this (as explained in #2927), but during normal operation it will either be CheatSpan::TargetCalls(some value > 0)
or CheatStatus::Uncheated
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See this
starknet-foundry/crates/cheatnet/src/state.rs
Line 141 in f2e7d0d
pub fn decrement_cheat_span(&mut self) { |
crates/cheatnet/src/runtime_extensions/forge_runtime_extension/cheatcodes/mock_call.rs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should also have test cases where mock_call_when
is mixed with mock_call
, these are the ones most likely to break/have some weird edge cases.
- rename cairo MockCallData enum into MockCalldata - use Serde derivation for MockCalldata instead of custom serializer.
} | ||
|
||
#[test] | ||
fn mock_calls_when_mixed() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add some cases where the same tests uses both mock_call
and mock_call_when
?
E.g. in case where you do start_mock_call_when(MockCalldata::Any, ...)
and then stop_mock_call(...)
and similar?
Closes #2861
Introduced changes
mock_call_when
,start_mock_call_when
andstop_mock_call_when
to support "dynamic" return value when mocking a contract entry point.Checklist
CHANGELOG.md