Skip to content

Commit

Permalink
Improve Mock macro to use strong type instead of String
Browse files Browse the repository at this point in the history
  • Loading branch information
ShenghaiWang committed Jun 29, 2023
1 parent 9d3ab01 commit 755281a
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ A practical collection of Swift Macros that help code correctly and smartly.
| |<pre>#formatDateComponents(from: Date(), to: Date(), allowedUnits: [.day, .hour, .minute, .second])<br>#formatDateComponents(fromInterval: 100, allowedUnits: [.day, .hour, .minute, .second])<br>#formatDateComponents(fromComponents: DateComponents(hour: 10), allowedUnits: [.day, .hour, .minute, .second])</pre>|
| #formatDateInterval |Format two dates into interval string |
| |<pre>#formatDateInterval(from: Date(), to: Date(), dateStyle: .short)</pre>|
| @Mock |Generate a static variable `mock` using the attached initializer. <br>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. |
| |<pre>class AStruct {<br> let a: Float<br> @Mock(typeName: "AStruct")<br> init(a: Float) {<br> self.a = a<br> }<br>}</pre>|
| @Mock |Generate a static variable `mock` using the attached initializer. <br>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. |
| |<pre>class AStruct {<br> let a: Float<br> @Mock(type: AStruct.self)<br> init(a: Float) {<br> self.a = a<br> }<br>}</pre>|
| #postNotification | An easy way to post notifications |
| |<pre>#postNotification(.NSCalendarDayChanged)</pre>|
| @Singleton |Generate Swift singleton code for struct and class types |
Expand Down
2 changes: 1 addition & 1 deletion Sources/Client/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Macros/Mock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftMacros/SwiftMacros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion Tests/MacroTests/MockTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 755281a

Please sign in to comment.