diff --git a/burst-kotlin-plugin-tests/src/test/kotlin/app/cash/burst/kotlin/BurstKotlinPluginTest.kt b/burst-kotlin-plugin-tests/src/test/kotlin/app/cash/burst/kotlin/BurstKotlinPluginTest.kt index 02c4e07..79fb2f8 100644 --- a/burst-kotlin-plugin-tests/src/test/kotlin/app/cash/burst/kotlin/BurstKotlinPluginTest.kt +++ b/burst-kotlin-plugin-tests/src/test/kotlin/app/cash/burst/kotlin/BurstKotlinPluginTest.kt @@ -106,6 +106,33 @@ class BurstKotlinPluginTest { .contains("CoffeeTest.kt:7:12 Expected an enum for @Burst test parameter") } + @Test + fun unexpectedDefaultArgumentValue() { + val result = compile( + sourceFile = SourceFile.kotlin( + "CoffeeTest.kt", + """ + import app.cash.burst.Burst + import kotlin.test.Test + + val defaultEspresso = Espresso.Regular + + @Burst + class CoffeeTest { + @Test + fun test(espresso: Espresso = defaultEspresso) { + } + } + + enum class Espresso { Decaf, Regular, Double } + """, + ), + ) + assertEquals(KotlinCompilation.ExitCode.COMPILATION_ERROR, result.exitCode, result.messages) + assertThat(result.messages) + .contains("CoffeeTest.kt:9:12 @Burst default parameter must be an enum constant (or absent)") + } + @Test fun constructorParameters() { val result = compile( diff --git a/burst-kotlin-plugin/src/main/kotlin/app/cash/burst/kotlin/Argument.kt b/burst-kotlin-plugin/src/main/kotlin/app/cash/burst/kotlin/Argument.kt index 1913043..e143ca7 100644 --- a/burst-kotlin-plugin/src/main/kotlin/app/cash/burst/kotlin/Argument.kt +++ b/burst-kotlin-plugin/src/main/kotlin/app/cash/burst/kotlin/Argument.kt @@ -61,7 +61,18 @@ internal fun IrPluginContext.allPossibleArguments( val referenceClass = referenceClass(classId)?.owner ?: return null if (!referenceClass.isEnumClass) return null val enumEntries = referenceClass.declarations.filterIsInstance() - val defaultValueSymbol = (parameter.defaultValue?.expression as? IrGetEnumValue)?.symbol + + val defaultValueSymbol = parameter.defaultValue?.let { defaultValue -> + val expression = defaultValue.expression + if (expression !is IrGetEnumValue) { + throw BurstCompilationException( + "@Burst default parameter must be an enum constant (or absent)", + parameter, + ) + } + expression.symbol + } + return enumEntries.map { Argument( original = parameter, diff --git a/burst-kotlin-plugin/src/main/kotlin/app/cash/burst/kotlin/package.kt b/burst-kotlin-plugin/src/main/kotlin/app/cash/burst/kotlin/package.kt index 3e1f59f..5b756fc 100644 --- a/burst-kotlin-plugin/src/main/kotlin/app/cash/burst/kotlin/package.kt +++ b/burst-kotlin-plugin/src/main/kotlin/app/cash/burst/kotlin/package.kt @@ -1,6 +1,5 @@ package app.cash.burst.kotlin -import org.jetbrains.kotlin.name.CallableId import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name @@ -9,18 +8,6 @@ import org.jetbrains.kotlin.name.Name @JvmInline value class FqPackageName(val fqName: FqName) -fun FqPackageName(name: String): FqPackageName { - return FqPackageName(FqName(name)) -} +fun FqPackageName(name: String) = FqPackageName(FqName(name)) -fun FqPackageName.classId(name: String): ClassId { - return ClassId(fqName, Name.identifier(name)) -} - -fun FqPackageName.callableId(name: String): CallableId { - return CallableId(fqName, Name.identifier(name)) -} - -fun ClassId.callableId(name: String): CallableId { - return CallableId(this, Name.identifier(name)) -} +fun FqPackageName.classId(name: String) = ClassId(fqName, Name.identifier(name))