mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-21 23:00:43 -08:00
Createa a build_00_utilities project containing testing helpers, including the ConsoleTest abstract class.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
package com.pcholt.console.testutils
|
||||
|
||||
import com.google.common.truth.Truth
|
||||
import org.junit.Rule
|
||||
import org.junit.contrib.java.lang.system.SystemOutRule
|
||||
@@ -1,3 +1,4 @@
|
||||
import com.pcholt.console.testutils.ConsoleTest
|
||||
import org.junit.Test
|
||||
|
||||
class AnimalJavaTest : ConsoleTest() {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import com.pcholt.console.testutils.ConsoleTest
|
||||
import org.junit.Test
|
||||
|
||||
class AnimalKtTest : ConsoleTest() {
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
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(), " ")
|
||||
}
|
||||
Reference in New Issue
Block a user