Allow declaring mock interactions outside a Specification via MockInteractionSupport#2375
Allow declaring mock interactions outside a Specification via MockInteractionSupport#2375leonard84 wants to merge 1 commit into
Conversation
…eractionSupport Introduce the compiler SPI for routing mock interactions (and mock creation) through an owning Specification located outside the spec, plus the first mechanism that uses it: the `MockInteractionSupport` interface. A class implementing `spock.mock.MockInteractionSupport` supplies the owning spec through `getSpecification()`; its methods may then declare interactions and create mocks exactly as inside a spec. `SpockTransform` rewrites each declared method in place, routing creation/interactions through `getSpecification()`. A class cannot be both a `Specification` and a `MockInteractionSupport`. Rewritten methods are guarded with `Checks.notNull` so a missing (unattached) spec fails fast with a clear error. Foundation included here: - Spec-locator seam: `IRewriteResources.getSpecificationReference()` and `SpecialMethodCall.expand(Expression)`, consumed by `DeepBlockRewriter`, `SpecRewriter`, and `DefaultConditionRewriterResources`. - `MockingApi` converted from a class to an interface (with default methods) so non-spec types can mix in the factory methods; `Specification implements MockingApi`; `ClosureParameterTypeFromVariableType` accepts interface implementors. - External-rewriter infra: `ExternalRewriteResources`, `ExternalInteractionRewriter`, `ExternalInteractionMethod`, and the `Checks.notNull(T, String)` overload. Tests cover the seam, the MockingApi interface conversion, the closure-param path, the external rewrite resources, and MockInteractionSupport behaviour (cardinality, stubbing, mock creation, null-spec guard, static-scope error, both-spec-and-support error), plus an AST snapshot of the rewritten output.
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
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. Comment |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #2375 +/- ##
============================================
+ Coverage 82.33% 82.42% +0.08%
- Complexity 4877 4910 +33
============================================
Files 474 478 +4
Lines 15205 15314 +109
Branches 1935 1954 +19
============================================
+ Hits 12519 12622 +103
Misses 1993 1993
- Partials 693 699 +6
🚀 New features to boost your workflow:
|
✅ All tests passed ✅🏷️ Commit: 4ebfc49 Learn more about TestLens at testlens.app. |

Introduce the compiler SPI for routing mock interactions (and mock creation)
through an owning Specification located outside the spec, plus the first
mechanism that uses it: the
MockInteractionSupportinterface.A class implementing
spock.mock.MockInteractionSupportsupplies the owningspec through
getSpecification(); its methods may then declare interactionsand create mocks exactly as inside a spec.
SpockTransformrewrites eachdeclared method in place, routing creation/interactions through
getSpecification(). A class cannot be both aSpecificationand aMockInteractionSupport. Rewritten methods are guarded withChecks.notNullso a missing (unattached) spec fails fast with a clear error.
Foundation included here:
IRewriteResources.getSpecificationReference()andSpecialMethodCall.expand(Expression), consumed byDeepBlockRewriter,SpecRewriter, andDefaultConditionRewriterResources.MockingApiconverted from a class to an interface (with default methods) sonon-spec types can mix in the factory methods;
Specification implements MockingApi;ClosureParameterTypeFromVariableTypeaccepts interfaceimplementors.
ExternalRewriteResources,ExternalInteractionRewriter,ExternalInteractionMethod, and theChecks.notNull(T, String)overload.Tests cover the seam, the MockingApi interface conversion, the closure-param
path, the external rewrite resources, and MockInteractionSupport behaviour
(cardinality, stubbing, mock creation, null-spec guard, static-scope error,
both-spec-and-support error), plus an AST snapshot of the rewritten output.