Merge pull request #2365 from element-hq/feature/bma/testRoomList

Add Unit tests on MessagesView
This commit is contained in:
Benoit Marty 2024-02-08 15:02:47 +01:00 committed by GitHub
commit 5527c9634b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 394 additions and 51 deletions

View file

@ -35,15 +35,17 @@ fun ensureCalledOnce(block: (callback: EnsureCalledOnce) -> Unit) {
callback.assertSuccess()
}
class EnsureCalledOnceWithParam<T>(
private val expectedParam: T
) : (T) -> Unit {
class EnsureCalledOnceWithParam<T, R>(
private val expectedParam: T,
private val result: R,
) : (T) -> R {
private var counter = 0
override fun invoke(p1: T) {
override fun invoke(p1: T): R {
if (p1 != expectedParam) {
throw AssertionError("Expected to be called with $expectedParam, but was called with $p1")
}
counter++
return result
}
fun assertSuccess() {
@ -53,8 +55,15 @@ class EnsureCalledOnceWithParam<T>(
}
}
fun <T> ensureCalledOnceWithParam(param: T, block: (callback: EnsureCalledOnceWithParam<T>) -> Unit) {
val callback = EnsureCalledOnceWithParam(param)
/**
* Shortcut for [<T, R> ensureCalledOnceWithParam] with Unit result.
*/
fun <T> ensureCalledOnceWithParam(param: T, block: (callback: EnsureCalledOnceWithParam<T, Unit>) -> Unit) {
ensureCalledOnceWithParam(param, block, Unit)
}
fun <T, R> ensureCalledOnceWithParam(param: T, block: (callback: EnsureCalledOnceWithParam<T, R>) -> R, result: R) {
val callback = EnsureCalledOnceWithParam(param, result)
block(callback)
callback.assertSuccess()
}

View file

@ -28,6 +28,12 @@ class EnsureNeverCalledWithParam<T> : (T) -> Unit {
}
}
class EnsureNeverCalledWithParamAndResult<T, R> : (T) -> R {
override fun invoke(p1: T): R {
throw AssertionError("Should not be called and is called with $p1")
}
}
class EnsureNeverCalledWithTwoParams<T, U> : (T, U) -> Unit {
override fun invoke(p1: T, p2: U) {
throw AssertionError("Should not be called and is called with $p1 and $p2")