File tree 3 files changed +93
-0
lines changed
commonMain/kotlin/com/badoo/mvicore/common/lifecycle
commonTest/kotlin/com/badoo/mvicore/common
3 files changed +93
-0
lines changed Original file line number Diff line number Diff line change 6
6
/build
7
7
/captures
8
8
.externalNativeBuild
9
+ /out
Original file line number Diff line number Diff line change
1
+ package com.badoo.mvicore.common.lifecycle
2
+
3
+ import com.badoo.mvicore.common.SimpleSource
4
+ import com.badoo.mvicore.common.Source
5
+ import com.badoo.mvicore.common.lifecycle.Lifecycle.Event.BEGIN
6
+ import com.badoo.mvicore.common.lifecycle.Lifecycle.Event.END
7
+ import com.badoo.mvicore.common.source
8
+
9
+ interface Lifecycle : Source <Lifecycle .Event > {
10
+ enum class Event {
11
+ BEGIN , END
12
+ }
13
+
14
+ fun manual () = ManualLifecycle ()
15
+ }
16
+
17
+ class ManualLifecycle (private val source : SimpleSource <Lifecycle .Event > = source()) : Lifecycle, Source<Lifecycle.Event> by source {
18
+ fun begin () {
19
+ source(BEGIN )
20
+ }
21
+
22
+ fun end () {
23
+ source(END )
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ package com.badoo.mvicore.common
2
+
3
+ import com.badoo.mvicore.common.binder.Binder
4
+ import com.badoo.mvicore.common.binder.NotNullConnector
5
+ import com.badoo.mvicore.common.binder.using
6
+ import kotlin.test.Test
7
+ import kotlin.test.assertEquals
8
+
9
+ class BinderTest {
10
+ private val source = source<Int >()
11
+ private val sink = TestSink <Int >()
12
+
13
+ @Test
14
+ fun binder_without_lifecycle_connects_source_and_sink () {
15
+ Binder ().apply {
16
+ bind(source to sink)
17
+ }
18
+
19
+ source.invoke(0 )
20
+ assertEquals(listOf (0 ), sink.values)
21
+ }
22
+
23
+ @Test
24
+ fun binder_without_lifecycle_does_not_connect_source_and_sink_after_cancel () {
25
+ val binder = Binder ().apply {
26
+ bind(source to sink)
27
+ }
28
+
29
+ binder.cancel()
30
+
31
+ source.invoke(0 )
32
+ assertEquals(emptyList(), sink.values)
33
+ }
34
+
35
+ @Test
36
+ fun binder_without_lifecycle_connects_source_and_sink_using_mapper () {
37
+ Binder ().apply {
38
+ bind(source to sink using { it + 1 })
39
+ }
40
+
41
+ source.invoke(0 )
42
+ assertEquals(listOf (1 ), sink.values)
43
+ }
44
+
45
+ @Test
46
+ fun binder_without_lifecycle_connects_source_and_sink_skips_nulls_from_mapper () {
47
+ Binder ().apply {
48
+ bind(source to sink using { if (it % 2 == 0 ) null else it })
49
+ }
50
+
51
+ source.invoke(0 )
52
+ source.invoke(1 )
53
+ source.invoke(2 )
54
+ assertEquals(listOf (1 ), sink.values)
55
+ }
56
+
57
+ @Test
58
+ fun binder_without_lifecycle_connects_source_and_sink_using_connector () {
59
+ val connector = NotNullConnector <Int , Int > { it }
60
+ Binder ().apply {
61
+ bind(source to sink using connector)
62
+ }
63
+
64
+ source.invoke(0 )
65
+ assertEquals(listOf (0 ), sink.values)
66
+ }
67
+ }
You can’t perform that action at this time.
0 commit comments