From 0a2a8a58c198b56f5f437446e46f4ae59d4d4fb8 Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Wed, 27 Jan 2021 12:55:55 +0300 Subject: [PATCH] Add test for overriding methods of Any from Swift --- .../tests/objcexport/expectedLazy.h | 10 +++++ .../tests/objcexport/overrideMethodsOfAny.kt | 18 ++++++++ .../objcexport/overrideMethodsOfAny.swift | 43 +++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 kotlin-native/backend.native/tests/objcexport/overrideMethodsOfAny.kt create mode 100644 kotlin-native/backend.native/tests/objcexport/overrideMethodsOfAny.swift diff --git a/kotlin-native/backend.native/tests/objcexport/expectedLazy.h b/kotlin-native/backend.native/tests/objcexport/expectedLazy.h index 39dcf92fc9a..1abfa69ecd7 100644 --- a/kotlin-native/backend.native/tests/objcexport/expectedLazy.h +++ b/kotlin-native/backend.native/tests/objcexport/expectedLazy.h @@ -663,6 +663,16 @@ __attribute__((swift_name("ArraysInitBlock"))) - (NSString *)log __attribute__((swift_name("log()"))); @end; +__attribute__((objc_subclassing_restricted)) +__attribute__((swift_name("OverrideMethodsOfAnyKt"))) +@interface KtOverrideMethodsOfAnyKt : KtBase + +/** + @note This method converts all Kotlin exceptions to errors. +*/ ++ (BOOL)testObj:(id)obj other:(id)other swift:(BOOL)swift error:(NSError * _Nullable * _Nullable)error __attribute__((swift_name("test(obj:other:swift:)"))); +@end; + __attribute__((objc_subclassing_restricted)) __attribute__((swift_name("ThrowsEmptyKt"))) @interface KtThrowsEmptyKt : KtBase diff --git a/kotlin-native/backend.native/tests/objcexport/overrideMethodsOfAny.kt b/kotlin-native/backend.native/tests/objcexport/overrideMethodsOfAny.kt new file mode 100644 index 00000000000..aea0e02b2a4 --- /dev/null +++ b/kotlin-native/backend.native/tests/objcexport/overrideMethodsOfAny.kt @@ -0,0 +1,18 @@ +package overrideMethodsOfAny + +import kotlin.test.* + +@Throws(Throwable::class) +fun test(obj: Any, other: Any, swift: Boolean) { + if (!swift) { + // Doesn't work for Swift, see https://youtrack.jetbrains.com/issue/KT-44613. + assertEquals(42, obj.hashCode()) + assertTrue(obj.equals(other)) + } + + assertTrue(obj.equals(obj)) + assertFalse(obj.equals(null)) + assertFalse(obj.equals(Any())) + + assertEquals("toString", obj.toString()) +} diff --git a/kotlin-native/backend.native/tests/objcexport/overrideMethodsOfAny.swift b/kotlin-native/backend.native/tests/objcexport/overrideMethodsOfAny.swift new file mode 100644 index 00000000000..db82afe687f --- /dev/null +++ b/kotlin-native/backend.native/tests/objcexport/overrideMethodsOfAny.swift @@ -0,0 +1,43 @@ +/* + * 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 + +private class SwiftOverridingMethodsOfAny : Hashable, Equatable, CustomStringConvertible { + var hashValue: Int { return 42 } + + static func == (lhs: SwiftOverridingMethodsOfAny, rhs: SwiftOverridingMethodsOfAny) -> Bool { + return true + } + + var description: String { return "toString" } +} + +private func testSwift() throws { + try OverrideMethodsOfAnyKt.test(obj: SwiftOverridingMethodsOfAny(), other: SwiftOverridingMethodsOfAny(), swift: true) +} + +private class ObjCOverridingMethodsOfAny : NSObject { + override var hash: Int { return 42 } + + override func isEqual(_ other: Any?) -> Bool { + return other is ObjCOverridingMethodsOfAny + } + + override var description: String { return "toString" } +} + +private func testObjC() throws { + try OverrideMethodsOfAnyKt.test(obj: ObjCOverridingMethodsOfAny(), other: ObjCOverridingMethodsOfAny(), swift: false) +} + +class OverrideMethodsOfAnyTests : SimpleTestProvider { + override init() { + super.init() + + test("TestSwift", testSwift) + test("TestObjC", testObjC) + } +}