mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 23:26:40 -08:00
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.
This commit is contained in:
@@ -74,11 +74,11 @@ public class Animal {
|
||||
private static void askForInformationAndSave(Scanner scan, AnimalNode current, QuestionNode previous, boolean previousToCurrentDecisionChoice) {
|
||||
//Failed to get it right and ran out of questions
|
||||
//Let's ask the user for the new information
|
||||
System.out.print("THE ANIMAL YOU WERE THINKING OF WAS A ");
|
||||
System.out.print("THE ANIMAL YOU WERE THINKING OF WAS A ? ");
|
||||
String animal = scan.nextLine();
|
||||
System.out.printf("PLEASE TYPE IN A QUESTION THAT WOULD DISTINGUISH A %s FROM A %s ", animal, current.getAnimal());
|
||||
System.out.printf("PLEASE TYPE IN A QUESTION THAT WOULD DISTINGUISH A %s FROM A %s ? ", animal, current.getAnimal());
|
||||
String newQuestion = scan.nextLine();
|
||||
System.out.printf("FOR A %s THE ANSWER WOULD BE ", animal);
|
||||
System.out.printf("FOR A %s THE ANSWER WOULD BE ? ", animal);
|
||||
boolean newAnswer = readYesOrNo(scan);
|
||||
//Add it to our question store
|
||||
addNewAnimal(current, previous, animal, newQuestion, newAnswer, previousToCurrentDecisionChoice);
|
||||
54
03_Animal/java/test/AnimalJavaTest.kt
Normal file
54
03_Animal/java/test/AnimalJavaTest.kt
Normal file
@@ -0,0 +1,54 @@
|
||||
import org.junit.Test
|
||||
|
||||
class AnimalJavaTest : ConsoleTest() {
|
||||
|
||||
@Test
|
||||
fun `given a standard setup, find the fish`() {
|
||||
assertConversation(
|
||||
"""
|
||||
$title
|
||||
ARE YOU THINKING OF AN ANIMAL ? {YES}
|
||||
DOES IT SWIM ? {YES}
|
||||
IS IT A FISH ? {YES}
|
||||
WHY NOT TRY ANOTHER ANIMAL?
|
||||
ARE YOU THINKING OF AN ANIMAL ? {QUIT}
|
||||
"""
|
||||
) {
|
||||
Animal.main(emptyArray())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given a standard setup, create a cow, and verify`() {
|
||||
assertConversation(
|
||||
"""
|
||||
$title
|
||||
ARE YOU THINKING OF AN ANIMAL ? {YES}
|
||||
DOES IT SWIM ? {NO}
|
||||
IS IT A BIRD ? {NO}
|
||||
THE ANIMAL YOU WERE THINKING OF WAS A ? {COW}
|
||||
PLEASE TYPE IN A QUESTION THAT WOULD DISTINGUISH A
|
||||
COW FROM A BIRD
|
||||
? {DOES IT EAT GRASS}
|
||||
FOR A COW THE ANSWER WOULD BE ? {YES}
|
||||
ARE YOU THINKING OF AN ANIMAL ? {YES}
|
||||
DOES IT SWIM ? {NO}
|
||||
DOES IT EAT GRASS ? {YES}
|
||||
IS IT A COW ? {YES}
|
||||
WHY NOT TRY ANOTHER ANIMAL?
|
||||
ARE YOU THINKING OF AN ANIMAL ? {QUIT}
|
||||
"""
|
||||
) {
|
||||
Animal.main(emptyArray())
|
||||
}
|
||||
}
|
||||
|
||||
private val title = """
|
||||
ANIMAL
|
||||
CREATIVE COMPUTING MORRISTOWN, NEW JERSEY
|
||||
|
||||
PLAY 'GUESS THE ANIMAL'
|
||||
THINK OF AN ANIMAL AND THE COMPUTER WILL TRY TO GUESS IT.
|
||||
"""
|
||||
}
|
||||
|
||||
35
03_Animal/java/test/ConsoleTest.kt
Normal file
35
03_Animal/java/test/ConsoleTest.kt
Normal file
@@ -0,0 +1,35 @@
|
||||
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(), " ")
|
||||
}
|
||||
54
03_Animal/kotlin/test/AnimalKtTest.kt
Normal file
54
03_Animal/kotlin/test/AnimalKtTest.kt
Normal file
@@ -0,0 +1,54 @@
|
||||
import org.junit.Test
|
||||
|
||||
class AnimalKtTest : ConsoleTest() {
|
||||
|
||||
@Test
|
||||
fun `given a standard setup, find the fish`() {
|
||||
assertConversation(
|
||||
"""
|
||||
$title
|
||||
ARE YOU THINKING OF AN ANIMAL? {YES}
|
||||
DOES IT SWIM? {YES}
|
||||
IS IT A FISH? {YES}
|
||||
WHY NOT TRY ANOTHER ANIMAL?
|
||||
ARE YOU THINKING OF AN ANIMAL? {QUIT}
|
||||
"""
|
||||
) {
|
||||
main()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given a standard setup, create a cow, and verify`() {
|
||||
assertConversation(
|
||||
"""
|
||||
$title
|
||||
ARE YOU THINKING OF AN ANIMAL? {YES}
|
||||
DOES IT SWIM? {NO}
|
||||
IS IT A BIRD? {NO}
|
||||
THE ANIMAL YOU WERE THINKING OF WAS A? {COW}
|
||||
PLEASE TYPE IN A QUESTION THAT WOULD DISTINGUISH A
|
||||
COW FROM A BIRD
|
||||
? {DOES IT EAT GRASS}
|
||||
FOR A COW THE ANSWER WOULD BE? {YES}
|
||||
ARE YOU THINKING OF AN ANIMAL? {YES}
|
||||
DOES IT SWIM? {NO}
|
||||
DOES IT EAT GRASS? {YES}
|
||||
IS IT A COW? {YES}
|
||||
WHY NOT TRY ANOTHER ANIMAL?
|
||||
ARE YOU THINKING OF AN ANIMAL? {QUIT}
|
||||
"""
|
||||
) {
|
||||
main()
|
||||
}
|
||||
}
|
||||
|
||||
private val title = """
|
||||
ANIMAL
|
||||
CREATIVE COMPUTING MORRISTOWN, NEW JERSEY
|
||||
|
||||
PLAY 'GUESS THE ANIMAL'
|
||||
THINK OF AN ANIMAL AND THE COMPUTER WILL TRY TO GUESS IT.
|
||||
"""
|
||||
}
|
||||
|
||||
35
03_Animal/kotlin/test/ConsoleTest.kt
Normal file
35
03_Animal/kotlin/test/ConsoleTest.kt
Normal file
@@ -0,0 +1,35 @@
|
||||
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