Skip to content

jimmya/sourcery-templates

Repository files navigation

Sourcery Templates

This repository contains 4 Sourcery templates which you can use in your projects: AutoMockable and AutoStubbable

Installation

For now the easiest way is to add this repository as a submodule. To use it in your .sourcery.yml config files simply add a path to the Sources/Templates directory (or directly Sources/Templates/AutoMockable.swifttemplate/Sources/Templates/AutoStubbable.swifttemplate if you only intend to use one).

templates:
    - SourceryTemplatesCheckoutDirectory/Sources/Templates/AutoMockable.swifttemplate

AutoMockable.swifttemplate

This template generates mock definitions for any protocol that has the // sourcery: AutoMockable annotation. It will create definitions for all properties and methods in the main definition of the protocol (extensions are excluded).

Definition:

// sourcery: AutoMockable
protocol MockProtocolDeclaration {
    var immutableProperty: Int { get }
    var mutableProperty: Int { get set }

    func method(property: Int) -> Int
}

Generated mock:

class DefaultMockProtocolDeclarationMock: MockProtocolDeclaration {

    var invokedImmutablePropertyGetter = false
    var invokedImmutablePropertyGetterCount = 0
    var stubbedImmutableProperty: Int! = 0

    var immutableProperty: Int {
        invokedImmutablePropertyGetter = true
        invokedImmutablePropertyGetterCount += 1
        return stubbedImmutableProperty
    }

    var invokedMutablePropertySetter = false
    var invokedMutablePropertySetterCount = 0
    var invokedMutableProperty: Int?
    var invokedMutablePropertyList: [Int] = []
    var invokedMutablePropertyGetter = false
    var invokedMutablePropertyGetterCount = 0
    var stubbedMutableProperty: Int! = 0

    var mutableProperty: Int {
        get {
            invokedMutablePropertyGetter = true
            invokedMutablePropertyGetterCount += 1
            return stubbedMutableProperty
        }
        set {
            invokedMutablePropertySetter = true
            invokedMutablePropertySetterCount += 1
            invokedMutableProperty = newValue
            invokedMutablePropertyList.append(newValue)
        }
    }

    var invokedMethod = false
    var invokedMethodCount = 0
    var invokedMethodParameters: (property: Int, Void)?
    var invokedMethodParametersList: [(property: Int, Void)] = []
    var stubbedMethodResult: Int! = 0
    var invokedMethodExpectation = XCTestExpectation(description: "\(#function) expectation")

    func method(property: Int) -> Int {
        defer { invokedMethodExpectation.fulfill() }
        invokedMethod = true
        invokedMethodCount += 1
        invokedMethodParameters = (property: property, ())
        invokedMethodParametersList.append((property: property, ()))
        return stubbedMethodResult
    }
}

Arguments

Add the following arguments to your .sourcery.yml file to provide any required imports for the generated file.

args:
  imports: [Foundation, XCTest]
  testableImports: [ModuleContainingYourProtocol]

To customize the naming of the class mock generation you can add a mockPrefix and/or mockSuffix argument. By default the mockPrefix is Default and mockSuffix is Mock, resulting in DefaultProtocolMock.

If you would only like a prefix, you should add the mockSuffix argument with an empty string value: mockSuffix: ""

The same applies when you only want a suffix, add the mockPrefix argument with an empty string value.

Example:

args:
  mockPrefix: "Apple"
  mockSuffix: "Pear"
// sourcery: AutoMockable
protocol ExampleProtocol { }

// Generated
class AppleExampleProtocolPear: ExampleProtocol {

}

After generating you can use the mock definitions to easily setup your tests, provide input and assert.

AutoStubbable.swifttemplate

This template generates stub methods for all your models. It'll attempt to default any property to make initialisation easy. This way you can focus on initialising what matters in your test.

Definition:

// sourcery: AutoStubbable
struct MockDomainModelDeclaration {
    let property: Int
    let optionalProperty: String?
}

Generated stub:

internal extension MockDomainModelDeclaration {
    static func stub(
        property: Int = 0,
        optionalProperty: String? = nil
    ) -> MockDomainModelDeclaration {
        MockDomainModelDeclaration(
            property: property,
            optionalProperty: optionalProperty
        )
    }
}

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages