-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMacroUsageTests.swift
247 lines (210 loc) · 6.51 KB
/
MacroUsageTests.swift
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//
// MacroUsageTests.swift
//
// Created by Ahmed Ali (github.com/Ahmed-Ali) on 22/04/2024.
//
import BuildDSL
import XCTest
@Builder
struct Author: Equatable {
let name: String
}
@Builder
struct Post: Equatable {
let id: UUID
let author: Author
let content: String
let mediaUrl: String?
var comments: [Comment] = []
var views: Int = 0
}
@Builder
public struct Comment: Equatable {
let content: String
var isTopAnswer: Bool = false
@Default("ThumbsUp")
let upvoteEmoji: String
}
@Builder
struct ComplexStrcut: Equatable {
@Ignore
var post: Post = .init(
id: UUID(),
author: Author(name: "Ahmed"),
content: "Content",
mediaUrl:
nil
)
@Default(Comment(
content: "",
isTopAnswer: false,
upvoteEmoji: ""
))
let comment: Comment
@Default(["ThumbsUp", "ThumbsDown"])
let upvoteEmojis: [String]
let nonDefault: Int
}
@Builder
struct StructWithExplitClosures {
let closure: () -> String
@Default({ "Default from @Default attribute" })
let withDefualt: () -> String
var closureWithInitializationValue: () -> String = { "Default through initialization value" }
let optionalClosure: (() -> String)!
}
@Builder
struct StructWithClosureType {
typealias ClosureType = () -> String
@Escaping
let closure: ClosureType
@Default({ "Default from @Default attribute" })
@Escaping
let withDefualt: ClosureType
@Escaping
var closureWithInitializationValue: ClosureType = { "Default through initialization value" }
@Escaping
let optionalClosure: ClosureType?
}
final class MacroUsageTests: XCTestCase {
func testInitWithMissingValues_ShouldReturnNil() throws {
XCTAssertNil(Comment { $0 })
}
func testInitWithAllRequiredValues_ShouldReturnValue() throws {
XCTAssertEqual(Comment { $0
.content("content")
}, Comment(content: "content", upvoteEmoji: "ThumbsUp"))
}
func testBuildMethoWithMissingValues_ShouldFail() throws {
XCTAssertThrowsError(try Comment.build { $0 }.get())
}
func testBuildMethoWithNoMissingValues_ShouldFail() throws {
XCTAssertNotEqual(try Comment.build { $0
.content("content")
}.get(), Comment(content: "content", upvoteEmoji: "Name"))
}
func testInitOuterAndInnerStructs_ShouldReturnValue() throws {
let expectedPost = Post(
id: UUID(),
author: Author(name: "Ahmed"),
content: "Content",
mediaUrl: nil
)
XCTAssertEqual(Post { $0
.id(expectedPost.id)
.author(Author(name: "Ahmed"))
.content("Content")
}, expectedPost)
}
func testInitOuterAndBuildInnerStructsWithMissingInnerStructValues_ShouldReturnNil() throws {
XCTAssertNil(Post { $0
.id(UUID())
.authorBuilder { $0 }
.content("Content")
})
}
func testInitOuterAndBuildInnerStructsWithNoMissingInnerStructValues_ShouldReturnValue() throws {
let expectedPost = Post(
id: UUID(),
author: Author(name: "Ahmed"),
content: "Content",
mediaUrl: nil
)
XCTAssertEqual(Post { $0
.id(expectedPost.id)
.authorBuilder { $0
.name("Ahmed")
}
.content("Content")
}, expectedPost)
}
func testBuildOuterAndBuildInnerStructsWithMissingInnerStructValues_ShouldFail() throws {
XCTAssertThrowsError(try Post.build { $0
.id(UUID())
.authorBuilder { $0 }
.content("Content")
}.get())
}
func testBuildOuterAndBuildInnerStructsWithNoMissingInnerStructValues_ShouldReturnValue(
) throws {
let expectedPost = Post(
id: UUID(),
author: Author(name: "Ahmed"),
content: "Content",
mediaUrl: nil
)
XCTAssertEqual(try Post.build { $0
.id(expectedPost.id)
.authorBuilder { $0
.name("Ahmed")
}
.content("Content")
}.get(), expectedPost)
}
func testVarWithDefaultValueUnchanged_ShouldRetainDefaultValue(
) throws {
XCTAssertEqual(Comment { $0
.content("Content")
}?.isTopAnswer, false)
}
func testVarWithDefaultValueChanged_ShouldRetainChangedValue(
) throws {
XCTAssertEqual(Comment { $0
.content("Content")
.isTopAnswer(true)
}?.isTopAnswer, true)
}
func testComplexType() throws {
let complex = ComplexStrcut { $0
.nonDefault(10)
}!
XCTAssertEqual(complex.post.author.name, "Ahmed")
XCTAssertEqual(complex.nonDefault, 10)
}
func testExplicitClosures() throws {
let instance = StructWithExplitClosures { $0
.closure {
"closure"
}
.optionalClosure {
"optionalClosure"
}
}
XCTAssertEqual(instance?.closure(), "closure")
XCTAssertEqual(instance?.optionalClosure?(), "optionalClosure")
XCTAssertEqual(instance?.withDefualt(), "Default from @Default attribute")
XCTAssertEqual(
instance?.closureWithInitializationValue(),
"Default through initialization value"
)
}
func testEscaping() throws {
let instance = StructWithClosureType { $0
.closure {
"closure"
}
.optionalClosure {
"optionalClosure"
}
}
XCTAssertEqual(instance?.closure(), "closure")
XCTAssertEqual(instance?.optionalClosure?(), "optionalClosure")
XCTAssertEqual(instance?.withDefualt(), "Default from @Default attribute")
XCTAssertEqual(
instance?.closureWithInitializationValue(),
"Default through initialization value"
)
}
func testUseBuildersInUserAPIs() throws {
func createComment(@Comment.ResultBuilder with resultBuilder: Comment.Closure) -> Comment? {
try? Comment.build(resultBuilder).get()
}
let comment = createComment { $0
.content("Content")
.isTopAnswer(true)
}
XCTAssertEqual(comment?.content, "Content")
XCTAssertEqual(comment?.isTopAnswer, true)
XCTAssertEqual(comment?.upvoteEmoji, "ThumbsUp")
}
}