-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test cases for stability inference (#4163)
Should pass since 1.5.8-beta01 compose compiler plugin
- Loading branch information
Showing
6 changed files
with
165 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
compose/integrations/composable-test-cases/testcases/stability/lib/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 changes: 8 additions & 0 deletions
8
...tions/composable-test-cases/testcases/stability/lib/src/commonMain/kotlin/Dependencies.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 changes: 21 additions & 0 deletions
21
compose/integrations/composable-test-cases/testcases/stability/main/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 changes: 111 additions & 0 deletions
111
...ntegrations/composable-test-cases/testcases/stability/main/src/commonTest/kotlin/Tests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |