-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathCodableInheritanceTests.swift
More file actions
197 lines (170 loc) · 6.32 KB
/
CodableInheritanceTests.swift
File metadata and controls
197 lines (170 loc) · 6.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#if SWIFT_SYNTAX_EXTENSION_MACRO_FIXED
import SwiftDiagnostics
import SwiftSyntax
import SwiftSyntaxMacros
import SwiftSyntaxMacroExpansion
import SwiftSyntaxMacrosTestSupport
import XCTest
@testable import PluginCore
final class CodableInheritanceTests: XCTestCase {
func testMisuseOnNonClassDeclaration() throws {
assertMacroExpansion(
"""
@Codable
@Inherits(decodable: false, encodable: false)
struct SomeCodable {
let value: String
}
""",
expandedSource:
"""
struct SomeCodable {
let value: String
}
extension SomeCodable: Decodable {
init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.value = try container.decode(String.self, forKey: CodingKeys.value)
}
}
extension SomeCodable: Encodable {
func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.value, forKey: CodingKeys.value)
}
}
extension SomeCodable {
enum CodingKeys: String, CodingKey {
case value = "value"
}
}
""",
diagnostics: [
.init(
id: Inherits.misuseID,
message:
"@Inherits only applicable to class declarations",
line: 2, column: 1,
fixIts: [
.init(message: "Remove @Inherits attribute")
]
)
]
)
}
func testWithNoInheritance() throws {
assertMacroExpansion(
"""
@Codable
@Inherits(decodable: false, encodable: false)
class SomeCodable {
var value: String = ""
init() { }
}
""",
expandedSource:
"""
class SomeCodable {
var value: String = ""
init() { }
required init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
do {
self.value = try container.decodeIfPresent(String.self, forKey: CodingKeys.value) ?? ""
} catch {
self.value = ""
}
}
func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.value, forKey: CodingKeys.value)
}
enum CodingKeys: String, CodingKey {
case value = "value"
}
}
extension SomeCodable: Decodable {
}
extension SomeCodable: Encodable {
}
""",
conformsTo: []
)
}
func testWithExplicitInheritance() throws {
assertMacroExpansion(
"""
@Codable
@Inherits(decodable: true, encodable: true)
class SomeCodable: SuperCodable {
var value: String = ""
init() { }
}
""",
expandedSource:
"""
class SomeCodable: SuperCodable {
var value: String = ""
init() { }
required init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
do {
self.value = try container.decodeIfPresent(String.self, forKey: CodingKeys.value) ?? ""
} catch {
self.value = ""
}
try super.init(from: decoder)
}
override func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.value, forKey: CodingKeys.value)
try super.encode(to: encoder)
}
enum CodingKeys: String, CodingKey {
case value = "value"
}
}
""",
conformsTo: []
)
}
func testWithExplicitPartialInheritance() throws {
assertMacroExpansion(
"""
@Codable
@Inherits(decodable: true, encodable: false)
class SomeCodable: SuperDecodable {
var value: String = ""
init() { }
}
""",
expandedSource:
"""
class SomeCodable: SuperDecodable {
var value: String = ""
init() { }
required init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
do {
self.value = try container.decodeIfPresent(String.self, forKey: CodingKeys.value) ?? ""
} catch {
self.value = ""
}
try super.init(from: decoder)
}
func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.value, forKey: CodingKeys.value)
}
enum CodingKeys: String, CodingKey {
case value = "value"
}
}
extension SomeCodable: Encodable {
}
""",
conformsTo: []
)
}
}
#endif