Skip to content

Add ValueTask detection tests#408

Merged
rjmurillo merged 5 commits intomainfrom
codex/locate-and-fix-important-bug
Jun 7, 2025
Merged

Add ValueTask detection tests#408
rjmurillo merged 5 commits intomainfrom
codex/locate-and-fix-important-bug

Conversation

@rjmurillo
Copy link
Copy Markdown
Owner

@rjmurillo rjmurillo commented May 31, 2025

  • fix ValueTask string comparison for task-return detection
  • add regression tests for ValueTask setup analysis

Summary by CodeRabbit

  • Tests
    • Added new test cases to verify analyzer behavior with ValueTask-based asynchronous methods.
    • Extended the test client with methods returning ValueTask and ValueTask to support these scenarios.

Copilot AI review requested due to automatic review settings May 31, 2025 02:35
@rjmurillo rjmurillo requested a review from MattKotsenas as a code owner May 31, 2025 02:35
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented May 31, 2025

📝 Walkthrough

Walkthrough

The test suite for SetupShouldNotIncludeAsyncResultAnalyzer was updated to include scenarios involving ValueTask and ValueTask<string>. Two new virtual methods were added to the AsyncClient class to support these tests, and a new diagnostic test case was introduced for setups accessing .Result on a ValueTask<string>.

Changes

File(s) Change Summary
tests/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.cs Added tests for ValueTask scenarios; added ValueTaskAsync() and GenericValueTaskAsync() methods to AsyncClient.

Possibly related issues

Possibly related PRs

  • Bug/fix issue 143 #221: Both enhance async result handling in the analyzer, with a focus on ValueTask and related diagnostics.
  • Bug/fix issue 144 #224: Both modify analyzer logic and test structure for async setup diagnostics, especially for ValueTask scenarios.

Suggested labels

analyzers

Suggested reviewers

  • MattKotsenas

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 026e564 and ac0d3ce.

📒 Files selected for processing (1)
  • tests/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.cs (2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.cs`: I need your help tracking down and fixing some bugs that have been reported in this codebase.

I suspect the bugs are related to:

  • Incorrect handling of edge cases
  • O...

**/*.cs: I need your help tracking down and fixing some bugs that have been reported in this codebase.

I suspect the bugs are related to:

  • Incorrect handling of edge cases
  • Off-by-one errors in loops or array indexing
  • Unexpected data types
  • Uncaught exceptions
  • Concurrency issues
  • Improper configuration settings

To diagnose:

  1. Review the code carefully and systematically
  2. Trace the relevant code paths
  3. Consider boundary conditions and potential error states
  4. Look for antipatterns that tend to cause bugs
  5. Run the code mentally with example inputs
  6. Think about interactions between components

When you find potential bugs, for each one provide:

  1. File path and line number(s)
  2. Description of the issue and why it's a bug
  3. Example input that would trigger the bug
  4. Suggestions for how to fix it

After analysis, please update the code with your proposed fixes. Try to match the existing code style. Add regression tests if possible to prevent the bugs from recurring.

I appreciate your diligence and attention to detail! Let me know if you need any clarification on the intended behavior of the code.

  • tests/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.cs
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: build (ubuntu-24.04-arm)
  • GitHub Check: build (windows-latest)
🔇 Additional comments (2)
tests/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.cs (2)

21-21: The ValueTask test case looks correct and follows the established pattern.

The new test case for ValueTask<string>.Result correctly follows the same pattern as the existing Task<string>.Result test case on line 19, ensuring consistent diagnostic detection for both Task and ValueTask scenarios.


47-49: The ValueTask methods are implemented correctly and consistently.

The new virtual methods follow the established patterns:

  • ValueTaskAsync() uses ValueTask.CompletedTask (equivalent to Task.CompletedTask)
  • GenericValueTaskAsync() uses ValueTask.FromResult(string.Empty) (equivalent to Task.FromResult(string.Empty))

This provides comprehensive test coverage for both generic and non-generic ValueTask scenarios.

✨ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate sequence diagram to generate a sequence diagram of the changes in 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.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR corrects the namespace used when detecting ValueTask return types in the analyzer and adds regression tests to ensure ValueTask-based setups are handled correctly.

  • Fix string comparison for ValueTask in IsMethodReturnTypeTask
  • Add test methods and analyzer tests for ValueTask and ValueTask<T> scenarios
  • Extend AsyncClient test stub with ValueTask-returning members

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
tests/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.cs Added tests for ValueTaskAsync(), GenericValueTaskAsync() and .Result diagnostics
src/Common/ISymbolExtensions.cs Corrected the namespace comparison from System.Threading.ValueTask to System.Threading.Tasks.ValueTask
Comments suppressed due to low confidence (2)

src/Common/ISymbolExtensions.cs:101

  • Relying on ToDisplayString and string comparisons for matching Task/ValueTask is fragile. Consider using the symbol model (e.g., methodSymbol.ReturnType.OriginalDefinition) or SymbolEqualityComparer to compare against the actual Task and ValueTask types.
string type = methodSymbol.ToDisplayString();

tests/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.cs:16

  • [nitpick] Consider adding a test case for .ThrowsAsync(...) on ValueTask-returning methods (e.g., Setup(c => c.GenericValueTaskAsync()).ThrowsAsync(new Exception())) to ensure exception-based setups are analyzed correctly.
["""new Mock<AsyncClient>().Setup(c => c.ValueTaskAsync());"""]

@codacy-production
Copy link
Copy Markdown

codacy-production bot commented May 31, 2025

Coverage summary from Codacy

See diff coverage on Codacy

Coverage variation Diff coverage
+0.00% (target: -1.00%) (target: 95.00%)
Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (cd99b6a) 876 791 90.30%
Head commit (ac0d3ce) 876 (+0) 791 (+0) 90.30% (+0.00%)

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#408) 0 0 ∅ (not applicable)

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

See your quality gate settings    Change summary preferences

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a2c62b6 and 026e564.

📒 Files selected for processing (2)
  • src/Common/ISymbolExtensions.cs (1 hunks)
  • tests/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.cs (2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.cs`: I need your help tracking down and fixing some bugs that have been reported in this codebase.

I suspect the bugs are related to:

  • Incorrect handling of edge cases
  • O...

**/*.cs: I need your help tracking down and fixing some bugs that have been reported in this codebase.

I suspect the bugs are related to:

  • Incorrect handling of edge cases
  • Off-by-one errors in loops or array indexing
  • Unexpected data types
  • Uncaught exceptions
  • Concurrency issues
  • Improper configuration settings

To diagnose:

  1. Review the code carefully and systematically
  2. Trace the relevant code paths
  3. Consider boundary conditions and potential error states
  4. Look for antipatterns that tend to cause bugs
  5. Run the code mentally with example inputs
  6. Think about interactions between components

When you find potential bugs, for each one provide:

  1. File path and line number(s)
  2. Description of the issue and why it's a bug
  3. Example input that would trigger the bug
  4. Suggestions for how to fix it

After analysis, please update the code with your proposed fixes. Try to match the existing code style. Add regression tests if possible to prevent the bugs from recurring.

I appreciate your diligence and attention to detail! Let me know if you need any clarification on the intended behavior of the code.

  • src/Common/ISymbolExtensions.cs
  • tests/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.cs
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: build (windows-latest)
  • GitHub Check: build (ubuntu-24.04-arm)
🔇 Additional comments (4)
src/Common/ISymbolExtensions.cs (1)

103-103: LGTM! Namespace fix is correct.

The namespace correction from "System.Threading.ValueTask" to "System.Threading.Tasks.ValueTask" is accurate and aligns with the correct .NET namespace for ValueTask.

tests/Moq.Analyzers.Test/SetupShouldNotIncludeAsyncResultAnalyzerTests.cs (3)

16-18: LGTM! Well-structured test additions for ValueTask scenarios.

The new test cases properly cover ValueTask scenarios and follow the same patterns as the existing Task tests. The test data includes both basic ValueTask setup and generic ValueTask with Returns pattern.


23-23: LGTM! Proper test coverage for ValueTask Result property access.

This test case correctly validates that accessing .Result on a ValueTask<T> triggers the Moq1201 diagnostic, providing good regression test coverage for the fixed ValueTask detection logic.


41-43: LGTM! Well-implemented test helper methods.

The new virtual methods in the AsyncClient class are properly implemented:

  • ValueTaskAsync() returns ValueTask.CompletedTask
  • GenericValueTaskAsync() returns ValueTask.FromResult(string.Empty)

These follow the same patterns as the existing Task methods and provide the necessary test infrastructure for the new ValueTask test cases.

coderabbitai[bot]
coderabbitai bot previously approved these changes May 31, 2025
- Enhanced `copilot-instructions.md` with repository description and contribution guidelines.
- Updated package versions in `Packages.props` for `Meziantou.Analyzer`, `Polyfill`, and `Verify.Xunit`.
- Modified `test.runsettings` to add exclusions for code coverage.
- Added summary comment and new method `IsTaskOrValueResultProperty` in `SetupShouldBeUsedOnlyForOverridableMembersAnalyzer.cs`.
- Introduced `IsConstructor` method in `ISymbolExtensions.cs` and removed `IsMethodReturnTypeTask`.
- Updated `SquiggleCop.Baseline.yaml` to include new warnings and adjust existing entries.
@rjmurillo rjmurillo enabled auto-merge (squash) June 4, 2025 16:59
@qlty-cloud-legacy
Copy link
Copy Markdown

Code Climate has analyzed commit ac0d3ce and detected 0 issues on this pull request.

View more on Code Climate.

@rjmurillo rjmurillo changed the title Fix ValueTask detection Add ValueTask detection tests Jun 7, 2025
@rjmurillo rjmurillo disabled auto-merge June 7, 2025 05:50
@rjmurillo rjmurillo merged commit 2bd9421 into main Jun 7, 2025
12 checks passed
@rjmurillo rjmurillo deleted the codex/locate-and-fix-important-bug branch June 7, 2025 05:50
@rjmurillo rjmurillo added this to the vNext milestone Jun 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants