Skip to content

Commit 9660392

Browse files
[NFC] BridgeJS: Cover more Array cases in runtime tests (swiftwasm#678)
BridgeJS: Cover more Array cases in runtime tests
1 parent 33c03e9 commit 9660392

File tree

6 files changed

+6479
-5678
lines changed

6 files changed

+6479
-5678
lines changed

Tests/BridgeJSRuntimeTests/ArraySupportTests.swift

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import JavaScriptKit
77
@JSFunction init(id: String) throws(JSException)
88
}
99

10+
@JS protocol ArrayElementProtocol {
11+
var value: Int { get set }
12+
}
13+
1014
@JSClass struct ArraySupportImports {
1115
@JSFunction static func jsIntArrayLength(_ items: [Int]) throws(JSException) -> Int
1216

@@ -22,12 +26,27 @@ import JavaScriptKit
2226
_ values: [ArrayElementObject]
2327
) throws(JSException) -> [ArrayElementObject]
2428

29+
@JSFunction static func jsRoundTripOptionalIntArray(_ values: [Int?]) throws(JSException) -> [Int?]
30+
@JSFunction static func jsRoundTripOptionalStringArray(_ values: [String?]) throws(JSException) -> [String?]
31+
@JSFunction static func jsRoundTripOptionalBoolArray(_ values: [Bool?]) throws(JSException) -> [Bool?]
32+
@JSFunction static func jsRoundTripOptionalJSValueArray(_ values: [JSValue?]) throws(JSException) -> [JSValue?]
33+
@JSFunction static func jsRoundTripOptionalJSObjectArray(_ values: [JSObject?]) throws(JSException) -> [JSObject?]
34+
@JSFunction static func jsRoundTripOptionalJSClassArray(
35+
_ values: [ArrayElementObject?]
36+
) throws(JSException) -> [ArrayElementObject?]
37+
2538
@JSFunction static func jsSumNumberArray(_ values: [Double]) throws(JSException) -> Double
2639
@JSFunction static func jsCreateNumberArray() throws(JSException) -> [Double]
40+
41+
@JSFunction static func runJsArraySupportTests() throws(JSException)
2742
}
2843

2944
final class ArraySupportTests: XCTestCase {
3045

46+
func testRunJsArraySupportTests() throws {
47+
try ArraySupportImports.runJsArraySupportTests()
48+
}
49+
3150
func testRoundTripIntArray() throws {
3251
let values = [1, 2, 3, 4, 5]
3352
let result = try ArraySupportImports.jsRoundTripIntArray(values)
@@ -105,4 +124,99 @@ final class ArraySupportTests: XCTestCase {
105124

106125
XCTAssertEqual(try ArraySupportImports.jsRoundTripJSClassArray([]), [])
107126
}
127+
128+
func testRoundTripOptionalIntArray() throws {
129+
let values = [1, nil, 3, nil, 5]
130+
let result = try ArraySupportImports.jsRoundTripOptionalIntArray(values)
131+
XCTAssertEqual(result, values)
132+
}
133+
134+
func testRoundTripOptionalStringArray() throws {
135+
let values = ["hello", nil, "world", nil, "🎉"]
136+
let result = try ArraySupportImports.jsRoundTripOptionalStringArray(values)
137+
XCTAssertEqual(result, values)
138+
}
139+
140+
func testRoundTripOptionalBoolArray() throws {
141+
let values = [true, nil, false, nil, true]
142+
let result = try ArraySupportImports.jsRoundTripOptionalBoolArray(values)
143+
XCTAssertEqual(result, values)
144+
}
145+
146+
func testRoundTripOptionalJSValueArray() throws {
147+
let values: [JSValue?] = [.number(1), nil, .string("hello"), nil, .object(.global)]
148+
let result = try ArraySupportImports.jsRoundTripOptionalJSValueArray(values)
149+
XCTAssertEqual(result, values)
150+
}
151+
152+
func testRoundTripOptionalJSObjectArray() throws {
153+
let values = [.global, nil, JSObject(), nil, ["a": 1, "b": 2]]
154+
let result = try ArraySupportImports.jsRoundTripOptionalJSObjectArray(values)
155+
XCTAssertEqual(result, values)
156+
}
157+
158+
func testRoundTripOptionalJSClassArray() throws {
159+
let values = try [
160+
ArrayElementObject(id: "1"), nil, ArrayElementObject(id: "2"), nil, ArrayElementObject(id: "3"),
161+
]
162+
let result = try ArraySupportImports.jsRoundTripOptionalJSClassArray(values)
163+
XCTAssertEqual(result, values)
164+
XCTAssertEqual(try result[0]?.id, "1")
165+
XCTAssertEqual(result[1], nil)
166+
XCTAssertEqual(try result[2]?.id, "2")
167+
XCTAssertEqual(result[3], nil)
168+
XCTAssertEqual(try result[4]?.id, "3")
169+
170+
XCTAssertEqual(try ArraySupportImports.jsRoundTripOptionalJSClassArray([]), [])
171+
}
172+
}
173+
174+
@JS enum ArraySupportExports {
175+
@JS static func roundTripIntArray(_ v: [Int]) -> [Int] { v }
176+
@JS static func roundTripStringArray(_ v: [String]) -> [String] { v }
177+
@JS static func roundTripDoubleArray(_ v: [Double]) -> [Double] { v }
178+
@JS static func roundTripBoolArray(_ v: [Bool]) -> [Bool] { v }
179+
@JS static func roundTripUnsafeRawPointerArray(_ v: [UnsafeRawPointer]) -> [UnsafeRawPointer] { v }
180+
@JS static func roundTripUnsafeMutableRawPointerArray(_ v: [UnsafeMutableRawPointer]) -> [UnsafeMutableRawPointer] {
181+
v
182+
}
183+
@JS static func roundTripOpaquePointerArray(_ v: [OpaquePointer]) -> [OpaquePointer] { v }
184+
@JS static func roundTripUnsafePointerArray(_ v: [UnsafePointer<UInt8>]) -> [UnsafePointer<UInt8>] { v }
185+
@JS static func roundTripUnsafeMutablePointerArray(
186+
_ v: [UnsafeMutablePointer<UInt8>]
187+
) -> [UnsafeMutablePointer<UInt8>] { v }
188+
@JS static func roundTripJSValueArray(_ v: [JSValue]) -> [JSValue] { v }
189+
@JS static func roundTripJSObjectArray(_ v: [JSObject]) -> [JSObject] { v }
190+
@JS static func roundTripCaseEnumArray(_ v: [Direction]) -> [Direction] { v }
191+
@JS static func roundTripStringRawValueEnumArray(_ v: [Theme]) -> [Theme] { v }
192+
@JS static func roundTripIntRawValueEnumArray(_ v: [HttpStatus]) -> [HttpStatus] { v }
193+
@JS static func roundTripStructArray(_ v: [DataPoint]) -> [DataPoint] { v }
194+
@JS static func roundTripSwiftClassArray(_ v: [Greeter]) -> [Greeter] { v }
195+
@JS static func roundTripNamespacedSwiftClassArray(_ v: [Utils.Converter]) -> [Utils.Converter] { v }
196+
@JS static func roundTripProtocolArray(_ v: [ArrayElementProtocol]) -> [ArrayElementProtocol] { v }
197+
@JS static func roundTripJSClassArray(_ v: [ArrayElementObject]) -> [ArrayElementObject] { v }
198+
199+
@JS static func roundTripOptionalIntArray(_ v: [Int?]) -> [Int?] { v }
200+
@JS static func roundTripOptionalStringArray(_ v: [String?]) -> [String?] { v }
201+
@JS static func roundTripOptionalJSObjectArray(_ v: [JSObject?]) -> [JSObject?] { v }
202+
@JS static func roundTripOptionalCaseEnumArray(_ v: [Direction?]) -> [Direction?] { v }
203+
@JS static func roundTripOptionalStringRawValueEnumArray(_ v: [Theme?]) -> [Theme?] { v }
204+
@JS static func roundTripOptionalIntRawValueEnumArray(_ v: [HttpStatus?]) -> [HttpStatus?] { v }
205+
@JS static func roundTripOptionalStructArray(_ v: [DataPoint?]) -> [DataPoint?] { v }
206+
@JS static func roundTripOptionalSwiftClassArray(_ v: [Greeter?]) -> [Greeter?] { v }
207+
@JS static func roundTripOptionalJSClassArray(_ v: [ArrayElementObject?]) -> [ArrayElementObject?] { v }
208+
209+
@JS static func roundTripNestedIntArray(_ v: [[Int]]) -> [[Int]] { v }
210+
@JS static func roundTripNestedStringArray(_ v: [[String]]) -> [[String]] { v }
211+
@JS static func roundTripNestedDoubleArray(_ v: [[Double]]) -> [[Double]] { v }
212+
@JS static func roundTripNestedBoolArray(_ v: [[Bool]]) -> [[Bool]] { v }
213+
@JS static func roundTripNestedStructArray(_ v: [[DataPoint]]) -> [[DataPoint]] { v }
214+
@JS static func roundTripNestedCaseEnumArray(_ v: [[Direction]]) -> [[Direction]] { v }
215+
@JS static func roundTripNestedSwiftClassArray(_ v: [[Greeter]]) -> [[Greeter]] { v }
216+
217+
// MARK: - Multiple Array Parameters
218+
@JS static func multiArrayFirst(_ a: [Int], _ b: [String]) -> [Int] { a }
219+
@JS static func multiArraySecond(_ a: [Int], _ b: [String]) -> [String] { b }
220+
@JS static func multiOptionalArrayFirst(_ a: [Int]?, _ b: [String]?) -> [Int]? { a }
221+
@JS static func multiOptionalArraySecond(_ a: [Int]?, _ b: [String]?) -> [String]? { b }
108222
}

Tests/BridgeJSRuntimeTests/ExportAPITests.swift

Lines changed: 0 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ func runJsWorks() -> Void
6868
return v
6969
}
7070

71-
@JS func roundTripJSValueArray(v: [JSValue]) -> [JSValue] {
72-
return v
73-
}
74-
7571
@JS func roundTripOptionalJSValueArray(v: [JSValue]?) -> [JSValue]? {
7672
return v
7773
}
@@ -1275,71 +1271,6 @@ enum GraphOperations {
12751271

12761272
// MARK: - Array Tests
12771273

1278-
// Primitive arrays
1279-
@JS func roundTripIntArray(_ values: [Int]) -> [Int] {
1280-
return values
1281-
}
1282-
1283-
@JS func roundTripStringArray(_ values: [String]) -> [String] {
1284-
return values
1285-
}
1286-
1287-
@JS func roundTripDoubleArray(_ values: [Double]) -> [Double] {
1288-
return values
1289-
}
1290-
1291-
@JS func roundTripBoolArray(_ values: [Bool]) -> [Bool] {
1292-
return values
1293-
}
1294-
1295-
// Enum arrays
1296-
@JS func roundTripDirectionArray(_ values: [Direction]) -> [Direction] {
1297-
return values
1298-
}
1299-
1300-
@JS func roundTripStatusArray(_ values: [Status]) -> [Status] {
1301-
return values
1302-
}
1303-
1304-
@JS func roundTripThemeArray(_ values: [Theme]) -> [Theme] {
1305-
return values
1306-
}
1307-
1308-
@JS func roundTripHttpStatusArray(_ values: [HttpStatus]) -> [HttpStatus] {
1309-
return values
1310-
}
1311-
1312-
// Struct arrays
1313-
@JS func roundTripDataPointArray(_ points: [DataPoint]) -> [DataPoint] {
1314-
return points
1315-
}
1316-
1317-
// Class arrays
1318-
@JS func roundTripGreeterArray(_ greeters: [Greeter]) -> [Greeter] {
1319-
return greeters
1320-
}
1321-
1322-
// Arrays of optional elements
1323-
@JS func roundTripOptionalIntArray(_ values: [Int?]) -> [Int?] {
1324-
return values
1325-
}
1326-
1327-
@JS func roundTripOptionalStringArray(_ values: [String?]) -> [String?] {
1328-
return values
1329-
}
1330-
1331-
@JS func roundTripOptionalDataPointArray(_ points: [DataPoint?]) -> [DataPoint?] {
1332-
return points
1333-
}
1334-
1335-
@JS func roundTripOptionalDirectionArray(_ directions: [Direction?]) -> [Direction?] {
1336-
return directions
1337-
}
1338-
1339-
@JS func roundTripOptionalStatusArray(_ statuses: [Status?]) -> [Status?] {
1340-
return statuses
1341-
}
1342-
13431274
// Optional arrays
13441275
@JS func roundTripOptionalIntArrayType(_ values: [Int]?) -> [Int]? {
13451276
return values
@@ -1353,94 +1284,6 @@ enum GraphOperations {
13531284
return greeters
13541285
}
13551286

1356-
// Nested arrays
1357-
1358-
@JS func roundTripNestedIntArray(_ values: [[Int]]) -> [[Int]] {
1359-
return values
1360-
}
1361-
1362-
@JS func roundTripNestedStringArray(_ values: [[String]]) -> [[String]] {
1363-
return values
1364-
}
1365-
1366-
@JS func roundTripNestedDoubleArray(_ values: [[Double]]) -> [[Double]] {
1367-
return values
1368-
}
1369-
1370-
@JS func roundTripNestedBoolArray(_ values: [[Bool]]) -> [[Bool]] {
1371-
return values
1372-
}
1373-
1374-
@JS func roundTripNestedDataPointArray(_ points: [[DataPoint]]) -> [[DataPoint]] {
1375-
return points
1376-
}
1377-
1378-
@JS func roundTripNestedDirectionArray(_ directions: [[Direction]]) -> [[Direction]] {
1379-
return directions
1380-
}
1381-
1382-
@JS func roundTripNestedGreeterArray(_ greeters: [[Greeter]]) -> [[Greeter]] {
1383-
return greeters
1384-
}
1385-
1386-
@JS func roundTripUnsafeRawPointerArray(_ values: [UnsafeRawPointer]) -> [UnsafeRawPointer] {
1387-
return values
1388-
}
1389-
@JS func roundTripUnsafeMutableRawPointerArray(_ values: [UnsafeMutableRawPointer]) -> [UnsafeMutableRawPointer] {
1390-
return values
1391-
}
1392-
@JS func roundTripOpaquePointerArray(_ values: [OpaquePointer]) -> [OpaquePointer] {
1393-
return values
1394-
}
1395-
@JS func roundTripUnsafePointerArray(_ values: [UnsafePointer<UInt8>]) -> [UnsafePointer<UInt8>] {
1396-
return values
1397-
}
1398-
@JS func roundTripUnsafeMutablePointerArray(_ values: [UnsafeMutablePointer<UInt8>]) -> [UnsafeMutablePointer<UInt8>] {
1399-
return values
1400-
}
1401-
1402-
@JS func consumeDataProcessorArrayType(_ processors: [DataProcessor]) -> Int {
1403-
return processors.count
1404-
}
1405-
1406-
@JS func roundTripDataProcessorArrayType(_ processors: [DataProcessor]) -> [DataProcessor] {
1407-
return processors
1408-
}
1409-
1410-
@JS func roundTripJSObjectArray(_ objects: [JSObject]) -> [JSObject] {
1411-
return objects
1412-
}
1413-
1414-
@JS func roundTripOptionalJSObjectArray(_ objects: [JSObject?]) -> [JSObject?] {
1415-
return objects
1416-
}
1417-
1418-
@JS func roundTripFooArray(_ foos: [Foo]) -> [Foo] {
1419-
return foos
1420-
}
1421-
1422-
@JS func roundTripOptionalFooArray(_ foos: [Foo?]) -> [Foo?] {
1423-
return foos
1424-
}
1425-
1426-
// MARK: - Multiple stack-based parameters (regression test for LIFO ordering)
1427-
1428-
@JS func multiArrayFirstNums(nums: [Int], strs: [String]) -> [Int] {
1429-
return nums
1430-
}
1431-
1432-
@JS func multiArrayFirstStrs(nums: [Int], strs: [String]) -> [String] {
1433-
return strs
1434-
}
1435-
1436-
@JS func multiOptionalArrayFirstA(a: [Int]?, b: [String]?) -> [Int]? {
1437-
return a
1438-
}
1439-
1440-
@JS func multiOptionalArrayFirstB(a: [Int]?, b: [String]?) -> [String]? {
1441-
return b
1442-
}
1443-
14441287
class ExportAPITests: XCTestCase {
14451288
func testAll() {
14461289
var hasDeinitGreeter = false

0 commit comments

Comments
 (0)