Add result type to EnsureCalledOnceWithParam
This commit is contained in:
parent
c3f6dbd1c1
commit
6c96b53d9f
1 changed files with 15 additions and 6 deletions
|
|
@ -35,15 +35,17 @@ fun ensureCalledOnce(block: (callback: EnsureCalledOnce) -> Unit) {
|
||||||
callback.assertSuccess()
|
callback.assertSuccess()
|
||||||
}
|
}
|
||||||
|
|
||||||
class EnsureCalledOnceWithParam<T>(
|
class EnsureCalledOnceWithParam<T, R>(
|
||||||
private val expectedParam: T
|
private val expectedParam: T,
|
||||||
) : (T) -> Unit {
|
private val result: R,
|
||||||
|
) : (T) -> R {
|
||||||
private var counter = 0
|
private var counter = 0
|
||||||
override fun invoke(p1: T) {
|
override fun invoke(p1: T): R {
|
||||||
if (p1 != expectedParam) {
|
if (p1 != expectedParam) {
|
||||||
throw AssertionError("Expected to be called with $expectedParam, but was called with $p1")
|
throw AssertionError("Expected to be called with $expectedParam, but was called with $p1")
|
||||||
}
|
}
|
||||||
counter++
|
counter++
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
fun assertSuccess() {
|
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)
|
block(callback)
|
||||||
callback.assertSuccess()
|
callback.assertSuccess()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue