-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #541 from pcholt/main
JVM Test framework
- Loading branch information
Showing
17 changed files
with
302 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
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(), " ") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import com.pcholt.console.testutils.ConsoleTest | ||
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. | ||
""" | ||
} | ||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import com.pcholt.console.testutils.ConsoleTest | ||
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. | ||
""" | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
sourceSets { | ||
test { | ||
java { | ||
srcDirs "../../$testSource" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
testSource=00_Utilities/jvmTestUtils/kotlin/test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
sourceSets { | ||
main { | ||
java { | ||
srcDirs "../../$gameSource" | ||
} | ||
} | ||
} | ||
|
||
application { | ||
mainClass = gameMain | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
gameSource=01_Acey_Ducey/java/src | ||
gameMain=AceyDucey17 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
gameSource=03_Animal/java | ||
gameSource=03_Animal/java/src | ||
gameTest=03_Animal/java/test | ||
gameMain=Animal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
gameSource=03_Animal/kotlin | ||
gameSource=03_Animal/kotlin/src | ||
gameTest=03_Animal/kotlin/test | ||
gameMain=AnimalKt |
Oops, something went wrong.