Skip to content

Instantly share code, notes, and snippets.

@gumil
Created April 3, 2019 07:51
Show Gist options
  • Save gumil/f373572f67bce365116a9de9067c14dd to your computer and use it in GitHub Desktop.
Save gumil/f373572f67bce365116a9de9067c14dd to your computer and use it in GitHub Desktop.
import kotlin.test.assertEquals
import kotlin.test.assertTrue
internal class Verifier<T> {
val function: (T) -> Unit = {
invokedValues.add(it)
}
private var invokedValues = mutableListOf<T>()
fun verifyInvokedWithValue(value: T, times: Int = 1) {
assertEquals(invokedValues.filter { it == value }.size, times)
}
fun verifyNoInvocations() {
assertTrue { invokedValues.isEmpty() }
}
fun verifyOrder(verifyBuilder: OrderedBuilder<T>.() -> Unit) {
val orderedBuilder = OrderedBuilder<T>()
verifyBuilder(orderedBuilder)
orderedBuilder.values.forEachIndexed { index, value ->
assertEquals(invokedValues[index], value)
}
}
internal class OrderedBuilder<T> {
private val _values = mutableListOf<T>()
val values: List<T> = _values
fun verify(value: T) {
_values.add(value)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment