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

Support default arguments #36

Merged
merged 1 commit into from
Oct 23, 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
## [Unreleased]
[Unreleased]: https://github.com/cashapp/burst/compare/0.5.0...HEAD

**Added**

* New: Use default parameter values to configure which specialization runs in the IDE.


## [0.5.0] *(2024-10-17)*
[0.5.0]: https://github.com/cashapp/burst/releases/tag/0.4.0
Expand Down
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,20 @@ Annotate your test class with `@Burst`, and accept an enum as a constructor para
```kotlin
@Burst
class DrinkSodaTest(
val soda: Soda,
val soda: Soda = Soda.Pepsi,
) {
...
}
```

Burst will specialize the test class for each value in the enum.
Burst will specialize the test class for each value in the enum. If you specified a default value
for the parameter, it'll be used when you run the test in the IDE.

Burst can also specialize individual test functions:

```kotlin
@Test
fun drinkFavoriteSodas(soda: Soda) {
fun drinkFavoriteSodas(soda: Soda = Soda.Pepsi) {
...
}
```
Expand All @@ -68,7 +69,10 @@ Use multiple enums for the combination of their variations.

```kotlin
@Test
fun collectSodas(soda: Soda, collectionsFactory: CollectionFactory) {
fun collectSodas(
soda: Soda = Soda.Pepsi,
collectionsFactory: CollectionFactory = CollectionFactory.MutableSetOf,
) {
...
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@
package app.cash.burst.gradle

import assertk.assertThat
import assertk.assertions.contains
import assertk.assertions.containsExactlyInAnyOrder
import assertk.assertions.isEmpty
import assertk.assertions.isEqualTo
import assertk.assertions.isFalse
import assertk.assertions.isIn
import assertk.assertions.isTrue
import java.io.File
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
Expand Down Expand Up @@ -70,59 +67,17 @@ class BurstGradlePluginTest {

val testResults = projectDir.resolve("lib/build/test-results")

// The original test class runs the default specialization.
with(readTestSuite(testResults.resolve("$testTaskName/TEST-CoffeeTest.xml"))) {
assertThat(testCases.map { it.name }).containsExactlyInAnyOrder(
"test[$platformName]",
"test_Milk[$platformName]",
"test_None[$platformName]",
"test_Oat[$platformName]",
)

val defaultFunction = testCases.single { it.name == "test[$platformName]" }
assertThat(defaultFunction.skipped).isFalse()

val defaultSpecialization = testCases.single { it.name == "test_None[$platformName]" }
assertThat(defaultSpecialization.skipped).isTrue()

val sampleSpecialization = testCases.single { it.name == "test_Milk[$platformName]" }
assertThat(sampleSpecialization.skipped).isFalse()
}

// The default test class is completely skipped.
with(readTestSuite(testResults.resolve("$testTaskName/TEST-CoffeeTest_Decaf.xml"))) {
assertThat(testCases.map { it.name }).containsExactlyInAnyOrder(
"test[$platformName]",
"test_Milk[$platformName]",
"test_None[$platformName]",
"test_Oat[$platformName]",
)
// There's no default specialization.
assertThat(testResults.resolve("$testTaskName/TEST-CoffeeTest.xml").exists()).isFalse()

val defaultFunction = testCases.single { it.name == "test[$platformName]" }
assertThat(defaultFunction.skipped).isTrue()

val defaultSpecialization = testCases.single { it.name == "test_None[$platformName]" }
assertThat(defaultSpecialization.skipped).isTrue()

val sampleSpecialization = testCases.single { it.name == "test_Milk[$platformName]" }
assertThat(sampleSpecialization.skipped).isTrue()
}

// Another test class is executed normally with nothing skipped.
// Each test class is executed normally with nothing skipped.
with(readTestSuite(testResults.resolve("$testTaskName/TEST-CoffeeTest_Regular.xml"))) {
assertThat(testCases.map { it.name }).containsExactlyInAnyOrder(
"test[$platformName]",
"test_Milk[$platformName]",
"test_None[$platformName]",
"test_Oat[$platformName]",
)

val defaultFunction = testCases.single { it.name == "test[$platformName]" }
assertThat(defaultFunction.skipped).isFalse()

val defaultSpecialization = testCases.single { it.name == "test_None[$platformName]" }
assertThat(defaultSpecialization.skipped).isTrue()

val sampleSpecialization = testCases.single { it.name == "test_Milk[$platformName]" }
assertThat(sampleSpecialization.skipped).isFalse()
}
Expand All @@ -142,7 +97,6 @@ class BurstGradlePluginTest {
val testSuite = readTestSuite(testXmlFile)

assertThat(testSuite.testCases.map { it.name }).containsExactlyInAnyOrder(
"test",
"test_Decaf_Milk",
"test_Decaf_None",
"test_Decaf_Oat",
Expand All @@ -154,12 +108,6 @@ class BurstGradlePluginTest {
"test_Regular_Oat",
)

val originalTest = testSuite.testCases.single { it.name == "test" }
assertThat(originalTest.skipped).isFalse()

val defaultSpecialization = testSuite.testCases.single { it.name == "test_Decaf_None" }
assertThat(defaultSpecialization.skipped).isTrue()

val sampleSpecialization = testSuite.testCases.single { it.name == "test_Regular_Milk" }
assertThat(sampleSpecialization.skipped).isFalse()
}
Expand All @@ -174,22 +122,8 @@ class BurstGradlePluginTest {

val testResults = projectDir.resolve("lib/build/test-results")

val coffeeTest = readTestSuite(testResults.resolve("test/TEST-CoffeeTest.xml"))
val coffeeTestTest = coffeeTest.testCases.single()
assertThat(coffeeTestTest.name).isEqualTo("test")
assertThat(coffeeTest.systemOut).isEqualTo(
"""
|set up Decaf None
|running Decaf None
|
""".trimMargin(),
)

val defaultTest = readTestSuite(testResults.resolve("test/TEST-CoffeeTest_Decaf_None.xml"))
val defaultTestTest = defaultTest.testCases.single()
assertThat(defaultTestTest.name).isEqualTo("test")
assertThat(defaultTestTest.skipped).isTrue()
assertThat(defaultTest.systemOut).isEmpty()
// There's no default specialization.
assertThat(testResults.resolve("test/TEST-CoffeeTest.xml").exists()).isFalse()

val sampleTest = readTestSuite(testResults.resolve("test/TEST-CoffeeTest_Regular_Milk.xml"))
val sampleTestTest = sampleTest.testCases.single()
Expand All @@ -215,6 +149,49 @@ class BurstGradlePluginTest {
assertThat(result.task(androidTestTaskName)!!.outcome).isIn(*SUCCESS_OUTCOMES)
}

@Test
fun defaultArguments() {
val projectDir = File("src/test/projects/defaultArguments")

val result = createRunner(projectDir, "clean", ":lib:test").build()
assertThat(result.task(":lib:test")!!.outcome).isIn(*SUCCESS_OUTCOMES)

val testResults = projectDir.resolve("lib/build/test-results")

// The original test class runs the default specialization.
with(readTestSuite(testResults.resolve("test/TEST-CoffeeTest.xml"))) {
assertThat(testCases.map { it.name }).containsExactlyInAnyOrder(
"test",
"test_None",
"test_Oat",
)

val defaultFunction = testCases.single { it.name == "test" }
assertThat(defaultFunction.skipped).isFalse()

val sampleSpecialization = testCases.single { it.name == "test_Oat" }
assertThat(sampleSpecialization.skipped).isFalse()
}

// No subclass is generated for the default specialization.
assertThat(testResults.resolve("test/TEST-CoffeeTest_Regular.xml").exists()).isFalse()

// Another test class is executed normally with nothing skipped.
with(readTestSuite(testResults.resolve("test/TEST-CoffeeTest_Double.xml"))) {
assertThat(testCases.map { it.name }).containsExactlyInAnyOrder(
"test",
"test_None",
"test_Oat",
)

val defaultFunction = testCases.single { it.name == "test" }
assertThat(defaultFunction.skipped).isFalse()

val sampleSpecialization = testCases.single { it.name == "test_Oat" }
assertThat(sampleSpecialization.skipped).isFalse()
}
}

private fun createRunner(
projectDir: File,
vararg taskNames: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile

buildscript {
repositories {
maven {
url = file("$rootDir/../../../../../build/testMaven").toURI()
}
mavenCentral()
google()
}
dependencies {
classpath("app.cash.burst:burst-gradle-plugin:${project.property("burstVersion")}")
classpath(libs.kotlin.gradlePlugin)
}
}

allprojects {
repositories {
maven {
url = file("$rootDir/../../../../../build/testMaven").toURI()
}
mavenCentral()
google()
}

tasks.withType(JavaCompile::class.java).configureEach {
sourceCompatibility = "1.8"
targetCompatibility = "1.8"
}

tasks.withType(KotlinJvmCompile::class.java).configureEach {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_1_8)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
plugins {
kotlin("jvm")
id("app.cash.burst")
}

dependencies {
testImplementation(kotlin("test"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import app.cash.burst.Burst
import kotlin.test.BeforeTest
import kotlin.test.Test

@Burst
class CoffeeTest(
private val espresso: Espresso = Espresso.Regular,
) {
@BeforeTest
fun setUp() {
println("set up $espresso")
}

@Test
fun test(dairy: Dairy = Dairy.Milk) {
println("running $espresso $dairy")
}
}

enum class Espresso { Decaf, Regular, Double }

enum class Dairy { None, Milk, Oat }
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
dependencyResolutionManagement {
versionCatalogs {
create("libs") {
from(files("../../../../../gradle/libs.versions.toml"))
}
}
}

include(":lib")
Loading