mockolo icon indicating copy to clipboard operation
mockolo copied to clipboard

Allow Computed Properties that return Self

Open uhooi opened this issue 2 years ago • 2 comments

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

  • https://github.com/uber/mockolo/pull/118

Version

  • Mockolo: 1.7.0

uhooi avatar Apr 12 '22 13:04 uhooi

If it were Self, would there be any problems?

sidepelican avatar May 19 '23 02:05 sidepelican

@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 {
                                   ^

uhooi avatar Sep 14 '23 09:09 uhooi