Files
basic-computer-games/03_Animal/kotlin/test/ConsoleTest.kt
Paul Holt 302e1a0e0b Testing added! Now some full behaviour tests on both Animal JVM implementations.
Still todo: Move the ConsoleTest library into its own module, add a gradle dependency on it.
2022-01-29 22:36:29 +11:00

35 lines
987 B
Kotlin

import com.google.common.truth.Truth
import org.junit.Rule
import org.junit.contrib.java.lang.system.SystemOutRule
import org.junit.contrib.java.lang.system.TextFromStandardInputStream
abstract class ConsoleTest {
@get:Rule
val inputRule = TextFromStandardInputStream.emptyStandardInputStream()
@get:Rule
val systemOutRule = SystemOutRule().enableLog()
val regexInputCommand = "\\{(.*)}".toRegex()
fun assertConversation(conversation: String, runMain: () -> Unit) {
inputRule.provideLines(*regexInputCommand
.findAll(conversation)
.map { it.groupValues[1] }
.toList().toTypedArray())
runMain()
Truth.assertThat(
systemOutRule.log.trimWhiteSpace()
)
.isEqualTo(
regexInputCommand
.replace(conversation, "").trimWhiteSpace()
)
}
private fun String.trimWhiteSpace() =
replace("[\\s]+".toRegex(), " ")
}