Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing improvements #29

Merged
merged 7 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ jobs:
- name: Build
run: ./gradlew build

- name: Coverage Report
run: ./gradlew testCodeCoverageReport

- name: Sonar
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
11 changes: 10 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ plugins {
alias(libs.plugins.kotlin.multiplatform) apply false
alias(libs.plugins.sonarqube)
alias(libs.plugins.jreleaser)
id("jacoco-report-aggregation")
}

allprojects {
Expand All @@ -14,6 +15,10 @@ allprojects {
description = "Kotlin compiler plugin for generating object mappers"
}

dependencies {
jacocoAggregation(project(":compiler-plugin"))
}

val branch = ByteArrayOutputStream()

exec {
Expand All @@ -28,6 +33,10 @@ sonar {
property("sonar.host.url", "https://sonarcloud.io")
property("sonar.qualitygate.wait", "true")
property("sonar.branch.name", branch.toString(Charsets.UTF_8))
property("sonar.coverage.jacoco.xmlReportPaths", layout.buildDirectory
.file("reports/jacoco/testCodeCoverageReport/testCodeCoverageReport.xml")
.get().asFile.absolutePath
)
}
}

Expand Down Expand Up @@ -80,4 +89,4 @@ jreleaser {
}
}
}
}
}
21 changes: 17 additions & 4 deletions compiler-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
alias(libs.plugins.kotlin.jvm)
id("java-test-fixtures")
id("maven-publish")
id("jacoco")
}

dependencies {
compileOnly(libs.kotlin.compiler.embeddable)

implementation(project(":mappie-api"))

testFixturesImplementation(libs.classgraph)
testFixturesImplementation(libs.okio)

testImplementation(kotlin("reflect"))
testImplementation(kotlin("test"))
testImplementation(libs.kotlin.compiler.embeddable)
testImplementation(project(":mappie-api"))
}

java {
Expand All @@ -23,7 +30,11 @@ publishing {
publications {
create<MavenPublication>("kotlin") {
artifactId = "mappie-compiler-plugin"
from(components["java"])

from((components["java"] as AdhocComponentWithVariants).apply {
withVariantsFromConfiguration(configurations["testFixturesApiElements"]) { skip() }
withVariantsFromConfiguration(configurations["testFixturesRuntimeElements"]) { skip() }
})

pom {
name = "tech.mappie:compiler-plugin"
Expand Down Expand Up @@ -67,10 +78,12 @@ publishing {

tasks.withType<Test> {
useJUnitPlatform()
finalizedBy(tasks.jacocoTestReport)
}

tasks.withType<KotlinCompile>().configureEach {
compilerOptions.freeCompilerArgs.add(
"-opt-in=org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI"
compilerOptions.freeCompilerArgs.addAll(
"-opt-in=org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI",
"-opt-in=org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import org.jetbrains.kotlin.ir.IrFileEntry
import org.jetbrains.kotlin.ir.backend.js.utils.valueArguments
import org.jetbrains.kotlin.ir.builders.declarations.addValueParameter
import org.jetbrains.kotlin.ir.builders.declarations.buildFun
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.createExpressionBody
import org.jetbrains.kotlin.ir.declarations.impl.IrClassImpl
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.IrCallImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrFunctionExpressionImpl
Expand Down Expand Up @@ -140,7 +140,7 @@ private class MapperReferenceCollector : BaseVisitor<IrFunctionExpression, Unit>

return when (val name = expression.symbol.owner.name) {
getterName(ObjectMappie<*, *>::forList.name) -> {
val mapper = expression.symbol.owner.parent as IrClassImpl
val mapper = expression.symbol.owner.parent as IrClass

val function = mapper.functions
.filter { it.name == IDENTIFIER_MAP_LIST }
Expand All @@ -149,7 +149,7 @@ private class MapperReferenceCollector : BaseVisitor<IrFunctionExpression, Unit>
function.symbol.wrap(expression.dispatchReceiver!!)
}
getterName(ObjectMappie<*, *>::forSet.name) -> {
val mapper = expression.symbol.owner.parent as IrClassImpl
val mapper = expression.symbol.owner.parent as IrClass

val function = mapper.functions
.filter { it.name == IDENTIFIER_MAP_SET }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package tech.mappie.testing.enums

import org.junit.jupiter.api.io.TempDir
import tech.mappie.testing.compilation.KotlinCompilation
import tech.mappie.testing.compilation.KotlinCompilation.ExitCode
import tech.mappie.testing.compilation.SourceFile.Companion.kotlin
import tech.mappie.testing.loadEnumMappieClass
import java.io.File
import kotlin.test.Test
import kotlin.test.assertEquals

class TwoEnumsWithSameEntriesTest {

enum class Input { SOME, OTHER }
enum class Output { SOME, OTHER }

@TempDir
private lateinit var directory: File

@Test
fun `map two enums with the same entries should succeed`() {
KotlinCompilation(directory).apply {
sources = buildList {
add(
kotlin("Test.kt",
"""
import tech.mappie.api.EnumMappie
import tech.mappie.testing.enums.TwoEnumsWithSameEntriesTest.*

class Mapper : EnumMappie<Input, Output>()
"""
)
)
}
}.compile {
assertEquals(ExitCode.OK, exitCode)
assertEquals("", messages)

val mapper = classLoader
.loadEnumMappieClass<Input, Output>("Mapper")
.constructors
.first()
.call()

Input.entries.forEach { entry ->
assertEquals(Output.valueOf(entry.name), mapper.map(entry))
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package tech.mappie.testing

import tech.mappie.api.EnumMappie
import java.net.URLClassLoader
import kotlin.reflect.KClass

fun <FROM : Enum<*>, TO : Enum<*>> URLClassLoader.loadEnumMappieClass(name: String) =
loadClass(name).kotlin as KClass<EnumMappie<FROM, TO>>
Loading