Skip to content

Commit cf60e85

Browse files
BridgeJS: Migrate Dictionary tests to use conventional style
1 parent 78800eb commit cf60e85

File tree

9 files changed

+384
-437
lines changed

9 files changed

+384
-437
lines changed

Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -724,57 +724,19 @@ struct StackCodegen {
724724
switch type {
725725
case .string, .int, .uint, .bool, .float, .double,
726726
.jsObject(nil), .jsValue, .swiftStruct, .swiftHeapObject, .unsafePointer,
727-
.swiftProtocol, .caseEnum, .associatedValueEnum, .rawValueEnum, .array:
727+
.swiftProtocol, .caseEnum, .associatedValueEnum, .rawValueEnum, .array, .dictionary:
728728
return "\(raw: type.swiftType).bridgeJSStackPop()"
729729
case .jsObject(let className?):
730730
return "\(raw: className)(unsafelyWrapping: JSObject.bridgeJSStackPop())"
731731
case .nullable(let wrappedType, let kind):
732732
return liftNullableExpression(wrappedType: wrappedType, kind: kind)
733-
case .dictionary(let valueType):
734-
return liftDictionaryExpression(valueType: valueType)
735733
case .closure:
736734
return "JSObject.bridgeJSStackPop()"
737735
case .void, .namespaceEnum:
738736
return "()"
739737
}
740738
}
741739

742-
func liftDictionaryExpression(valueType: BridgeType) -> ExprSyntax {
743-
switch valueType {
744-
case .jsObject(let className?) where className != "JSObject":
745-
return """
746-
{
747-
let __dict = [String: JSObject].bridgeJSStackPop()
748-
return __dict.mapValues { \(raw: className)(unsafelyWrapping: $0) }
749-
}()
750-
"""
751-
case .nullable, .closure:
752-
return liftDictionaryExpressionInline(valueType: valueType)
753-
case .void, .namespaceEnum:
754-
fatalError("Invalid dictionary value type: \(valueType)")
755-
default:
756-
return "[String: \(raw: valueType.swiftType)].bridgeJSStackPop()"
757-
}
758-
}
759-
760-
private func liftDictionaryExpressionInline(valueType: BridgeType) -> ExprSyntax {
761-
let valueLift = liftExpression(for: valueType)
762-
let swiftTypeName = valueType.swiftType
763-
return """
764-
{
765-
let __count = Int(_swift_js_pop_i32())
766-
var __result: [String: \(raw: swiftTypeName)] = [:]
767-
__result.reserveCapacity(__count)
768-
for _ in 0..<__count {
769-
let __value = \(valueLift)
770-
let __key = String.bridgeJSStackPop()
771-
__result[__key] = __value
772-
}
773-
return __result
774-
}()
775-
"""
776-
}
777-
778740
private func liftNullableExpression(wrappedType: BridgeType, kind: JSOptionalKind) -> ExprSyntax {
779741
let typeName = kind == .null ? "Optional" : "JSUndefinedOr"
780742
switch wrappedType {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
@_spi(BridgeJS) import JavaScriptKit
2+
import XCTest
3+
4+
@JSClass struct DictionarySupportImports {
5+
@JSFunction static func jsRoundTripDictionaryInt(_ values: [String: Int]) throws(JSException) -> [String: Int]
6+
@JSFunction static func jsRoundTripDictionaryBool(_ values: [String: Bool]) throws(JSException) -> [String: Bool]
7+
@JSFunction static func jsRoundTripDictionaryDouble(
8+
_ values: [String: Double]
9+
) throws(JSException) -> [String: Double]
10+
@JSFunction static func jsRoundTripDictionaryJSObject(
11+
_ values: [String: JSObject]
12+
) throws(JSException) -> [String: JSObject]
13+
@JSFunction static func jsRoundTripDictionaryJSValue(
14+
_ values: [String: JSValue]
15+
) throws(JSException) -> [String: JSValue]
16+
@JSFunction static func jsRoundTripDictionaryDoubleArray(
17+
_ values: [String: [Double]]
18+
) throws(JSException) -> [String: [Double]]
19+
}
20+
21+
final class DictionarySupportTests: XCTestCase {
22+
23+
private func roundTripTest<T: Equatable>(_ fn: ([String: T]) throws -> [String: T], _ input: [String: T]) throws {
24+
let result = try fn(input)
25+
XCTAssertEqual(result, input)
26+
}
27+
28+
func testRoundTripDictionaryInt() throws {
29+
try roundTripTest(DictionarySupportImports.jsRoundTripDictionaryInt, ["a": 1, "b": 2])
30+
}
31+
32+
func testRoundTripDictionaryBool() throws {
33+
try roundTripTest(DictionarySupportImports.jsRoundTripDictionaryBool, ["yes": true, "no": false])
34+
}
35+
36+
func testRoundTripDictionaryDouble() throws {
37+
try roundTripTest(DictionarySupportImports.jsRoundTripDictionaryDouble, ["pi": 3.14, "tau": 6.28])
38+
}
39+
40+
func testRoundTripDictionaryJSObject() throws {
41+
try roundTripTest(DictionarySupportImports.jsRoundTripDictionaryJSObject, ["global": JSObject.global])
42+
}
43+
44+
func testRoundTripDictionaryJSValue() throws {
45+
try roundTripTest(
46+
DictionarySupportImports.jsRoundTripDictionaryJSValue,
47+
["number": .number(123.5), "boolean": .boolean(true), "string": .string("hello"), "null": .null]
48+
)
49+
}
50+
51+
func testRoundTripDictionaryDoubleArray() throws {
52+
try roundTripTest(DictionarySupportImports.jsRoundTripDictionaryDoubleArray, ["xs": [1.0, 2.5], "ys": []])
53+
}
54+
}

Tests/BridgeJSRuntimeTests/DictionaryTests.swift

Lines changed: 0 additions & 96 deletions
This file was deleted.

0 commit comments

Comments
 (0)