Skip to content

Commit

Permalink
Migrated compatibility tests to gradle-plugin (#114)
Browse files Browse the repository at this point in the history
* Migrated tests from separate projects to the integration tests of gradle-plugin
  • Loading branch information
stefankoppier authored Oct 25, 2024
1 parent be9d2b5 commit 82ac120
Show file tree
Hide file tree
Showing 59 changed files with 617 additions and 1,750 deletions.
45 changes: 0 additions & 45 deletions .github/workflows/compatibility-test.yml

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ build/

#Kotlin
.kotlin
kotlin-js-store

### IntelliJ IDEA ###
.idea
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package tech.mappie.resolving

import tech.mappie.api.Mappie
import tech.mappie.util.IDENTIFIER_MAPPING
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.expressions.IrBlockBody
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrReturn
import tech.mappie.api.Mappie
import tech.mappie.util.BaseVisitor
import tech.mappie.util.IDENTIFIER_MAPPING
import tech.mappie.util.isMappieMapFunction
import tech.mappie.util.isSubclassOf

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ class ClassMappingRequestBuilder(private val constructor: IrConstructor, private
} else {
when (source) {
is ImplicitPropertyMappingSource -> source.copy(transformation = transformation(source, target))
else -> throw MappiePanicException("Only ImplicitPropertyMappingSource should occur when resolving a transformation.")
is FunctionMappingSource -> source.copy(transformation = transformation(source, target))
else -> throw MappiePanicException("Only ImplicitPropertyMappingSource should occur when resolving a transformation. Got ${source::class}")
}
}
}.ifEmpty {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ data class FunctionMappingSource(
val function: IrFunction,
val parameter: Name,
val parameterType: IrType,
val transformation: PropertyMappingTransformation?,
) : ImplicitClassMappingSource {
override val type = function.returnType
override val type = type(function.returnType, transformation)
}

data class ExplicitPropertyMappingSource(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ImplicitClassMappingSourcesCollector : BaseVisitor<Map<Name, ImplicitClass
override fun visitFunction(declaration: IrFunction, data: Pair<Name, IrType>): Map<Name, ImplicitClassMappingSource> =
if (declaration.isJavaLikeGetter()) {
val name = Name.identifier(declaration.name.asString().removePrefix("get").replaceFirstChar { it.lowercaseChar() })
mapOf(name to FunctionMappingSource(declaration, data.first, data.second))
mapOf(name to FunctionMappingSource(declaration, data.first, data.second, null))
} else {
emptyMap()
}
Expand Down
4 changes: 1 addition & 3 deletions gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ plugins {
}

dependencies {
compileOnly(libs.kotlin.gradle.plugin.api)

implementation(libs.kotlin.gradle.plugin)

testImplementation(kotlin("test"))
testImplementation(gradleTestKit())
testImplementation(libs.assertj.core)
}

Expand Down Expand Up @@ -43,6 +40,7 @@ tasks.compileKotlin {
tasks.test {
useJUnitPlatform()

dependsOn(project.tasks.publishToMavenLocal)
dependsOn(project(":compiler-plugin").tasks.publishToMavenLocal)
dependsOn(project(":mappie-api").tasks.named("publishKotlinMultiplatformPublicationToMavenLocal"))
dependsOn(project(":mappie-api").tasks.named("publishJvmPublicationToMavenLocal"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@file:Suppress("unused")

package tech.mappie

import org.gradle.api.Project
Expand Down Expand Up @@ -73,7 +71,6 @@ class MappieGradlePlugin : KotlinCompilerPluginSupportPlugin {
}

private companion object {
private const val KOTLIN_GRADLE_PLUGIN_NAME = "kotlin-gradle-plugin"
private val SUPPORTED_KOTLIN_VERSIONS = listOf(
Regex("1\\.9\\.[0-9]+(-.+)?"), // Versions 1.9.y
Regex("2\\.[0-9]+\\.[0-9]+(-.+)?"), // Versions 2.x.y
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package tech.mappie.testing.configuration
package tech.mappie.testing

import tech.mappie.testing.TestBase
import org.junit.jupiter.api.Test

class IntegrationTest : TestBase() {
Expand Down
30 changes: 27 additions & 3 deletions gradle-plugin/src/test/kotlin/tech/mappie/testing/TestBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,37 @@ abstract class TestBase {

protected lateinit var runner: GradleRunner

protected open val kotlinVersion = "2.0.21"

@BeforeEach
fun setup() {
runner = GradleRunner.create().apply {
forwardOutput()
withPluginClasspath()
withProjectDir(directory)
}

directory.resolve("src/main/kotlin").mkdirs()
directory.resolve("src/main/java").mkdirs()
directory.resolve("src/test/kotlin").mkdirs()

println("Using kotlin version $kotlinVersion")

kotlin("settings.gradle.kts",
"""
pluginManagement {
repositories {
mavenLocal()
gradlePluginPortal()
}
}
""".trimIndent()
)

kotlin("build.gradle.kts",
"""
plugins {
id("org.jetbrains.kotlin.jvm") version "2.0.20"
id("tech.mappie.plugin")
id("org.jetbrains.kotlin.jvm") version "$kotlinVersion"
id("tech.mappie.plugin") version "$version"
}
dependencies {
Expand All @@ -45,6 +60,9 @@ abstract class TestBase {
tasks.test {
useJUnitPlatform()
testLogging {
exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
}
}
""".trimIndent()
Expand All @@ -57,6 +75,12 @@ abstract class TestBase {
}
}

protected fun java(file: String, @Language("java") code: String) {
directory.resolve(file).apply {
appendText(code)
}
}

companion object {
private val version = javaClass.classLoader.getResourceAsStream("version.properties").use {
Properties().apply { load(it) }.getProperty("version")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package tech.mappie.testing.compatibility.java

import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import tech.mappie.testing.TestBase

class JavaClassWithConstructorCompatibilityTest : TestBase() {

@BeforeEach
fun setUp() {
java("src/main/java/JavaClass.java",
"""
public class JavaClass {
private String value;
public JavaClass(String value) {
this.value = value;
}
public String getValue() {
return this.value;
}
@Override
public boolean equals(Object obj) {
if (obj == null) return false;
if (obj.getClass() != this.getClass()) return false;
return ((JavaClass) obj).value.equals(this.value);
}
}
""".trimIndent()
)

kotlin("src/main/kotlin/KotlinClass.kt",
"""
data class KotlinClass(val value: String)
""".trimIndent()
)
}

@Test
fun `map Java class to Kotlin class via getter`() {
kotlin("src/main/kotlin/Mapper.kt",
"""
import tech.mappie.api.ObjectMappie
object Mapper : ObjectMappie<JavaClass, KotlinClass>() {
override fun map(from: JavaClass) = mapping {
to::value fromValue from.value!!
}
}
""".trimIndent()
)

kotlin("src/test/kotlin/MapperTest.kt",
"""
import kotlin.test.*
class MapperTest {
@Test
fun test() {
assertEquals(
KotlinClass("value"),
Mapper.map(JavaClass("value"))
)
}
}
""".trimIndent()
)

runner.withArguments("build").build()
}

@Test
fun `map Kotlin class to Java class via getter`() {
kotlin("src/main/kotlin/Mapper.kt",
"""
import tech.mappie.api.ObjectMappie
object Mapper : ObjectMappie<KotlinClass, JavaClass>()
""".trimIndent()
)

kotlin("src/test/kotlin/MapperTest.kt",
"""
import kotlin.test.*
class MapperTest {
@Test
fun test() {
assertEquals(
JavaClass("value"),
Mapper.map(KotlinClass("value"))
)
}
}
""".trimIndent()
)

runner.withArguments("build").build()
}
}
Loading

0 comments on commit 82ac120

Please sign in to comment.