Skip to content

Conversation

@fummicc1
Copy link
Collaborator

@fummicc1 fummicc1 commented Nov 22, 2025

Motivation

When mockable protocol inherits Actor, its should be actor-isolated.
When we use that mock-class, setting handler method is not easy because we can't update actor-isolated properties from test site. This can be solved by adding setHandler methods for actor mock-class.

/// @mockable
protocol P: Actor {
    func f()
}

will generate:

>
BeforeAfter
actor PMock: P {
    init() { }


    private(set) var fCallCount = 0
    var fHandler: (() -> ())?
    func f() {
        fCallCount += 1
        if let fHandler = fHandler {
            fHandler()
        }
        
    }
}
actor PMock: P {
    init() { }


    private(set) var fCallCount = 0
    var fHandler: (() -> ())?
    func setFHandler(_ handler: (() -> ())?) {
        fHandler = handler
    }
    func f() {
        fCallCount += 1
        if let fHandler = fHandler {
            fHandler()
        }
        
    }
}

I think setFHandler is helpful for users to set mock implementation.

Implementation

If output test-double is actor, mockolo also renders like the following:

func setFooHandler(_ handler: ((String) async -> Result<String, Error>)?) {
    fooHandler = handler
}

To classify if test-double is class or actor, I added new property mockDeclKind to RenderContext.

@fummicc1 fummicc1 force-pushed the feature/add-actor-setter-method branch from 4e55c11 to bd482c5 Compare November 22, 2025 16:35
@fummicc1 fummicc1 marked this pull request as ready for review November 22, 2025 16:47
Copy link
Collaborator

@sidepelican sidepelican left a comment

Choose a reason for hiding this comment

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

LGTM

@sidepelican sidepelican merged commit 1410201 into uber:master Nov 25, 2025
8 checks passed
@fummicc1 fummicc1 deleted the feature/add-actor-setter-method branch November 27, 2025 10:43
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