Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test cases for expect actual functions with default values #2846

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ val Project.isFailingJsCase: Boolean

@OptIn(ExternalVariantApi::class)
fun KotlinMultiplatformExtension.configureTargets() {
val addMinGw = project.isInIdea

jvm("desktop")
configureJsTargets()
ios()
Expand All @@ -27,8 +29,32 @@ fun KotlinMultiplatformExtension.configureTargets() {
macosX64()
macosArm64()
// We use linux agents on CI. So it doesn't run the tests, but it builds the klib anyway which is time consuming.
if (project.isInIdea) mingwX64()
if (addMinGw) mingwX64()
linuxX64()

val commonMain = sourceSets.getByName("commonMain")
val nativeMain = sourceSets.create("nativeMain")
nativeMain.dependsOn(commonMain)

val iosMain = sourceSets.getByName("iosMain")
iosMain.dependsOn(nativeMain)
sourceSets.getByName("iosSimulatorArm64Main").dependsOn(iosMain)
sourceSets.getByName("iosArm64Main").dependsOn(iosMain)
sourceSets.getByName("iosX64Main").dependsOn(iosMain)

val macosMain = sourceSets.create("macosMain")
macosMain.dependsOn(nativeMain)
sourceSets.getByName("macosX64Main").dependsOn(macosMain)
sourceSets.getByName("macosArm64Main").dependsOn(macosMain)

val linuxMain = sourceSets.create("linuxMain")
linuxMain.dependsOn(nativeMain)
sourceSets.getByName("linuxX64Main").dependsOn(linuxMain)

if (addMinGw) {
sourceSets.getByName("mingwX64Main").dependsOn(nativeMain)
}

}

fun KotlinMultiplatformExtension.configureJsTargets() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import kotlin.jvm.JvmInline

@JvmInline
value class CurrentPlatform internal constructor(internal val value: Int) {

fun name(): String {
return when (this) {
Desktop -> "Desktop"
Web -> "Web"
Native -> "Native"
else -> "Unknown CurrentPlatform"
}
}
companion object {
val Desktop = CurrentPlatform(0)
val Web = CurrentPlatform(10)
val Native = CurrentPlatform(20)
}
}

expect val currentPlatform: CurrentPlatform
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

actual val currentPlatform: CurrentPlatform = CurrentPlatform.Desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

actual val currentPlatform: CurrentPlatform = CurrentPlatform.Web
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

actual val currentPlatform: CurrentPlatform = CurrentPlatform.Native
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ if (casesToRun.isDefault()) {
module(":testcase-valueClass-lib", "testcases/valueClass/lib")
module(":testcase-valueClass-main", "testcases/valueClass/main")

module(":testcase-expectActualFun-lib", "testcases/expectActualFun/lib")
module(":testcase-expectActualFun-main", "testcases/expectActualFun/main")

module(":testcase-lambdas-lib", "testcases/lambdas/lib")
module(":testcase-lambdas-main", "testcases/lambdas/main")
}
Expand Down
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()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package my.abc

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import com.example.common.TextContainerNode
import com.example.common.TextLeafNode

@Composable
expect fun SimpleComposable()

@Composable
expect fun WithDefaultIntParam(i: Int = 10)

@Composable
expect fun WithDefaultStringParam(s: String = "defaultStringValue")

var savedComposableLambda: (@Composable () -> Unit)? = null

expect fun TakesComposableLambda(l: @Composable () -> Unit)

expect fun TakesComposableLambdaWithDefault(l: @Composable () -> Unit = { TextLeafNode("Default") })


fun TakesComposableLambdaWithDefaultIntNotExpect(i: Int = 100, l: @Composable () -> Unit) {
savedComposableLambda = {
TextContainerNode("Common-$i", l)
}
}
expect fun TakesComposableLambdaWithDefaultInt(i: Int = 100, l: @Composable () -> Unit)

@Composable
fun defaultStringValueComposable(): String {
return "defaultStringValueComposable"
}

@Composable
expect fun ExpectComposableDefaultValueProvidedByAnotherComposable(
value: String = defaultStringValueComposable(),
content: @Composable (v: String) -> Unit
)

@Composable
expect fun UseRememberInDefaultValueOfExpectFun(
value: String = remember { "defaultRememberedValue" },
content: @Composable (v: String) -> Unit
)

@Composable
expect fun <T> ExpectWithTypeParameter(
value: T,
content: @Composable (T) -> Unit //= { TextLeafNode(value.toString()) }
)

@Composable
expect fun <T> ExpectWithTypeParameterAndDefaultLambda(
value: T,
transform: (T) -> T = { it }
)

@Composable
expect fun <T> ExpectWithTypeParameterAndDefaultComposableLambda(
value: T,
content: @Composable (T) -> Unit = createDefaultContent(value)
)

fun <T> createDefaultContent(value: T): @Composable (T) -> Unit {
return { TextLeafNode(value.toString()) }
}

@Composable
expect fun <T> ExpectWithTypeParameterInReturnAndDefaultComposableLambda(
value: T,
calculate: @Composable (T) -> T = { value }
): T

expect class ExpectClass() {

@Composable
fun ExpectComposableFunWithDefaultInt(i: Int = 11011)
}

expect class ExpectClassWithString() {

@Composable
fun ExpectComposableFunWithDefaultComposableLambda(
s: String,
transform: @Composable (String) -> String = { s }
)
}

expect class ExpectClassWithStringProperty constructor(s: String) {

val property: String

@Composable
fun ExpectComposableFunWithDefaultComposableLambda(
transform: @Composable (String) -> String = { property }
)
}

expect class ExpectClassWithT<T>() {

@Composable
fun ExpectComposableFunWithDefaultComposableLambda(t: T, transform: @Composable (T) -> T = { t })
}

expect class ExpectClassWithTProp<T>(t: T) {

val tVal: T

@Composable
fun ExpectComposableFunWithDefaultComposableLambda(transform: @Composable (T) -> T = { tVal })
}

expect class ExpectClassWithTProp2<T>(t: T) {

val tVal: T

@Composable
fun ExpectComposableFunWithDefaultComposableLambda(t: T = tVal)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package my.abc

import androidx.compose.runtime.Composable
import com.example.common.TextContainerNode
import com.example.common.TextLeafNode

@Composable
actual fun SimpleComposable() {
TextLeafNode("SimpleComposable-Desktop")
}

@Composable
actual fun WithDefaultIntParam(i: Int) {
TextLeafNode("SimpleComposable-Desktop-$i")
}

@Composable
actual fun WithDefaultStringParam(s: String) {
TextLeafNode("SimpleComposable-Desktop-$s")
}

actual fun TakesComposableLambda(l: @Composable () -> Unit) {
savedComposableLambda = {
TextContainerNode("Desktop", l)
}
}

actual fun TakesComposableLambdaWithDefault(l: @Composable () -> Unit) {
savedComposableLambda = {
TextContainerNode("Desktop", l)
}
}

actual fun TakesComposableLambdaWithDefaultInt(i: Int, l: @Composable () -> Unit) {
savedComposableLambda = {
TextContainerNode("Desktop-$i", l)
}
}

@Composable
actual fun ExpectComposableDefaultValueProvidedByAnotherComposable(
value: String,
content: @Composable (v: String) -> Unit
) {
content("Desktop-$value")
}

@Composable
actual fun UseRememberInDefaultValueOfExpectFun(
value: String,
content: @Composable (v: String) -> Unit
) {
content("Desktop-$value")
}

@Composable
actual fun <T> ExpectWithTypeParameter(
value: T,
content: @Composable (T) -> Unit
) {
TextContainerNode("Desktop") {
content(value)
}
}

@Composable
actual fun <T> ExpectWithTypeParameterAndDefaultLambda(
value: T,
transform: (T) -> T
) {
TextContainerNode("Desktop") {
TextLeafNode(transform(value).toString())
}
}

@Composable
actual fun <T> ExpectWithTypeParameterAndDefaultComposableLambda(
value: T,
content: @Composable (T) -> Unit
) {
TextContainerNode("Desktop") {
content(value)
}
}

@Composable
actual fun <T> ExpectWithTypeParameterInReturnAndDefaultComposableLambda(
value: T,
calculate: @Composable (T) -> T
): T {
return calculate(value)
}

actual class ExpectClass actual constructor() {

@Composable
actual fun ExpectComposableFunWithDefaultInt(i: Int) {
TextLeafNode("Desktop(i = $i)")
}
}

actual class ExpectClassWithString actual constructor() {

@Composable
actual fun ExpectComposableFunWithDefaultComposableLambda(
s: String, transform: @Composable (String) -> String
) {
TextLeafNode("Desktop(s = ${transform(s)})")
}
}

actual class ExpectClassWithStringProperty actual constructor(s: String) {

actual val property: String = s

@Composable
actual fun ExpectComposableFunWithDefaultComposableLambda(
transform: @Composable (String) -> String
) {
TextLeafNode("Desktop(s = ${transform(property)})")
}
}

actual class ExpectClassWithT<T> actual constructor() {

@Composable
actual fun ExpectComposableFunWithDefaultComposableLambda(t: T, transform: @Composable (T) -> T) {
TextLeafNode("Desktop(t = ${transform(t)})")
}
}

actual class ExpectClassWithTProp<T> actual constructor(t: T) {

actual val tVal: T = t

@Composable
actual fun ExpectComposableFunWithDefaultComposableLambda(transform: @Composable (T) -> T) {
TextLeafNode("Desktop(tProp = ${transform(tVal)})")
}
}

actual class ExpectClassWithTProp2<T> actual constructor(t: T) {

actual val tVal: T = t

@Composable
actual fun ExpectComposableFunWithDefaultComposableLambda(t: T) {
TextLeafNode("Desktop(tProp = $t)")
}
}
Loading