uber / mockolo

Efficient Mock Generator for Swift
Apache License 2.0
804 stars 85 forks source link

Support type composition (`&` keyword) #217

Closed fummicc1 closed 1 year ago

fummicc1 commented 1 year ago

Overview

When use & keyword to conform to multiple protocols, mockolo only picks up the first protocol.

This issue is already mentioned in #170

protocol FirstSwiftProtocol {
    var someBool: Bool { get }

    func doSomething()
}

protocol SecondSwiftProtocol {
    var someInt: Int { get set }

    func doSomethingElse() -> Bool
}

/// @mockable
protocol TestProtocol: FirstSwiftProtocol & SecondSwiftProtocol {
    func doSomethingSpecial()
}
///
/// @Generated by Mockolo
///

class TestProtocolMock: TestProtocol {
    init() { }
    init(someBool: Bool = false) {
        self.someBool = someBool
    }

    private(set) var someBoolSetCallCount = 0
    var someBool: Bool = false { didSet { someBoolSetCallCount += 1 } }

    private(set) var doSomethingCallCount = 0
    var doSomethingHandler: (() -> ())?
    func doSomething()  {
        doSomethingCallCount += 1
        if let doSomethingHandler = doSomethingHandler {
            doSomethingHandler()
        }

    }

    private(set) var doSomethingSpecialCallCount = 0
    var doSomethingSpecialHandler: (() -> ())?
    func doSomethingSpecial()  {
        doSomethingSpecialCallCount += 1
        if let doSomethingSpecialHandler = doSomethingSpecialHandler {
            doSomethingSpecialHandler()
        }

    }
}

Solution

checking if inheritedType is CompositionType or not.

fummicc1 commented 1 year ago

@sidepelican

Thank you for reviewing!

This will not work in nested tupled protocol but maybe enough with this implementation in majour usecase.

I wasn't aware of this case, you mean protocol A: (B & C) was not correctly parsed, right?

Screenshot 2023-03-06 at 12 43 54

I think this problem can be fixed at https://github.com/uber/mockolo/pull/217/commits/6253922b620d2362ac0c6369b85b5fcf698c376a . What do you think of it?