From e305710d91dc5b725a679faef7293842ac664534 Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Mon, 1 Feb 2021 16:38:07 +0300 Subject: [PATCH] Add tests for overriding Kotlin methods from Swift --- .../tests/objcexport/expectedLazy.h | 70 +++++++++++++++++++ .../tests/objcexport/overrideKotlinMethods.kt | 66 +++++++++++++++++ .../objcexport/overrideKotlinMethods.swift | 32 +++++++++ 3 files changed, 168 insertions(+) create mode 100644 kotlin-native/backend.native/tests/objcexport/overrideKotlinMethods.kt create mode 100644 kotlin-native/backend.native/tests/objcexport/overrideKotlinMethods.swift diff --git a/kotlin-native/backend.native/tests/objcexport/expectedLazy.h b/kotlin-native/backend.native/tests/objcexport/expectedLazy.h index 1abfa69ecd7..e146fda0a52 100644 --- a/kotlin-native/backend.native/tests/objcexport/expectedLazy.h +++ b/kotlin-native/backend.native/tests/objcexport/expectedLazy.h @@ -663,6 +663,76 @@ __attribute__((swift_name("ArraysInitBlock"))) - (NSString *)log __attribute__((swift_name("log()"))); @end; +__attribute__((swift_name("OverrideKotlinMethods2"))) +@protocol KtOverrideKotlinMethods2 +@required +- (int32_t)one __attribute__((swift_name("one()"))); +@end; + +__attribute__((swift_name("OverrideKotlinMethods3"))) +@interface KtOverrideKotlinMethods3 : KtBase +- (instancetype)init __attribute__((swift_name("init()"))) __attribute__((objc_designated_initializer)); ++ (instancetype)new __attribute__((availability(swift, unavailable, message="use object initializers instead"))); +@end; + +__attribute__((swift_name("OverrideKotlinMethods4"))) +@interface KtOverrideKotlinMethods4 : KtOverrideKotlinMethods3 +- (instancetype)init __attribute__((swift_name("init()"))) __attribute__((objc_designated_initializer)); ++ (instancetype)new __attribute__((availability(swift, unavailable, message="use object initializers instead"))); +- (int32_t)one __attribute__((swift_name("one()"))); +@end; + +__attribute__((swift_name("OverrideKotlinMethods5"))) +@protocol KtOverrideKotlinMethods5 +@required +- (int32_t)one __attribute__((swift_name("one()"))); +@end; + +__attribute__((swift_name("OverrideKotlinMethods6"))) +@protocol KtOverrideKotlinMethods6 +@required +@end; + +__attribute__((objc_subclassing_restricted)) +__attribute__((swift_name("OverrideKotlinMethodsKt"))) +@interface KtOverrideKotlinMethodsKt : KtBase + +/** + @note This method converts all Kotlin exceptions to errors. +*/ ++ (BOOL)test0Obj:(id)obj error:(NSError * _Nullable * _Nullable)error __attribute__((swift_name("test0(obj:)"))); + +/** + @note This method converts all Kotlin exceptions to errors. +*/ ++ (BOOL)test1Obj:(id)obj error:(NSError * _Nullable * _Nullable)error __attribute__((swift_name("test1(obj:)"))); + +/** + @note This method converts all Kotlin exceptions to errors. +*/ ++ (BOOL)test2Obj:(id)obj error:(NSError * _Nullable * _Nullable)error __attribute__((swift_name("test2(obj:)"))); + +/** + @note This method converts all Kotlin exceptions to errors. +*/ ++ (BOOL)test3Obj:(KtOverrideKotlinMethods3 *)obj error:(NSError * _Nullable * _Nullable)error __attribute__((swift_name("test3(obj:)"))); + +/** + @note This method converts all Kotlin exceptions to errors. +*/ ++ (BOOL)test4Obj:(KtOverrideKotlinMethods4 *)obj error:(NSError * _Nullable * _Nullable)error __attribute__((swift_name("test4(obj:)"))); + +/** + @note This method converts all Kotlin exceptions to errors. +*/ ++ (BOOL)test5Obj:(id)obj error:(NSError * _Nullable * _Nullable)error __attribute__((swift_name("test5(obj:)"))); + +/** + @note This method converts all Kotlin exceptions to errors. +*/ ++ (BOOL)test6Obj:(id)obj error:(NSError * _Nullable * _Nullable)error __attribute__((swift_name("test6(obj:)"))); +@end; + __attribute__((objc_subclassing_restricted)) __attribute__((swift_name("OverrideMethodsOfAnyKt"))) @interface KtOverrideMethodsOfAnyKt : KtBase diff --git a/kotlin-native/backend.native/tests/objcexport/overrideKotlinMethods.kt b/kotlin-native/backend.native/tests/objcexport/overrideKotlinMethods.kt new file mode 100644 index 00000000000..97d07423301 --- /dev/null +++ b/kotlin-native/backend.native/tests/objcexport/overrideKotlinMethods.kt @@ -0,0 +1,66 @@ +package overrideKotlinMethods + +import kotlin.test.* + +internal interface OverrideKotlinMethods0 { + fun one(): T +} + +internal interface OverrideKotlinMethods1 : OverrideKotlinMethods0 + +interface OverrideKotlinMethods2 { + fun one(): Int +} + +open class OverrideKotlinMethods3 { + internal open fun one(): Number = 3 +} + +open class OverrideKotlinMethods4 : OverrideKotlinMethods3(), OverrideKotlinMethods1, OverrideKotlinMethods2 { + override fun one(): Int = 2 +} + +interface OverrideKotlinMethods5 { + fun one(): Int +} + +interface OverrideKotlinMethods6 : OverrideKotlinMethods5 + +// Using `Any` because Kotlin forbids internal type in public function signature. +@Throws(Throwable::class) +fun test0(obj: Any) { + val obj0 = obj as OverrideKotlinMethods0<*> + assertEquals(1, obj0.one()) +} + +// Using `Any` because Kotlin forbids internal type in public function signature. +@Throws(Throwable::class) +fun test1(obj: Any) { + val obj1 = obj as OverrideKotlinMethods1<*> + assertEquals(1, obj1.one()) +} + +@Throws(Throwable::class) +fun test2(obj: OverrideKotlinMethods2) { + assertEquals(1, obj.one()) +} + +@Throws(Throwable::class) +fun test3(obj: OverrideKotlinMethods3) { + assertEquals(1, obj.one()) +} + +@Throws(Throwable::class) +fun test4(obj: OverrideKotlinMethods4) { + assertEquals(1, obj.one()) +} + +@Throws(Throwable::class) +fun test5(obj: OverrideKotlinMethods5) { + assertEquals(1, obj.one()) +} + +@Throws(Throwable::class) +fun test6(obj: OverrideKotlinMethods6) { + assertEquals(1, obj.one()) +} diff --git a/kotlin-native/backend.native/tests/objcexport/overrideKotlinMethods.swift b/kotlin-native/backend.native/tests/objcexport/overrideKotlinMethods.swift new file mode 100644 index 00000000000..3cf030162e1 --- /dev/null +++ b/kotlin-native/backend.native/tests/objcexport/overrideKotlinMethods.swift @@ -0,0 +1,32 @@ +/* + * Copyright 2010-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ + +import Kt + +class OverrideKotlinMethodsImpl : OverrideKotlinMethods4, OverrideKotlinMethods6 { + override func one() -> Int32 { + return 1 + } +} + +private func test1() throws { + let obj = OverrideKotlinMethodsImpl() + + try OverrideKotlinMethodsKt.test0(obj: obj) + try OverrideKotlinMethodsKt.test1(obj: obj) + try OverrideKotlinMethodsKt.test2(obj: obj) + try OverrideKotlinMethodsKt.test3(obj: obj) + try OverrideKotlinMethodsKt.test4(obj: obj) + try OverrideKotlinMethodsKt.test5(obj: obj) + try OverrideKotlinMethodsKt.test6(obj: obj) +} + +class OverrideKotlinMethodsTests : SimpleTestProvider { + override init() { + super.init() + + test("Test1", test1) + } +}