Skip to content

Commit 3605c58

Browse files
committed
add unit test for mutable macro
1 parent 5cb2cc1 commit 3605c58

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

Components/Macro/Sources/Macros/MutableMacro.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ public struct MutableMacro: MemberMacro {
2323

2424
let functions = try variables.compactMap { variableDecl -> FunctionDeclSyntax? in
2525
guard let variableBinding = variableDecl.bindings.first,
26-
let variableType = variableBinding.typeAnnotation?.type else {
27-
throw MacroError.typeAnnotationRequiredFor(variableName: variableDecl.bindings.first?.pattern.description ?? "unknown")
26+
let variableType = variableBinding.typeAnnotation?.type.trimmed else {
27+
let variableName = variableDecl.bindings.first?.pattern.trimmedDescription
28+
throw MacroError.typeAnnotationRequiredFor(variableName: variableName ?? "unknown")
2829
}
2930

3031
let variableName = TokenSyntax(stringLiteral: variableBinding.pattern.description)
@@ -34,7 +35,9 @@ public struct MutableMacro: MemberMacro {
3435
let modifiers = ModifierListSyntax(arrayLiteral: .init(name: .keyword(.mutating)))
3536
let bodyItem = CodeBlockItemSyntax.Item.expr(.init(stringLiteral: "self.\(variableName.text)=\(variableName.text)"))
3637
let body = CodeBlockSyntax(statements: .init(arrayLiteral: .init(item: bodyItem)))
37-
return FunctionDeclSyntax(modifiers: modifiers,
38+
39+
return FunctionDeclSyntax(leadingTrivia: .newlines(2),
40+
modifiers: modifiers,
3841
identifier: .identifier("set"),
3942
signature: .init(input: .init(parameterList: parameterList)),
4043
body: body

Components/Macro/Tests/MutableMacroTests.swift

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import XCTest
44
import Macros
55

66
let testMacros: [String: Macro.Type] = [
7-
"mutable": MutableMacro.self
7+
"Mutable": MutableMacro.self
88
]
99

1010
final class MutableMacroTests: XCTestCase {
@@ -18,8 +18,9 @@ final class MutableMacroTests: XCTestCase {
1818
var someVar: Bool = false
1919
}
2020
""",
21-
expandedSource: """
22-
@Mutable
21+
expandedSource:
22+
"""
23+
2324
struct SomeStruct {
2425
let id: String = ""
2526
var someVar: Bool = false
@@ -42,13 +43,19 @@ final class MutableMacroTests: XCTestCase {
4243
var someVar: Bool = false
4344
}
4445
""",
45-
expandedSource: """
46-
@Mutable
46+
expandedSource:
47+
"""
48+
4749
class SomeStruct {
4850
let id: String = ""
4951
var someVar: Bool = false
5052
}
5153
""",
54+
diagnostics: [
55+
.init(message: "onlyApplicableToStruct",
56+
line: 1,
57+
column: 1)
58+
],
5259
macros: testMacros
5360
)
5461
}
@@ -62,13 +69,19 @@ final class MutableMacroTests: XCTestCase {
6269
var someVar = false
6370
}
6471
""",
65-
expandedSource: """
66-
@Mutable
72+
expandedSource:
73+
"""
74+
6775
struct SomeStruct {
6876
let id: String = ""
6977
var someVar = false
7078
}
7179
""",
80+
diagnostics: [
81+
.init(message: "typeAnnotationRequiredFor(variableName: \"someVar\")",
82+
line: 1,
83+
column: 1)
84+
],
7285
macros: testMacros
7386
)
7487
}

0 commit comments

Comments
 (0)