Native: prepare existing tests for cinterop adding ExperimentalForeignApi

cinterop tool should add ExperimentalForeignApi to all generated
declarations by default. This commit prepares existing tests for this.

^KT-58362
This commit is contained in:
Svyatoslav Scherbina
2023-07-06 12:57:52 +02:00
committed by Space Team
parent 93ef98cbec
commit 3280d3ef80
4 changed files with 22 additions and 2 deletions
@@ -2,6 +2,7 @@
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. * Copyright 2010-2023 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. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/ */
@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
package kt55938lib package kt55938lib
@@ -79,7 +79,7 @@ abstract class AbstractNativeCInteropKT39120Test : AbstractNativeCInteropBaseTes
private fun filterContentsOutput(contents: String, pattern: String) = private fun filterContentsOutput(contents: String, pattern: String) =
contents.split("\n").filter { contents.split("\n").filter {
it.contains(Regex(pattern)) it.contains(Regex(pattern))
}.joinToString(separator = "\n") }.joinToString(separator = "\n").replace("@ExperimentalForeignApi ", "")
private fun createTestCaseNoTestRun(module: TestModule.Exclusive, compilerArgs: TestCompilerArgs) = TestCase( private fun createTestCaseNoTestRun(module: TestModule.Exclusive, compilerArgs: TestCompilerArgs) = TestCase(
id = TestCaseId.Named(module.name), id = TestCaseId.Named(module.name),
@@ -49,6 +49,12 @@ abstract class AbstractNativeCInteropTest : AbstractNativeCInteropBaseTest() {
abstract val defFileName: String abstract val defFileName: String
// All declarations generated by cinterop now have ExperimentalForeignApi annotations.
// There is no sense in cluttering every test expected data with it, so we simply ignore it
// in the actual data by default:
open val ignoreExperimentalForeignApi: Boolean
get() = true
@Synchronized @Synchronized
protected fun runTest(@TestDataFile testPath: String) { protected fun runTest(@TestDataFile testPath: String) {
// FIXME: check the following failures under Android with -fmodules // FIXME: check the following failures under Android with -fmodules
@@ -88,6 +94,13 @@ abstract class AbstractNativeCInteropTest : AbstractNativeCInteropBaseTest() {
} }
} else { } else {
val klibContents = testCompilationResult.assertSuccess().resultingArtifact.getContents(kotlinNativeClassLoader.classLoader) val klibContents = testCompilationResult.assertSuccess().resultingArtifact.getContents(kotlinNativeClassLoader.classLoader)
.let {
if (ignoreExperimentalForeignApi) {
it.replace("@ExperimentalForeignApi ", "")
} else {
it
}
}
val expectedContents = goldenFile.readText() val expectedContents = goldenFile.readText()
assertEquals(StringUtilRt.convertLineSeparators(expectedContents), StringUtilRt.convertLineSeparators(klibContents)) { assertEquals(StringUtilRt.convertLineSeparators(expectedContents), StringUtilRt.convertLineSeparators(klibContents)) {
"Test failed. CInterop compilation result was: $testCompilationResult" "Test failed. CInterop compilation result was: $testCompilationResult"
@@ -20,6 +20,8 @@ import kotlin.test.assertTrue
@TestDataPath("\$PROJECT_ROOT") @TestDataPath("\$PROJECT_ROOT")
@EnforcedProperty(ClassLevelProperty.COMPILER_OUTPUT_INTERCEPTOR, "NONE") @EnforcedProperty(ClassLevelProperty.COMPILER_OUTPUT_INTERCEPTOR, "NONE")
class LinkerOutputTestKT55578 : AbstractNativeLinkerOutputTest() { class LinkerOutputTestKT55578 : AbstractNativeLinkerOutputTest() {
private val defaultCompilerArguments = listOf("-opt-in=kotlinx.cinterop.ExperimentalForeignApi")
private val testDir = File("native/native.tests/testData/CInterop/KT-55578/") private val testDir = File("native/native.tests/testData/CInterop/KT-55578/")
private val hint1 = "<<HINT1>>" private val hint1 = "<<HINT1>>"
@@ -166,7 +168,11 @@ class LinkerOutputTestKT55578 : AbstractNativeLinkerOutputTest() {
files += TestFile.createCommitted(file, this) files += TestFile.createCommitted(file, this)
} }
return compileToExecutable(module, libraries.asList().map { it.asLibraryDependency() }, extraArgs) return compileToExecutable(
module,
libraries.asList().map { it.asLibraryDependency() },
defaultCompilerArguments + extraArgs
)
} }
private fun compileKlib(defFile: File, sourceFile: File? = null, extraArgs: List<String> = emptyList()): KLIB { private fun compileKlib(defFile: File, sourceFile: File? = null, extraArgs: List<String> = emptyList()): KLIB {