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

Validate default parameter values #39

Merged
merged 1 commit into from
Oct 24, 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
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IrEnumEntry>()
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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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))