From 053ca4852c6f57e9dd438dafc22dacd3c5de494c Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Sun, 6 May 2018 10:26:55 -0400 Subject: [PATCH] Convert acceptance tests to kotlin --- .../kotlin/acceptance/AcceptanceTest.java | 10 --- src/test/kotlin/acceptance/AcceptanceTest.kt | 10 +++ .../fixture/LispInterpreterFixture.java | 76 ------------------- .../fixture/LispInterpreterFixture.kt | 74 ++++++++++++++++++ 4 files changed, 84 insertions(+), 86 deletions(-) delete mode 100644 src/test/kotlin/acceptance/AcceptanceTest.java create mode 100644 src/test/kotlin/acceptance/AcceptanceTest.kt delete mode 100644 src/test/kotlin/acceptance/fixture/LispInterpreterFixture.java create mode 100644 src/test/kotlin/acceptance/fixture/LispInterpreterFixture.kt diff --git a/src/test/kotlin/acceptance/AcceptanceTest.java b/src/test/kotlin/acceptance/AcceptanceTest.java deleted file mode 100644 index 854d208..0000000 --- a/src/test/kotlin/acceptance/AcceptanceTest.java +++ /dev/null @@ -1,10 +0,0 @@ -package acceptance; - -import fitnesse.junit.FitNesseRunner; -import org.junit.runner.RunWith; - -@RunWith(FitNesseRunner.class) -@FitNesseRunner.Suite("TranscendentalLisp") -@FitNesseRunner.FitnesseDir("fitnesse") -@FitNesseRunner.OutputDir("fitnesse/fitnesse-results") -public class AcceptanceTest {} diff --git a/src/test/kotlin/acceptance/AcceptanceTest.kt b/src/test/kotlin/acceptance/AcceptanceTest.kt new file mode 100644 index 0000000..0e79dd7 --- /dev/null +++ b/src/test/kotlin/acceptance/AcceptanceTest.kt @@ -0,0 +1,10 @@ +package acceptance + +import fitnesse.junit.FitNesseRunner +import org.junit.runner.RunWith + +@RunWith(FitNesseRunner::class) +@FitNesseRunner.Suite("TranscendentalLisp") +@FitNesseRunner.FitnesseDir("fitnesse") +@FitNesseRunner.OutputDir("fitnesse/fitnesse-results") +class AcceptanceTest diff --git a/src/test/kotlin/acceptance/fixture/LispInterpreterFixture.java b/src/test/kotlin/acceptance/fixture/LispInterpreterFixture.java deleted file mode 100644 index 4370b95..0000000 --- a/src/test/kotlin/acceptance/fixture/LispInterpreterFixture.java +++ /dev/null @@ -1,76 +0,0 @@ -package acceptance.fixture; - -import application.LispMain; -import environment.RuntimeEnvironment; -import interpreter.LispInterpreter; -import interpreter.LispInterpreterBuilder; -import table.ExecutionContext; -import table.FunctionTable; -import util.Path; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.PrintStream; - -public class LispInterpreterFixture { - - private static ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - private static ExecutionContext executionContext = ExecutionContext.INSTANCE; - private static RuntimeEnvironment environment = RuntimeEnvironment.INSTANCE; - private static LispInterpreter interpreter = null; - - public static void resetInterpreter() { - cleanUp(); - buildInterpreter(); - } - - public static void cleanUp() { - LispInterpreterBuilder.INSTANCE.reset(); - FunctionTable.INSTANCE.resetFunctionTable(); - executionContext.clearContext(); - environment.reset(); - } - - public static void buildInterpreter() { - LispInterpreterBuilder.INSTANCE.setOutput(new PrintStream(outputStream)); - LispInterpreterBuilder.INSTANCE.setErrorOutput(new PrintStream(outputStream)); - LispInterpreterBuilder.INSTANCE.setNotInteractive(); - LispInterpreterBuilder.INSTANCE.setLanguageFileNames(LispMain.Companion.getLANGUAGE_FILE_NAMES()); - // LispInterpreterBuilder.INSTANCE.setTerminationFunction(LispInterpreterFixture::terminate); - // LispInterpreterBuilder.INSTANCE.setErrorTerminationFunction(LispInterpreterFixture::terminateFromError); - - interpreter = LispInterpreterBuilder.INSTANCE.build(); - } - - public static void terminate() {} - - public static void terminateFromError() { - throw new RuntimeException("Error Termination"); - } - - public String evaluateText(String input) { - environment.setInputName("fitnesse"); - environment.setInput(new ByteArrayInputStream(input.getBytes())); - environment.setPath(""); - - return evaluate(); - } - - public String evaluateFile(String inputFile) throws FileNotFoundException { - environment.setInputName(inputFile); - environment.setInput(new FileInputStream(inputFile)); - environment.setPath(Path.INSTANCE.getPathPrefix(inputFile)); - - return evaluate(); - } - - private String evaluate() { - interpreter.interpret(); - String output = outputStream.toString(); - outputStream.reset(); - - return output.trim(); - } -} diff --git a/src/test/kotlin/acceptance/fixture/LispInterpreterFixture.kt b/src/test/kotlin/acceptance/fixture/LispInterpreterFixture.kt new file mode 100644 index 0000000..24765cc --- /dev/null +++ b/src/test/kotlin/acceptance/fixture/LispInterpreterFixture.kt @@ -0,0 +1,74 @@ +package acceptance.fixture + +import application.LispMain +import environment.RuntimeEnvironment +import interpreter.LispInterpreter +import interpreter.LispInterpreterBuilder +import table.ExecutionContext +import table.FunctionTable +import util.Path + +import java.io.ByteArrayInputStream +import java.io.ByteArrayOutputStream +import java.io.FileInputStream +import java.io.FileNotFoundException +import java.io.PrintStream + +class LispInterpreterFixture { + + fun evaluateText(input: String): String { + RuntimeEnvironment.inputName = "fitnesse" + RuntimeEnvironment.input = ByteArrayInputStream(input.toByteArray()) + RuntimeEnvironment.path = "" + + return evaluate() + } + + @Throws(FileNotFoundException::class) + fun evaluateFile(inputFile: String): String { + RuntimeEnvironment.inputName = inputFile + RuntimeEnvironment.input = FileInputStream(inputFile) + RuntimeEnvironment.path = Path.getPathPrefix(inputFile) + + return evaluate() + } + + private fun evaluate(): String { + interpreter.interpret() + val output = outputStream.toString() + outputStream.reset() + + return output.trim() + } + + companion object { + + private val outputStream = ByteArrayOutputStream() + private lateinit var interpreter: LispInterpreter + + @JvmStatic + fun resetInterpreter() { + cleanUp() + buildInterpreter() + } + + @JvmStatic + fun cleanUp() { + LispInterpreterBuilder.reset() + FunctionTable.resetFunctionTable() + ExecutionContext.clearContext() + RuntimeEnvironment.reset() + } + + private fun buildInterpreter() { + LispInterpreterBuilder.setOutput(PrintStream(outputStream)) + LispInterpreterBuilder.setErrorOutput(PrintStream(outputStream)) + LispInterpreterBuilder.setNotInteractive() + LispInterpreterBuilder.setLanguageFileNames(*LispMain.LANGUAGE_FILE_NAMES) + LispInterpreterBuilder.setTerminationFunction { } + LispInterpreterBuilder.setErrorTerminationFunction { throw RuntimeException("Error Termination") } + + interpreter = LispInterpreterBuilder.build() + } + } +}