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.03k stars 104 forks source link

Added support for root typealiases #350

Open 666tos opened 1 year ago

666tos commented 1 year ago

Mock template was customised to add single feature, which I named root-typealiases

Root typealiases are placed in root of the resulting file and are NOT nested inside type (as typealiases do). They are meant to solve problem partially described in https://github.com/MakeAWishFoundation/SwiftyMocky/issues/184 Problem occurs when mock is generated for protocol name, which clashes with type from another library. Since there is no concept of module in Sourcery/SwiftyMocky, compiler gets confused which type should be used for inheritance, for example:

Typealias example:

Our module:

    //sourcery: root-typealias = "Feature = MyModule.Feature"
    protocol Feature: AutoMockable {}

Another module:

    public struct Feature {}

Resulting Mock.generated.swift:

    open class FeatureMock: Feature, Mock { <--- 'Feature' is ambiguous for type lookup in this context
        public typealias Feature = MyModule.Feature
    } 

In this implementation this problem can be fixed by adding annotation for Feature protocol, like this:

    //sourcery: root-typealias = "Feature = MyModule.Feature"
    protocol Feature: AutoMockable {}

Resulting Mock.generated.swift:

    public typealias Feature = MyModule.Feature
    open class FeatureMock: Feature, Mock {}  <--- No ambiguity for type lookup in this context