Native: add tests for ExperimentalForeignApi on cinterop declarations

Add tests checking that all (top-level) declarations generated by
cinterop now have ExperimentalForeignApi annotation.

^KT-58362
This commit is contained in:
Svyatoslav Scherbina
2023-07-19 15:41:32 +02:00
committed by Space Team
parent 3f3f6eb5a8
commit bcc4a891be
30 changed files with 345 additions and 0 deletions
@@ -0,0 +1,29 @@
package dependency {
@ExperimentalForeignApi enum class E1 private constructor(value: UInt) : Enum<E1>, CEnum {
@ConstantValue.UInt(value = 0.toUInt()) enum entry ONE
@ConstantValue.UInt(value = 1.toUInt()) enum entry TWO
@ConstantValue.UInt(value = 2.toUInt()) enum entry THREE
override val value: UInt
class Var constructor(rawPtr: NativePtr /* = NativePtr */) : CEnumVar {
var value: E1
@CEnumVarTypeSize(size = 4) @Deprecated(level = DeprecationLevel.WARNING, message = "Use sizeOf<T>() or alignOf<T>() instead.", replaceWith = ReplaceWith(expression = "", imports = {})) companion object : CPrimitiveVar.Type
}
companion object {
@Deprecated(level = DeprecationLevel.WARNING, message = "Will be removed.", replaceWith = ReplaceWith(expression = "", imports = {})) fun byValue(value: UInt): E1
}
}
}
package dependency {
@ExperimentalForeignApi typealias E2 = UInt
@ExperimentalForeignApi typealias E2Var = UIntVarOf<E2 /* = UInt */>
@ExperimentalForeignApi const val EIGHT: E2 /* = UInt */ = 8.toUInt()
@ExperimentalForeignApi const val FOUR: E2 /* = UInt */ = 4.toUInt()
}
@@ -0,0 +1 @@
headers = enum.h
@@ -0,0 +1,6 @@
package dependency {
@CCall(id = "knifunptr_dependency1_bar") @ExperimentalForeignApi external fun bar()
@CCall(id = "knifunptr_dependency0_foo") @ExperimentalForeignApi external fun foo()
}
@@ -0,0 +1 @@
headers = function.h
@@ -0,0 +1,11 @@
package dependency {
@ExperimentalForeignApi const val MACRO_CONST_GLOBAL: Int = 1
@ExperimentalForeignApi val MACRO_GLOBAL: Int
@CCall(id = "knifunptr_dependency3_MACRO_GLOBAL_getter") get
@CCall(id = "knifunptr_dependency0_getValue") @ExperimentalForeignApi external fun getValue(): Int
@ExperimentalForeignApi var global: Int
@CCall(id = "knifunptr_dependency1_global_getter") get
@CCall(id = "knifunptr_dependency2_global_setter") set
}
@@ -0,0 +1 @@
headers = global.h
@@ -0,0 +1,31 @@
package dependency {
@ExternalObjCClass @ExperimentalForeignApi open class MyClass : ObjCObjectBase {
protected constructor()
companion object : MyClassMeta, ObjCClassOf<MyClass>
}
@ExternalObjCClass @ExperimentalForeignApi open class MyClassMeta : ObjCObjectBaseMeta {
protected constructor()
}
}
package dependency {
@ObjCMethod(encoding = "v16@0:8", isStret = false, selector = "classMethod") @ExperimentalForeignApi external fun MyClassMeta.classMethod()
@ExperimentalForeignApi var MyClassMeta.classProperty: Any?
@ObjCMethod(encoding = "@16@0:8", isStret = false, selector = "classProperty") get
@ObjCMethod(encoding = "v24@0:8@16", isStret = false, selector = "setClassProperty:") set
@ObjCMethod(encoding = "@16@0:8", isStret = false, selector = "classProperty") @ExperimentalForeignApi external fun MyClassMeta.classProperty(): Any?
@ObjCFactory(encoding = "@16@0:8", isStret = false, selector = "init") @CCall.ConsumesReceiver @CCall.ReturnsRetained @ExperimentalForeignApi external fun <T : MyClass> ObjCClassOf<T>.create(): T?
@Deprecated(level = DeprecationLevel.ERROR, message = "Use factory method instead", replaceWith = ReplaceWith(expression = "MyClass.create()", imports = {})) @ObjCMethod(encoding = "@16@0:8", isStret = false, selector = "init") @CCall.ConsumesReceiver @CCall.ReturnsRetained @ExperimentalForeignApi external fun MyClass.init(): MyClass?
@ObjCMethod(encoding = "v16@0:8", isStret = false, selector = "instanceMethod") @ExperimentalForeignApi external fun MyClass.instanceMethod()
@ExperimentalForeignApi var MyClass.instanceProperty: Int
@ObjCMethod(encoding = "i16@0:8", isStret = false, selector = "instanceProperty") get
@ObjCMethod(encoding = "v20@0:8i16", isStret = false, selector = "setInstanceProperty:") set
@ObjCMethod(encoding = "i16@0:8", isStret = false, selector = "instanceProperty") @ExperimentalForeignApi external fun MyClass.instanceProperty(): Int
@ObjCMethod(encoding = "v24@0:8@16", isStret = false, selector = "setClassProperty:") @ExperimentalForeignApi external fun MyClassMeta.setClassProperty(classProperty: Any?)
@ObjCMethod(encoding = "v20@0:8i16", isStret = false, selector = "setInstanceProperty:") @ExperimentalForeignApi external fun MyClass.setInstanceProperty(instanceProperty: Int)
}
@@ -0,0 +1,2 @@
language = Objective-C
headers = objccategory.h
@@ -0,0 +1,27 @@
package dependency {
@ExternalObjCClass @ExperimentalForeignApi open class MyClass : ObjCObjectBase {
@ObjCConstructor(designated = true, initSelector = "init") constructor()
var instanceProperty: Int
@ObjCMethod(encoding = "i16@0:8", isStret = false, selector = "instanceProperty") get
@ObjCMethod(encoding = "v20@0:8i16", isStret = false, selector = "setInstanceProperty:") set
@Deprecated(level = DeprecationLevel.ERROR, message = "Use constructor instead", replaceWith = ReplaceWith(expression = "MyClass()", imports = {})) @ObjCMethod(encoding = "@16@0:8", isStret = false, selector = "init") @CCall.ConsumesReceiver @CCall.ReturnsRetained open external fun init(): MyClass?
@ObjCMethod(encoding = "v16@0:8", isStret = false, selector = "instanceMethod") open external fun instanceMethod()
@ObjCMethod(encoding = "i16@0:8", isStret = false, selector = "instanceProperty") open external fun instanceProperty(): Int
@ObjCMethod(encoding = "v20@0:8i16", isStret = false, selector = "setInstanceProperty:") open external fun setInstanceProperty(instanceProperty: Int)
companion object : MyClassMeta, ObjCClassOf<MyClass>
}
@ExternalObjCClass @ExperimentalForeignApi open class MyClassMeta : ObjCObjectBaseMeta {
protected constructor()
var classProperty: Any?
@ObjCMethod(encoding = "@16@0:8", isStret = false, selector = "classProperty") get
@ObjCMethod(encoding = "v24@0:8@16", isStret = false, selector = "setClassProperty:") set
@ObjCMethod(encoding = "v16@0:8", isStret = false, selector = "classMethod") open external fun classMethod()
@ObjCMethod(encoding = "@16@0:8", isStret = false, selector = "classProperty") open external fun classProperty(): Any?
@ObjCMethod(encoding = "v24@0:8@16", isStret = false, selector = "setClassProperty:") open external fun setClassProperty(classProperty: Any?)
}
}
@@ -0,0 +1,2 @@
language = Objective-C
headers = objcclass.h
@@ -0,0 +1,23 @@
package dependency {
@ExternalObjCClass(protocolGetter = "kniprot_dependency0_MyProtocol") @ExperimentalForeignApi interface MyProtocolProtocol : ObjCObject {
var instanceProperty: Any?
@ObjCMethod(encoding = "@16@0:8", isStret = false, selector = "instanceProperty") get
@ObjCMethod(encoding = "v24@0:8@16", isStret = false, selector = "setInstanceProperty:") set
@ObjCMethod(encoding = "@16@0:8", isStret = false, selector = "instanceMethod") fun instanceMethod(): Any?
@ObjCMethod(encoding = "@16@0:8", isStret = false, selector = "instanceProperty") fun instanceProperty(): Any?
@ObjCMethod(encoding = "v24@0:8@16", isStret = false, selector = "setInstanceProperty:") fun setInstanceProperty(instanceProperty: Any?)
}
@ExternalObjCClass(protocolGetter = "kniprot_dependency0_MyProtocol") @ExperimentalForeignApi interface MyProtocolProtocolMeta : ObjCObjectMeta /* = ObjCClass */ {
var classProperty: Int
@ObjCMethod(encoding = "i16@0:8", isStret = false, selector = "classProperty") get
@ObjCMethod(encoding = "v20@0:8i16", isStret = false, selector = "setClassProperty:") set
@ObjCMethod(encoding = "i16@0:8", isStret = false, selector = "classMethod") fun classMethod(): Int
@ObjCMethod(encoding = "i16@0:8", isStret = false, selector = "classProperty") fun classProperty(): Int
@ObjCMethod(encoding = "v20@0:8i16", isStret = false, selector = "setClassProperty:") fun setClassProperty(classProperty: Int)
}
}
@@ -0,0 +1,2 @@
language = Objective-C
headers = objcprotocol.h
@@ -0,0 +1,21 @@
package dependency {
@ExternalObjCClass @ExperimentalForeignApi open class MyClass : ObjCObjectBase {
protected constructor()
companion object : MyClassMeta, ObjCClassOf<MyClass>
}
@ExternalObjCClass @ExperimentalForeignApi open class MyClassMeta : ObjCObjectBaseMeta {
protected constructor()
}
}
package dependency {
@ExperimentalForeignApi typealias MyClassRef = MyClass?
@ExperimentalForeignApi typealias MyClassRefVar = ObjCObjectVar<MyClassRef? /* = MyClass? */>
@ExperimentalForeignApi typealias MyId = Any?
@ExperimentalForeignApi typealias MyIdVar = ObjCObjectVar<MyId? /* = Any? */>
}
@@ -0,0 +1,2 @@
language = Objective-C
headers = objctypedef.h
@@ -0,0 +1,12 @@
package dependency {
@CStruct(spelling = "struct { int x; }") @ExperimentalForeignApi class Foo constructor(rawPtr: NativePtr /* = NativePtr */) : CStructVar {
var x: Int
@CStruct.MemberAt(offset = 0.toLong()) get
@CStruct.MemberAt(offset = 0.toLong()) set
@CStruct.VarType(align = 4, size = 4.toLong()) @Deprecated(level = DeprecationLevel.WARNING, message = "Use sizeOf<T>() or alignOf<T>() instead.", replaceWith = ReplaceWith(expression = "", imports = {})) companion object : CStructVar.Type
}
}
@@ -0,0 +1 @@
headers = struct.h
@@ -0,0 +1,18 @@
package dependency {
@CStruct(spelling = "struct { }") @ExperimentalForeignApi class T constructor(rawPtr: NativePtr /* = NativePtr */) : CStructVar {
@CStruct.VarType(align = 1, size = 0.toLong()) @Deprecated(level = DeprecationLevel.WARNING, message = "Use sizeOf<T>() or alignOf<T>() instead.", replaceWith = ReplaceWith(expression = "", imports = {})) companion object : CStructVar.Type
}
}
package dependency {
@ExperimentalForeignApi typealias MyInt = Int
@ExperimentalForeignApi typealias MyIntVar = IntVarOf<MyInt /* = Int */>
@ExperimentalForeignApi typealias MyPointer = COpaquePointer
@ExperimentalForeignApi typealias MyPointerVar = CPointerVarOf<MyPointer /* = CPointer<out CPointed> */>
@ExperimentalForeignApi typealias MyS = S
@ExperimentalForeignApi typealias MyT = T
}
@@ -0,0 +1 @@
headers = typedef.h
@@ -0,0 +1,8 @@
enum E1 {
ONE, TWO, THREE
};
enum E2 {
FOUR = 4,
EIGHT = 8
};
@@ -0,0 +1,2 @@
void foo(void);
void bar() {}
@@ -0,0 +1,6 @@
#define MACRO_CONST_GLOBAL 1
int getValue(void);
#define MACRO_GLOBAL getValue()
extern int global;
@@ -0,0 +1,10 @@
@interface MyClass
@end
@interface MyClass (MyClassCategory)
-(instancetype) init;
-(void) instanceMethod;
+(void) classMethod;
@property int instanceProperty;
@property (class) id classProperty;
@end
@@ -0,0 +1,7 @@
@interface MyClass
-(instancetype) init;
-(void) instanceMethod;
+(void) classMethod;
@property int instanceProperty;
@property (class) id classProperty;
@end
@@ -0,0 +1,6 @@
@protocol MyProtocol
-(id) instanceMethod;
+(int) classMethod;
@property id instanceProperty;
@property (class) int classProperty;
@end
@@ -0,0 +1,6 @@
@interface MyClass
@end
typedef MyClass MyClass2;
typedef MyClass* MyClassRef;
typedef id MyId;
@@ -0,0 +1,3 @@
struct Foo {
int x;
};
@@ -0,0 +1,8 @@
typedef int MyInt;
typedef void* MyPointer;
struct S;
struct T {};
typedef struct S MyS;
typedef struct T MyT;
@@ -0,0 +1,80 @@
/*
* 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.
*/
package org.jetbrains.kotlin.konan.blackboxtest;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.util.KtTestUtil;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.GenerateNativeTestsKt}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("native/native.tests/testData/CInterop/experimental/cases")
@TestDataPath("$PROJECT_ROOT")
public class CInteropExperimentalTestGenerated extends AbstractNativeCInteropExperimentalTest {
@Test
public void testAllFilesPresentInCases() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("native/native.tests/testData/CInterop/experimental/cases"), Pattern.compile("^([^_](.+))$"), null, false);
}
@Test
@TestMetadata("enum")
public void testEnum() throws Exception {
runTest("native/native.tests/testData/CInterop/experimental/cases/enum/");
}
@Test
@TestMetadata("function")
public void testFunction() throws Exception {
runTest("native/native.tests/testData/CInterop/experimental/cases/function/");
}
@Test
@TestMetadata("global")
public void testGlobal() throws Exception {
runTest("native/native.tests/testData/CInterop/experimental/cases/global/");
}
@Test
@TestMetadata("objccategory")
public void testObjccategory() throws Exception {
runTest("native/native.tests/testData/CInterop/experimental/cases/objccategory/");
}
@Test
@TestMetadata("objcclass")
public void testObjcclass() throws Exception {
runTest("native/native.tests/testData/CInterop/experimental/cases/objcclass/");
}
@Test
@TestMetadata("objcprotocol")
public void testObjcprotocol() throws Exception {
runTest("native/native.tests/testData/CInterop/experimental/cases/objcprotocol/");
}
@Test
@TestMetadata("objctypedef")
public void testObjctypedef() throws Exception {
runTest("native/native.tests/testData/CInterop/experimental/cases/objctypedef/");
}
@Test
@TestMetadata("struct")
public void testStruct() throws Exception {
runTest("native/native.tests/testData/CInterop/experimental/cases/struct/");
}
@Test
@TestMetadata("typedef")
public void testTypedef() throws Exception {
runTest("native/native.tests/testData/CInterop/experimental/cases/typedef/");
}
}
@@ -168,6 +168,12 @@ fun main() {
) {
model("CInterop/frameworkIncludeCategories/cases", pattern = "^([^_](.+))$", recursive = false)
}
testClass<AbstractNativeCInteropExperimentalTest>(
suiteTestClassName = "CInteropExperimentalTestGenerated"
) {
model("CInterop/experimental/cases", pattern = "^([^_](.+))$", recursive = false)
}
}
// ObjCExport tests.
@@ -43,6 +43,18 @@ abstract class AbstractNativeCInteropIncludeCategoriesTest : AbstractNativeCInte
get() = "dependency.def"
}
// This test checks that cinterop-generated declarations have an experimental annotation.
abstract class AbstractNativeCInteropExperimentalTest : AbstractNativeCInteropTest() {
override val fmodules: Boolean
get() = false
override val defFileName: String
get() = "dependency.def"
override val ignoreExperimentalForeignApi: Boolean
get() = false
}
@Tag("cinterop")
abstract class AbstractNativeCInteropTest : AbstractNativeCInteropBaseTest() {
abstract val fmodules: Boolean