Skip to content

Commit 87b28e3

Browse files
committed
[cinterop] Add integration test for cinterop modules.
1 parent b9e6693 commit 87b28e3

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

gradle/libs.versions.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,19 @@ kotlin = "2.0.0"
33

44
gradlePublishPlugin = "1.2.1"
55

6+
junit-jupiter = "5.8.0"
7+
8+
kotest = "5.9.1"
9+
610
[libraries]
711

812
plugin-kotlin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
913

14+
test-junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }
15+
test-junit-jupiter-launcher = { module = "org.junit.jupiter:junit-jupiter-engine" }
16+
test-kotest-assertions = { module = "io.kotest:kotest-assertions-core", version.ref = "kotest" }
17+
18+
1019
[plugins]
1120
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
1221
gradle-publish = { id = "com.gradle.plugin-publish", version.ref = "gradlePublishPlugin" }

plugin/build.gradle.kts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ plugins {
77
dependencies {
88
implementation(gradleApi())
99
implementation(libs.plugin.kotlin)
10+
11+
testImplementation(gradleTestKit())
12+
testImplementation(libs.test.junit.jupiter)
13+
testImplementation(libs.test.kotest.assertions)
14+
testRuntimeOnly(libs.test.junit.jupiter.launcher)
15+
}
16+
17+
tasks.named<Test>("test") {
18+
useJUnitPlatform()
1019
}
1120

1221
version = "0.5.1"
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package io.github.ttypic.swiftklib.gradle
2+
3+
import io.kotest.matchers.nulls.shouldNotBeNull
4+
import io.kotest.matchers.shouldBe
5+
import org.gradle.testkit.runner.BuildResult
6+
import org.gradle.testkit.runner.GradleRunner
7+
import org.gradle.testkit.runner.TaskOutcome
8+
import org.intellij.lang.annotations.Language
9+
import org.junit.jupiter.api.BeforeEach
10+
import org.junit.jupiter.api.Test
11+
import org.junit.jupiter.api.io.TempDir
12+
import java.io.File
13+
14+
class CinteropModulesTest {
15+
@TempDir
16+
lateinit var testProjectDir: File
17+
private lateinit var settingsFile: File
18+
private lateinit var buildFile: File
19+
private lateinit var swiftLocation: File
20+
private lateinit var swiftCodeFile: File
21+
private lateinit var kotlinLocation: File
22+
private lateinit var kotlinCodeFile: File
23+
private lateinit var gradlePropertiesFile: File
24+
25+
@BeforeEach
26+
fun setup() {
27+
settingsFile = File(testProjectDir, "settings.gradle.kts")
28+
buildFile = File(testProjectDir, "build.gradle.kts")
29+
swiftLocation = File(testProjectDir, "swift")
30+
swiftCodeFile = File(swiftLocation, "test.swift")
31+
kotlinLocation = File(testProjectDir, "src/commonMain/kotlin/test")
32+
kotlinCodeFile = File(kotlinLocation, "Test.kt")
33+
gradlePropertiesFile = File(testProjectDir, "gradle.properties")
34+
}
35+
36+
@Test
37+
fun `build with imported UIKit framework is successful`() {
38+
testBuild(
39+
swiftCode = """
40+
import UIKit
41+
42+
@objc public class TestView: UIView {}
43+
""".trimIndent(),
44+
kotlinCode = """
45+
import test.TestView
46+
47+
val view = TestView()
48+
""".trimIndent(),
49+
) {
50+
task(":build")
51+
.shouldNotBeNull()
52+
.outcome.shouldBe(TaskOutcome.SUCCESS)
53+
}
54+
}
55+
56+
private fun testBuild(
57+
@Language("swift")
58+
swiftCode: String,
59+
@Language("kotlin")
60+
kotlinCode: String = "",
61+
swiftklibName: String = "test",
62+
swiftklibPackage: String = "test",
63+
asserter: BuildResult.() -> Unit,
64+
) {
65+
gradlePropertiesFile.writeText(
66+
"""
67+
kotlin.mpp.enableCInteropCommonization=true
68+
""".trimIndent()
69+
)
70+
@Language("kotlin")
71+
val settingsKts = """
72+
pluginManagement {
73+
includeBuild("..")
74+
}
75+
76+
dependencyResolutionManagement {
77+
repositories {
78+
mavenCentral()
79+
}
80+
}
81+
""".trimIndent()
82+
settingsFile.writeText(settingsKts)
83+
84+
@Language("kotlin")
85+
val buildKts = """
86+
plugins {
87+
embeddedKotlin("multiplatform")
88+
id("io.github.ttypic.swiftklib")
89+
}
90+
91+
kotlin {
92+
compilerOptions {
93+
optIn.addAll(
94+
"kotlinx.cinterop.ExperimentalForeignApi",
95+
)
96+
}
97+
98+
listOf(
99+
iosX64(),
100+
iosArm64(),
101+
iosSimulatorArm64(),
102+
).forEach {
103+
it.compilations {
104+
val main by getting {
105+
cinterops.create("$swiftklibName")
106+
}
107+
}
108+
}
109+
}
110+
111+
swiftklib {
112+
create("$swiftklibName") {
113+
path = file("${swiftLocation.absolutePath}")
114+
packageName("$swiftklibPackage")
115+
}
116+
}
117+
""".trimIndent()
118+
buildFile.writeText(buildKts)
119+
120+
swiftLocation.mkdirs()
121+
kotlinLocation.mkdirs()
122+
123+
swiftCodeFile.writeText(swiftCode)
124+
kotlinCodeFile.writeText(kotlinCode)
125+
126+
GradleRunner.create()
127+
.withProjectDir(testProjectDir)
128+
.withArguments("build")
129+
.withPluginClasspath()
130+
.build()
131+
.asserter()
132+
}
133+
}

0 commit comments

Comments
 (0)