aboutsummaryrefslogtreecommitdiffstats
path: root/kotlinx-coroutines-core/common/test/flow/terminal/LastTest.kt
blob: e7699ccc65c0e8931a181defbd350c33e0548f6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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())

    }
}