diff --git a/Sources/SkipBridgeToSwiftSamples/Samples.swift b/Sources/SkipBridgeToSwiftSamples/Samples.swift index 100aa22..36f6396 100644 --- a/Sources/SkipBridgeToSwiftSamples/Samples.swift +++ b/Sources/SkipBridgeToSwiftSamples/Samples.swift @@ -381,6 +381,18 @@ public var kotlinClosure1Var: (Int) -> String = { i in "value = \(i)" } public var kotlinClosure1PrimitivesVar: (Int64) -> Int = { l in Int(l / 1000) } public var kotlinClosure1OptionalsVar: (String?) -> Int? = { s in s?.count } +// MARK: Async Closures + +public var kotlinAsyncClosure0Var: () async -> Void = { print("original") } +public var kotlinAsyncClosure0ProtocolVar: () async -> any KotlinProtocol = { KotlinHelperClass() } +public var kotlinAsyncClosure1Var: (Int) async -> String = { i in "value = \(i)" } +public var kotlinAsyncClosure1PrimitivesVar: (Int64) async -> Int = { l in Int(l / 1000) } +public var kotlinAsyncClosure1OptionalsVar: (String?) async -> Int? = { s in s?.count } +public var kotlinAsyncClosure2Var: (Int64, Int32) async -> Double = { Double($0) + Double($1) } +public var kotlinAsyncClosure3Var: (Int64, Int32, Int16) async -> Double = { Double($0) + Double($1) + Double($2) } +public var kotlinAsyncClosure4Var: (Int64, Int32, Int16, Double) async -> Double = { Double($0) + Double($1) + Double($2) + Double($3) } +public var kotlinAsyncClosure5Var: (Int64, Int32, Int16, Double, Float) async -> Double = { Double($0) + Double($1) + Double($2) + Double($3) + Double($4) } + // MARK: Containers public var kotlinIntArrayVar = [1, 2, 3] diff --git a/Sources/SkipBridgeToSwiftSamplesTestsSupport/TestsSupport.swift b/Sources/SkipBridgeToSwiftSamplesTestsSupport/TestsSupport.swift index ed884cf..7782cdf 100644 --- a/Sources/SkipBridgeToSwiftSamplesTestsSupport/TestsSupport.swift +++ b/Sources/SkipBridgeToSwiftSamplesTestsSupport/TestsSupport.swift @@ -830,6 +830,46 @@ public func testSupport_kotlinClosure1OptionalsVar(value: String?) -> Int? { return i1 == i2 ? i1 : (i1 ?? 0) * 10000 + (i2 ?? 0) } +public func testSupport_kotlinAsyncClosure0Var() async { + await kotlinAsyncClosure0Var() + kotlinAsyncClosure0Var = { try! await Task.sleep(nanoseconds: 1000000); print("reassigned") } + await kotlinAsyncClosure0Var() +} + +public func testSupport_kotlinAsyncClosure0ProtocolVar() async -> String { + let first = await kotlinAsyncClosure0ProtocolVar().stringValue() + + kotlinAsyncClosure0ProtocolVar = { + let helper = KotlinHelperClass() + helper.stringVar = "updated" + return helper + } + + let second = await kotlinAsyncClosure0ProtocolVar().stringValue() + return "\(first)/\(second)" +} + +public func testSupport_kotlinAsyncClosure1Var(value: Int) async -> String { + let s1 = await kotlinAsyncClosure1Var(value) + kotlinAsyncClosure1Var = { i in "value = \(i)" } + let s2 = await kotlinAsyncClosure1Var(value) + return s1 == s2 ? s1 : s1 + "/" + s2 +} + +public func testSupport_kotlinAsyncClosure1PrimitivesVar(value: Int64) async -> Int { + let i1 = await kotlinAsyncClosure1PrimitivesVar(value) + kotlinAsyncClosure1PrimitivesVar = { l in Int(l / 1000) } + let i2 = await kotlinAsyncClosure1PrimitivesVar(value) + return i1 == i2 ? i1 : i1 * 10000 + i2 +} + +public func testSupport_kotlinAsyncClosure1OptionalsVar(value: String?) async -> Int? { + let i1 = await kotlinAsyncClosure1OptionalsVar(value) + kotlinAsyncClosure1OptionalsVar = { s in s?.count } + let i2 = await kotlinAsyncClosure1OptionalsVar(value) + return i1 == i2 ? i1 : (i1 ?? 0) * 10000 + (i2 ?? 0) +} + public func testSupport_kotlinClosure1Parameter(value: (Int) -> String) { // We're only testing that using a closure as a parameter compiles cleanly } diff --git a/Tests/SkipBridgeToSwiftSamplesTestsSupportTests/BridgeToSwiftSamplesTests.swift b/Tests/SkipBridgeToSwiftSamplesTestsSupportTests/BridgeToSwiftSamplesTests.swift index 1efd705..9d7a6a4 100644 --- a/Tests/SkipBridgeToSwiftSamplesTestsSupportTests/BridgeToSwiftSamplesTests.swift +++ b/Tests/SkipBridgeToSwiftSamplesTestsSupportTests/BridgeToSwiftSamplesTests.swift @@ -304,6 +304,32 @@ final class BridgeToSwiftTests: XCTestCase { XCTAssertEqual(testSupport_kotlinClosure1OptionalsVar(value: nil), nil) } + func testAsyncClosure0Var() async { + await testSupport_kotlinAsyncClosure0Var() + } + + func testAsyncClosure0ProtocolVar() async { + let result = await testSupport_kotlinAsyncClosure0ProtocolVar() + XCTAssertEqual(result, "s/updated") + } + + func testAsyncClosure1Var() async { + let result = await testSupport_kotlinAsyncClosure1Var(value: 100) + XCTAssertEqual(result, "value = 100") + } + + func testAsyncClosure1PrimitivesVar() async { + let result = await testSupport_kotlinAsyncClosure1PrimitivesVar(value: Int64(3000)) + XCTAssertEqual(result, 3) + } + + func testAsyncClosure1OptionalsVar() async { + let result1 = await testSupport_kotlinAsyncClosure1OptionalsVar(value: "abc") + XCTAssertEqual(result1, 3) + let result2 = await testSupport_kotlinAsyncClosure1OptionalsVar(value: nil) + XCTAssertEqual(result2, nil) + } + func testArrays() { let roundtripped = testSupport_kotlinIntArrayVar(value: [4, 5, 6]) XCTAssertEqual(roundtripped, [4, 5, 6])