@@ -3,19 +3,21 @@ package com.github.michaelbull.result.coroutines.binding
3
3
import com.github.michaelbull.result.Err
4
4
import com.github.michaelbull.result.Ok
5
5
import com.github.michaelbull.result.Result
6
- import com.github.michaelbull.result. coroutines.runBlockingTest
6
+ import kotlinx. coroutines.ExperimentalCoroutinesApi
7
7
import kotlinx.coroutines.delay
8
+ import kotlinx.coroutines.test.runTest
8
9
import kotlin.test.Test
9
10
import kotlin.test.assertEquals
10
11
import kotlin.test.assertFalse
11
12
import kotlin.test.assertTrue
12
13
14
+ @ExperimentalCoroutinesApi
13
15
class SuspendableBindingTest {
14
16
15
17
private object BindingError
16
18
17
19
@Test
18
- fun returnsOkIfAllBindsSuccessful () {
20
+ fun returnsOkIfAllBindsSuccessful () = runTest {
19
21
suspend fun provideX (): Result <Int , BindingError > {
20
22
delay(1 )
21
23
return Ok (1 )
@@ -26,23 +28,20 @@ class SuspendableBindingTest {
26
28
return Ok (2 )
27
29
}
28
30
29
- return runBlockingTest {
30
- val result = binding<Int , BindingError > {
31
- val x = provideX().bind()
32
- val y = provideY().bind()
33
- x + y
34
- }
35
-
36
- assertTrue(result is Ok )
37
- assertEquals(
38
- expected = 3 ,
39
- actual = result.value
40
- )
31
+ val result = binding<Int , BindingError > {
32
+ val x = provideX().bind()
33
+ val y = provideY().bind()
34
+ x + y
41
35
}
36
+
37
+ assertEquals(
38
+ expected = Ok (3 ),
39
+ actual = result
40
+ )
42
41
}
43
42
44
43
@Test
45
- fun returnsOkIfAllBindsOfDifferentTypeAreSuccessful () {
44
+ fun returnsOkIfAllBindsOfDifferentTypeAreSuccessful () = runTest {
46
45
suspend fun provideX (): Result <String , BindingError > {
47
46
delay(1 )
48
47
return Ok (" 1" )
@@ -53,23 +52,20 @@ class SuspendableBindingTest {
53
52
return Ok (x + 2 )
54
53
}
55
54
56
- return runBlockingTest {
57
- val result = binding<Int , BindingError > {
58
- val x = provideX().bind()
59
- val y = provideY(x.toInt()).bind()
60
- y
61
- }
62
-
63
- assertTrue(result is Ok )
64
- assertEquals(
65
- expected = 3 ,
66
- actual = result.value
67
- )
55
+ val result = binding<Int , BindingError > {
56
+ val x = provideX().bind()
57
+ val y = provideY(x.toInt()).bind()
58
+ y
68
59
}
60
+
61
+ assertEquals(
62
+ expected = Ok (3 ),
63
+ actual = result
64
+ )
69
65
}
70
66
71
67
@Test
72
- fun returnsFirstErrIfBindingFailed () {
68
+ fun returnsFirstErrIfBindingFailed () = runTest {
73
69
suspend fun provideX (): Result <Int , BindingError > {
74
70
delay(1 )
75
71
return Ok (1 )
@@ -85,27 +81,25 @@ class SuspendableBindingTest {
85
81
return Ok (2 )
86
82
}
87
83
88
- return runBlockingTest {
89
- val result = binding<Int , BindingError > {
90
- val x = provideX().bind()
91
- val y = provideY().bind()
92
- val z = provideZ().bind()
93
- x + y + z
94
- }
95
-
96
- assertTrue(result is Err )
97
- assertEquals(
98
- expected = BindingError ,
99
- actual = result.error
100
- )
84
+ val result = binding<Int , BindingError > {
85
+ val x = provideX().bind()
86
+ val y = provideY().bind()
87
+ val z = provideZ().bind()
88
+ x + y + z
101
89
}
90
+
91
+ assertEquals(
92
+ expected = Err (BindingError ),
93
+ actual = result
94
+ )
102
95
}
103
96
104
97
@Test
105
- fun returnsStateChangedUntilFirstBindFailed () {
98
+ fun returnsStateChangedUntilFirstBindFailed () = runTest {
106
99
var xStateChange = false
107
100
var yStateChange = false
108
101
var zStateChange = false
102
+
109
103
suspend fun provideX (): Result <Int , BindingError > {
110
104
delay(1 )
111
105
xStateChange = true
@@ -124,27 +118,25 @@ class SuspendableBindingTest {
124
118
return Err (BindingError )
125
119
}
126
120
127
- runBlockingTest {
128
- val result = binding<Int , BindingError > {
129
- val x = provideX().bind()
130
- val y = provideY().bind()
131
- val z = provideZ().bind()
132
- x + y + z
133
- }
134
-
135
- assertTrue(result is Err )
136
- assertEquals(
137
- expected = BindingError ,
138
- actual = result.error
139
- )
140
- assertTrue(xStateChange)
141
- assertTrue(yStateChange)
142
- assertFalse(zStateChange)
121
+ val result = binding<Int , BindingError > {
122
+ val x = provideX().bind()
123
+ val y = provideY().bind()
124
+ val z = provideZ().bind()
125
+ x + y + z
143
126
}
127
+
128
+ assertEquals(
129
+ expected = Err (BindingError ),
130
+ actual = result
131
+ )
132
+
133
+ assertTrue(xStateChange)
134
+ assertTrue(yStateChange)
135
+ assertFalse(zStateChange)
144
136
}
145
137
146
138
@Test
147
- fun returnsFirstErrIfBindingsOfDifferentTypesFailed () {
139
+ fun returnsFirstErrIfBindingsOfDifferentTypesFailed () = runTest {
148
140
suspend fun provideX (): Result <Int , BindingError > {
149
141
delay(1 )
150
142
return Ok (1 )
@@ -160,19 +152,16 @@ class SuspendableBindingTest {
160
152
return Ok (2 )
161
153
}
162
154
163
- return runBlockingTest {
164
- val result = binding<Int , BindingError > {
165
- val x = provideX().bind()
166
- val y = provideY().bind()
167
- val z = provideZ().bind()
168
- x + y.toInt() + z
169
- }
170
-
171
- assertTrue(result is Err )
172
- assertEquals(
173
- expected = BindingError ,
174
- actual = result.error
175
- )
155
+ val result = binding<Int , BindingError > {
156
+ val x = provideX().bind()
157
+ val y = provideY().bind()
158
+ val z = provideZ().bind()
159
+ x + y.toInt() + z
176
160
}
161
+
162
+ assertEquals(
163
+ expected = Err (BindingError ),
164
+ actual = result
165
+ )
177
166
}
178
167
}
0 commit comments