|
| 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