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

Writing some basic test code #220

Merged
merged 2 commits into from
Oct 4, 2024
Merged
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
3 changes: 3 additions & 0 deletions compiler-integration-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.Text
import land.sungbin.composeinvestigator.runtime.ComposableInformation
import land.sungbin.composeinvestigator.runtime.currentComposableInvalidationTracer

val simpleTextTable by lazy { currentComposableInvalidationTracer }

@Composable fun SimpleText(value: String = "") {
Text(value)
}

fun simpleText() = ComposableInformation(
name = "SimpleText",
packageName = "land.sungbin.composeinvestigator.compiler.test",
fileName = "SimpleText.kt",
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ import land.sungbin.composeinvestigator.runtime.ComposableInvalidationLogger
import land.sungbin.composeinvestigator.runtime.ComposeInvestigatorConfig
import land.sungbin.composeinvestigator.runtime.InvalidationType

typealias Investigated = Pair<ComposableInformation, InvalidationType>

object TestConfiguration {
val logs = mutableListOf<Pair<ComposableInformation, InvalidationType>>()
val logs: List<Investigated>
field = mutableListOf()

init {
ComposeInvestigatorConfig.logger = ComposableInvalidationLogger { composable, type ->
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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),
),
)
}
}
Original file line number Diff line number Diff line change
@@ -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,
),
),
),
),
),
),
)
}
}