swift-sourcery-templates

Advanced Protocol Mock, Type Erasure and dependency-forwarding Code-generation templates for Swift (using Sourcery).
Three templates:
Mocks.swifttemplate — generates protocol mock classes for test doubles with call counting, handler closures, and smart defaults
TypeErase.swifttemplate — generates type erasure wrappers
Component.swifttemplate — generates the forwarding class that satisfies a dependency-injection protocol, for trees where each level declares what it consumes. See Components
The first two support protocols with associated types, generic functions with constraints, @escaping closure parameters,
and provide smart defaults for RxSwift and Combine types. Compatible with RIBs architecture patterns.
Generated mocks compile with zero diagnostics under -strict-concurrency=complete and in the
Swift 6 language mode: async and nonisolated are carried through to the mock, a protocol's
global actor is declared on the mock class, and a Sendable protocol gets an @unchecked
conformance. See Concurrency.
swift-sourcery-templates is open source under the Apache License 2.0 and works in any SwiftPM package
or Xcode project on its own. Modaal.dev supports this repository and also
makes code and design authoring tools for native iOS and Android apps: the tools give an agent the
project's context and the commands to build, check and run the app, and let a person design the
screens and edit the code beside the agent.
Agent skill
skills/swift-sourcery-mocks/ is an agent skill: the
setup below, written for a coding agent working in a repository that adopts these templates. It
picks the lane, writes the config, prints the mocks a target generates without building it, and
names the cause when generation produces a mock that does not conform.
Four channels install it, over one tree.
1. Any of ~75 agents, through the cross-agent CLI. -g installs for every project on the
machine instead of this one; --list lists without installing.
npx skills add modaal-agent/swift-sourcery-templates
2. As a Claude Code plugin. The repository root is the marketplace, and skills/ is the
plugin's root:
/plugin marketplace add modaal-agent/swift-sourcery-templates
/plugin install swift-sourcery-mocks@swift-sourcery-templates
/plugin update swift-sourcery-mocks@swift-sourcery-templates installs a new version.
3. By hand.
git clone https://github.com/modaal-agent/swift-sourcery-templates
cp -r swift-sourcery-templates/skills/* ~/.claude/skills/
.claude/skills/ inside a project installs it for that project alone.
4. claude.ai and the Skills API. The frontmatter carries only the Agent Skills standard's keys,
so skills/swift-sourcery-mocks/ packages and uploads unedited.
A prompt to try in a Swift package:
The Payments package's tests use a hand-written FakeRefundService. Generate mocks for the
protocols in Sources/Payments into the PaymentsTests target, and rewrite RefundFlowTests to
use RefundServiceMock.
Generated Mock API
For a protocol method:
/// sourcery: ProtocolMock
protocol DataService {
func fetchData(id: String, completion: @escaping (String?, Error?) -> Void)
}
The template generates:
final class DataServiceMock: DataService {
func fetchData(id: String, completion: @escaping (String?, Error?) -> Void) {
fetchDataCallCount += 1
fetchDataArgs.append(id)
if let __fetchDataHandler = self.fetchDataHandler {
__fetchDataHandler(id, completion)
}
}
var fetchDataCallCount: Int = 0
var fetchDataArgs: [String] = []
var fetchDataHandler: ((_ id: String, _ completion: @escaping (String?, Error?) -> Void) -> ())? = nil
}