|
| 1 | +import Foundation |
| 2 | +import SwiftParser |
| 3 | +import SwiftSyntax |
| 4 | +import Testing |
| 5 | + |
| 6 | +@testable import BridgeJSCore |
| 7 | +@testable import BridgeJSSkeleton |
| 8 | + |
| 9 | +@Suite struct GenericExportDiagnosticsTests { |
| 10 | + // MARK: - Diagnostics |
| 11 | + |
| 12 | + @Test |
| 13 | + func genericParameterRequiresBridgeableConstraint() throws { |
| 14 | + do { |
| 15 | + _ = try resolveApp( |
| 16 | + source: """ |
| 17 | + @JS public func identity<T>(_ value: T) -> T { value } |
| 18 | + """ |
| 19 | + ) |
| 20 | + Issue.record("Expected a generic-constraint diagnostic, but resolution succeeded") |
| 21 | + } catch let error as BridgeJSCoreDiagnosticError { |
| 22 | + let combined = error.diagnostics.map(\.diagnostic.message).joined(separator: "\n") |
| 23 | + #expect( |
| 24 | + combined.contains("Generic parameter 'T' must be constrained to '_BridgedSwiftGenericBridgeable'") |
| 25 | + ) |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + func genericWhereClauseUnsupported() throws { |
| 31 | + do { |
| 32 | + _ = try resolveApp( |
| 33 | + source: """ |
| 34 | + @JS public func identity<T: _BridgedSwiftGenericBridgeable>(_ value: T) -> T where T: Sendable { value } |
| 35 | + """ |
| 36 | + ) |
| 37 | + Issue.record("Expected a where-clause diagnostic, but resolution succeeded") |
| 38 | + } catch let error as BridgeJSCoreDiagnosticError { |
| 39 | + let combined = error.diagnostics.map(\.diagnostic.message).joined(separator: "\n") |
| 40 | + #expect(combined.contains("'where' clauses are not supported")) |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + func asyncGenericExportUnsupported() throws { |
| 46 | + do { |
| 47 | + _ = try resolveApp( |
| 48 | + source: """ |
| 49 | + @JS public func f<T: _BridgedSwiftGenericBridgeable>(_ v: T) async -> T { v } |
| 50 | + """ |
| 51 | + ) |
| 52 | + Issue.record("Expected an async-generic diagnostic, but resolution succeeded") |
| 53 | + } catch let error as BridgeJSCoreDiagnosticError { |
| 54 | + let combined = error.diagnostics.map(\.diagnostic.message).joined(separator: "\n") |
| 55 | + #expect(combined.contains("Generic @JS functions cannot be 'async' yet.")) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + func wrappedGenericOptionalReturnUnsupported() throws { |
| 61 | + do { |
| 62 | + _ = try resolveApp( |
| 63 | + source: """ |
| 64 | + @JS public func f<T: _BridgedSwiftGenericBridgeable>(_ v: T) -> T? { v } |
| 65 | + """ |
| 66 | + ) |
| 67 | + Issue.record("Expected a wrapped-generic diagnostic, but resolution succeeded") |
| 68 | + } catch let error as BridgeJSCoreDiagnosticError { |
| 69 | + let combined = error.diagnostics.map(\.diagnostic.message).joined(separator: "\n") |
| 70 | + #expect(combined.contains("may only be used as a bare type")) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + func genericExportMemberUnsupported() throws { |
| 76 | + do { |
| 77 | + _ = try resolveApp( |
| 78 | + source: """ |
| 79 | + @JS class Box { |
| 80 | + @JS func member<T: _BridgedSwiftGenericBridgeable>(_ value: T) -> T { value } |
| 81 | + } |
| 82 | + """ |
| 83 | + ) |
| 84 | + Issue.record("Expected a generic-member diagnostic, but resolution succeeded") |
| 85 | + } catch let error as BridgeJSCoreDiagnosticError { |
| 86 | + let combined = error.diagnostics.map(\.diagnostic.message).joined(separator: "\n") |
| 87 | + #expect( |
| 88 | + combined.contains("Generic @JS functions are only supported as top-level functions") |
| 89 | + ) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + func fullyUnusedGenericParameterRejected() throws { |
| 95 | + do { |
| 96 | + _ = try resolveApp( |
| 97 | + source: """ |
| 98 | + @JS public func combine<T: _BridgedSwiftGenericBridgeable, U: _BridgedSwiftGenericBridgeable>(_ a: T) -> T { a } |
| 99 | + """ |
| 100 | + ) |
| 101 | + Issue.record("Expected a fully-unused-generic diagnostic, but resolution succeeded") |
| 102 | + } catch let error as BridgeJSCoreDiagnosticError { |
| 103 | + let combined = error.diagnostics.map(\.diagnostic.message).joined(separator: "\n") |
| 104 | + #expect(combined.contains("must be used in at least one parameter")) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + func genericParameterMustBeUsedAsParameter() throws { |
| 110 | + do { |
| 111 | + _ = try resolveApp( |
| 112 | + source: """ |
| 113 | + @JS public func f<T: _BridgedSwiftGenericBridgeable>() -> T { fatalError() } |
| 114 | + """ |
| 115 | + ) |
| 116 | + Issue.record("Expected a generic-parameter-usage diagnostic, but resolution succeeded") |
| 117 | + } catch let error as BridgeJSCoreDiagnosticError { |
| 118 | + let combined = error.diagnostics.map(\.diagnostic.message).joined(separator: "\n") |
| 119 | + #expect(combined.contains("must be used in at least one parameter")) |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + func genericConcreteReturnUnsupported() throws { |
| 125 | + do { |
| 126 | + _ = try resolveApp( |
| 127 | + source: """ |
| 128 | + @JS public func f<T: _BridgedSwiftGenericBridgeable>(_ v: T) -> String { "" } |
| 129 | + """ |
| 130 | + ) |
| 131 | + Issue.record("Expected a concrete-return diagnostic, but resolution succeeded") |
| 132 | + } catch let error as BridgeJSCoreDiagnosticError { |
| 133 | + let combined = error.diagnostics.map(\.diagnostic.message).joined(separator: "\n") |
| 134 | + #expect(combined.contains("must return the generic type or Void")) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + // MARK: - Positive control |
| 139 | + |
| 140 | + // MARK: - Utilities |
| 141 | + |
| 142 | + private func resolveApp(source appSource: String) throws -> BridgeJSSkeleton { |
| 143 | + let swiftAPI = SwiftToSkeleton( |
| 144 | + progress: .silent, |
| 145 | + moduleName: "App", |
| 146 | + exposeToGlobal: false, |
| 147 | + externalModuleIndex: ExternalModuleIndex(dependencies: []) |
| 148 | + ) |
| 149 | + let sourceFile = Parser.parse(source: appSource) |
| 150 | + swiftAPI.addSourceFile(sourceFile, inputFilePath: "App.swift") |
| 151 | + return try swiftAPI.finalize() |
| 152 | + } |
| 153 | +} |
0 commit comments