Skip to content

Instantly share code, notes, and snippets.

@weixinfree
Last active January 10, 2023 10:56
Show Gist options
  • Select an option

  • Save weixinfree/00707f3649d40b8d93de6febda6c7158 to your computer and use it in GitHub Desktop.

Select an option

Save weixinfree/00707f3649d40b8d93de6febda6c7158 to your computer and use it in GitHub Desktop.
kotlin test routine 封装 given,when,then
fun <T> given(desc: String = "", block: () -> T): TestRoutine<T> {
return TestRoutine("given: $desc", block)
}
class TestRoutine<T>(
private val message: String = "",
private val block: () -> T
) {
fun <R> when_(desc: String = "", whenBlock: (T) -> R): TestRoutine<R> {
val block = { whenBlock(block()) }
return TestRoutine("$message\nwhen: $desc", block)
}
fun then(desc: String = "", assertBlock: (T) -> Unit) {
assertBlock(block())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment