diff --git a/fixtures-processor/src/main/kotlin/com/theblueground/fixtures/ProcessedParameterMapper.kt b/fixtures-processor/src/main/kotlin/com/theblueground/fixtures/ProcessedParameterMapper.kt index 54c9439..12cf410 100644 --- a/fixtures-processor/src/main/kotlin/com/theblueground/fixtures/ProcessedParameterMapper.kt +++ b/fixtures-processor/src/main/kotlin/com/theblueground/fixtures/ProcessedParameterMapper.kt @@ -3,6 +3,7 @@ package com.theblueground.fixtures import com.google.devtools.ksp.symbol.KSClassDeclaration import com.google.devtools.ksp.symbol.KSDeclaration import com.google.devtools.ksp.symbol.KSType +import com.google.devtools.ksp.symbol.KSTypeAlias import com.google.devtools.ksp.symbol.KSValueParameter import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy import com.squareup.kotlinpoet.TypeName @@ -20,19 +21,18 @@ internal class ProcessedParameterMapper( fun mapParameter(parameterValue: KSValueParameter): ProcessedFixtureParameter { val resolvedType = parameterValue.type.resolve() - return mapParameter( - parameterValue = parameterValue, - parameterType = resolvedType, - parameterClassDeclaration = (resolvedType.declaration as KSClassDeclaration), - ) + return when (val declaration = resolvedType.declaration) { + is KSTypeAlias -> mapParameter(parameterValue, declaration.type.resolve()) + else -> mapParameter(parameterValue, resolvedType) + } } private fun mapParameter( parameterValue: KSValueParameter, parameterType: KSType, - parameterClassDeclaration: KSClassDeclaration, ): ProcessedFixtureParameter { val name = parameterValue.name!!.asString() + val parameterClassDeclaration = parameterType.declaration as KSClassDeclaration return when { parameterType.hasFixtureAdapter(processedFixtureAdapters) -> mapFixtureAdapterParameter( diff --git a/fixtures-processor/src/test/kotlin/com/theblueground/fixtures/FixtureProcessorTest.kt b/fixtures-processor/src/test/kotlin/com/theblueground/fixtures/FixtureProcessorTest.kt index a3410e7..d342b84 100644 --- a/fixtures-processor/src/test/kotlin/com/theblueground/fixtures/FixtureProcessorTest.kt +++ b/fixtures-processor/src/test/kotlin/com/theblueground/fixtures/FixtureProcessorTest.kt @@ -271,6 +271,51 @@ class FixtureProcessorTest : KSPTest() { assertThat(secondTimeGeneratedContent).doesNotMatch(firstTimeGeneratedContent) } + @Test + fun `should generate a builder function with resolved type for typealias`() { + // Given + val fixtureSource = """ + package $packageName + + import com.theblueground.fixtures.Fixture + + import java.math.BigDecimal + + @Fixture + data class $fixtureName( + val bigDecimalAliasValue: BigDecimalAlias, + ) + + typealias BigDecimalAlias = BigDecimal + """.trimIndent() + val fixtureFile = kotlin(name = "$fixtureName.kt", contents = fixtureSource) + + // When + val result = compile( + arguments = mapOf("fixtures.run" to "true"), + sourceFiles = listOf(fixtureFile), + ) + val generatedContent = getGeneratedContent( + packageName = packageName, + filename = "${fixtureName}Fixture.kt", + ) + + // Then + assertThat(result.exitCode).isEqualTo(KotlinCompilation.ExitCode.OK) + val expected = """ + package $packageName + + import java.math.BigDecimal + + public fun create$fixtureName(bigDecimalAliasValue: BigDecimal = BigDecimal.ZERO): TestClass = + $packageName.$fixtureName( + bigDecimalAliasValue = bigDecimalAliasValue + ) + + """.trimIndent() + assertThat(generatedContent).isEqualTo(expected) + } + @Test fun `should not generate a builder function while not running tests`() { // Given