From f7af8bc7635adbebafa744c28ca10fdc41692233 Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Thu, 11 Feb 2021 16:23:38 +0300 Subject: [PATCH] Add test for SAM-converted lambdas passed to Obj-C Based on https://youtrack.jetbrains.com/issue/KT-44799 --- .../tests/objcexport/expectedLazy.h | 13 +++++++++++++ .../tests/objcexport/funInterfaces.kt | 15 +++++++++++++++ .../tests/objcexport/funInterfaces.swift | 15 +++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 kotlin-native/backend.native/tests/objcexport/funInterfaces.kt create mode 100644 kotlin-native/backend.native/tests/objcexport/funInterfaces.swift diff --git a/kotlin-native/backend.native/tests/objcexport/expectedLazy.h b/kotlin-native/backend.native/tests/objcexport/expectedLazy.h index e146fda0a52..d01dc46ecb9 100644 --- a/kotlin-native/backend.native/tests/objcexport/expectedLazy.h +++ b/kotlin-native/backend.native/tests/objcexport/expectedLazy.h @@ -377,6 +377,19 @@ __attribute__((swift_name("EmptyEnum"))) + (KtKotlinArray *)values __attribute__((swift_name("values()"))); @end; +__attribute__((swift_name("FunInterface"))) +@protocol KtFunInterface +@required +- (int32_t)run __attribute__((swift_name("run()"))); +@end; + +__attribute__((objc_subclassing_restricted)) +__attribute__((swift_name("FunInterfacesKt"))) +@interface KtFunInterfacesKt : KtBase ++ (id)getObject __attribute__((swift_name("getObject()"))); ++ (id)getLambda __attribute__((swift_name("getLambda()"))); +@end; + __attribute__((swift_name("FHolder"))) @interface KtFHolder : KtBase - (instancetype)init __attribute__((swift_name("init()"))) __attribute__((objc_designated_initializer)); diff --git a/kotlin-native/backend.native/tests/objcexport/funInterfaces.kt b/kotlin-native/backend.native/tests/objcexport/funInterfaces.kt new file mode 100644 index 00000000000..945133f0963 --- /dev/null +++ b/kotlin-native/backend.native/tests/objcexport/funInterfaces.kt @@ -0,0 +1,15 @@ +package funinterfaces + +fun interface FunInterface { + fun run(): Int +} + +fun getObject(): FunInterface { + return object : FunInterface { + override fun run() = 1 + } +} + +fun getLambda(): FunInterface { + return FunInterface { 2 } +} diff --git a/kotlin-native/backend.native/tests/objcexport/funInterfaces.swift b/kotlin-native/backend.native/tests/objcexport/funInterfaces.swift new file mode 100644 index 00000000000..97a057bbd35 --- /dev/null +++ b/kotlin-native/backend.native/tests/objcexport/funInterfaces.swift @@ -0,0 +1,15 @@ +import Kt + +// Based on https://youtrack.jetbrains.com/issue/KT-44799. +private func testSAMConversion() throws { + try assertEquals(actual: FunInterfacesKt.getObject().run(), expected: 1) + try assertEquals(actual: FunInterfacesKt.getLambda().run(), expected: 2) +} + +class FunInterfacesTests : SimpleTestProvider { + override init() { + super.init() + + test("TestSAMConversion", testSAMConversion) + } +}