MakeAWishFoundation / SwiftyMocky

Framework for automatic mock generation. Adds a set of handy methods, simplifying testing. One of the best and most complete solutions, including generics support and much more.
MIT License
1.04k stars 106 forks source link

Invalid MockFile if customising Objc interface with annotation #283

Closed strfn closed 3 years ago

strfn commented 3 years ago

The generated MockFile is invalid (not compilable) if a static swift method needs to be exposed to Objective-c with a custom selector. i.e. using @objc(thenewselect) The StaticVerify and StaticPerform part of the mock has the @objc annotation partly stripped out

Given the following protocol

//sourcery: AutoMockable
//sourcery: ObjcProtocol
@objc public protocol ProtocolWithObjc {

    @objc(doStaticStuffWithParameter1: andParameter2:)
    static func doStaticStuff(parameter1: String, parameter2: String)

    @objc(doStuffWithParameter1: andParameter2:)
    static func doStuff(parameter1: String, parameter2: String)
}

The generated mockfile will be like

 public struct StaticVerify {
        fileprivate var method: StaticMethodType

        (doStaticStuffWithParameter1: andParameter2:)
        public static func doStaticStuff(parameter1: Parameter<String>, parameter2: Parameter<String>) -> StaticVerify { return StaticVerify(method: .sm_doStaticStuff__parameter1_parameter1parameter2_parameter2(`parameter1`, `parameter2`))}
        (doStuffWithParameter1: andParameter2:)
        public static func doStuff(parameter1: Parameter<String>, parameter2: Parameter<String>) -> StaticVerify { return StaticVerify(method: .sm_doStuff__parameter1_parameter1parameter2_parameter2(`parameter1`, `parameter2`))}
    }

    public struct StaticPerform {
        fileprivate var method: StaticMethodType
        var performs: Any

        (doStaticStuffWithParameter1: andParameter2:)
        public static func doStaticStuff(parameter1: Parameter<String>, parameter2: Parameter<String>, perform: @escaping (String, String) -> Void) -> StaticPerform {
            return StaticPerform(method: .sm_doStaticStuff__parameter1_parameter1parameter2_parameter2(`parameter1`, `parameter2`), performs: perform)
        }
        (doStuffWithParameter1: andParameter2:)
        public static func doStuff(parameter1: Parameter<String>, parameter2: Parameter<String>, perform: @escaping (String, String) -> Void) -> StaticPerform {
            return StaticPerform(method: .sm_doStuff__parameter1_parameter1parameter2_parameter2(`parameter1`, `parameter2`), performs: perform)
        }
    }
strfn commented 3 years ago

More than happy to contribute back a fix if anyone can point me out to the right process to update and test the template.

g-amichnia commented 3 years ago

Sorry for late reply, was on paternity leave for few months. Looking into it.

strfn commented 3 years ago

All I can say …. CONGRATULATIONS !

Happy to take care of it with some guidance (if it is not just quicker for you to fix it)

g-amichnia commented 3 years ago

Done in 4.1