All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tinder.scarlet.testutils.TestStreamObserver.kt Maven / Gradle / Ivy

/*
 * © 2018 Match Group, LLC.
 */

package com.tinder.scarlet.testutils

import com.tinder.scarlet.Stream
import io.reactivex.processors.PublishProcessor
import org.assertj.core.api.Assertions.assertThat

class TestStreamObserver(stream: Stream) {
    private val publishProcessor = PublishProcessor.create()
    private val testSubscriber = publishProcessor.test()

    init {
        stream.subscribe(publishProcessor)
    }

    val values: List
        get() = testSubscriber.values()

    val errors: List
        get() = testSubscriber.errors()

    val completions: Long
        get() = testSubscriber.completions()

    fun awaitCount(exactly: Int) {
        testSubscriber.awaitCount(exactly)
        assertThat(values.size).isEqualTo(exactly)
    }

    fun awaitValues(vararg valueAsserts: ValueAssert) {
        awaitCount(valueAsserts.size)
        testSubscriber.assertNoErrors()
        valueAsserts.zip(values).forEachIndexed { index, (valueAssert, value) ->
            try {
                valueAssert.execute(value)
            } catch (cause: AssertionError) {
                throw AssertionError("Assertion at index $index failed", cause)
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy