Skip to content

Commit ecbc6b9

Browse files
committed
update log tests
1 parent fbe1635 commit ecbc6b9

File tree

7 files changed

+61
-9
lines changed

7 files changed

+61
-9
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ on:
66
pull_request:
77
branches: [ master ]
88

9+
env:
10+
CI: true
11+
912
jobs:
1013
build:
1114
runs-on: ubuntu-latest

.github/workflows/review-suggest.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ name: reviewdog-suggester
22
on:
33
pull_request:
44
types: [opened, synchronize, reopened]
5+
6+
env:
7+
CI: true
8+
59
jobs:
610
kotlin:
711
name: runner / suggester / spotless

.github/workflows/unit-test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ on:
66
pull_request:
77
branches: [ master ]
88

9+
env:
10+
CI: true
11+
912
jobs:
1013
build:
1114
runs-on: ubuntu-latest

buildSrc/src/main/kotlin/deps.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@file:Suppress("unused", "ClassName", "SpellCheckingInspection")
22

3+
import org.gradle.api.Project
34
import org.gradle.api.artifacts.dsl.DependencyHandler
45
import org.gradle.kotlin.dsl.project
56
import org.gradle.plugin.use.PluginDependenciesSpec
@@ -114,3 +115,8 @@ fun DependencyHandler.addUnitTest(testImplementation: Boolean = true) {
114115
add(configName, deps.test.kotlinJUnit)
115116
add(configName, deps.coroutines.test)
116117
}
118+
119+
val Project.isCiBuild: Boolean
120+
get() = providers.environmentVariable("CI")
121+
.forUseAtConfigurationTime()
122+
.orNull == "true"

mvi/mvi-testing/build.gradle.kts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,28 @@ android {
1616
}
1717

1818
buildTypes {
19+
debug {
20+
(!isCiBuild).let {
21+
buildConfigField(
22+
type = it::class.java.simpleName,
23+
name = "ENABLE_LOG_TEST",
24+
value = it.toString(),
25+
)
26+
}
27+
}
1928
release {
2029
isMinifyEnabled = false
2130
proguardFiles(
2231
getDefaultProguardFile("proguard-android-optimize.txt"),
2332
"proguard-rules.pro"
2433
)
34+
false.let {
35+
buildConfigField(
36+
type = it::class.java.simpleName,
37+
name = "ENABLE_LOG_TEST",
38+
value = it.toString(),
39+
)
40+
}
2541
}
2642
}
2743
compileOptions {

mvi/mvi-testing/src/main/java/com/flowmvi/mvi_testing/BaseMviViewModelTest.kt

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
1414
import kotlinx.coroutines.delay
1515
import kotlinx.coroutines.flow.Flow
1616
import kotlinx.coroutines.flow.collect
17+
import kotlinx.coroutines.flow.onCompletion
1718
import kotlinx.coroutines.flow.toList
1819
import kotlinx.coroutines.launch
1920
import kotlinx.coroutines.test.runBlockingTest
@@ -54,26 +55,37 @@ abstract class BaseMviViewModelTest<
5455
expectedStates: List<Either<(S) -> Unit, S>>,
5556
expectedEvents: List<Either<(E) -> Unit, E>>,
5657
delayAfterDispatchingIntents: Duration = Duration.ZERO,
57-
logging: Boolean = true,
58+
logging: Boolean = BuildConfig.ENABLE_LOG_TEST,
5859
intentsBeforeCollecting: Flow<I>? = null,
5960
otherAssertions: (suspend () -> Unit)? = null,
6061
) = testDispatcher.runBlockingTest {
62+
fun logIfEnabled(s: () -> String) = if (logging) println(s()) else Unit
63+
6164
val vm = vmProducer()
62-
intentsBeforeCollecting?.collect { vm.processIntent(it) }
65+
intentsBeforeCollecting
66+
?.onCompletion { logIfEnabled { "---------------" } }
67+
?.collect {
68+
vm.processIntent(it)
69+
logIfEnabled { "[BEFORE] Dispatch $it -> $vm" }
70+
}
71+
72+
logIfEnabled { "[START] $vm" }
6373

6474
val states = mutableListOf<S>()
6575
val events = mutableListOf<E>()
6676

6777
val stateJob = launch(start = CoroutineStart.UNDISPATCHED) { vm.viewState.toList(states) }
6878
val eventJob = launch(start = CoroutineStart.UNDISPATCHED) { vm.singleEvent.toList(events) }
6979

70-
intents.collect { vm.processIntent(it) }
80+
intents.collect {
81+
vm.processIntent(it)
82+
logIfEnabled { "[DISPATCH] Dispatch $it -> $vm" }
83+
}
7184
delay(delayAfterDispatchingIntents)
85+
logIfEnabled { "---------------" }
7286

73-
if (logging) {
74-
println(states)
75-
println(events)
76-
}
87+
logIfEnabled { "[DONE] states=${states.joinToStringWithIndex()}" }
88+
logIfEnabled { "[DONE] events=${events.joinToStringWithIndex()}" }
7789

7890
assertEquals(expectedStates.size, states.size, "States size")
7991
expectedStates.withIndex().zip(states).forEach { (indexedValue, state) ->
@@ -112,3 +124,13 @@ abstract class BaseMviViewModelTest<
112124
}
113125

114126
fun <T> Iterable<T>.mapRight(): List<Either<(T) -> Unit, T>> = map { it.right() }
127+
128+
private fun <T> List<T>.joinToStringWithIndex(): String {
129+
return withIndex().joinToString(
130+
separator = ",\n",
131+
prefix = "[\n",
132+
postfix = "]",
133+
) { (i, v) ->
134+
" [$i]: $v"
135+
}
136+
}

test-utils/src/main/java/com/hoc/flowmvi/test_utils/TestCoroutineDispatcherRule.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@ class TestCoroutineDispatcherRule(val testCoroutineDispatcher: TestCoroutineDisp
1313
TestWatcher() {
1414
override fun starting(description: Description) {
1515
Dispatchers.setMain(testCoroutineDispatcher)
16-
println("$this::starting $description")
1716
}
1817

1918
override fun finished(description: Description) {
2019
Dispatchers.resetMain()
2120
testCoroutineDispatcher.cleanupTestCoroutines()
22-
println("$this::finished $description")
2321
}
2422
}

0 commit comments

Comments
 (0)