-
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathBridgeJSLinkTests.swift
More file actions
81 lines (72 loc) · 3.25 KB
/
Copy pathBridgeJSLinkTests.swift
File metadata and controls
81 lines (72 loc) · 3.25 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
import Foundation
import SwiftSyntax
import SwiftParser
import Testing
@testable import BridgeJSLink
@testable import BridgeJSCore
@testable import TS2Skeleton
@Suite struct BridgeJSLinkTests {
private func snapshot(
bridgeJSLink: BridgeJSLink,
name: String? = nil,
filePath: String = #filePath,
function: String = #function,
sourceLocation: Testing.SourceLocation = #_sourceLocation
) throws {
let (outputJs, outputDts) = try bridgeJSLink.link()
try assertSnapshot(
name: name,
filePath: filePath,
function: function,
sourceLocation: sourceLocation,
input: outputJs.data(using: .utf8)!,
fileExtension: "js"
)
try assertSnapshot(
name: name,
filePath: filePath,
function: function,
sourceLocation: sourceLocation,
input: outputDts.data(using: .utf8)!,
fileExtension: "d.ts"
)
}
static let inputsDirectory = URL(fileURLWithPath: #filePath).deletingLastPathComponent().appendingPathComponent(
"Inputs"
)
static func collectInputs(extension: String) -> [String] {
let fileManager = FileManager.default
let inputs = try! fileManager.contentsOfDirectory(atPath: Self.inputsDirectory.path)
return inputs.filter { $0.hasSuffix(`extension`) }
}
@Test(arguments: collectInputs(extension: ".swift"))
func snapshotExport(input: String) throws {
let url = Self.inputsDirectory.appendingPathComponent(input)
let sourceFile = Parser.parse(source: try String(contentsOf: url, encoding: .utf8))
let swiftAPI = ExportSwift(progress: .silent, moduleName: "TestModule")
try swiftAPI.addSourceFile(sourceFile, input)
let name = url.deletingPathExtension().lastPathComponent
let (_, outputSkeleton) = try #require(try swiftAPI.finalize())
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
let outputSkeletonData = try encoder.encode(outputSkeleton)
var bridgeJSLink = BridgeJSLink(sharedMemory: false)
try bridgeJSLink.addExportedSkeletonFile(data: outputSkeletonData)
try snapshot(bridgeJSLink: bridgeJSLink, name: name + ".Export")
}
@Test(arguments: collectInputs(extension: ".d.ts"))
func snapshotImport(input: String) throws {
let url = Self.inputsDirectory.appendingPathComponent(input)
let tsconfigPath = url.deletingLastPathComponent().appendingPathComponent("tsconfig.json")
var importTS = ImportTS(progress: .silent, moduleName: "TestModule")
let nodePath = try #require(which("node"))
try importTS.addSourceFile(url.path, tsconfigPath: tsconfigPath.path, nodePath: nodePath)
let name = url.deletingPathExtension().deletingPathExtension().lastPathComponent
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
let outputSkeletonData = try encoder.encode(importTS.skeleton)
var bridgeJSLink = BridgeJSLink(sharedMemory: false)
try bridgeJSLink.addImportedSkeletonFile(data: outputSkeletonData)
try snapshot(bridgeJSLink: bridgeJSLink, name: name + ".Import")
}
}