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

feat: mock_call with dynamic return data #2904

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

ptisserand
Copy link
Contributor

@ptisserand ptisserand commented Jan 31, 2025

Closes #2861

Introduced changes

  • Add mock_call_when, start_mock_call_when and stop_mock_call_when to support "dynamic" return value when mocking a contract entry point.

Checklist

  • Linked relevant issue
  • Updated relevant documentation
  • Added relevant tests
  • Performed self-review of the code
  • Added changes to CHANGELOG.md

@ptisserand ptisserand force-pushed the feat/dynamic-mock-call branch from 0a08540 to d75e50b Compare January 31, 2025 17:21
@ptisserand ptisserand marked this pull request as ready for review February 4, 2025 00:23
@ptisserand ptisserand force-pushed the feat/dynamic-mock-call branch from 89e963b to 02a7dcd Compare February 4, 2025 14:33
let key_zero = (call.entry_point_selector, Felt::zero());

match contract_functions.get(&key) {
Some(CheatStatus::Cheated(_, CheatSpan::TargetCalls(0))) => {
Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

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

Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See this

pub fn decrement_cheat_span(&mut self) {

Copy link
Member

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.
@ptisserand ptisserand requested a review from a team as a code owner February 18, 2025 02:59
}

#[test]
fn mock_calls_when_mixed() {
Copy link
Member

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?

@cptartur cptartur requested a review from franciszekjob March 4, 2025 11:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

mock_call with dynamic return data
3 participants