|
| 1 | +import Testing |
| 2 | + |
| 3 | +@testable import BridgeJSSkeleton |
| 4 | + |
| 5 | +@Suite struct GenericImportDiagnosticsTests { |
| 6 | + |
| 7 | + @Test |
| 8 | + func genericParameterRequiresBridgeableConstraint() { |
| 9 | + expectDiagnostic( |
| 10 | + source: """ |
| 11 | + @JSFunction func identity<T>(_ value: T) throws(JSException) -> T |
| 12 | + """, |
| 13 | + contains: "Generic parameter 'T' must be constrained to 'BridgedSwiftGenericBridgeable'" |
| 14 | + ) |
| 15 | + } |
| 16 | + |
| 17 | + @Test |
| 18 | + func genericWhereClauseUnsupported() { |
| 19 | + expectDiagnostic( |
| 20 | + source: """ |
| 21 | + @JSFunction func identity<T: BridgedSwiftGenericBridgeable>(_ value: T) throws(JSException) -> T where T: Sendable |
| 22 | + """, |
| 23 | + contains: "'where' clauses are not supported on @JSFunction" |
| 24 | + ) |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + func asyncGenericImportUnsupported() { |
| 29 | + expectDiagnostic( |
| 30 | + source: """ |
| 31 | + @JSFunction func identityAsync<T: BridgedSwiftGenericBridgeable>(_ value: T) async throws(JSException) -> T |
| 32 | + """, |
| 33 | + contains: "Generic @JSFunction declarations cannot be 'async' yet." |
| 34 | + ) |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + func genericImportedMethodIsParsed() throws { |
| 39 | + let skeleton = try makeSkeleton( |
| 40 | + """ |
| 41 | + @JSClass struct Box { |
| 42 | + @JSFunction func member<T: BridgedSwiftGenericBridgeable>(_ value: T) throws(JSException) -> T |
| 43 | + } |
| 44 | + """, |
| 45 | + moduleName: "App" |
| 46 | + ) |
| 47 | + let imported = try #require(skeleton.imported) |
| 48 | + let types = imported.children.flatMap { $0.types } |
| 49 | + let box = try #require(types.first { $0.name == "Box" }) |
| 50 | + let method = try #require(box.methods.first { $0.name == "member" }) |
| 51 | + #expect(method.genericParameters == ["T"]) |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + func genericImportedConstructorIsParsed() throws { |
| 56 | + let skeleton = try makeSkeleton( |
| 57 | + """ |
| 58 | + @JSClass struct Box { |
| 59 | + @JSFunction init<T: BridgedSwiftGenericBridgeable>(_ value: T) throws(JSException) |
| 60 | + } |
| 61 | + """, |
| 62 | + moduleName: "App" |
| 63 | + ) |
| 64 | + let imported = try #require(skeleton.imported) |
| 65 | + let types = imported.children.flatMap { $0.types } |
| 66 | + let box = try #require(types.first { $0.name == "Box" }) |
| 67 | + let constructor = try #require(box.constructor) |
| 68 | + #expect(constructor.genericParameters == ["T"]) |
| 69 | + #expect(constructor.parameters.map(\.type) == [.generic("T")]) |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + func genericImportedConstructorUnconstrainedParamIsRejected() { |
| 74 | + expectDiagnostic( |
| 75 | + source: """ |
| 76 | + @JSClass struct Box { |
| 77 | + @JSFunction init<T>(_ value: T) throws(JSException) |
| 78 | + } |
| 79 | + """, |
| 80 | + contains: |
| 81 | + "Generic parameter 'T' must be constrained to 'BridgedSwiftGenericBridgeable' to be used with @JSFunction." |
| 82 | + ) |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + func genericImportedConstructorUnusedTypeParamIsRejected() { |
| 87 | + expectDiagnostic( |
| 88 | + source: """ |
| 89 | + @JSClass struct Box { |
| 90 | + @JSFunction init<T: BridgedSwiftGenericBridgeable>(_ value: Int) throws(JSException) |
| 91 | + } |
| 92 | + """, |
| 93 | + contains: |
| 94 | + "The generic parameter 'T' must be used in a parameter of a generic @JSFunction initializer." |
| 95 | + ) |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + func genericImportedConstructorAsyncIsRejected() { |
| 100 | + expectDiagnostic( |
| 101 | + source: """ |
| 102 | + @JSClass struct Box { |
| 103 | + @JSFunction init<T: BridgedSwiftGenericBridgeable>(_ value: T) async throws(JSException) |
| 104 | + } |
| 105 | + """, |
| 106 | + contains: "Generic @JSFunction declarations cannot be 'async' yet." |
| 107 | + ) |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + func genericImportedConstructorUnsupportedWrapperFormIsRejected() { |
| 112 | + expectDiagnostic( |
| 113 | + source: """ |
| 114 | + @JSClass struct Box { |
| 115 | + @JSFunction init<T: BridgedSwiftGenericBridgeable>(_ value: [[T]]) throws(JSException) |
| 116 | + } |
| 117 | + """, |
| 118 | + contains: "may only be used as a bare type" |
| 119 | + ) |
| 120 | + } |
| 121 | + |
| 122 | + @Test(arguments: [ |
| 123 | + ("[[T]]", "@JSFunction func f<T: BridgedSwiftGenericBridgeable>(_ v: [[T]]) throws(JSException)"), |
| 124 | + ("[T?]", "@JSFunction func f<T: BridgedSwiftGenericBridgeable>(_ v: [T?]) throws(JSException)"), |
| 125 | + ("T??", "@JSFunction func f<T: BridgedSwiftGenericBridgeable>(_ v: T??) throws(JSException)"), |
| 126 | + ("[Int: T]", "@JSFunction func f<T: BridgedSwiftGenericBridgeable>(_ v: [Int: T]) throws(JSException)"), |
| 127 | + ]) |
| 128 | + func unsupportedGenericWrapperFormsInParameter(label: String, source: String) { |
| 129 | + expectDiagnostic( |
| 130 | + source: source, |
| 131 | + contains: "may only be used as a bare type" |
| 132 | + ) |
| 133 | + } |
| 134 | + |
| 135 | + @Test(arguments: [ |
| 136 | + ("[[T]]", "@JSFunction func f<T: BridgedSwiftGenericBridgeable>(_ v: T) throws(JSException) -> [[T]]"), |
| 137 | + ("[T?]", "@JSFunction func f<T: BridgedSwiftGenericBridgeable>(_ v: T) throws(JSException) -> [T?]"), |
| 138 | + ("T??", "@JSFunction func f<T: BridgedSwiftGenericBridgeable>(_ v: T) throws(JSException) -> T??"), |
| 139 | + ("[Int: T]", "@JSFunction func f<T: BridgedSwiftGenericBridgeable>(_ v: T) throws(JSException) -> [Int: T]"), |
| 140 | + ]) |
| 141 | + func unsupportedGenericWrapperFormsInReturn(label: String, source: String) { |
| 142 | + expectDiagnostic( |
| 143 | + source: source, |
| 144 | + contains: "may only be used as a bare type" |
| 145 | + ) |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + func genericImportedMethodAsyncIsRejected() { |
| 150 | + expectDiagnostic( |
| 151 | + source: """ |
| 152 | + @JSClass struct Box { |
| 153 | + @JSFunction func member<T: BridgedSwiftGenericBridgeable>(_ value: T) async throws(JSException) -> T |
| 154 | + } |
| 155 | + """, |
| 156 | + contains: "Generic @JSFunction declarations cannot be 'async' yet." |
| 157 | + ) |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + func genericImportedMethodUnconstrainedParamIsRejected() { |
| 162 | + expectDiagnostic( |
| 163 | + source: """ |
| 164 | + @JSClass struct Box { |
| 165 | + @JSFunction func member<T>(_ value: T) throws(JSException) -> T |
| 166 | + } |
| 167 | + """, |
| 168 | + contains: |
| 169 | + "Generic parameter 'T' must be constrained to 'BridgedSwiftGenericBridgeable' to be used with @JSFunction." |
| 170 | + ) |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + func genericImportedFunctionUnusedTypeParamIsRejected() { |
| 175 | + expectDiagnostic( |
| 176 | + source: """ |
| 177 | + @JSFunction func unused<T: BridgedSwiftGenericBridgeable>() throws(JSException) -> Int |
| 178 | + """, |
| 179 | + contains: |
| 180 | + "The generic parameter 'T' must be used in a parameter or return type of a generic @JSFunction declaration." |
| 181 | + ) |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + func genericImportedMethodUnusedTypeParamIsRejected() { |
| 186 | + expectDiagnostic( |
| 187 | + source: """ |
| 188 | + @JSClass struct Box { |
| 189 | + @JSFunction func member<T: BridgedSwiftGenericBridgeable>() throws(JSException) -> Int |
| 190 | + } |
| 191 | + """, |
| 192 | + contains: |
| 193 | + "The generic parameter 'T' must be used in a parameter or return type of a generic @JSFunction declaration." |
| 194 | + ) |
| 195 | + } |
| 196 | + |
| 197 | + @Test |
| 198 | + func genericImportedReturnOnlyTypeParamIsAllowed() throws { |
| 199 | + let skeleton = try makeSkeleton( |
| 200 | + """ |
| 201 | + @JSFunction func make<T: BridgedSwiftGenericBridgeable>() throws(JSException) -> T |
| 202 | + """, |
| 203 | + moduleName: "App" |
| 204 | + ) |
| 205 | + let imported = try #require(skeleton.imported) |
| 206 | + let functions = imported.children.flatMap { $0.functions } |
| 207 | + let function = try #require(functions.first { $0.name == "make" }) |
| 208 | + #expect(function.genericParameters == ["T"]) |
| 209 | + } |
| 210 | +} |
0 commit comments