File tree 8 files changed +28
-30
lines changed
kotlinx-coroutines-core/common
8 files changed +28
-30
lines changed Original file line number Diff line number Diff line change @@ -123,7 +123,7 @@ public suspend fun <T> Flow<T>.first(predicate: suspend (T) -> Boolean): T {
123
123
124
124
/* *
125
125
* The terminal operator that returns the first element emitted by the flow and then cancels flow's collection.
126
- * Returns [ null] if the flow was empty.
126
+ * Returns ` null` if the flow was empty.
127
127
*/
128
128
public suspend fun <T : Any > Flow<T>.firstOrNull (): T ? {
129
129
var result: Any? = NULL
@@ -135,12 +135,12 @@ public suspend fun <T : Any> Flow<T>.firstOrNull(): T? {
135
135
} catch (e: AbortFlowException ) {
136
136
// Do nothing
137
137
}
138
- return result.takeUnless { it == NULL } as T ?
138
+ return result.takeUnless { it == = NULL } as T ?
139
139
}
140
140
141
141
/* *
142
- * The terminal operator that returns the first element emitted by the flow and then cancels flow's collection.
143
- * Returns [ null] if the flow did not contain an element matching the [predicate].
142
+ * The terminal operator that returns the first element emitted by the flow matching the given [predicate] and then cancels flow's collection.
143
+ * Returns ` null` if the flow did not contain an element matching the [predicate].
144
144
*/
145
145
public suspend fun <T : Any > Flow<T>.firstOrNull (predicate : suspend (T ) -> Boolean ): T ? {
146
146
var result: Any? = NULL
@@ -154,5 +154,5 @@ public suspend fun <T : Any> Flow<T>.firstOrNull(predicate: suspend (T) -> Boole
154
154
} catch (e: AbortFlowException ) {
155
155
// Do nothing
156
156
}
157
- return result.takeUnless { it == NULL } as T ?
157
+ return result.takeUnless { it == = NULL } as T ?
158
158
}
Original file line number Diff line number Diff line change @@ -210,12 +210,6 @@ class AsyncTest : TestBase() {
210
210
finish(13 )
211
211
}
212
212
213
- class BadClass {
214
- override fun equals (other : Any? ): Boolean = error(" equals" )
215
- override fun hashCode (): Int = error(" hashCode" )
216
- override fun toString (): String = error(" toString" )
217
- }
218
-
219
213
@Test
220
214
fun testDeferBadClass () = runTest {
221
215
val bad = BadClass ()
Original file line number Diff line number Diff line change @@ -80,3 +80,8 @@ public fun wrapperDispatcher(context: CoroutineContext): CoroutineContext {
80
80
81
81
public suspend fun wrapperDispatcher (): CoroutineContext = wrapperDispatcher(coroutineContext)
82
82
83
+ class BadClass {
84
+ override fun equals (other : Any? ): Boolean = error(" equals" )
85
+ override fun hashCode (): Int = error(" hashCode" )
86
+ override fun toString (): String = error(" toString" )
87
+ }
Original file line number Diff line number Diff line change @@ -152,12 +152,6 @@ class WithTimeoutOrNullTest : TestBase() {
152
152
assertSame(bad, result)
153
153
}
154
154
155
- class BadClass {
156
- override fun equals (other : Any? ): Boolean = error(" Should not be called" )
157
- override fun hashCode (): Int = error(" Should not be called" )
158
- override fun toString (): String = error(" Should not be called" )
159
- }
160
-
161
155
@Test
162
156
fun testNullOnTimeout () = runTest {
163
157
expect(1 )
Original file line number Diff line number Diff line change @@ -107,12 +107,6 @@ class WithTimeoutTest : TestBase() {
107
107
assertSame(bad, result)
108
108
}
109
109
110
- class BadClass {
111
- override fun equals (other : Any? ): Boolean = error(" Should not be called" )
112
- override fun hashCode (): Int = error(" Should not be called" )
113
- override fun toString (): String = error(" Should not be called" )
114
- }
115
-
116
110
@Test
117
111
fun testExceptionOnTimeout () = runTest {
118
112
expect(1 )
Original file line number Diff line number Diff line change @@ -241,12 +241,6 @@ class RendezvousChannelTest : TestBase() {
241
241
finish(12 )
242
242
}
243
243
244
- class BadClass {
245
- override fun equals (other : Any? ): Boolean = error(" equals" )
246
- override fun hashCode (): Int = error(" hashCode" )
247
- override fun toString (): String = error(" toString" )
248
- }
249
-
250
244
@Test
251
245
fun testProduceBadClass () = runTest {
252
246
val bad = BadClass ()
Original file line number Diff line number Diff line change @@ -150,4 +150,14 @@ class FirstTest : TestBase() {
150
150
assertEquals(1 , flow.firstOrNull())
151
151
finish(2 )
152
152
}
153
+
154
+ @Test
155
+ fun testBadClass () = runTest {
156
+ val instance = BadClass ()
157
+ val flow = flowOf(instance)
158
+ assertSame(instance, flow.first())
159
+ assertSame(instance, flow.firstOrNull())
160
+ assertSame(instance, flow.first { true })
161
+ assertSame(instance, flow.firstOrNull { true })
162
+ }
153
163
}
Original file line number Diff line number Diff line change @@ -17,7 +17,6 @@ class SingleTest : TestBase() {
17
17
18
18
assertEquals(239L , flow.single())
19
19
assertEquals(239L , flow.singleOrNull())
20
-
21
20
}
22
21
23
22
@Test
@@ -63,4 +62,12 @@ class SingleTest : TestBase() {
63
62
assertNull(flowOf<Int ?>(null ).single())
64
63
assertFailsWith<NoSuchElementException > { flowOf<Int ?>().single() }
65
64
}
65
+
66
+ @Test
67
+ fun testBadClass () = runTest {
68
+ val instance = BadClass ()
69
+ val flow = flowOf(instance)
70
+ assertSame(instance, flow.single())
71
+ assertSame(instance, flow.singleOrNull())
72
+ }
66
73
}
You can’t perform that action at this time.
0 commit comments