AlphaMacroKit
is made to enhance Swift development by providing useful Swift macros. For now this library introduces AutoNew
and EnumCaseChecker
macros to solve specific challenges in unit testing area.
- iOS 13.0+
- macOS 10.15+
- watchOS 6.0+
- tvOS 13.0+
- Swift 5.9+
In Xcode:
- Click project ⭢ Under projects tab select your project ⭢ Package Dependencies ⭢ Plus icon
- Use this URL https://github.com/Arideno/AlphaMacroKit
In Package.swift:
dependencies: [
.package(url: "https://github.com/Arideno/AlphaMacroKit", from: "0.1.0")
]
The AutoNew
macro simplifies the creation of instances, especially for structs and enums with multiple non-optional properties. It generates a new method, providing default values and reducing boilerplate code. This is useful in unit testing as you usually need to create some stub structs for tests. For example:
extension UUID {
static func new() -> UUID { .init() }
}
extension String {
static func new() -> String { .init() }
}
@AutoNew
struct User {
let id: UUID
let name: String
// Other properties
}
let newUser = User.new() // User(id: UUID(), name: "")
let newUser2 = User.new(name: "Test name") // User(id: UUID(), name: "Test name")
Similarly, EnumCaseChecker
enhances enums by adding computed properties for each case, allowing for more readable and maintainable code. This is particularly useful in unit testing scenarios where enum states are frequently checked. Example:
@EnumCaseChecker
enum State {
case loading
case success
case error(String)
}
let state = State.loading
print(state.isLoading) // true
These macros aim to streamline Swift development, making code more concise and expressive, and significantly improving the unit testing experience.
The latest documentation for AlphaMacroKit
is available
here.
If you happen to encounter any problem or you have any suggestion, please, don't hesitate to open an issue or reach out to me at andrii.moisol@gmail.com. This is an open source code project, so feel free to collaborate by raising a pull-request or sharing your feedback.
If you love this library, understand all the effort it takes to maintain it and would like to support me, you can buy me a coffee by following this link:
You can also sponsor me by hitting the GitHub Sponsor button. All help is very much appreciated.
AlphaMacroKit
is available under the MIT license. See the LICENSE file for more info.