Oleksandr Karpovich
10 months ago
committed by
GitHub
6 changed files with 165 additions and 2 deletions
@ -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() |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,8 @@ |
|||||||
|
data class UnstableDataClassWithPrivateVar(private var i: Int) { |
||||||
|
|
||||||
|
fun inc() { i++ } |
||||||
|
fun getI() = i |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
data class StableDataClassWithPrivateVal(private val i: Int) |
@ -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() |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -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…
Reference in new issue