-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor testing API to improve readability
- Loading branch information
1 parent
ae7c4ca
commit 476f842
Showing
6 changed files
with
146 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
transmission-test/src/main/java/com/yigitozgumus/transmissiontesting/TestSuite.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.yigitozgumus.transmissiontesting | ||
|
||
import com.trendyol.transmission.Transmission | ||
import com.trendyol.transmission.effect.EffectWrapper | ||
import com.trendyol.transmission.transformer.Transformer | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.flow.toList | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.test.TestScope | ||
import kotlinx.coroutines.test.UnconfinedTestDispatcher | ||
import kotlinx.coroutines.test.advanceUntilIdle | ||
import kotlinx.coroutines.test.runTest | ||
|
||
class TestSuite { | ||
private var orderedInitialProcessing: List<Transmission> = emptyList() | ||
private var transformer: Transformer? = null | ||
private lateinit var router: TestRouter | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
fun initialize(transformer: Transformer): TestSuite { | ||
this.transformer = transformer | ||
router = TestRouter(transformer, UnconfinedTestDispatcher()) | ||
return this | ||
} | ||
|
||
fun register(registry: RegistryScope.() -> Unit = {}): TestSuite { | ||
router.registry = RegistryScopeImpl().apply(registry) | ||
return this | ||
} | ||
|
||
fun processBeforeTesting(vararg transmissions: Transmission): TestSuite { | ||
orderedInitialProcessing = transmissions.toList() | ||
return this | ||
} | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
fun runTest( | ||
transmission: Transmission, | ||
scope: suspend TransformerTestScope.(scope: TestScope) -> Unit | ||
) { | ||
|
||
runTest { | ||
val dataStream: MutableList<Transmission.Data> = mutableListOf() | ||
val effectStream: MutableList<EffectWrapper> = mutableListOf() | ||
try { | ||
backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) { | ||
router.dataStream.toList(dataStream) | ||
} | ||
backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) { | ||
router.effectStream.toList(effectStream) | ||
} | ||
val testScope = object : TransformerTestScope { | ||
override val dataStream: List<Transmission.Data> = dataStream | ||
override val effectStream: List<EffectWrapper> = effectStream | ||
} | ||
orderedInitialProcessing.forEach { | ||
when (it) { | ||
is Transmission.Data -> throw IllegalArgumentException("Transmission.Data should not be sent for processing") | ||
is Transmission.Effect -> router.sendEffect(it) | ||
is Transmission.Signal -> router.sendSignal(it) | ||
} | ||
transformer?.waitProcessingToFinish() | ||
} | ||
if (transmission is Transmission.Signal) { | ||
router.sendSignal(transmission) | ||
} else if (transmission is Transmission.Effect) { | ||
router.sendEffect(transmission) | ||
} | ||
transformer?.waitProcessingToFinish() | ||
testScope.scope(this) | ||
} finally { | ||
advanceUntilIdle() | ||
router.clear() | ||
} | ||
} | ||
} | ||
} |
87 changes: 8 additions & 79 deletions
87
transmission-test/src/main/java/com/yigitozgumus/transmissiontesting/TestWithExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters