Skip to content

Escape method and property names when rendering interaction mismatches#2394

Open
Develop-KIM wants to merge 1 commit into
spockframework:masterfrom
Develop-KIM:fix/interaction-mismatch-dollar-2364
Open

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

Conversation

@Develop-KIM

Copy link
Copy Markdown

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.

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>
@coderabbitai

coderabbitai Bot commented Jul 10, 2026

Copy link
Copy Markdown

Review Change Stack

📝 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

Possibly related PRs

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 states the main change: escaping method and property names in interaction mismatch rendering.
Description check ✅ Passed The description matches the fix and test coverage for escaped method and property names.
Linked Issues check ✅ Passed The code addresses #2364 by escaping names before reparsing synthetic Groovy conditions, restoring similarity reports.
Out of Scope Changes check ✅ Passed The changes shown are directly related to the mismatch-rendering fix and its tests.
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.

exceptionMessage == expected
}

def "can describe method name mismatch when the method name contains a dollar sign"() {

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.

Suggested change
def "can describe method name mismatch when the method name contains a dollar sign"() {
@Issue("https://github.com/spockframework/spock/issues/2364")
def "can describe method name mismatch when the method name contains a dollar sign"() {

exceptionMessage == expected
}

def "can describe property name mismatch when the property name contains a dollar sign"() {

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.

Suggested change
def "can describe property name mismatch when the property name contains a dollar sign"() {
@Issue("https://github.com/spockframework/spock/issues/2364")
def "can describe property name mismatch when the property name contains a dollar sign"() {

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

testlens-app Bot commented Jul 11, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 432c4f2
▶️ Tests: 100370 executed
⚪️ Checks: 33/33 completed


Learn more about TestLens at testlens.app.

@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 (cdccb7f) to head (432c4f2).

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #2394      +/-   ##
============================================
+ Coverage     82.28%   82.30%   +0.01%     
- Complexity     4877     4883       +6     
============================================
  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.

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