io.mockk.Answers.kt Maven / Gradle / Ivy
package io.mockk
/**
* Returns one constant reply
*/
data class ConstantAnswer(val constantValue: T) : Answer {
override fun answer(call: Call) = constantValue
override fun toString(): String = "const($constantValue)"
}
/**
* Delegates reply to the lambda function
*/
data class FunctionAnswer(val answerFunc: (Call) -> T) : Answer {
override fun answer(call: Call): T = answerFunc(call)
override fun toString(): String = "answer()"
}
/**
* Allows to check if has one more element in answer
*/
interface ManyAnswerable : Answer {
val hasMore: Boolean
}
/**
* Returns many different replies, each time moving the next list element.
* Stops at the end.
*/
data class ManyAnswersAnswer(val answers: List>) : ManyAnswerable {
private var n = 0
private var prevAnswer: Answer? = null
val manyAnswers = answers.map { if (it is ManyAnswerable) it else SingleAnswer(it) }
inner class SingleAnswer(val wrapped: Answer) : ManyAnswerable {
var answered = false
override val hasMore: Boolean
get() = !answered
override fun answer(call: Call): T {
answered = true
return wrapped.answer(call)
}
}
private fun nextAnswerable(): ManyAnswerable? {
while (n < answers.size) {
if (manyAnswers[n].hasMore) {
return manyAnswers[n]
}
prevAnswer = manyAnswers[n]
n++
}
return null
}
override val hasMore: Boolean
get() = nextAnswerable()?.hasMore ?: false
override fun answer(call: Call): T {
val next = nextAnswerable()
if (next != null) {
return next.answer(call)
}
val prev = prevAnswer
if (prev != null) {
return prev.answer(call)
}
throw RuntimeException("In many answers answer no answer available")
}
}
/**
* Throws exception instead of function reply
*/
data class ThrowingAnswer(val ex: Throwable) : Answer {
override fun answer(call: Call): Nothing {
throw ex
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy