Add inherited types macro parameter #144
Open
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.
🚀 Enhancement: Spy Class Inheritance Support with
inheritedType
Approach n°3 of this issue : #47
I started my work from the pr of @MaxenceMottard
#136
📋 Summary
This pull request adds support for class inheritance in the
@Spyable
macro via the newinheritedType
parameter, allowing a spy class to inherit from another spy class. This feature also includes special handling of theopen
access level to support modular architectures.🎯 Main Objective: Spy Class Inheritance
Identified Need
Enable a generated spy class to inherit from another spy class to create coherent test hierarchies:
Concrete Use Cases
🏗️ Technical Challenge: Modular Architectures
Separate Package Problem
In certain architectures, tests are not in the same package as the protocols. We therefore need to offer the possibility of inheriting spy classes between packages.
Discovered Edge Case:
open
Access LevelTo be able to override an init from a class that comes from another package, the class must be
open
. That's why I added support foropen
.However, Swift does not allow
open
initializers:✅ Implemented Solution
1. New
inheritedType
ParameterAdded a simple parameter for spy class inheritance:
2. Smart
open
Initializer HandlingThe macro automatically detects when access level is
open
and generates the initializer aspublic
:3. Current Limitation: Single Inheritance Only
For now, only single type inheritance is supported. Multiple inheritance is not supported at this time, supporting a single protocol is our only path forward.
🔧 Technical Changes
Main Modified Files
Spyable.swift
inheritedType: String?
parameterAccessLevelModifierRewriter.swift
open
+InitializerDeclSyntax
open
→public
for initializersExtractor.swift
extractInheritedType()
method to extract inherited typeSpyFactory.swift
override
when necessarySpyableMacro.swift
Comprehensive Tests Added
UT_AccessLevelModifierRewriter.swift
: 12 tests for access level handlingUT_SpyableMacro.swift
: 6 tests for new use cases withinheritedType
🎯 Enabled Use Cases
1. Simple Inheritance
2. Inheritance Chain
3. Modular Architecture with
open