Skip to content

Escape method and property names when rendering interaction mismatches#2394

Merged
AndreasTu merged 1 commit into
spockframework:masterfrom
Develop-KIM:fix/interaction-mismatch-dollar-2364
Jul 13, 2026
Merged

Escape method and property names when rendering interaction mismatches#2394
AndreasTu merged 1 commit into
spockframework:masterfrom
Develop-KIM:fix/interaction-mismatch-dollar-2364

Conversation

@Develop-KIM

Copy link
Copy Markdown
Contributor

Fixes #2364

Problem

When a mock interaction is unsatisfied, Spock lists the unmatched invocations
"ordered by similarity". For a name mismatch it builds a synthetic condition
(methodName == "<name>" or propertyName == "<name>") and re-parses it as
Groovy to produce the aligned similarity report.

If the name contains a $, that $ inside the double-quoted literal starts a
GString interpolation, so the Groovy lexer fails with
unknown recognition error type: ...LexerNoViableAltException on stderr and the
similarity report is dropped:

1 * mock.compareTo('a')
methodName == "compareTo$"
|
false

Fix

Escape the name for a double-quoted Groovy literal (", $, \, and control
characters) before formatting the condition, in both EqualMethodNameConstraint
and EqualPropertyNameConstraint. The condition parses again and the report
renders:

1 * mock.compareTo('a')
methodName == "compareTo\$"
|          |
compareTo  false
           1 difference (90% similarity)
           compareTo(-)
           compareTo($)

Names without special characters format exactly as before, so existing output
is unchanged.

Tests

  • TooFewInvocations: two functional specs for a method name and a property
    name containing $, red before the fix and green after.
  • TextUtilSpec: unit coverage for the new
    TextUtil.escapeGroovyDoubleQuotedString helper.

Verified on Groovy variants 4.0 and 5.0; the mock, condition, and util specs
pass.

AI assistance: developed with help from Claude. I have reviewed the change and
understand and stand behind every line.

@coderabbitai

coderabbitai Bot commented Jul 10, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 5a953413-043a-4f5b-b1ac-f52cbc0ac40c

📥 Commits

Reviewing files that changed from the base of the PR and between 432c4f2 and 52b87ef.

📒 Files selected for processing (6)
  • docs/release_notes.adoc
  • spock-core/src/main/java/org/spockframework/mock/constraint/EqualMethodNameConstraint.java
  • spock-core/src/main/java/org/spockframework/mock/constraint/EqualPropertyNameConstraint.java
  • spock-core/src/main/java/org/spockframework/util/TextUtil.java
  • spock-specs/src/test/groovy/org/spockframework/smoke/mock/TooFewInvocations.groovy
  • spock-specs/src/test/groovy/org/spockframework/util/TextUtilSpec.groovy
🚧 Files skipped from review as they are similar to previous changes (6)
  • spock-specs/src/test/groovy/org/spockframework/util/TextUtilSpec.groovy
  • spock-core/src/main/java/org/spockframework/mock/constraint/EqualMethodNameConstraint.java
  • spock-core/src/main/java/org/spockframework/util/TextUtil.java
  • docs/release_notes.adoc
  • spock-specs/src/test/groovy/org/spockframework/smoke/mock/TooFewInvocations.groovy
  • spock-core/src/main/java/org/spockframework/mock/constraint/EqualPropertyNameConstraint.java

📝 Walkthrough

Walkthrough

Adds Groovy-safe escaping for mocked method and property names in mismatch diagnostics, with regression tests for dollar signs and related characters. Release notes document the fix.

Changes

Mismatch rendering

Layer / File(s) Summary
Groovy-safe mismatch rendering
spock-core/src/main/java/org/spockframework/..., docs/release_notes.adoc
Adds TextUtil.escapeGroovyDoubleQuotedString and applies it when rendering method and property name mismatches.
Dollar-sign regression coverage
spock-specs/src/test/groovy/org/spockframework/...
Tests escaped method/property diagnostics and utility escaping for dollar signs, quotes, backslashes, and newlines.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: andreastu

Poem

A bunny found a dollar in the name,
That made the lexer lose the game.
Now quotes and signs are safely tied,
Similarity reports stay by our side.
Hop, hop—clean diagnostics shine!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: escaping method and property names in mismatch rendering.
Description check ✅ Passed The description accurately explains the bug, the fix, and the added tests for this changeset.
Linked Issues check ✅ Passed The code escapes names in both constraints and adds coverage, satisfying the issue's rendering and lexer-error requirements.
Out of Scope Changes check ✅ Passed The changes are focused on the reported bug, its helper, tests, and release notes, with no clear out-of-scope additions.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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

Comment @coderabbitai help to get the list of available commands.

@greptile-apps

greptile-apps Bot commented Jul 10, 2026

Copy link
Copy Markdown

Greptile Summary

This PR fixes interaction mismatch rendering for mocked names that contain Groovy string-special characters.

  • Adds a helper for escaping Groovy double-quoted string literals.
  • Uses the helper for method and property name mismatch conditions.
  • Adds regression tests for $ in method and property names.
  • Adds unit tests for the new escaping helper.
  • Documents the fix in the release notes.

Confidence Score: 5/5

This looks safe to merge.

  • No blocking issues found in the changed code.

Important Files Changed

Filename Overview
spock-core/src/main/java/org/spockframework/util/TextUtil.java Adds Groovy double-quoted literal escaping for quotes, dollar signs, backslashes, and control characters.
spock-core/src/main/java/org/spockframework/mock/constraint/EqualMethodNameConstraint.java Escapes expected method names before formatting the synthetic mismatch condition.
spock-core/src/main/java/org/spockframework/mock/constraint/EqualPropertyNameConstraint.java Escapes expected property names before formatting the synthetic mismatch condition.
spock-specs/src/test/groovy/org/spockframework/smoke/mock/TooFewInvocations.groovy Adds interaction diagnostics tests for method and property names containing $.
spock-specs/src/test/groovy/org/spockframework/util/TextUtilSpec.groovy Adds unit coverage for the new Groovy double-quoted string escaping helper.
docs/release_notes.adoc Adds a release note for the interaction mismatch rendering fix.

Reviews (1): Last reviewed commit: "Escape method and property names in inte..." | Re-trigger Greptile

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@spock-specs/src/test/groovy/org/spockframework/smoke/mock/TooFewInvocations.groovy`:
- Around line 747-813: Add reverse-direction tests alongside the existing
dollar-sign mismatch cases in the feature methods covering method and property
name mismatches: invoke the method/property whose actual name contains “$”,
while declaring the expected interaction with the non-dollar name. Assert the
resulting TooFewInvocationsError message includes the correct unmatched
invocation, escaped name comparison, and similarity report, confirming no lexer
error occurs.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 418a60ad-541d-486f-b859-56f97dc0d99d

📥 Commits

Reviewing files that changed from the base of the PR and between cdccb7f and 432c4f2.

📒 Files selected for processing (6)
  • docs/release_notes.adoc
  • spock-core/src/main/java/org/spockframework/mock/constraint/EqualMethodNameConstraint.java
  • spock-core/src/main/java/org/spockframework/mock/constraint/EqualPropertyNameConstraint.java
  • spock-core/src/main/java/org/spockframework/util/TextUtil.java
  • spock-specs/src/test/groovy/org/spockframework/smoke/mock/TooFewInvocations.groovy
  • spock-specs/src/test/groovy/org/spockframework/util/TextUtilSpec.groovy

@AndreasTu AndreasTu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for your contribution.
Just two small things.
We annotate test, which cover issues with the @Issue annotation.

@AndreasTu AndreasTu added this to the 2.5 milestone Jul 11, 2026
@testlens-app

This comment has been minimized.

@codecov

codecov Bot commented Jul 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.30%. Comparing base (1a21162) to head (52b87ef).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #2394      +/-   ##
============================================
+ Coverage     82.28%   82.30%   +0.01%     
- Complexity     4878     4882       +4     
============================================
  Files           474      474              
  Lines         15251    15258       +7     
  Branches       1959     1961       +2     
============================================
+ Hits          12550    12558       +8     
  Misses         2002     2002              
+ Partials        699      698       -1     
Files with missing lines Coverage Δ
...ork/mock/constraint/EqualMethodNameConstraint.java 100.00% <100.00%> (ø)
...k/mock/constraint/EqualPropertyNameConstraint.java 100.00% <100.00%> (ø)
...rc/main/java/org/spockframework/util/TextUtil.java 61.36% <100.00%> (+3.33%) ⬆️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

EqualMethodNameConstraint and EqualPropertyNameConstraint build a
synthetic `methodName == "<name>"` condition and re-parse it as Groovy
to render the similarity of an unmatched invocation. A `$` in the name
starts a GString interpolation, so the Groovy lexer fails with
LexerNoViableAltException (printed to stderr) and the similarity report
is dropped.

Escape the name for a double-quoted Groovy literal before formatting it,
so the condition parses. Names without special characters are unchanged.

Fixes spockframework#2364

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@Develop-KIM Develop-KIM force-pushed the fix/interaction-mismatch-dollar-2364 branch from 432c4f2 to 52b87ef Compare July 13, 2026 00:08
@Develop-KIM

Copy link
Copy Markdown
Contributor Author

Thanks for the review, @AndreasTu! I've added the @Issue("…/2364") annotation to both feature methods (with the import spock.lang.Issue) and rebased onto the latest master.

Regarding the reverse-direction test coderabbit suggested: I left it out, since only the declared name is baked into the re-parsed Groovy source, while the actual invoked name only flows through as a runtime value and never reaches the lexer — so a reverse case wouldn't exercise the fix.

@AndreasTu AndreasTu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you again for the fix!

@AndreasTu AndreasTu enabled auto-merge (squash) July 13, 2026 05:48
@AndreasTu AndreasTu merged commit 303cc56 into spockframework:master Jul 13, 2026
37 checks passed
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.

Strange behaviour when having an interaction with too few invocations and unmatched calls where the method constraint ends in a dollar sign

2 participants