You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Update README.md to enhance documentation on handling overloaded methods in Swift-Spyable
This commit refines the section on method overloading (polymorphism) by clarifying the functionality of the `@Spyable` macro. It introduces a detailed explanation of the naming algorithm used to generate unique identifiers for overloaded methods, along with updated examples demonstrating the generated spy properties. The changes aim to improve user understanding of how distinct identifiers are created and the rules governing the naming convention.
Copy file name to clipboardExpand all lines: README.md
+33-16Lines changed: 33 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -152,31 +152,48 @@ internal class DebugProtocolSpy: DebugProtocol {
152
152
#endif
153
153
```
154
154
155
-
### Handling Overloaded Methods
155
+
### Handling Overloaded Methods (Polymorphism)
156
156
157
-
When a protocol contains multiple functions with the same name (i.e. function overloading), `@Spyable` ensures each generated spy property remains uniquely identifiable. It does so by extending the prefix with return types and parameter types when necessary.
157
+
When a protocol contains multiple functions with the same name (method overloading/polymorphism), `@Spyable` ensures
158
+
each generated spy property remains uniquely identifiable. The macro uses a sophisticated naming algorithm that combines
159
+
function names, parameter names, parameter types, and return types to create distinct identifiers.
158
160
159
-
For example:
161
+
#### Basic Polymorphism Examples
162
+
163
+
Consider this protocol with overloaded methods:
160
164
161
165
```swift
162
-
funcloadData() ->String
163
-
funcloadData() ->Int
164
-
funcloadData(id: Int) ->String
165
-
funcloadData(id: Int) ->String?
166
-
funcloadData(id: Int?) -> [String]?
166
+
@Spyable
167
+
protocolDataService {
168
+
funcloadData() ->String
169
+
funcloadData() ->Int
170
+
}
167
171
```
168
172
169
-
Will generate distinct spy identifiers like:
173
+
The generated spy will have these distinct identifiers:
170
174
175
+
```swift
176
+
publicclassDataServiceSpy: DataService {
177
+
// For: func loadData() -> String
178
+
publicvar loadDataStringCallsCount =0
179
+
publicvar loadDataStringReturnValue: String!
180
+
181
+
// For: func loadData() -> Int
182
+
publicvar loadDataIntCallsCount =0
183
+
publicvar loadDataIntReturnValue: Int!
184
+
}
171
185
```
172
-
loadDataString
173
-
loadDataInt
174
-
loadDataIdIntString
175
-
loadDataIdIntOptionalString
176
-
loadDataIdIntOptionalOptionalArrayString
177
-
```
178
186
179
-
This disambiguation avoids naming collisions while preserving accurate call tracking.
187
+
#### Naming Convention Algorithm
188
+
189
+
The naming algorithm follows these rules:
190
+
191
+
1.**Function Name**: Always starts with the base function name
192
+
2.**Parameter Names**: Adds capitalized first parameter names (ignoring `_` parameters)
193
+
3.**Parameter Types**: In descriptive mode, appends sanitized parameter types
194
+
4.**Return Type**: In descriptive mode, appends sanitized return type
195
+
5.**Special Keywords**: Includes `async`, `throws`, `escaping`, `Sendable`, etc.
196
+
6.**Type Sanitization**: Converts `[Type]` to `ArrayType`, `[Key: Value]` to `DictionaryKeyValue`, removes forbidden characters `:<>[](), -&` and converts `?` to `Optional`
0 commit comments