Browse Source

web: update compiler plugin test cases to make them runnable (#1447)

Co-authored-by: Oleksandr Karpovich <oleksandr.karpovich@jetbrains.com>
pull/1452/head
Oleksandr Karpovich 3 years ago committed by GitHub
parent
commit
742c4a40fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      web/compose-compiler-integration/main-template/src/commonMain/kotlin/Expect.kt
  2. 33
      web/compose-compiler-integration/main-template/src/jsMain/kotlin/Deps.kt
  3. 7
      web/compose-compiler-integration/testcases/passing/ComposableWithDefaultValuesDefinedByOtherParams.kt
  4. 25
      web/compose-compiler-integration/testcases/passing/ComposableWithParamsWithDefaultValues.kt
  5. 28
      web/compose-compiler-integration/testcases/passing/ComposableWithTypeParams.kt
  6. 12
      web/compose-compiler-integration/testcases/passing/PassingComposableToConstructor.kt
  7. 2
      web/gradle.properties

4
web/compose-compiler-integration/main-template/src/commonMain/kotlin/Expect.kt

@ -0,0 +1,4 @@
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Composition
expect fun callComposable(content: @Composable () -> Unit)

33
web/compose-compiler-integration/main-template/src/jsMain/kotlin/Deps.kt

@ -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)
}

7
web/compose-compiler-integration/testcases/passing/ComposableWithDefaultValuesDefinedByOtherParams.kt

@ -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
}

25
web/compose-compiler-integration/testcases/passing/ComposableWithParamsWithDefaultValues.kt

@ -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)
}
@ -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() }
}

28
web/compose-compiler-integration/testcases/passing/ComposableWithTypeParams.kt

@ -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
@ -41,4 +48,5 @@ fun <T> MySelect(
options: List<T>,
onChange: (T) -> Unit
) {
onChange(options.first())
}

12
web/compose-compiler-integration/testcases/passing/PassingComposableToConstructor.kt

@ -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

2
web/gradle.properties

@ -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

Loading…
Cancel
Save