[ObjCExport] Add suspend function and @Throws tests

KT-66115, KT-66239
This commit is contained in:
eugene.levenetc
2024-03-01 10:39:23 +01:00
committed by Space Team
parent ac1f664a65
commit 1f49e01f1a
12 changed files with 372 additions and 20 deletions
@@ -4,7 +4,23 @@ import org.jetbrains.kotlin.analysis.api.symbols.KtCallableSymbol
import org.jetbrains.kotlin.backend.konan.KonanFqNames
import org.jetbrains.kotlin.name.ClassId
/**
* Returns if this callable has [Throws] annotation.
* Super callables aren't included.
*
* ```kotlin
* class A {
* @Throws
* fun foo()
* }
* class B: A() {
* override fun foo()
* }
*
* A.foo.hasThrowsAnnotation = true
* B.foo.hasThrowsAnnotation = false
* ```
*/
internal val KtCallableSymbol.hasThrowsAnnotation: Boolean
get() {
return annotationsList.hasAnnotation(ClassId.topLevel(KonanFqNames.throws))
@@ -2,9 +2,6 @@ package org.jetbrains.kotlin.objcexport.analysisApiUtils
import org.jetbrains.kotlin.analysis.api.KtAnalysisSession
import org.jetbrains.kotlin.analysis.api.symbols.KtClassOrObjectSymbol
import org.jetbrains.kotlin.backend.konan.objcexport.ObjCClassType
import org.jetbrains.kotlin.backend.konan.objcexport.ObjCMethod
import org.jetbrains.kotlin.backend.konan.objcexport.swiftNameAttribute
import org.jetbrains.kotlin.builtins.StandardNames
import org.jetbrains.kotlin.name.ClassId
@@ -19,16 +16,4 @@ context(KtAnalysisSession)
internal val ClassId.isThrowable: Boolean
get() {
return StandardNames.FqNames.throwable == this.asSingleFqName()
}
internal fun buildThrowableAsErrorMethod(): ObjCMethod {
return ObjCMethod(
comment = null,
isInstanceMethod = true,
returnType = ObjCClassType("NSError"),
selectors = listOf("asError"),
parameters = emptyList(),
attributes = listOf(swiftNameAttribute("asError()")),
origin = null
)
}
}
@@ -0,0 +1,20 @@
package org.jetbrains.kotlin.objcexport
import org.jetbrains.kotlin.backend.konan.objcexport.ObjCClassType
import org.jetbrains.kotlin.backend.konan.objcexport.ObjCMethod
import org.jetbrains.kotlin.backend.konan.objcexport.swiftNameAttribute
/**
* See K1: [org.jetbrains.kotlin.backend.konan.objcexport.ObjCExportTranslatorImpl.buildThrowableAsErrorMethod]
*/
internal fun buildThrowableAsErrorMethod(): ObjCMethod {
return ObjCMethod(
comment = null,
isInstanceMethod = true,
returnType = ObjCClassType("NSError"),
selectors = listOf("asError"),
parameters = emptyList(),
attributes = listOf(swiftNameAttribute("asError()")),
origin = null
)
}
@@ -6,8 +6,6 @@ import org.jetbrains.kotlin.analysis.api.symbols.markers.KtSymbolWithModality
import org.jetbrains.kotlin.analysis.api.types.KtNonErrorClassType
import org.jetbrains.kotlin.backend.konan.objcexport.*
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.name.StandardClassIds
import org.jetbrains.kotlin.objcexport.analysisApiUtils.buildThrowableAsErrorMethod
import org.jetbrains.kotlin.objcexport.analysisApiUtils.isThrowable
import org.jetbrains.kotlin.objcexport.analysisApiUtils.isVisibleInObjC
@@ -0,0 +1,46 @@
/*
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.objcexport.tests
import org.jetbrains.kotlin.analysis.api.analyze
import org.jetbrains.kotlin.objcexport.analysisApiUtils.isThrowable
import org.jetbrains.kotlin.objcexport.testUtils.InlineSourceCodeAnalysis
import org.jetbrains.kotlin.objcexport.testUtils.getClassOrFail
import org.jetbrains.kotlin.objcexport.testUtils.getPropertyOrFail
import org.junit.jupiter.api.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue
class IsThrowableTest(
private val inlineSourceCodeAnalysis: InlineSourceCodeAnalysis,
) {
@Test
fun `test - fake throwable`() {
val file = inlineSourceCodeAnalysis.createKtFile(
"""
class Throwable
""".trimIndent()
)
analyze(file) {
assertFalse(file.getClassOrFail("Throwable").isThrowable)
}
}
@Test
fun `test - true throwable`() {
val file = inlineSourceCodeAnalysis.createKtFile(
"""
var foo: Throwable? = null
""".trimIndent()
)
analyze(file) {
val isThrowable = file.getPropertyOrFail("foo").returnType.expandedClassSymbol?.isThrowable ?: false
assertTrue(isThrowable)
}
}
}