Comments (13)
No problem. I’m still working on the fix but I’ll upload it as soon as it’s done.
from swiftmockgeneratorforxcode.
I've added this feature now and uploaded the app here. One note, I couldn't support methods with where
clauses due to some limitations with SourceKit but I've got a plan to support this soon.
from swiftmockgeneratorforxcode.
Any news on this issue?
from swiftmockgeneratorforxcode.
Thanks for raising this issue. Generic methods aren't supported yet. But I'll look at implementing this ASAP.
from swiftmockgeneratorforxcode.
That would be great. I was trying to think of how to handle this myself, just modifying the generated mock but I was coming up short. What are you thinking as a possible solution?
from swiftmockgeneratorforxcode.
It seems like we can use Any
for capturing generic paramters. We can't get anymore accurate than that because a generic type might conform to a protocol with associated type requirements which would not compile.
Same goes for capturing types. T.Type
is captured by Any.Type
.
Returning generic types gets a little tricky but we can cast our stubbed value to the generic type in the return statement.
What do you think?
protocol AssociatedTypeProtocol {
associatedtype A
}
protocol GenericMethod {
func test<T>(a: T)
func test<T: NSObject>(b: T)
func test<T: NSObject>(c: T.Type)
func testReturn1<T>() -> T?
func testReturn2<T>() -> T
func testReturn3<T: NSObject>() -> T
func test<T: AssociatedTypeProtocol>(d: T)
}
class GenericMethodMock: GenericMethod {
var invokedTestA = false
var invokedTestACount = 0
var invokedTestAParameters: (a: Any, Void)?
var invokedTestAParametersList = [(a: Any, Void)]()
func test<T>(a: T) {
invokedTestA = true
invokedTestACount += 1
invokedTestAParameters = (a, ())
invokedTestAParametersList.append((a, ()))
}
var invokedTestB = false
var invokedTestBCount = 0
var invokedTestBParameters: (b: Any, Void)?
var invokedTestBParametersList = [(b: Any, Void)]()
func test<T: NSObject>(b: T) {
invokedTestB = true
invokedTestBCount += 1
invokedTestBParameters = (b, ())
invokedTestBParametersList.append((b, ()))
}
var invokedTestC = false
var invokedTestCCount = 0
var invokedTestCParameters: (c: Any.Type, Void)?
var invokedTestCParametersList = [(c: Any.Type, Void)]()
func test<T: NSObject>(c: T.Type) {
invokedTestC = true
invokedTestCCount += 1
invokedTestCParameters = (c, ())
invokedTestCParametersList.append((c, ()))
}
var invokedTestReturn1 = false
var invokedTestReturn1Count = 0
var stubbedTestReturn1Result: Any!
func testReturn1<T>() -> T? {
invokedTestReturn1 = true
invokedTestReturn1Count += 1
return stubbedTestReturn1Result as? T
}
var invokedTestReturn2 = false
var invokedTestReturn2Count = 0
var stubbedTestReturn2Result: Any!
func testReturn2<T>() -> T {
invokedTestReturn2 = true
invokedTestReturn2Count += 1
return stubbedTestReturn2Result as! T
}
var invokedTestReturn3 = false
var invokedTestReturn3Count = 0
var stubbedTestReturn3Result: Any!
func testReturn3<T: NSObject>() -> T {
invokedTestReturn3 = true
invokedTestReturn3Count += 1
return stubbedTestReturn3Result as! T
}
var invokedTestD = false
var invokedTestDCount = 0
var invokedTestDParameters: (d: Any, Void)?
var invokedTestDParametersList = [(d: Any, Void)]()
func test<T: AssociatedTypeProtocol>(d: T) {
invokedTestD = true
invokedTestDCount += 1
invokedTestDParameters = (d, ())
invokedTestDParametersList.append((d, ()))
}
}
from swiftmockgeneratorforxcode.
I think this looks great! Thank you for being so attentive and quick to come up with a solution. Would you be able to merge the changes in? Right now I am working on switching some older mocks over to use your generator and I have one I can not yet switch without this change.
from swiftmockgeneratorforxcode.
Fantastic! Thanks for being so attentive. Your library has saved us a lot of time and made it easier to teach those that are learning about tests.
from swiftmockgeneratorforxcode.
Okay so what I am seeing here is a little bit different than what you had posted above in the edge case that an array of generic type is one of the parameters or the return type also
func bar<T: Foo>(a: [T], completion: @escaping () -> Void)
var invokedBarCount = 0
var invokedBarParameters: (a: [T], Void)?
var invokedBarParametersList = [(a: [T], Void)]()
var stubbedBarCompletionResult: (Bool, Void)?
func bar<T: Foo>(a: [T], completion: @escaping () -> Void) {
invokedBar = true
invokedBarCount += 1
invokedBarParameters = (a, ())
invokedBarParametersList.append((a, ()))
if let result = stubbedBarCompletionResult {
completion(result.0)
}
}```
from swiftmockgeneratorforxcode.
Thanks for raising this case and I’m glad this plugin is saving you some time. The information I get from SourceKit is really basic (just a string for a parameter type) which is why my plugin won’t recognise the type within an array (and many other cases). I’m working on ditching it in favour of a custom syntax parser which I’m writing myself. But I’ll make generics my priority when it’s done.
Let’s leave this issue open and I’ll write any updates here.
from swiftmockgeneratorforxcode.
Sounds good! Let me know if you need anything. For the time being I just swapped out "T" for "Any" and left a note that it needed to be done if the mock was regenerated. Works great :)
from swiftmockgeneratorforxcode.
The release to fix your generic array is finally ready
I've replaced SourceKit with a custom parser which will hopefully speed things up going forward.
There are still some types that aren't supported yet (see the README) but I'll add support for them soon.
from swiftmockgeneratorforxcode.
fantastic thank you!
from swiftmockgeneratorforxcode.
Related Issues (20)
- Installation? HOT 1
- Using this with Objective C HOT 1
- Typo in permission error message
- Generated code does not follow the enclosing class access control HOT 1
- Extension doesn't work on Big Sur HOT 16
- ObjectToTest ???? HOT 2
- Can't find Mock Generator on Big Sur HOT 1
- import AST errors HOT 2
- Stubs are not generated when the member variable end in a '$'
- Optional properties have a force-unwrapped stub
- Installation via Homebrew
- Stopped working under Big Sur and Xcode 12.4 HOT 1
- Extensions not supported HOT 1
- Indentation follows Xcode ctrl-i
- Naming Conventions
- Generate async mocks HOT 6
- Cannot compile because of missing private framework `SwiftToolkit`
- Optional function parameter break generator
- Handle generic completion handlers in mocked function
- Can I enter it in the function?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swiftmockgeneratorforxcode.