uber / mockolo

Efficient Mock Generator for Swift
Apache License 2.0
813 stars 86 forks source link

Allow Computed Properties that return Self #185

Open uhooi opened 2 years ago

uhooi commented 2 years ago

Computed properties that return Self also return Self in the mock.

/// @mockable
protocol FooRepository {
    static var shared: Self { get }
}

I would like the mock class return.

final class FooRepositoryMock: FooRepository {
    init() { }

    static private(set) var sharedSetCallCount = 0
-   static private var _shared: Self!  { didSet { sharedSetCallCount += 1 } }
-   static var shared: Self {
+   static private var _shared: FooRepositoryMock!  { didSet { sharedSetCallCount += 1 } }
+   static var shared: FooRepositoryMock {
        get { return _shared }
        set { _shared = newValue }
    }
}

Reference

Version

sidepelican commented 1 year ago

If it were Self, would there be any problems?

uhooi commented 1 year ago

@sidepelican Mock causes a compile error.

❌  .../OutputMocks.swift:99:99: stored property cannot have covariant 'Self' type

    static private var _shared: Self!  { didSet { sharedSetCallCount += 1 } }
                       ^
❌  .../OutputMocks.swift:99:99: mutable property cannot have covariant 'Self' type

    public static var shared: Self {
                                   ^