forked from swiftwasm/JavaScriptKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBridgeJSCodegenTests.swift
More file actions
204 lines (192 loc) · 9.23 KB
/
BridgeJSCodegenTests.swift
File metadata and controls
204 lines (192 loc) · 9.23 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
import Foundation
import SwiftSyntax
import SwiftParser
import Testing
@testable import BridgeJSCore
@testable import BridgeJSSkeleton
@Suite struct BridgeJSCodegenTests {
static let inputsDirectory = URL(fileURLWithPath: #filePath).deletingLastPathComponent().appendingPathComponent(
"Inputs"
).appendingPathComponent("MacroSwift")
static let multifileInputsDirectory = URL(fileURLWithPath: #filePath).deletingLastPathComponent()
.appendingPathComponent("Inputs").appendingPathComponent("MacroSwift").appendingPathComponent("Multifile")
private func snapshotCodegen(
skeleton: BridgeJSSkeleton,
name: String,
filePath: String = #filePath,
function: String = #function,
sourceLocation: Testing.SourceLocation = #_sourceLocation
) throws {
var swiftParts: [String] = []
if let closureSupport = try ClosureCodegen().renderSupport(for: skeleton) {
swiftParts.append(closureSupport)
}
if let exported = skeleton.exported {
let exportSwift = ExportSwift(
progress: .silent,
moduleName: skeleton.moduleName,
skeleton: exported
)
if let s = try exportSwift.finalize() {
swiftParts.append(s)
}
}
if let imported = skeleton.imported {
let importTS = ImportTS(progress: .silent, moduleName: skeleton.moduleName, skeleton: imported)
if let s = try importTS.finalize() {
swiftParts.append(s)
}
}
let combinedSwift =
swiftParts
.map { $0.trimmingCharacters(in: .newlines) }
.filter { !$0.isEmpty }
.joined(separator: "\n\n")
try assertSnapshot(
name: name,
filePath: filePath,
function: function,
sourceLocation: sourceLocation,
input: combinedSwift.data(using: String.Encoding.utf8)!,
fileExtension: "swift"
)
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
let skeletonData = try encoder.encode(skeleton)
try assertSnapshot(
name: name,
filePath: filePath,
function: function,
sourceLocation: sourceLocation,
input: skeletonData,
fileExtension: "json"
)
}
static func collectInputs() -> [String] {
let fileManager = FileManager.default
let inputs = try! fileManager.contentsOfDirectory(atPath: Self.inputsDirectory.path)
return inputs.filter { $0.hasSuffix(".swift") }.sorted()
}
@Test(arguments: collectInputs())
func codegenSnapshot(input: String) throws {
let url = Self.inputsDirectory.appendingPathComponent(input)
let name = url.deletingPathExtension().lastPathComponent
let sourceFile = Parser.parse(source: try String(contentsOf: url, encoding: .utf8))
let swiftAPI = SwiftToSkeleton(progress: .silent, moduleName: "TestModule", exposeToGlobal: false)
swiftAPI.addSourceFile(sourceFile, inputFilePath: input)
let skeleton = try swiftAPI.finalize()
try snapshotCodegen(skeleton: skeleton, name: name)
}
@Test(arguments: [
"Namespaces.swift",
"StaticFunctions.swift",
"StaticProperties.swift",
"EnumNamespace.swift",
])
func codegenSnapshotWithGlobal(input: String) throws {
let url = Self.inputsDirectory.appendingPathComponent(input)
let name = url.deletingPathExtension().lastPathComponent
let sourceFile = Parser.parse(source: try String(contentsOf: url, encoding: .utf8))
let swiftAPI = SwiftToSkeleton(progress: .silent, moduleName: "TestModule", exposeToGlobal: true)
swiftAPI.addSourceFile(sourceFile, inputFilePath: input)
let skeleton = try swiftAPI.finalize()
try snapshotCodegen(skeleton: skeleton, name: name + ".Global")
}
@Test
func codegenCrossFileTypeResolution() throws {
let swiftAPI = SwiftToSkeleton(progress: .silent, moduleName: "TestModule", exposeToGlobal: false)
let classBURL = Self.multifileInputsDirectory.appendingPathComponent("CrossFileClassB.swift")
swiftAPI.addSourceFile(
Parser.parse(source: try String(contentsOf: classBURL, encoding: .utf8)),
inputFilePath: "CrossFileClassB.swift"
)
let classAURL = Self.multifileInputsDirectory.appendingPathComponent("CrossFileClassA.swift")
swiftAPI.addSourceFile(
Parser.parse(source: try String(contentsOf: classAURL, encoding: .utf8)),
inputFilePath: "CrossFileClassA.swift"
)
let skeleton = try swiftAPI.finalize()
try snapshotCodegen(skeleton: skeleton, name: "CrossFileTypeResolution")
}
@Test
func codegenCrossFileTypeResolutionReverseOrder() throws {
let swiftAPI = SwiftToSkeleton(progress: .silent, moduleName: "TestModule", exposeToGlobal: false)
let classAURL = Self.multifileInputsDirectory.appendingPathComponent("CrossFileClassA.swift")
swiftAPI.addSourceFile(
Parser.parse(source: try String(contentsOf: classAURL, encoding: .utf8)),
inputFilePath: "CrossFileClassA.swift"
)
let classBURL = Self.multifileInputsDirectory.appendingPathComponent("CrossFileClassB.swift")
swiftAPI.addSourceFile(
Parser.parse(source: try String(contentsOf: classBURL, encoding: .utf8)),
inputFilePath: "CrossFileClassB.swift"
)
let skeleton = try swiftAPI.finalize()
try snapshotCodegen(skeleton: skeleton, name: "CrossFileTypeResolution.ReverseOrder")
}
@Test
func codegenCrossFileFunctionTypes() throws {
let swiftAPI = SwiftToSkeleton(progress: .silent, moduleName: "TestModule", exposeToGlobal: false)
let functionBURL = Self.multifileInputsDirectory.appendingPathComponent("CrossFileFunctionB.swift")
swiftAPI.addSourceFile(
Parser.parse(source: try String(contentsOf: functionBURL, encoding: .utf8)),
inputFilePath: "CrossFileFunctionB.swift"
)
let functionAURL = Self.multifileInputsDirectory.appendingPathComponent("CrossFileFunctionA.swift")
swiftAPI.addSourceFile(
Parser.parse(source: try String(contentsOf: functionAURL, encoding: .utf8)),
inputFilePath: "CrossFileFunctionA.swift"
)
let skeleton = try swiftAPI.finalize()
try snapshotCodegen(skeleton: skeleton, name: "CrossFileFunctionTypes")
}
@Test
func codegenCrossFileFunctionTypesReverseOrder() throws {
let swiftAPI = SwiftToSkeleton(progress: .silent, moduleName: "TestModule", exposeToGlobal: false)
let functionAURL = Self.multifileInputsDirectory.appendingPathComponent("CrossFileFunctionA.swift")
swiftAPI.addSourceFile(
Parser.parse(source: try String(contentsOf: functionAURL, encoding: .utf8)),
inputFilePath: "CrossFileFunctionA.swift"
)
let functionBURL = Self.multifileInputsDirectory.appendingPathComponent("CrossFileFunctionB.swift")
swiftAPI.addSourceFile(
Parser.parse(source: try String(contentsOf: functionBURL, encoding: .utf8)),
inputFilePath: "CrossFileFunctionB.swift"
)
let skeleton = try swiftAPI.finalize()
try snapshotCodegen(skeleton: skeleton, name: "CrossFileFunctionTypes.ReverseOrder")
}
@Test
func codegenCrossFileExtension() throws {
let swiftAPI = SwiftToSkeleton(progress: .silent, moduleName: "TestModule", exposeToGlobal: false)
let classURL = Self.multifileInputsDirectory.appendingPathComponent("CrossFileExtensionClass.swift")
swiftAPI.addSourceFile(
Parser.parse(source: try String(contentsOf: classURL, encoding: .utf8)),
inputFilePath: "CrossFileExtensionClass.swift"
)
let extensionURL = Self.multifileInputsDirectory.appendingPathComponent("CrossFileExtension.swift")
swiftAPI.addSourceFile(
Parser.parse(source: try String(contentsOf: extensionURL, encoding: .utf8)),
inputFilePath: "CrossFileExtension.swift"
)
let skeleton = try swiftAPI.finalize()
try snapshotCodegen(skeleton: skeleton, name: "CrossFileExtension")
}
@Test
func codegenSkipsEmptySkeletons() throws {
let swiftAPI = SwiftToSkeleton(progress: .silent, moduleName: "TestModule", exposeToGlobal: false)
let importedURL = Self.multifileInputsDirectory.appendingPathComponent("ImportedFunctions.swift")
swiftAPI.addSourceFile(
Parser.parse(source: try String(contentsOf: importedURL, encoding: .utf8)),
inputFilePath: "ImportedFunctions.swift"
)
let exportedOnlyURL = Self.multifileInputsDirectory.appendingPathComponent("ExportedOnly.swift")
swiftAPI.addSourceFile(
Parser.parse(source: try String(contentsOf: exportedOnlyURL, encoding: .utf8)),
inputFilePath: "ExportedOnly.swift"
)
let skeleton = try swiftAPI.finalize()
#expect(skeleton.exported == nil, "Empty exported skeleton should be omitted")
try snapshotCodegen(skeleton: skeleton, name: "CrossFileSkipsEmptySkeletons")
}
}