From 4909cfcd57188f5b77bd29ed22b5d56db9c88d57 Mon Sep 17 00:00:00 2001 From: Ji Sungbin Date: Fri, 4 Oct 2024 04:27:08 +0900 Subject: [PATCH 1/2] Write BasicLayoutTest --- .../compiler/test/BasicLayout.kt | 19 +++++---- .../compiler/test/TestConfiguration.kt | 4 +- .../compiler/test/BasicLayoutTest.kt | 41 ++++++++++++++++++- 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/compiler-integration-test/src/main/kotlin/land/sungbin/composeinvestigator/compiler/test/BasicLayout.kt b/compiler-integration-test/src/main/kotlin/land/sungbin/composeinvestigator/compiler/test/BasicLayout.kt index 09c6c7fe..f1131565 100644 --- a/compiler-integration-test/src/main/kotlin/land/sungbin/composeinvestigator/compiler/test/BasicLayout.kt +++ b/compiler-integration-test/src/main/kotlin/land/sungbin/composeinvestigator/compiler/test/BasicLayout.kt @@ -8,15 +8,18 @@ package land.sungbin.composeinvestigator.compiler.test import androidx.compose.runtime.Composable -import androidx.compose.runtime.mock.Linear import androidx.compose.runtime.mock.Text +import land.sungbin.composeinvestigator.runtime.ComposableInformation +import land.sungbin.composeinvestigator.runtime.currentComposableInvalidationTracer + +val basicLayoutTable by lazy { currentComposableInvalidationTracer } @Composable fun BasicLayout() { - Text("Root") - Linear { - repeat(3) { - Text("Child $it") - } - } - Text("Tail") + Text("") } + +fun basicLayout() = ComposableInformation( + name = "BasicLayout", + packageName = "land.sungbin.composeinvestigator.compiler.test", + fileName = "BasicLayout.kt" +) diff --git a/compiler-integration-test/src/main/kotlin/land/sungbin/composeinvestigator/compiler/test/TestConfiguration.kt b/compiler-integration-test/src/main/kotlin/land/sungbin/composeinvestigator/compiler/test/TestConfiguration.kt index b99757ca..34716c00 100644 --- a/compiler-integration-test/src/main/kotlin/land/sungbin/composeinvestigator/compiler/test/TestConfiguration.kt +++ b/compiler-integration-test/src/main/kotlin/land/sungbin/composeinvestigator/compiler/test/TestConfiguration.kt @@ -12,8 +12,10 @@ import land.sungbin.composeinvestigator.runtime.ComposableInvalidationLogger import land.sungbin.composeinvestigator.runtime.ComposeInvestigatorConfig import land.sungbin.composeinvestigator.runtime.InvalidationType +typealias Investigated = Pair + object TestConfiguration { - val logs = mutableListOf>() + val logs = mutableListOf() init { ComposeInvestigatorConfig.logger = ComposableInvalidationLogger { composable, type -> diff --git a/compiler-integration-test/src/test/kotlin/land/sungbin/composeinvestigator/compiler/test/BasicLayoutTest.kt b/compiler-integration-test/src/test/kotlin/land/sungbin/composeinvestigator/compiler/test/BasicLayoutTest.kt index fc2c9274..3371884f 100644 --- a/compiler-integration-test/src/test/kotlin/land/sungbin/composeinvestigator/compiler/test/BasicLayoutTest.kt +++ b/compiler-integration-test/src/test/kotlin/land/sungbin/composeinvestigator/compiler/test/BasicLayoutTest.kt @@ -7,16 +7,55 @@ package land.sungbin.composeinvestigator.compiler.test +import androidx.compose.runtime.RecomposeScope +import androidx.compose.runtime.currentRecomposeScope import androidx.compose.runtime.mock.compositionTest +import androidx.compose.runtime.mock.expectNoChanges +import assertk.assertThat +import assertk.assertions.containsExactly +import assertk.assertions.containsOnly import kotlin.test.BeforeTest import kotlin.test.Test +import land.sungbin.composeinvestigator.compiler.test.TestConfiguration.logs +import land.sungbin.composeinvestigator.runtime.InvalidationReason +import land.sungbin.composeinvestigator.runtime.InvalidationType class BasicLayoutTest { @BeforeTest fun prepare() { TestConfiguration.reset() + basicLayoutTable.reset() } - @Test fun basic(): Unit = compositionTest { + @Test fun initialComposition() = compositionTest { compose { BasicLayout() } + assertThat(logs).containsOnly( + Investigated( + basicLayout(), + InvalidationType.Processed(InvalidationReason.Initial), + ) + ) + } + + @Test fun skipRecomposition() = compositionTest { + var recomposeScope: RecomposeScope? = null + + compose { + recomposeScope = currentRecomposeScope + BasicLayout() + } + + recomposeScope!!.invalidate() + expectNoChanges() + + assertThat(logs).containsExactly( + Investigated( + basicLayout(), + InvalidationType.Processed(InvalidationReason.Initial), + ), + Investigated( + basicLayout(), + InvalidationType.Skipped, + ), + ) } } From b3c417ac6190edadb456968a53a2c9cec102609f Mon Sep 17 00:00:00 2001 From: Ji Sungbin Date: Fri, 4 Oct 2024 09:56:34 +0900 Subject: [PATCH 2/2] Writing some basic test code --- compiler-integration-test/build.gradle.kts | 3 + .../compiler/test/SimpleLayout.kt | 38 ++++++ .../test/{BasicLayout.kt => SimpleText.kt} | 12 +- .../compiler/test/TestConfiguration.kt | 3 +- .../compiler/test/BasicLayoutTest.kt | 61 --------- .../compiler/test/SimpleLayoutTest.kt | 127 ++++++++++++++++++ .../compiler/test/SimpleTextTest.kt | 111 +++++++++++++++ 7 files changed, 287 insertions(+), 68 deletions(-) create mode 100644 compiler-integration-test/src/main/kotlin/land/sungbin/composeinvestigator/compiler/test/SimpleLayout.kt rename compiler-integration-test/src/main/kotlin/land/sungbin/composeinvestigator/compiler/test/{BasicLayout.kt => SimpleText.kt} (70%) delete mode 100644 compiler-integration-test/src/test/kotlin/land/sungbin/composeinvestigator/compiler/test/BasicLayoutTest.kt create mode 100644 compiler-integration-test/src/test/kotlin/land/sungbin/composeinvestigator/compiler/test/SimpleLayoutTest.kt create mode 100644 compiler-integration-test/src/test/kotlin/land/sungbin/composeinvestigator/compiler/test/SimpleTextTest.kt diff --git a/compiler-integration-test/build.gradle.kts b/compiler-integration-test/build.gradle.kts index 2b39482c..d7ac2fd8 100644 --- a/compiler-integration-test/build.gradle.kts +++ b/compiler-integration-test/build.gradle.kts @@ -19,6 +19,9 @@ kotlin { "land.sungbin.composeinvestigator.runtime.ExperimentalComposeInvestigatorApi", ) freeCompilerArgs.addAll("-P", "plugin:land.sungbin.composeinvestigator.compiler:verbose=true") + sourceSets.all { + languageSettings.enableLanguageFeature("ExplicitBackingFields") + } } } diff --git a/compiler-integration-test/src/main/kotlin/land/sungbin/composeinvestigator/compiler/test/SimpleLayout.kt b/compiler-integration-test/src/main/kotlin/land/sungbin/composeinvestigator/compiler/test/SimpleLayout.kt new file mode 100644 index 00000000..1c648131 --- /dev/null +++ b/compiler-integration-test/src/main/kotlin/land/sungbin/composeinvestigator/compiler/test/SimpleLayout.kt @@ -0,0 +1,38 @@ +/* + * Developed by Ji Sungbin 2024. + * + * Licensed under the MIT. + * Please see full license: https://github.com/jisungbin/ComposeInvestigator/blob/main/LICENSE + */ + +package land.sungbin.composeinvestigator.compiler.test + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.mock.Linear +import androidx.compose.runtime.mock.Text +import land.sungbin.composeinvestigator.runtime.ComposableInformation +import land.sungbin.composeinvestigator.runtime.currentComposableInvalidationTracer + +val simpleLayoutTable by lazy { currentComposableInvalidationTracer } + +@Composable fun SimpleLayout(value: Int = 0) { + Linear { + LambdaText(value::toString) + } +} + +@Composable fun LambdaText(calucation: () -> String) { + Text(calucation()) +} + +fun simpleLayout() = ComposableInformation( + name = "SimpleLayout", + packageName = "land.sungbin.composeinvestigator.compiler.test", + fileName = "SimpleLayout.kt", +) + +fun lambdaText() = ComposableInformation( + name = "LambdaText", + packageName = "land.sungbin.composeinvestigator.compiler.test", + fileName = "SimpleLayout.kt", +) diff --git a/compiler-integration-test/src/main/kotlin/land/sungbin/composeinvestigator/compiler/test/BasicLayout.kt b/compiler-integration-test/src/main/kotlin/land/sungbin/composeinvestigator/compiler/test/SimpleText.kt similarity index 70% rename from compiler-integration-test/src/main/kotlin/land/sungbin/composeinvestigator/compiler/test/BasicLayout.kt rename to compiler-integration-test/src/main/kotlin/land/sungbin/composeinvestigator/compiler/test/SimpleText.kt index f1131565..37aef9ca 100644 --- a/compiler-integration-test/src/main/kotlin/land/sungbin/composeinvestigator/compiler/test/BasicLayout.kt +++ b/compiler-integration-test/src/main/kotlin/land/sungbin/composeinvestigator/compiler/test/SimpleText.kt @@ -12,14 +12,14 @@ import androidx.compose.runtime.mock.Text import land.sungbin.composeinvestigator.runtime.ComposableInformation import land.sungbin.composeinvestigator.runtime.currentComposableInvalidationTracer -val basicLayoutTable by lazy { currentComposableInvalidationTracer } +val simpleTextTable by lazy { currentComposableInvalidationTracer } -@Composable fun BasicLayout() { - Text("") +@Composable fun SimpleText(value: String = "") { + Text(value) } -fun basicLayout() = ComposableInformation( - name = "BasicLayout", +fun simpleText() = ComposableInformation( + name = "SimpleText", packageName = "land.sungbin.composeinvestigator.compiler.test", - fileName = "BasicLayout.kt" + fileName = "SimpleText.kt", ) diff --git a/compiler-integration-test/src/main/kotlin/land/sungbin/composeinvestigator/compiler/test/TestConfiguration.kt b/compiler-integration-test/src/main/kotlin/land/sungbin/composeinvestigator/compiler/test/TestConfiguration.kt index 34716c00..59259b98 100644 --- a/compiler-integration-test/src/main/kotlin/land/sungbin/composeinvestigator/compiler/test/TestConfiguration.kt +++ b/compiler-integration-test/src/main/kotlin/land/sungbin/composeinvestigator/compiler/test/TestConfiguration.kt @@ -15,7 +15,8 @@ import land.sungbin.composeinvestigator.runtime.InvalidationType typealias Investigated = Pair object TestConfiguration { - val logs = mutableListOf() + val logs: List + field = mutableListOf() init { ComposeInvestigatorConfig.logger = ComposableInvalidationLogger { composable, type -> diff --git a/compiler-integration-test/src/test/kotlin/land/sungbin/composeinvestigator/compiler/test/BasicLayoutTest.kt b/compiler-integration-test/src/test/kotlin/land/sungbin/composeinvestigator/compiler/test/BasicLayoutTest.kt deleted file mode 100644 index 3371884f..00000000 --- a/compiler-integration-test/src/test/kotlin/land/sungbin/composeinvestigator/compiler/test/BasicLayoutTest.kt +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Developed by Ji Sungbin 2024. - * - * Licensed under the MIT. - * Please see full license: https://github.com/jisungbin/ComposeInvestigator/blob/main/LICENSE - */ - -package land.sungbin.composeinvestigator.compiler.test - -import androidx.compose.runtime.RecomposeScope -import androidx.compose.runtime.currentRecomposeScope -import androidx.compose.runtime.mock.compositionTest -import androidx.compose.runtime.mock.expectNoChanges -import assertk.assertThat -import assertk.assertions.containsExactly -import assertk.assertions.containsOnly -import kotlin.test.BeforeTest -import kotlin.test.Test -import land.sungbin.composeinvestigator.compiler.test.TestConfiguration.logs -import land.sungbin.composeinvestigator.runtime.InvalidationReason -import land.sungbin.composeinvestigator.runtime.InvalidationType - -class BasicLayoutTest { - @BeforeTest fun prepare() { - TestConfiguration.reset() - basicLayoutTable.reset() - } - - @Test fun initialComposition() = compositionTest { - compose { BasicLayout() } - assertThat(logs).containsOnly( - Investigated( - basicLayout(), - InvalidationType.Processed(InvalidationReason.Initial), - ) - ) - } - - @Test fun skipRecomposition() = compositionTest { - var recomposeScope: RecomposeScope? = null - - compose { - recomposeScope = currentRecomposeScope - BasicLayout() - } - - recomposeScope!!.invalidate() - expectNoChanges() - - assertThat(logs).containsExactly( - Investigated( - basicLayout(), - InvalidationType.Processed(InvalidationReason.Initial), - ), - Investigated( - basicLayout(), - InvalidationType.Skipped, - ), - ) - } -} diff --git a/compiler-integration-test/src/test/kotlin/land/sungbin/composeinvestigator/compiler/test/SimpleLayoutTest.kt b/compiler-integration-test/src/test/kotlin/land/sungbin/composeinvestigator/compiler/test/SimpleLayoutTest.kt new file mode 100644 index 00000000..b11595b7 --- /dev/null +++ b/compiler-integration-test/src/test/kotlin/land/sungbin/composeinvestigator/compiler/test/SimpleLayoutTest.kt @@ -0,0 +1,127 @@ +/* + * Developed by Ji Sungbin 2024. + * + * Licensed under the MIT. + * Please see full license: https://github.com/jisungbin/ComposeInvestigator/blob/main/LICENSE + */ + +package land.sungbin.composeinvestigator.compiler.test + +import androidx.compose.runtime.RecomposeScope +import androidx.compose.runtime.currentRecomposeScope +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mock.compositionTest +import androidx.compose.runtime.mock.expectChanges +import androidx.compose.runtime.mock.expectNoChanges +import androidx.compose.runtime.mutableIntStateOf +import androidx.compose.runtime.setValue +import assertk.assertThat +import assertk.assertions.containsExactly +import assertk.assertions.containsOnly +import kotlin.test.BeforeTest +import kotlin.test.Test +import land.sungbin.composeinvestigator.compiler.test.TestConfiguration.logs +import land.sungbin.composeinvestigator.runtime.ChangedArgument +import land.sungbin.composeinvestigator.runtime.InvalidationReason +import land.sungbin.composeinvestigator.runtime.InvalidationType +import land.sungbin.composeinvestigator.runtime.Stability +import land.sungbin.composeinvestigator.runtime.ValueArgument + +class SimpleLayoutTest { + @BeforeTest fun prepare() { + TestConfiguration.reset() + simpleLayoutTable.reset() + } + + @Test fun initialComposition() = compositionTest { + compose { SimpleLayout() } + assertThat(logs).containsOnly( + Investigated( + simpleLayout(), + InvalidationType.Processed(InvalidationReason.Initial), + ), + Investigated( + lambdaText(), + InvalidationType.Processed(InvalidationReason.Initial), + ), + ) + } + + @Test fun skipRecomposition() = compositionTest { + var recomposeScope: RecomposeScope? = null + + compose { + recomposeScope = currentRecomposeScope + SimpleLayout() + } + + recomposeScope!!.invalidate() + expectNoChanges() + + assertThat(logs).containsExactly( + Investigated( + simpleLayout(), + InvalidationType.Processed(InvalidationReason.Initial), + ), + Investigated( + lambdaText(), + InvalidationType.Processed(InvalidationReason.Initial), + ), + Investigated( + simpleLayout(), + InvalidationType.Skipped, + ), + ) + } + + @Test fun recomposition() = compositionTest { + var value by mutableIntStateOf(0) + + compose { + SimpleLayout(value) + } + + value++ + expectChanges() + + assertThat(logs).containsExactly( + Investigated( + simpleLayout(), + InvalidationType.Processed(InvalidationReason.Initial), + ), + Investigated( + lambdaText(), + InvalidationType.Processed(InvalidationReason.Initial), + ), + Investigated( + simpleLayout(), + InvalidationType.Processed( + InvalidationReason.ArgumentChanged( + listOf( + ChangedArgument( + previous = ValueArgument( + name = "value", + type = "kotlin.Int", + valueString = "0", + valueHashCode = 0, + stability = Stability.Stable, + ), + new = ValueArgument( + name = "value", + type = "kotlin.Int", + valueString = "1", + valueHashCode = 1, + stability = Stability.Stable, + ), + ), + ), + ), + ), + ), + Investigated( + lambdaText(), + InvalidationType.Processed(InvalidationReason.Invalidate), + ), + ) + } +} diff --git a/compiler-integration-test/src/test/kotlin/land/sungbin/composeinvestigator/compiler/test/SimpleTextTest.kt b/compiler-integration-test/src/test/kotlin/land/sungbin/composeinvestigator/compiler/test/SimpleTextTest.kt new file mode 100644 index 00000000..1f498663 --- /dev/null +++ b/compiler-integration-test/src/test/kotlin/land/sungbin/composeinvestigator/compiler/test/SimpleTextTest.kt @@ -0,0 +1,111 @@ +/* + * Developed by Ji Sungbin 2024. + * + * Licensed under the MIT. + * Please see full license: https://github.com/jisungbin/ComposeInvestigator/blob/main/LICENSE + */ + +package land.sungbin.composeinvestigator.compiler.test + +import androidx.compose.runtime.RecomposeScope +import androidx.compose.runtime.currentRecomposeScope +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mock.compositionTest +import androidx.compose.runtime.mock.expectChanges +import androidx.compose.runtime.mock.expectNoChanges +import androidx.compose.runtime.mutableIntStateOf +import androidx.compose.runtime.setValue +import assertk.assertThat +import assertk.assertions.containsExactly +import assertk.assertions.containsOnly +import kotlin.test.BeforeTest +import kotlin.test.Test +import land.sungbin.composeinvestigator.compiler.test.TestConfiguration.logs +import land.sungbin.composeinvestigator.runtime.ChangedArgument +import land.sungbin.composeinvestigator.runtime.InvalidationReason +import land.sungbin.composeinvestigator.runtime.InvalidationType +import land.sungbin.composeinvestigator.runtime.Stability +import land.sungbin.composeinvestigator.runtime.ValueArgument + +class SimpleTextTest { + @BeforeTest fun prepare() { + TestConfiguration.reset() + simpleTextTable.reset() + } + + @Test fun initialComposition() = compositionTest { + compose { SimpleText() } + assertThat(logs).containsOnly( + Investigated( + simpleText(), + InvalidationType.Processed(InvalidationReason.Initial), + ), + ) + } + + @Test fun skipRecomposition() = compositionTest { + var recomposeScope: RecomposeScope? = null + + compose { + recomposeScope = currentRecomposeScope + SimpleText() + } + + recomposeScope!!.invalidate() + expectNoChanges() + + assertThat(logs).containsExactly( + Investigated( + simpleText(), + InvalidationType.Processed(InvalidationReason.Initial), + ), + Investigated( + simpleText(), + InvalidationType.Skipped, + ), + ) + } + + @Test fun recomposition() = compositionTest { + var value by mutableIntStateOf(0) + + compose { + SimpleText(value.toString()) + } + + value++ + expectChanges() + + assertThat(logs).containsExactly( + Investigated( + simpleText(), + InvalidationType.Processed(InvalidationReason.Initial), + ), + Investigated( + simpleText(), + InvalidationType.Processed( + InvalidationReason.ArgumentChanged( + listOf( + ChangedArgument( + previous = ValueArgument( + name = "value", + type = "kotlin.String", + valueString = "0", + valueHashCode = 48, + stability = Stability.Stable, + ), + new = ValueArgument( + name = "value", + type = "kotlin.String", + valueString = "1", + valueHashCode = 49, + stability = Stability.Stable, + ), + ), + ), + ), + ), + ), + ) + } +}