forked from Kotlin/kotlinx.coroutines
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Flow.last and Flow.lastOrNull operators (Kotlin#2662)
Fixes Kotlin#2246
- Loading branch information
1 parent
5c3b008
commit 76f157f
Showing
3 changed files
with
72 additions
and
0 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
45 changes: 45 additions & 0 deletions
45
kotlinx-coroutines-core/common/test/flow/terminal/LastTest.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,45 @@ | ||
/* | ||
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.coroutines.flow | ||
|
||
import kotlinx.coroutines.* | ||
import kotlin.test.* | ||
|
||
class LastTest : TestBase() { | ||
@Test | ||
fun testLast() = runTest { | ||
val flow = flowOf(1, 2, 3) | ||
assertEquals(3, flow.last()) | ||
assertEquals(3, flow.lastOrNull()) | ||
} | ||
|
||
@Test | ||
fun testNulls() = runTest { | ||
val flow = flowOf(1, null) | ||
assertNull(flow.last()) | ||
assertNull(flow.lastOrNull()) | ||
} | ||
|
||
@Test | ||
fun testNullsLastOrNull() = runTest { | ||
val flow = flowOf(null, 1) | ||
assertEquals(1, flow.lastOrNull()) | ||
} | ||
|
||
@Test | ||
fun testEmptyFlow() = runTest { | ||
assertFailsWith<NoSuchElementException> { emptyFlow<Int>().last() } | ||
assertNull(emptyFlow<Int>().lastOrNull()) | ||
} | ||
|
||
@Test | ||
fun testBadClass() = runTest { | ||
val instance = BadClass() | ||
val flow = flowOf(instance) | ||
assertSame(instance, flow.last()) | ||
assertSame(instance, flow.lastOrNull()) | ||
|
||
} | ||
} |