Browse Source

Add test cases for stability inference (#4163)

Should pass since 1.5.8-beta01 compose compiler plugin
pull/4177/head
Oleksandr Karpovich 10 months ago committed by GitHub
parent
commit
f121805763
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 4
      compose/integrations/composable-test-cases/gradle.properties
  2. 3
      compose/integrations/composable-test-cases/settings.gradle.kts
  3. 20
      compose/integrations/composable-test-cases/testcases/stability/lib/build.gradle.kts
  4. 8
      compose/integrations/composable-test-cases/testcases/stability/lib/src/commonMain/kotlin/Dependencies.kt
  5. 21
      compose/integrations/composable-test-cases/testcases/stability/main/build.gradle.kts
  6. 111
      compose/integrations/composable-test-cases/testcases/stability/main/src/commonTest/kotlin/Tests.kt

4
compose/integrations/composable-test-cases/gradle.properties

@ -1,7 +1,7 @@
org.gradle.jvmargs=-Xmx2048M -XX:MaxMetaspaceSize=512m org.gradle.jvmargs=-Xmx2048M -XX:MaxMetaspaceSize=512m
kotlin.code.style=official kotlin.code.style=official
android.useAndroidX=true android.useAndroidX=true
kotlin.version=1.9.21 kotlin.version=1.9.22
agp.version=7.3.0 agp.version=7.3.0
compose.version=1.6.0-dev1357 compose.version=1.6.0-dev1357
@ -9,7 +9,7 @@ kotlinx.coroutines.version=1.8.0-RC
#empty by default - a default version will be used #empty by default - a default version will be used
#compose.kotlinCompilerPluginVersion=23.12.18 #compose.kotlinCompilerPluginVersion=23.12.18
compose.kotlinCompilerPluginVersion=1.5.4 compose.kotlinCompilerPluginVersion=1.5.8-beta01
# default|failingJs - see enum class CasesToRun # default|failingJs - see enum class CasesToRun
tests.casesToRun=default tests.casesToRun=default

3
compose/integrations/composable-test-cases/settings.gradle.kts

@ -122,6 +122,9 @@ if (casesToRun.isDefault()) {
module(":testcase-expectActual-lib", "testcases/expectActual/lib") module(":testcase-expectActual-lib", "testcases/expectActual/lib")
module(":testcase-expectActual-main", "testcases/expectActual/main") module(":testcase-expectActual-main", "testcases/expectActual/main")
module(":testcase-stability-lib", "testcases/stability/lib")
module(":testcase-stability-main", "testcases/stability/main")
} }
/** /**

20
compose/integrations/composable-test-cases/testcases/stability/lib/build.gradle.kts

@ -0,0 +1,20 @@
plugins {
kotlin("multiplatform")
id("org.jetbrains.compose")
}
kotlin {
configureTargets()
sourceSets {
val commonMain by getting {
dependencies {
implementation(compose.runtime)
implementation(getCommonLib())
}
}
val commonTest by getting {
configureCommonTestDependencies()
}
}
}

8
compose/integrations/composable-test-cases/testcases/stability/lib/src/commonMain/kotlin/Dependencies.kt

@ -0,0 +1,8 @@
data class UnstableDataClassWithPrivateVar(private var i: Int) {
fun inc() { i++ }
fun getI() = i
}
data class StableDataClassWithPrivateVal(private val i: Int)

21
compose/integrations/composable-test-cases/testcases/stability/main/build.gradle.kts

@ -0,0 +1,21 @@
plugins {
kotlin("multiplatform")
id("org.jetbrains.compose")
}
kotlin {
configureTargets()
sourceSets {
val commonMain by getting {
dependencies {
implementation(compose.runtime)
implementation(getCommonLib())
implementation(getLibDependencyForMain())
}
}
val commonTest by getting {
configureCommonTestDependencies()
}
}
}

111
compose/integrations/composable-test-cases/testcases/stability/main/src/commonTest/kotlin/Tests.kt

@ -0,0 +1,111 @@
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import com.example.common.TextLeafNode
import com.example.common.composeText
import kotlinx.coroutines.Job
import kotlinx.coroutines.test.runTest
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
class Tests {
/**
* Here we use an unstable parameter, and therefore
* we expect the Composable function will NOT skip body execution.
*/
@Test
fun testUnstableParameter() = runTest {
val i = UnstableDataClassWithPrivateVar(0)
val job = Job()
val state = mutableStateOf(0)
val root = composeText(coroutineContext + job) {
UseUnstableDataClassInstance(i)
TextLeafNode("state=" + state.value.toString())
}
assertEquals("root:{UnstableDataClassWithPrivateVar(i=0), state=0}", root.dump())
assertEquals(1, i.getI())
state.value += 1
testScheduler.advanceUntilIdle()
assertEquals("root:{UnstableDataClassWithPrivateVar(i=1), state=1}", root.dump())
assertEquals(2, i.getI())
}
@Test
fun testUnstableParameterOfLocalType() = runTest {
val i = LocalUnstableDataClassWithPrivateVar(0)
val job = Job()
val state = mutableStateOf(0)
val root = composeText(coroutineContext + job) {
UseLocalUnstableDataClassWithPrivateVar(i)
TextLeafNode("state=" + state.value.toString())
}
assertEquals("root:{LocalUnstableDataClassWithPrivateVar(i=0), state=0}", root.dump())
assertEquals(1, i.getI())
state.value += 1
testScheduler.advanceUntilIdle()
assertEquals("root:{LocalUnstableDataClassWithPrivateVar(i=1), state=1}", root.dump())
assertEquals(2, i.getI())
}
@Test
fun testStableParameter() = runTest {
val i = StableDataClassWithPrivateVal(0)
val job = Job()
val state = mutableStateOf(0)
val root = composeText(coroutineContext + job) {
UseStableDataClassWithPrivateVar(i)
TextLeafNode("state=" + state.value.toString())
}
assertEquals("root:{StableDataClassWithPrivateVal(i=0), counter=0, state=0}", root.dump())
assertEquals(1, counter)
state.value += 1
testScheduler.advanceUntilIdle()
assertEquals("root:{StableDataClassWithPrivateVal(i=0), counter=0, state=1}", root.dump())
assertEquals(1, counter)
}
@BeforeTest
fun before() {
counter = 0
}
}
private var counter = 0
@Composable
fun UseUnstableDataClassInstance(i: UnstableDataClassWithPrivateVar) {
TextLeafNode(i.toString())
i.inc()
}
@Composable
fun UseStableDataClassWithPrivateVar(i: StableDataClassWithPrivateVal) {
TextLeafNode(i.toString())
TextLeafNode("counter=$counter")
counter++
}
/**
* Same as [UnstableDataClassWithPrivateVar] but defined in the same module that a function which uses it
*/
data class LocalUnstableDataClassWithPrivateVar(private var i: Int) {
fun inc() { i++ }
fun getI() = i
}
@Composable
fun UseLocalUnstableDataClassWithPrivateVar(i: LocalUnstableDataClassWithPrivateVar) {
TextLeafNode(i.toString())
i.inc()
}
Loading…
Cancel
Save