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 async and throwing property support #267

Merged
merged 11 commits into from
Oct 26, 2024

Conversation

sidepelican
Copy link
Collaborator

Main feature

Supported { get async throws }.

/// @mockable
protocol AsyncThrowsVars {
    var getAndThrows: MyValue { get throws }
    var getAndAsync: MyValue { get async }
    var getAndAsyncAndThrows: Int { get async throws(any Error) }
}
  • Generated
class AsyncThrowsVarsMock: AsyncThrowsVars {
    init() { }
    init(getAndThrows: MyValue, getAndAsync: MyValue, getAndAsyncAndThrows: Int = 0) {
        self.getAndThrowsHandler = { getAndThrows }
        self.getAndAsyncHandler = { getAndAsync }
        self.getAndAsyncAndThrowsHandler = { getAndAsyncAndThrows }
    }


    var getAndThrowsHandler: (() throws -> MyValue)?
    var getAndThrows: MyValue {
        get throws {
            if let getAndThrowsHandler = getAndThrowsHandler {
                return try getAndThrowsHandler()
            }
            fatalError("getAndThrowsHandler returns can't have a default value thus its handler must be set")
        }
    }

    var getAndAsyncHandler: (() async -> MyValue)?
    var getAndAsync: MyValue {
        get async {
            if let getAndAsyncHandler = getAndAsyncHandler {
                return await getAndAsyncHandler()
            }
            fatalError("getAndAsyncHandler returns can't have a default value thus its handler must be set")
        }
    }

    var getAndAsyncAndThrowsHandler: (() async throws(any Error) -> Int)?
    var getAndAsyncAndThrows: Int {
        get async throws(any Error) {
            if let getAndAsyncAndThrowsHandler = getAndAsyncAndThrowsHandler {
                return try await getAndAsyncAndThrowsHandler()
            }
            return 0
        }
    }
}

Side effects

  • For var foo: Int { get }, since the value is never set through original protocol, the call count has been removed.

@sidepelican sidepelican marked this pull request as draft October 25, 2024 10:12
@sidepelican
Copy link
Collaborator Author

sidepelican commented Oct 25, 2024

  • it should be tested for generic(using associated type) property
    → mockolo already handled generic type as Any with tyepalias

@sidepelican sidepelican marked this pull request as ready for review October 26, 2024 08:28
case .rethrows:
return true
case .typed(let errorType):
return errorType != .neverType && errorType != "Swift.\(String.neverType)"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throws(Never) means no throws. Without this consideration, there would be warnings in the generated code.

@@ -368,11 +368,48 @@ extension VariableDeclSyntax {
typeName = vtype
}

let storageType: VariableModel.MockStorageType
switch v.accessorBlock?.accessors {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block is very complicated 🤪 but mockolo handles both class and protocol here, so it has to be this way.

@@ -29,7 +41,7 @@ final class VariableModel: Model {
}

var underlyingName: String {
if isStatic || type.defaultVal() == nil {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The computed property is generated as below, but I think it is meaningless, so I will remove it.
https://github.com/uber/mockolo/pull/267/files#diff-0680dee8c19da68e406e1ecf918c1c39fe5c55214b3e399ac8dd929fe6b72c7cL43-L48

Copy link
Collaborator

@fummicc1 fummicc1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your PR! I will take more time to finish reviewing this PR.

Copy link
Collaborator

@fummicc1 fummicc1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much!

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.

2 participants