Allow mock() to return fulfillingAdditionalTypes with return-type-aware dispatch#213
Merged
Allow mock() to return fulfillingAdditionalTypes with return-type-aware dispatch#213
Conversation
The mock return type validation now accepts types listed in fulfillingAdditionalTypes in addition to Self/type name (concrete) or the extended type (extension). This supports the common pattern where a concrete @INSTANTIABLE type fulfills a protocol and the mock method returns the protocol type. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #213 +/- ##
=======================================
Coverage 99.94% 99.94%
=======================================
Files 40 40
Lines 5584 5601 +17
=======================================
+ Hits 5581 5598 +17
Misses 3 3
🚀 New features to boost your workflow:
|
When an @INSTANTIABLE type has fulfillingAdditionalTypes and a mock returning a protocol type, the generator now checks whether the mock's return type is compatible with the property being constructed. If not (e.g. mock returns MyServiceProtocol but the property expects MyService), it falls through to init instead of calling .mock. Adds mockReturnType to Instantiable and mockReturnTypeIsCompatible(withPropertyType:) to encapsulate the check. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Tests that extension @INSTANTIABLE types with fulfillingAdditionalTypes correctly use .mock when the property type matches the mock return type, and fall through to .instantiate when the property expects the concrete type. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
fdce3ab to
c137d24
Compare
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
fulfillingAdditionalTypesin addition to the declaring typeSelf, the type name, or any additional typemockReturnTypeagainst the property type before deciding to call.mockvsinit/instantiate— a mock returningMyServiceProtocolis used when the parent's property isMyServiceProtocol, but falls through toinit/instantiatewhen the parent needs the concreteMyServicemockReturnType: TypeDescription?toInstantiableandmockReturnTypeIsCompatible(withPropertyType:)to encapsulate the dispatch logicTest plan
mockMethodReturningAdditionalTypeProducesNoDiagnostic— concrete type returning a protocol fromfulfillingAdditionalTypes(macro validation)extension_mockMethodReturningAdditionalTypeProducesNoDiagnostic— extension returning a protocol fromfulfillingAdditionalTypes(macro validation)mock_userMockReturningAdditionalTypeUsedForProtocolProperty— concrete: generator uses.mockwhen property is the protocol typemock_userMockReturningAdditionalTypeNotUsedForConcreteProperty— concrete: generator falls through toinitwhen property is the concrete typemock_extensionUserMockReturningAdditionalTypeUsedForProtocolProperty— extension: generator uses.mockwhen property is the protocol typemock_extensionUserMockReturningAdditionalTypeNotUsedForConcreteProperty— extension: generator falls through to.instantiatewhen property is the concrete type🤖 Generated with Claude Code