From 755281a40b07a7efec83fab418182a84f18f2ac6 Mon Sep 17 00:00:00 2001 From: Tim Wang Date: Thu, 29 Jun 2023 14:05:48 +1000 Subject: [PATCH] Improve Mock macro to use strong type instead of String --- README.md | 4 ++-- Sources/Client/main.swift | 2 +- Sources/Macros/Mock.swift | 2 +- Sources/SwiftMacros/SwiftMacros.swift | 2 +- Tests/MacroTests/MockTests.swift | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index be2c6d2..c97e075 100644 --- a/README.md +++ b/README.md @@ -34,8 +34,8 @@ A practical collection of Swift Macros that help code correctly and smartly. | |
#formatDateComponents(from: Date(), to: Date(), allowedUnits: [.day, .hour, .minute, .second])
#formatDateComponents(fromInterval: 100, allowedUnits: [.day, .hour, .minute, .second])
#formatDateComponents(fromComponents: DateComponents(hour: 10), allowedUnits: [.day, .hour, .minute, .second])
| | #formatDateInterval |Format two dates into interval string | | |
#formatDateInterval(from: Date(), to: Date(), dateStyle: .short)
| -| @Mock |Generate a static variable `mock` using the attached initializer.
For custmoised data type, it will use `Type.mock`. In case there is no this value, need to define this yourself or use `@Mock` or `@AddInit` to generate this variable. | -| |
class AStruct {
let a: Float
@Mock(typeName: "AStruct")
init(a: Float) {
self.a = a
}
}
| +| @Mock |Generate a static variable `mock` using the attached initializer.
For custmoised data type, it will use `Type.mock`. In case there is no this value, need to define this yourself or use `@Mock` or `@AddInit(withMock: true)` to generate this variable. | +| |
class AStruct {
let a: Float
@Mock(type: AStruct.self)
init(a: Float) {
self.a = a
}
}
| | #postNotification | An easy way to post notifications | | |
#postNotification(.NSCalendarDayChanged)
| | @Singleton |Generate Swift singleton code for struct and class types | diff --git a/Sources/Client/main.swift b/Sources/Client/main.swift index a8ad917..bb56ca2 100644 --- a/Sources/Client/main.swift +++ b/Sources/Client/main.swift @@ -87,7 +87,7 @@ print(urlrequest?.timeoutInterval) class AStruct { let a: Float - @Mock(typeName: "AStruct") + @Mock(type: AStruct.self) init(a: Float) { self.a = a } diff --git a/Sources/Macros/Mock.swift b/Sources/Macros/Mock.swift index 89c6cff..66ec12a 100644 --- a/Sources/Macros/Mock.swift +++ b/Sources/Macros/Mock.swift @@ -12,7 +12,7 @@ public struct Mock: PeerMacro { guard let initializer = declaration.as(InitializerDeclSyntax.self) else { throw MacroDiagnostics.errorMacroUsage(message: "Can only apply to an initializer") } - guard let typeName = node.argument(for: "typeName")?.as(StringLiteralExprSyntax.self)?.segments.first else { + guard let typeName = node.argument(for: "type")?.as(MemberAccessExprSyntax.self)?.base else { throw MacroDiagnostics.errorMacroUsage(message: "Must specify type name") } diff --git a/Sources/SwiftMacros/SwiftMacros.swift b/Sources/SwiftMacros/SwiftMacros.swift index d69aefb..c37c7ba 100644 --- a/Sources/SwiftMacros/SwiftMacros.swift +++ b/Sources/SwiftMacros/SwiftMacros.swift @@ -121,7 +121,7 @@ public macro formatDateInterval(from fromDate: Date, timeZone: TimeZone? = nil) -> String = #externalMacro(module: "Macros", type: "FormatDateInterval") @attached(peer, names: named(mock)) -public macro Mock(typeName: String, randomMockValue: Bool = true) = #externalMacro(module: "Macros", type: "Mock") +public macro Mock(type: Any.Type, randomMockValue: Bool = true) = #externalMacro(module: "Macros", type: "Mock") @freestanding(expression) public macro postNotification(_ name: NSNotification.Name, diff --git a/Tests/MacroTests/MockTests.swift b/Tests/MacroTests/MockTests.swift index 13b08f0..e53c6e4 100644 --- a/Tests/MacroTests/MockTests.swift +++ b/Tests/MacroTests/MockTests.swift @@ -14,7 +14,7 @@ final class MockTests: XCTestCase { struct AStruct { let a: Int - @Mock(typeName: "AStruct", randomMockValue: false) + @Mock(type: AStruct.self, randomMockValue: false) init(a: Int) { self.a = a }