|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2026 Apple Inc. and the Swift.org project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of Swift.org project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import Foundation |
| 16 | +import JExtractSwiftLib |
| 17 | +import SwiftJavaConfigurationShared |
| 18 | +import Testing |
| 19 | + |
| 20 | +@Suite |
| 21 | +struct InputSwiftTests { |
| 22 | + let fileManager = FileManager.default |
| 23 | + |
| 24 | + @Test(arguments: [JExtractGenerationMode.jni, .ffm]) |
| 25 | + func loadSwiftinterface(mode: JExtractGenerationMode) throws { |
| 26 | + let tempDirectory: URL = fileManager.temporaryDirectory |
| 27 | + .appending(path: "loadSwiftinterface-\(UUID())") |
| 28 | + let outSwiftURL = tempDirectory.appending(path: "swift") |
| 29 | + let outJavaURL = tempDirectory.appending(path: "java") |
| 30 | + |
| 31 | + try withTemporaryFile( |
| 32 | + fileName: "MyDependent", |
| 33 | + extension: "swiftinterface", |
| 34 | + contents: """ |
| 35 | + public struct Foo {} |
| 36 | + """, |
| 37 | + in: tempDirectory |
| 38 | + ) { swiftInterfaceURL in |
| 39 | + var config = Configuration() |
| 40 | + config.mode = mode |
| 41 | + config.javaPackage = "com.example" |
| 42 | + config.inputSwiftDirectory = swiftInterfaceURL.absoluteURL.path() |
| 43 | + config.swiftModule = "MySwift" |
| 44 | + config.outputSwiftDirectory = outSwiftURL.absoluteURL.path() |
| 45 | + config.outputJavaDirectory = outJavaURL.absoluteURL.path() |
| 46 | + |
| 47 | + try SwiftToJava(config: config, dependentConfigs: []) |
| 48 | + .run() |
| 49 | + } |
| 50 | + |
| 51 | + let javaPackageRoot = |
| 52 | + outJavaURL |
| 53 | + .appending(path: "com") |
| 54 | + .appending(path: "example") |
| 55 | + let expectedSources: [URL] = [ |
| 56 | + outSwiftURL.appending(path: "MySwiftModule+SwiftJava.swift"), |
| 57 | + outSwiftURL.appending(path: "MyDependent+SwiftJava.swift"), |
| 58 | + javaPackageRoot.appending(path: "Foo.java"), |
| 59 | + javaPackageRoot.appending(path: "MySwift.java"), |
| 60 | + ] |
| 61 | + |
| 62 | + for expectedSource in expectedSources { |
| 63 | + #expect(fileManager.fileExists(atPath: expectedSource.path())) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Test(arguments: [JExtractGenerationMode.jni, .ffm]) |
| 68 | + func loadEmptySwiftinterface(mode: JExtractGenerationMode) throws { |
| 69 | + let tempDirectory: URL = fileManager.temporaryDirectory |
| 70 | + .appending(path: "loadEmptySwiftinterface-\(UUID())") |
| 71 | + let outSwiftURL = tempDirectory.appending(path: "swift") |
| 72 | + let outJavaURL = tempDirectory.appending(path: "java") |
| 73 | + |
| 74 | + try withTemporaryFile( |
| 75 | + fileName: "MyDependent", |
| 76 | + extension: "swiftinterface", |
| 77 | + contents: "", |
| 78 | + in: tempDirectory |
| 79 | + ) { swiftInterfaceURL in |
| 80 | + var config = Configuration() |
| 81 | + config.mode = mode |
| 82 | + config.writeEmptyFiles = true |
| 83 | + config.javaPackage = "com.example" |
| 84 | + config.inputSwiftDirectory = swiftInterfaceURL.absoluteURL.path() |
| 85 | + config.swiftModule = "MySwift" |
| 86 | + config.outputSwiftDirectory = outSwiftURL.absoluteURL.path() |
| 87 | + config.outputJavaDirectory = outJavaURL.absoluteURL.path() |
| 88 | + |
| 89 | + try SwiftToJava(config: config, dependentConfigs: []) |
| 90 | + .run() |
| 91 | + } |
| 92 | + |
| 93 | + let javaPackageRoot = |
| 94 | + outJavaURL |
| 95 | + .appending(path: "com") |
| 96 | + .appending(path: "example") |
| 97 | + let expectedSources: [URL] = [ |
| 98 | + outSwiftURL.appending(path: "MySwiftModule+SwiftJava.swift"), |
| 99 | + outSwiftURL.appending(path: "MyDependent+SwiftJava.swift"), |
| 100 | + javaPackageRoot.appending(path: "MySwift.java"), |
| 101 | + ] |
| 102 | + |
| 103 | + for expectedSource in expectedSources { |
| 104 | + #expect(fileManager.fileExists(atPath: expectedSource.path())) |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments