Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for opaque type (some keyword) to function parameter type in AutoMockable.stencil #1197

Merged

Conversation

paul1893
Copy link
Contributor

Context

some keyword known to be used primarily with SwiftUI but also with function parameter type (resulting in opaque parameter) since Swift 5.7 following this proposal seems to not being supported in the AutoMockable.stencil.

In this PR

This PR proposes to handle opaque parameters by updating the stencil accordingly.
As for now in Swift 5.9, some keyword is not supported for return type or closure parameter type when used in protocols:

var a: some StubProtocol { get } // 'some' type cannot be the return type of a protocol requirement; did you mean to add an associated type?
func k(x: ((some StubProtocol)?) -> Void, y: (some StubProtocol) -> Void) // 'some' cannot appear in parameter position in parameter type '((some StubProtocol)?) -> Void'

So here is the cases derived from #1186.

protocol SomeProtocol {
    func a(_ x: (some StubProtocol)?, y: (some StubProtocol)!, z: some StubProtocol)
    func b(x: (some StubProtocol)?, y: (some StubProtocol)!, z: some StubProtocol) async -> String
    func someConfusingFuncName(x: some StubProtocol)
    func c(someConfusingArgumentName: some StubProtocol)
    func d(_ x: (some StubWithSomeNameProtocol)?)
}

And the generated output:

class SomeProtocolMock: SomeProtocol {




    //MARK: - a

    var ayzCallsCount = 0
    var ayzCalled: Bool {
        return ayzCallsCount > 0
    }
    var ayzReceivedArguments: (x: (any StubProtocol)?, y: (any StubProtocol)?, z: any StubProtocol)?
    var ayzReceivedInvocations: [(x: (any StubProtocol)?, y: (any StubProtocol)?, z: any StubProtocol)] = []
    var ayzClosure: (((any StubProtocol)?, (any StubProtocol)?, any StubProtocol) -> Void)?

    func a(_ x: (some StubProtocol)?, y: (some StubProtocol)!, z: some StubProtocol) {
        ayzCallsCount += 1
        ayzReceivedArguments = (x: x, y: y, z: z)
        ayzReceivedInvocations.append((x: x, y: y, z: z))
        ayzClosure?(x, y, z)
    }

    //MARK: - b

    var bxyzCallsCount = 0
    var bxyzCalled: Bool {
        return bxyzCallsCount > 0
    }
    var bxyzReceivedArguments: (x: (any StubProtocol)?, y: (any StubProtocol)?, z: any StubProtocol)?
    var bxyzReceivedInvocations: [(x: (any StubProtocol)?, y: (any StubProtocol)?, z: any StubProtocol)] = []
    var bxyzReturnValue: String!
    var bxyzClosure: (((any StubProtocol)?, (any StubProtocol)?, any StubProtocol) async -> String)?

    func b(x: (some StubProtocol)?, y: (some StubProtocol)!, z: some StubProtocol) async -> String {
        bxyzCallsCount += 1
        bxyzReceivedArguments = (x: x, y: y, z: z)
        bxyzReceivedInvocations.append((x: x, y: y, z: z))
        if let bxyzClosure = bxyzClosure {
            return await bxyzClosure(x, y, z)
        } else {
            return bxyzReturnValue
        }
    }

    //MARK: - someConfusingFuncName

    var someConfusingFuncNameXCallsCount = 0
    var someConfusingFuncNameXCalled: Bool {
        return someConfusingFuncNameXCallsCount > 0
    }
    var someConfusingFuncNameXReceivedX: (any StubProtocol)?
    var someConfusingFuncNameXReceivedInvocations: [(any StubProtocol)] = []
    var someConfusingFuncNameXClosure: ((any StubProtocol) -> Void)?

    func someConfusingFuncName(x: some StubProtocol) {
        someConfusingFuncNameXCallsCount += 1
        someConfusingFuncNameXReceivedX = x
        someConfusingFuncNameXReceivedInvocations.append(x)
        someConfusingFuncNameXClosure?(x)
    }

    //MARK: - c

    var cSomeConfusingArgumentNameCallsCount = 0
    var cSomeConfusingArgumentNameCalled: Bool {
        return cSomeConfusingArgumentNameCallsCount > 0
    }
    var cSomeConfusingArgumentNameReceivedSomeConfusingArgumentName: (any StubProtocol)?
    var cSomeConfusingArgumentNameReceivedInvocations: [(any StubProtocol)] = []
    var cSomeConfusingArgumentNameClosure: ((any StubProtocol) -> Void)?

    func c(someConfusingArgumentName: some StubProtocol) {
        cSomeConfusingArgumentNameCallsCount += 1
        cSomeConfusingArgumentNameReceivedSomeConfusingArgumentName = someConfusingArgumentName
        cSomeConfusingArgumentNameReceivedInvocations.append(someConfusingArgumentName)
        cSomeConfusingArgumentNameClosure?(someConfusingArgumentName)
    }

    //MARK: - d

    var dCallsCount = 0
    var dCalled: Bool {
        return dCallsCount > 0
    }
    var dReceivedX: (any StubWithSomeNameProtocol)?
    var dReceivedInvocations: [(any StubWithSomeNameProtocol)?] = []
    var dClosure: (((any StubWithSomeNameProtocol)?) -> Void)?

    func d(_ x: (some StubWithSomeNameProtocol)?) {
        dCallsCount += 1
        dReceivedX = x
        dReceivedInvocations.append(x)
        dClosure?(x)
    }

}

@art-divin
Copy link
Collaborator

👋🏻 Hey @paul1893 ,

thank you for your MR!

Sorry for the delayed response. Could you please rebase (I prefer merging tbh) with the latest master branch? CI would verify and we can merge.

Thank you 🙏🏻

@paul1893
Copy link
Contributor Author

paul1893 commented Sep 9, 2023

Sure. Here it is.

@art-divin
Copy link
Collaborator

Thank you.

Seems like an assertion located at TemplatesTests.swift:90 is failing on Linux.

Here's the log.

Now there are 2 copies of TemplateTests files. So, you'd need to add your changes to folder located at /Templates/Tests/Context_Linux. This is due to a bug in Swift compiler under linux, and a missing feature in SPM, we need to duplicate test resources with small alternation - AutoCodable needs to be omitted from Linux tests.

@paul1893
Copy link
Contributor Author

Crystal clear. Thanks @art-divin. Should be good now 🙂

@art-divin art-divin merged commit 55ef3b6 into krzysztofzablocki:master Sep 10, 2023
2 checks passed
@paul1893 paul1893 deleted the tech/opaque-type-support branch November 9, 2023 10:59
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.

3 participants