Skip to content

Commit

Permalink
Fixed bug with wrong map function to transform selection
Browse files Browse the repository at this point in the history
  • Loading branch information
stefankoppier committed Jul 5, 2024
1 parent 8e7573b commit a3ee84b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ import org.jetbrains.kotlin.ir.builders.*
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrFunctionAccessExpression
import org.jetbrains.kotlin.ir.expressions.IrPropertyReference
import org.jetbrains.kotlin.ir.types.IrSimpleType
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.classOrFail
import org.jetbrains.kotlin.ir.types.typeOrFail
import org.jetbrains.kotlin.ir.util.*
import tech.mappie.MappieIrRegistrar
import tech.mappie.resolving.enums.ExplicitEnumMappingTarget
Expand All @@ -34,7 +29,7 @@ class MappieIrTransformer(private val symbols: List<MappieDefinition>) : IrEleme
if (declaration.accept(ShouldTransformCollector(), Unit)) {
var function = declaration.declarations
.filterIsInstance<IrSimpleFunction>()
.first { it.name == IDENTIFIER_MAP }
.first { it.name == IDENTIFIER_MAP && it.overriddenSymbols.isNotEmpty() }

if (function.isFakeOverride) {
declaration.declarations.removeIf { it is IrSimpleFunction && function.name == IDENTIFIER_MAP }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package tech.mappie.testing

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
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 java.io.File

class MapperClassCanMultipleMapFunctionsTest {

data class Input(val text: String)
data class Output(val text: String, val int: Int)

@TempDir
private lateinit var directory: File

@Test
fun `mapper with multiple map functions should succeed`() {
KotlinCompilation(directory).apply {
sources = buildList {
add(
kotlin("Test.kt",
"""
import tech.mappie.api.ObjectMappie
import tech.mappie.testing.MapperClassCanMultipleMapFunctionsTest.*
class Mapper(private val int: Int) : ObjectMappie<Input, Output>() {
fun map(value: String) = value
override fun map(from: Input) = mapping {
Output::int fromValue int
}
fun map(value: Int) = int
}
"""
)
)
}
}.compile {
assertThat(exitCode).isEqualTo(ExitCode.OK)
assertThat(messages).isEmpty()

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

assertThat(mapper.map(Input("test")))
.isEqualTo(Output("test", 10))
}
}

}

0 comments on commit a3ee84b

Please sign in to comment.