GithubHelp home page GithubHelp logo

sourcery-templates's Introduction

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 definions 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
        )
    }
}

sourcery-templates's People

Contributors

jimmya avatar arnedevries avatar schroefdop avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.