-
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathDiagnosticsTests.swift
More file actions
371 lines (346 loc) · 13.7 KB
/
Copy pathDiagnosticsTests.swift
File metadata and controls
371 lines (346 loc) · 13.7 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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import SwiftParser
import SwiftSyntax
import Testing
@testable import BridgeJSCore
@testable import BridgeJSSkeleton
@Suite struct DiagnosticsTests {
/// Returns the first parameter's type node from a function in the source (the first `@JS func`-like decl), for pinpointing diagnostics.
private func firstParameterTypeNode(source: String) -> TypeSyntax? {
let tree = Parser.parse(source: source)
for stmt in tree.statements {
if let funcDecl = stmt.item.as(FunctionDeclSyntax.self),
let firstParam = funcDecl.signature.parameterClause.parameters.first
{
return firstParam.type
}
}
return nil
}
@Test
func diagnosticIncludesLocationSourceAndHint() throws {
let source = "@JS func foo(_ bar: A<X>) {}\n"
let typeNode = try #require(firstParameterTypeNode(source: source))
let diagnostic = DiagnosticError(
node: typeNode,
message: "Unsupported type 'A<X>'.",
hint: "Only primitive types and types defined in the same module are allowed"
)
let description = diagnostic.formattedDescription(fileName: "-", colorize: false)
let expectedPrefix = """
<stdin>:1:21: error: Unsupported type 'A<X>'.
1 | @JS func foo(_ bar: A<X>) {}
| `- error: Unsupported type 'A<X>'.
2 |
""".trimmingCharacters(in: .whitespacesAndNewlines)
#expect(description.hasPrefix(expectedPrefix))
#expect(description.contains("Hint: Only primitive types and types defined in the same module are allowed"))
}
@Test
func diagnosticOmitsHintWhenNotProvided() throws {
let source = "@JS static func foo() {}\n"
let tree = Parser.parse(source: source)
let diagnostic = DiagnosticError(
node: tree,
message: "Top-level functions cannot be static",
hint: nil
)
let description = diagnostic.formattedDescription(fileName: "-", colorize: false)
let expectedPrefix = """
<stdin>:1:1: error: Top-level functions cannot be static
1 | @JS static func foo() {}
| `- error: Top-level functions cannot be static
2 |
""".trimmingCharacters(in: .whitespacesAndNewlines)
#expect(description.hasPrefix(expectedPrefix))
#expect(!description.contains("Hint:"))
}
@Test
func diagnosticUsesGivenFileNameNotStdin() throws {
let source = "@JS func foo(_ bar: A<X>) {}\n"
let typeNode = try #require(firstParameterTypeNode(source: source))
let diagnostic = DiagnosticError(
node: typeNode,
message: "Unsupported type 'A<X>'.",
hint: nil
)
let description = diagnostic.formattedDescription(fileName: "Sources/Foo.swift", colorize: false)
#expect(description.hasPrefix("Sources/Foo.swift:1:21: error: Unsupported type 'A<X>'."))
}
@Test
func diagnosticWithColorizeTrueIncludesANSISequences() throws {
let source = "@JS func foo(_ bar: A<X>) {}\n"
let typeNode = try #require(firstParameterTypeNode(source: source))
let diagnostic = DiagnosticError(
node: typeNode,
message: "Unsupported type 'A<X>'.",
hint: nil
)
let description = diagnostic.formattedDescription(fileName: "-", colorize: true)
let esc = "\u{001B}"
let boldRed = "\(esc)[1;31m"
let boldDefault = "\(esc)[1;39m"
let reset = "\(esc)[0;0m"
let cyan = "\(esc)[0;36m"
let underline = "\(esc)[4;39m"
let expected =
"<stdin>:1:21: \(boldRed)error: \(boldDefault)Unsupported type 'A<X>'.\(reset)\n"
+ "\(cyan) 1\(reset) | @JS func foo(_ bar: \(underline)A<X>\(reset)) {}\n"
+ " | `- \(boldRed)error: \(boldDefault)Unsupported type 'A<X>'.\(reset)\n"
+ "\(cyan) 2\(reset) | "
#expect(description == expected)
}
// MARK: - Context source lines
@Test
func showsOnePreviousLineWhenErrorNotOnFirstLine() throws {
let source = """
preamble
@JS func foo(_ bar: A<X>) {}
"""
let typeNode = try #require(firstParameterTypeNode(source: source))
let diagnostic = DiagnosticError(node: typeNode, message: "Unsupported type 'A<X>'.", hint: nil)
let description = diagnostic.formattedDescription(fileName: "-", colorize: false)
#expect(description.contains(" 1 | preamble"))
#expect(description.contains(" 2 | @JS func foo(_ bar: A<X>) {}"))
#expect(description.contains("<stdin>:2:"))
}
@Test
func showsThreePreviousLinesWhenAvailable() throws {
let source = """
first
second
third
@JS func foo(_ bar: A<X>) {}
"""
let typeNode = try #require(firstParameterTypeNode(source: source))
let diagnostic = DiagnosticError(node: typeNode, message: "Unsupported type 'A<X>'.", hint: nil)
let description = diagnostic.formattedDescription(fileName: "-", colorize: false)
#expect(description.contains(" 1 | first"))
#expect(description.contains(" 2 | second"))
#expect(description.contains(" 3 | third"))
#expect(description.contains(" 4 | @JS func foo(_ bar: A<X>) {}"))
#expect(description.contains("<stdin>:4:"))
}
@Test
func capsContextAtThreePreviousLines() throws {
let source = """
line0
line1
line2
line3
@JS func foo(_ bar: A<X>) {}
"""
let typeNode = try #require(firstParameterTypeNode(source: source))
let diagnostic = DiagnosticError(node: typeNode, message: "Unsupported type 'A<X>'.", hint: nil)
let description = diagnostic.formattedDescription(fileName: "-", colorize: false)
#expect(!description.contains(" 1 | line0"))
#expect(description.contains(" 2 | line1"))
#expect(description.contains(" 3 | line2"))
#expect(description.contains(" 4 | line3"))
#expect(description.contains(" 5 | @JS func foo(_ bar: A<X>) {}"))
#expect(description.contains("<stdin>:5:"))
}
@Test
func includesNextLineAfterErrorLine() throws {
let source = """
@JS func foo(
_ bar: A<X>
) {}
"""
let typeNode = try #require(firstParameterTypeNode(source: source))
let diagnostic = DiagnosticError(node: typeNode, message: "Unsupported type 'A<X>'.", hint: nil)
let description = diagnostic.formattedDescription(fileName: "-", colorize: false)
#expect(description.contains(" 1 | @JS func foo("))
#expect(description.contains(" 2 | _ bar: A<X>"))
#expect(description.contains(" 3 | ) {}"))
#expect(description.contains("<stdin>:2:"))
}
// MARK: - Nested type validation
@Test
func nestedStructInsideClassSucceeds() throws {
let source = """
@JS class User {
@JS struct Stats {
var health: Int
}
}
"""
let swiftAPI = SwiftToSkeleton(
progress: .silent,
moduleName: "TestModule",
exposeToGlobal: false,
externalModuleIndex: .empty
)
swiftAPI.addSourceFile(Parser.parse(source: source), inputFilePath: "test.swift")
let skeleton = try swiftAPI.finalize()
#expect(skeleton.exported != nil)
let structs = skeleton.exported?.structs ?? []
#expect(structs.count == 1)
#expect(structs.first?.swiftCallName == "User.Stats")
}
@Test
func nestedClassInsideStructSucceeds() throws {
let source = """
@JS struct Container {
var value: Int
@JS class Inner {
}
}
"""
let swiftAPI = SwiftToSkeleton(
progress: .silent,
moduleName: "TestModule",
exposeToGlobal: false,
externalModuleIndex: .empty
)
swiftAPI.addSourceFile(Parser.parse(source: source), inputFilePath: "test.swift")
let skeleton = try swiftAPI.finalize()
#expect(skeleton.exported != nil)
let classes = skeleton.exported?.classes ?? []
#expect(classes.count == 1)
#expect(classes.first?.swiftCallName == "Container.Inner")
}
@Test
func structInsideEnumNamespaceSucceeds() throws {
let source = """
@JS enum API {
@JS struct Point {
var x: Double
var y: Double
}
}
"""
let swiftAPI = SwiftToSkeleton(
progress: .silent,
moduleName: "TestModule",
exposeToGlobal: false,
externalModuleIndex: .empty
)
swiftAPI.addSourceFile(Parser.parse(source: source), inputFilePath: "test.swift")
let skeleton = try swiftAPI.finalize()
#expect(skeleton.exported != nil)
}
// MARK: - Struct init order validation
@Test
func structInitMismatchedOrderProducesDiagnostic() throws {
let source = """
@JS struct Animal {
var size: Double
var age: Int
@JS init(age: Int, size: Double) {
self.age = age
self.size = size
}
}
"""
let swiftAPI = SwiftToSkeleton(
progress: .silent,
moduleName: "TestModule",
exposeToGlobal: false,
externalModuleIndex: .empty
)
swiftAPI.addSourceFile(Parser.parse(source: source), inputFilePath: "test.swift")
#expect(throws: BridgeJSCoreDiagnosticError.self) {
_ = try swiftAPI.finalize()
}
}
@Test
func structInitMatchingOrderSucceeds() throws {
let source = """
@JS struct Point {
var x: Double
var y: Double
@JS init(x: Double, y: Double) {
self.x = x
self.y = y
}
}
"""
let swiftAPI = SwiftToSkeleton(
progress: .silent,
moduleName: "TestModule",
exposeToGlobal: false,
externalModuleIndex: .empty
)
swiftAPI.addSourceFile(Parser.parse(source: source), inputFilePath: "test.swift")
let skeleton = try swiftAPI.finalize()
#expect(skeleton.exported != nil)
}
@Test
func structWithoutExplicitInitSucceeds() throws {
let source = """
@JS struct Point {
var x: Double
var y: Double
}
"""
let swiftAPI = SwiftToSkeleton(
progress: .silent,
moduleName: "TestModule",
exposeToGlobal: false,
externalModuleIndex: .empty
)
swiftAPI.addSourceFile(Parser.parse(source: source), inputFilePath: "test.swift")
let skeleton = try swiftAPI.finalize()
#expect(skeleton.exported != nil)
}
// MARK: - Async return validation
@Test
func asyncReturnOfUnsupportedTypeIsDiagnosed() throws {
// Protocol existentials still can't be lowered through the imported-parameter ABI, so
// an async return of one must still be diagnosed.
let source = """
@JS protocol PayloadDelegate {
func notify()
}
@JS func loadPayload() async -> PayloadDelegate {
fatalError()
}
"""
let swiftAPI = SwiftToSkeleton(
progress: .silent,
moduleName: "TestModule",
exposeToGlobal: false,
externalModuleIndex: .empty
)
swiftAPI.addSourceFile(Parser.parse(source: source), inputFilePath: "test.swift")
let skeleton = try swiftAPI.finalize()
let exported = try #require(skeleton.exported)
let exportSwift = ExportSwift(progress: .silent, moduleName: skeleton.moduleName, skeleton: exported)
#expect(throws: BridgeJSCoreError.self) {
_ = try exportSwift.finalize()
}
}
@Test
func asyncReturnOfConvertibleTypeSucceeds() throws {
let source = """
@JS func loadCount() async -> Int {
1
}
"""
let swiftAPI = SwiftToSkeleton(
progress: .silent,
moduleName: "TestModule",
exposeToGlobal: false,
externalModuleIndex: .empty
)
swiftAPI.addSourceFile(Parser.parse(source: source), inputFilePath: "test.swift")
let skeleton = try swiftAPI.finalize()
let exported = try #require(skeleton.exported)
let exportSwift = ExportSwift(progress: .silent, moduleName: skeleton.moduleName, skeleton: exported)
#expect(try exportSwift.finalize() != nil)
}
@Test
func omitsNextLineWhenErrorIsOnLastLine() throws {
let source = """
preamble
@JS func foo(_ bar: A<X>)
"""
let typeNode = try #require(firstParameterTypeNode(source: source))
let diagnostic = DiagnosticError(node: typeNode, message: "Unsupported type 'A<X>'.", hint: nil)
let description = diagnostic.formattedDescription(fileName: "-", colorize: false)
#expect(description.contains(" 2 | @JS func foo(_ bar: A<X>)"))
#expect(description.contains("<stdin>:2:"))
// No line 3 in source, so output must not show a " 3 |" context line after the pointer
#expect(!description.contains(" 3 |"))
}
}