Files
kotlin-fork/kotlin-native/backend.native/tests/objcexport/throwableAsError.swift
T
Nikolay Kasyanov 4dcfd38236 ObjCExport: remove KotlinUnit from suspend funs returning Unit (#4635)
With `unitSuspendFunctionObjCExport=proper` binary option,
for Unit-returning suspend functions ObjCExport now generates
completion handlers without redundant `KotlinUnit*` result parameter.

So Swift (5.5+) can import these functions as Void-returning async
functions.

^KT-47399 Fixed
2021-11-18 10:01:33 +03:00

61 lines
1.8 KiB
Swift

import Kt
private func testUnwrapsToSame() throws {
let throwable = ThrowableAsError()
try assertSame(actual: throwable.asError().kotlinException as AnyObject, expected: throwable)
}
private func testCanThrowAndCatch() throws {
let throwable = ThrowableAsError()
let impl = ThrowsThrowableAsErrorImpl(throwable: throwable)
let caught = ThrowableAsErrorKt.callAndCatchThrowableAsError(throwsThrowableAsError: impl)
try assertSame(actual: caught, expected: throwable)
}
private class ThrowsThrowableAsErrorImpl : ThrowsThrowableAsError {
var throwable: KotlinThrowable
init(throwable: KotlinThrowable) {
self.throwable = throwable
}
func throwError() throws {
throw throwable.asError()
}
}
private func testCanThrowAndCatchSuspend() throws {
let throwable = ThrowableAsError()
let impl = ThrowsThrowableAsErrorSuspendImpl(throwable: throwable)
let caught = ThrowableAsErrorKt.callAndCatchThrowableAsErrorSuspend(throwsThrowableAsErrorSuspend: impl)
try assertSame(actual: caught, expected: throwable)
}
private class ThrowsThrowableAsErrorSuspendImpl : ThrowsThrowableAsErrorSuspend {
var throwable: KotlinThrowable
init(throwable: KotlinThrowable) {
self.throwable = throwable
}
#if LEGACY_SUSPEND_UNIT_FUNCTION_EXPORT
func throwError(completionHandler: @escaping (KotlinUnit?, Error?) -> Void) {
completionHandler(nil, throwable.asError())
}
#else
func throwError(completionHandler: @escaping (Error?) -> Void) {
completionHandler(throwable.asError())
}
#endif
}
class ThrowableAsErrorTests : SimpleTestProvider {
override init() {
super.init()
test("TestUnwrapsToSame", testUnwrapsToSame)
test("TestCanThrowAndCatch", testCanThrowAndCatch)
test("TestCanThrowAndCatchSuspend", testCanThrowAndCatchSuspend)
}
}