Skip to content

Allow mock() to return fulfillingAdditionalTypes with return-type-aware dispatch#213

Merged
dfed merged 3 commits intomainfrom
dfed--mock-additional-types
Apr 5, 2026
Merged

Allow mock() to return fulfillingAdditionalTypes with return-type-aware dispatch#213
dfed merged 3 commits intomainfrom
dfed--mock-additional-types

Conversation

@dfed
Copy link
Copy Markdown
Owner

@dfed dfed commented Apr 5, 2026

Summary

  • Mock return type validation now accepts types listed in fulfillingAdditionalTypes in addition to the declaring type
  • On concrete types: Self, the type name, or any additional type
  • On extensions: the extended type or any additional type
  • The generator checks mockReturnType against the property type before deciding to call .mock vs init/instantiate — a mock returning MyServiceProtocol is used when the parent's property is MyServiceProtocol, but falls through to init/instantiate when the parent needs the concrete MyService
  • Adds mockReturnType: TypeDescription? to Instantiable and mockReturnTypeIsCompatible(withPropertyType:) to encapsulate the dispatch logic
  • Updates the manual to document additional type return rules

Test plan

  • mockMethodReturningAdditionalTypeProducesNoDiagnostic — concrete type returning a protocol from fulfillingAdditionalTypes (macro validation)
  • extension_mockMethodReturningAdditionalTypeProducesNoDiagnostic — extension returning a protocol from fulfillingAdditionalTypes (macro validation)
  • mock_userMockReturningAdditionalTypeUsedForProtocolProperty — concrete: generator uses .mock when property is the protocol type
  • mock_userMockReturningAdditionalTypeNotUsedForConcreteProperty — concrete: generator falls through to init when property is the concrete type
  • mock_extensionUserMockReturningAdditionalTypeUsedForProtocolProperty — extension: generator uses .mock when property is the protocol type
  • mock_extensionUserMockReturningAdditionalTypeNotUsedForConcreteProperty — extension: generator falls through to .instantiate when property is the concrete type
  • All 709 tests pass
  • Lint passes
  • Coverage: all new code fully covered

🤖 Generated with Claude Code

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
Copy link
Copy Markdown

codecov bot commented Apr 5, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.94%. Comparing base (54179d4) to head (c137d24).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #213   +/-   ##
=======================================
  Coverage   99.94%   99.94%           
=======================================
  Files          40       40           
  Lines        5584     5601   +17     
=======================================
+ Hits         5581     5598   +17     
  Misses          3        3           
Files with missing lines Coverage Δ
Sources/SafeDICore/Generators/ScopeGenerator.swift 100.00% <100.00%> (ø)
Sources/SafeDICore/Models/InstantiableStruct.swift 100.00% <100.00%> (ø)
...rces/SafeDICore/Visitors/InstantiableVisitor.swift 100.00% <100.00%> (ø)
Sources/SafeDITool/SafeDITool.swift 99.70% <100.00%> (+<0.01%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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>
@dfed dfed changed the title Allow mock() to return fulfillingAdditionalTypes Allow mock() to return fulfillingAdditionalTypes with return-type-aware dispatch Apr 5, 2026
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>
@dfed dfed force-pushed the dfed--mock-additional-types branch from fdce3ab to c137d24 Compare April 5, 2026 02:36
@dfed dfed marked this pull request as ready for review April 5, 2026 03:21
@dfed dfed merged commit 6dd6df9 into main Apr 5, 2026
19 checks passed
@dfed dfed deleted the dfed--mock-additional-types branch April 5, 2026 03:22
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.

1 participant