Skip to content

Commit

Permalink
web: update compiler plugin test cases to make them runnable (#1447)
Browse files Browse the repository at this point in the history
Co-authored-by: Oleksandr Karpovich <[email protected]>
  • Loading branch information
eymar and Oleksandr Karpovich authored Nov 23, 2021
1 parent 1066bca commit 742c4a4
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Composition

expect fun callComposable(content: @Composable () -> Unit)
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import androidx.compose.runtime.*
import kotlinx.coroutines.*

class UnitApplier : Applier<Unit> {
override val current: Unit
get() = Unit

override fun down(node: Unit) {}
override fun up() {}
override fun insertTopDown(index: Int, instance: Unit) {}
override fun insertBottomUp(index: Int, instance: Unit) {}
override fun remove(index: Int, count: Int) {}
override fun move(from: Int, to: Int, count: Int) {}
override fun clear() {}
}

fun createRecomposer(): Recomposer {
val mainScope = CoroutineScope(
NonCancellable + Dispatchers.Main + DefaultMonotonicFrameClock
)

return Recomposer(mainScope.coroutineContext).also {
mainScope.launch(start = CoroutineStart.UNDISPATCHED) {
it.runRecomposeAndApplyChanges()
}
}
}


actual fun callComposable(content: @Composable () -> Unit) {
val c = ControlledComposition(UnitApplier(), createRecomposer())
c.setContent(content)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ fun main() {
callComposable {
ComposableWithDefaultParamsDefinedByOtherParams("a")
}
require(result == "aa") { "Actual result was - $result"}
}

fun callComposable(content: @Composable () -> Unit) {
val c = content
}

// @Module:Lib
import androidx.compose.runtime.Composable

var result = ""

@Composable
fun ComposableWithDefaultParamsDefinedByOtherParams(
a: String,
b: String = a
) {
result = a + b
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,40 @@ fun main() {
ComposableWithDifferentDefaultValuesForParameters(a = Any())
ComposableWithReturnAndWithDefaultLambda().invoke()
}
}

fun callComposable(content: @Composable () -> Unit) {
val c = content
require(intArrayOf(1, 2, 3, 4, 5, 6, 7).all { it in set }) { "Failed when running composables - ${set.joinToString()}" }
}

// @Module:Lib
import androidx.compose.runtime.Composable

var set = mutableSetOf<Int>()

@Composable
fun FooTakesLambda(block: () -> Unit = {}) {
fun FooTakesLambda(block: () -> Unit = { set.add(1) }) {
block()
}

@Composable
inline fun InlineFooTakesLambda(block: () -> Unit = {}) {
inline fun InlineFooTakesLambda(block: () -> Unit = { set.add(2) }) {
block()
}

@Composable
fun FooTakesComposableLambda(composable: @Composable () -> Unit = {}) {
fun FooTakesComposableLambda(composable: @Composable () -> Unit = { set.add(3) }) {
composable()
}

@Composable
inline fun InlineFooTakesComposableLambda(composable: @Composable () -> Unit = {}) {
inline fun InlineFooTakesComposableLambda(composable: @Composable () -> Unit = { set.add(4) }) {
composable()
}

@Composable
fun FooTakesTypedExtesionComposableLambdaWithExplicitTypesAndDefaultLambda(
t: String, k: Int, composable: @Composable String.(Int) -> Double = { (this + ". $it").toDouble() }
t: String, k: Int, composable: @Composable String.(Int) -> Double = {
set.add(5)
(this + ".$it").toDouble()
}
) {
t.composable(k)
}
Expand All @@ -56,12 +58,13 @@ fun ComposableWithDifferentDefaultValuesForParameters(
a: Any, i: Int = 1, b: Boolean = false, s: String = "s",
u: Unit = Unit, a2: Any = Any(), l: List<Any> = listOf("1")
) {
set.add(6)
a.toString() + "$i $b $s $u $a2 $l"
}

@Composable
fun ComposableWithReturnAndWithDefaultLambda(
l: @Composable () -> (@Composable () -> Unit) = { { } }
l: @Composable () -> (@Composable () -> Unit) = { { set.add(7) } }
): @Composable () -> Unit {
return { l() }
return { l().invoke() }
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
// @Module:Main
import androidx.compose.runtime.Composable
import androidx.compose.runtime.currentComposer
import androidx.compose.runtime.Composer
import androidx.compose.runtime.*

fun main() {
var set = mutableSetOf<Int>()
callComposable {

FooTakesTypedComposableLambda { "text" }
FooTakesTypedComposableLambda2(10) { it + 100 }
FooTakesTypedExtesionComposableLambda<String, Any, Unit>("text", Any()) { }
MySelect<String>(emptyList(), {})
FooTakesTypedComposableLambda {
set.add(1)
"text"
}
FooTakesTypedComposableLambda2(10) {
set.add(2)
it + 100
}
FooTakesTypedExtesionComposableLambda<String, Any, Unit>("text", Any()) {
set.add(3)
}
MySelect<String>(listOf("1")) {
set.add(4)
}
}
}

fun callComposable(content: @Composable () -> Unit) {
val c = content
require(intArrayOf(1, 2, 3, 4).all { it in set }) { "Failed when running composables" }
}


// @Module:Lib
import androidx.compose.runtime.Composable

Expand All @@ -41,4 +48,5 @@ fun <T> MySelect(
options: List<T>,
onChange: (T) -> Unit
) {
onChange(options.first())
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
import androidx.compose.runtime.Composable

fun main() {
val instance = testCase { }
var set = mutableSetOf<Int>()

val instance = testCase {
set.add(1)
}
val instance2 = TestCase2()

callComposable {
instance.composable()
instance2.composable()
set.add(2)
}
}

fun callComposable(content: @Composable () -> Unit) {
// does nothing
require(intArrayOf(1, 2).all { it in set }) { "Failed when running composables" }
}

// @Module:Lib
Expand Down
2 changes: 1 addition & 1 deletion web/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# __LATEST_COMPOSE_RELEASE_VERSION__
COMPOSE_CORE_VERSION=1.0.0-alpha4-build362
COMPOSE_CORE_VERSION=1.0.0-rc2
COMPOSE_WEB_VERSION=1.0.0-alpha4-build362
compose.web.buildSamples=false
compose.web.tests.integration.withFirefox
Expand Down

0 comments on commit 742c4a4

Please sign in to comment.