mock icon indicating copy to clipboard operation
mock copied to clipboard

Implement arguments aware overridable.

Open mneverov opened this issue 7 months ago • 2 comments

Currently, a mock "reset" is supported via allowOverride parameter. Consider an example when a mock is called multiple times with different arguments:

func f() {
    mock.Foo("foo")
    mock.Foo("bar")
    mock.Foo("baz")
}

In this case if only one call needs to be overridden it is impossible to use allowOverride as it resets all the calls. This PR adds a new param which is set up via WithOverridableExpectationsArgsAware. When provided, only expectations with matching argument matchers will be overridden, see the test examples.

Additional context: reset feature request, override expectations feature request Related recent control over expectations feature request

Summary by CodeRabbit

  • New Features
    • Added support for argument-aware overriding of mock expectations, allowing only expectations with matching arguments to be overridden.
    • Introduced a new controller option to enable argument-aware overridable expectations.
  • Tests
    • Added new tests to verify argument-aware overriding behavior and ensure correct handling of multiple expectations with distinct or matching arguments.
    • Provided an example demonstrating the new argument-aware overriding feature.

mneverov avatar Apr 15 '25 10:04 mneverov

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Apr 15 '25 10:04 CLAassistant

Walkthrough

This update introduces argument-aware overriding for expected calls in the gomock framework. A new boolean field, allowOverrideArgsAware, is added to the callSet struct, and a corresponding constructor is provided. When enabled, this feature allows only those expected calls with matching arguments to be overridden, rather than overriding all calls for a method. New controller options and tests are added to support and verify this behavior. Example and test files are updated to demonstrate and validate the argument-aware overriding mechanism.

Changes

File(s) Change Summary
gomock/callset.go Added allowOverrideArgsAware field to callSet, new constructor, and logic in Add for argument-aware overriding.
gomock/controller.go Introduced overridableExpectationsArgsAwareOption and WithOverridableExpectationsArgsAware for argument-aware control.
gomock/callset_test.go Added test for argument-aware overriding: verifies only matching-argument calls are overridden.
gomock/example_test.go Added example demonstrating argument-aware overriding using the new controller option.
gomock/overridable_controller_test.go Added tests to verify argument-aware overriding and matcher equality behavior.

Sequence Diagram(s)

sequenceDiagram
    participant Test as Test/Example
    participant Controller
    participant CallSet

    Test->>Controller: Create Controller (WithOverridableExpectationsArgsAware)
    Controller->>CallSet: Initialize with allowOverrideArgsAware=true

    Test->>Controller: Set expectation (method, args1)
    Controller->>CallSet: Add(method, args1)
    CallSet->>CallSet: Remove existing call with matching args1

    Test->>Controller: Set expectation (method, args2)
    Controller->>CallSet: Add(method, args2)
    CallSet->>CallSet: Remove existing call with matching args2

    Test->>Controller: Set expectation (method, args1) again
    Controller->>CallSet: Add(method, args1)
    CallSet->>CallSet: Remove previous call with args1 only

    Test->>Controller: Call method with args1
    Controller->>CallSet: Match expectation for args1 (most recent)

Poem

In the warren of code, a new path appears,
Where arguments matter—let’s all give three cheers!
Override with precision, not sweeping the floor,
Only matching old calls, we gently outscore.
With tests and examples, we hop on our way,
Argument-aware bunnies, coding all day!
🐇✨

[!TIP]

⚡💬 Agentic Chat (Pro Plan, General Availability)
  • We're introducing multi-step agentic chat in review comments and issue comments, within and outside of PR's. This feature enhances review and issue discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments and add commits to existing pull requests.
✨ Finishing Touches
  • [ ] 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

coderabbitai[bot] avatar Apr 15 '25 10:04 coderabbitai[bot]