mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 15:16:33 -08:00
37 lines
1.0 KiB
Kotlin
37 lines
1.0 KiB
Kotlin
package com.pcholt.console.testutils
|
|
|
|
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(), " ")
|
|
} |