Refactor interpreter tests

This commit is contained in:
Mike Cifelli 2018-08-10 18:39:41 -04:00
parent c2c53e0d0f
commit c08edda548
3 changed files with 44 additions and 46 deletions

View File

@ -77,7 +77,7 @@ class RuntimeEnvironmentTest {
RuntimeEnvironment.terminationFunction = { indicatorSet.add(TERMINATED_SUCCESSFULLY) }
RuntimeEnvironment.terminateSuccessfully()
assertThat(indicatorSet.contains(TERMINATED_SUCCESSFULLY)).isTrue()
assertThat(indicatorSet).contains(TERMINATED_SUCCESSFULLY)
}
@Test
@ -85,7 +85,7 @@ class RuntimeEnvironmentTest {
RuntimeEnvironment.errorTerminationFunction = { indicatorSet.add(TERMINATED_EXCEPTIONALLY) }
RuntimeEnvironment.terminateExceptionally()
assertThat(indicatorSet.contains(TERMINATED_EXCEPTIONALLY)).isTrue()
assertThat(indicatorSet).contains(TERMINATED_EXCEPTIONALLY)
}
@Test

View File

@ -20,11 +20,11 @@ class ExitTest : SymbolAndFunctionCleaner() {
private val indicatorSet = mutableSetOf<String>()
private fun assertTerminated() {
assertThat(indicatorSet.contains(TERMINATED)).isTrue()
assertThat(indicatorSet).contains(TERMINATED)
}
private fun assertNotTerminated() {
assertThat(indicatorSet.contains(TERMINATED)).isFalse()
assertThat(indicatorSet).doesNotContain(TERMINATED)
}
override fun additionalSetUp() {

View File

@ -2,12 +2,10 @@ package interpreter
import environment.RuntimeEnvironment
import interpreter.InteractiveLispInterpreter.Companion.PROMPT
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import testutil.TestUtilities.createInputStreamFromString
import java.io.ByteArrayOutputStream
import java.io.PrintStream
@ -31,14 +29,14 @@ class LispInterpreterTest {
}
private fun assertTerminated() {
assertTrue(indicatorSet.contains(TERMINATED))
assertThat(indicatorSet).contains(TERMINATED)
}
private fun assertErrorMessageWritten() {
assertTrue(errorOutputStream.toByteArray().isNotEmpty())
assertThat(errorOutputStream.toByteArray()).isNotEmpty()
}
@Before
@BeforeEach()
fun setUp() {
indicatorSet.clear()
outputStream.reset()
@ -47,43 +45,43 @@ class LispInterpreterTest {
LispInterpreterBuilder.reset()
}
@After
@AfterEach()
fun tearDown() {
RuntimeEnvironment.reset()
LispInterpreterBuilder.reset()
}
@Test
fun buildInteractiveInterpreter() {
fun `build an interactive interpreter`() {
setCommonFeatures()
LispInterpreterBuilder.setInput(System.`in`, "stdin")
val interpreter = LispInterpreterBuilder.build()
assertTrue(interpreter is InteractiveLispInterpreter)
assertThat(interpreter is InteractiveLispInterpreter).isTrue()
}
@Test
fun buildNonInteractiveInterpreter() {
fun `build a non interactive interpreter`() {
setCommonFeatures()
LispInterpreterBuilder.setInput(System.`in`, "stdin")
LispInterpreterBuilder.setNotInteractive()
val interpreter = LispInterpreterBuilder.build()
assertFalse(interpreter is InteractiveLispInterpreter)
assertFalse(interpreter is FileLispInterpreter)
assertThat(interpreter is InteractiveLispInterpreter).isFalse()
assertThat(interpreter is FileLispInterpreter).isFalse()
}
@Test
fun buildFileBasedInterpreter() {
fun `build a file based interpreter`() {
setCommonFeatures()
LispInterpreterBuilder.useFile(FILE)
val interpreter = LispInterpreterBuilder.build()
assertTrue(interpreter is FileLispInterpreter)
assertThat(interpreter is FileLispInterpreter).isTrue()
}
@Test
fun attemptToBuildInterpreterOnBadFile() {
fun `building an interpreter on a bad file terminates correctly`() {
setCommonFeatures()
LispInterpreterBuilder.useFile("does-not-exist.lisp")
LispInterpreterBuilder.build()
@ -93,18 +91,18 @@ class LispInterpreterTest {
}
@Test
fun makeSureDecoratorsAreInitializedWithDefaults() {
fun `the decorators are initialized with the correct defaults`() {
LispInterpreterBuilder.build()
assertEquals("", RuntimeEnvironment.decoratePrompt(""))
assertEquals("", RuntimeEnvironment.decorateValueOutput(""))
assertEquals("", RuntimeEnvironment.decorateWarningOutput(""))
assertEquals("", RuntimeEnvironment.decorateErrorOutput(""))
assertEquals("", RuntimeEnvironment.decorateCriticalOutput(""))
assertThat(RuntimeEnvironment.decoratePrompt("")).isEmpty()
assertThat(RuntimeEnvironment.decorateValueOutput("")).isEmpty()
assertThat(RuntimeEnvironment.decorateWarningOutput("")).isEmpty()
assertThat(RuntimeEnvironment.decorateErrorOutput("")).isEmpty()
assertThat(RuntimeEnvironment.decorateCriticalOutput("")).isEmpty()
}
@Test
fun makeSureDecoratorsAreSetCorrectly() {
fun `the decorators are set correctly`() {
LispInterpreterBuilder.setPromptDecorator { s -> "#$s#" }
LispInterpreterBuilder.setValueOutputDecorator { s -> "@$s@" }
LispInterpreterBuilder.setWarningOutputDecorator { s -> "%$s%" }
@ -112,52 +110,52 @@ class LispInterpreterTest {
LispInterpreterBuilder.setCriticalOutputDecorator { s -> "$$s$" }
LispInterpreterBuilder.build()
assertEquals("#x#", RuntimeEnvironment.decoratePrompt("x"))
assertEquals("@x@", RuntimeEnvironment.decorateValueOutput("x"))
assertEquals("%x%", RuntimeEnvironment.decorateWarningOutput("x"))
assertEquals("*x*", RuntimeEnvironment.decorateErrorOutput("x"))
assertEquals("\$x$", RuntimeEnvironment.decorateCriticalOutput("x"))
assertThat(RuntimeEnvironment.decoratePrompt("x")).isEqualTo("#x#")
assertThat(RuntimeEnvironment.decorateValueOutput("x")).isEqualTo("@x@")
assertThat(RuntimeEnvironment.decorateWarningOutput("x")).isEqualTo("%x%")
assertThat(RuntimeEnvironment.decorateErrorOutput("x")).isEqualTo("*x*")
assertThat(RuntimeEnvironment.decorateCriticalOutput("x")).isEqualTo("\$x$")
}
@Test
fun fileBasedInterpreterWorks_PrintsLastValueOnly() {
fun `the file based interpreter works and prints the last value only`() {
setCommonFeatures()
LispInterpreterBuilder.useFile(FILE)
LispInterpreterBuilder.build().interpret()
assertEquals("PICKLE\n\n", outputStream.toString())
assertEquals("", errorOutputStream.toString())
assertThat(outputStream.toString()).isEqualTo("PICKLE\n\n")
assertThat(errorOutputStream.toString()).isEmpty()
}
@Test
fun interactiveInterpreterWorks() {
fun `the interactive interpreter works`() {
setCommonFeatures()
LispInterpreterBuilder.setInput(createInputStreamFromString("'pickle"), "input")
LispInterpreterBuilder.build().interpret()
assertEquals("$PROMPT\nPICKLE\n$PROMPT\n", outputStream.toString())
assertEquals("", errorOutputStream.toString())
assertThat(outputStream.toString()).isEqualTo("$PROMPT\nPICKLE\n$PROMPT\n")
assertThat(errorOutputStream.toString()).isEmpty()
}
@Test
fun interpreterHandlesInputError() {
fun `the interpreter handles bad input`() {
setCommonFeatures()
LispInterpreterBuilder.setNotInteractive()
LispInterpreterBuilder.setInput(createInputStreamFromString("['pickle"), "input")
LispInterpreterBuilder.build().interpret()
assertEquals("PICKLE\n\n", outputStream.toString())
assertEquals("[error] illegal character >>[<< - line 1, column 1\n", errorOutputStream.toString())
assertThat(outputStream.toString()).isEqualTo("PICKLE\n\n")
assertThat(errorOutputStream.toString()).isEqualTo("[error] illegal character >>[<< - line 1, column 1\n")
}
@Test
fun interpreterHandlesEvaluationError() {
fun `the interpreter handles an evaluation error`() {
setCommonFeatures()
LispInterpreterBuilder.setNotInteractive()
LispInterpreterBuilder.setInput(createInputStreamFromString("pickle"), "input")
LispInterpreterBuilder.build().interpret()
assertEquals("\n", outputStream.toString())
assertEquals("[error] symbol PICKLE has no value\n", errorOutputStream.toString())
assertThat(outputStream.toString()).isEqualTo("\n")
assertThat(errorOutputStream.toString()).isEqualTo("[error] symbol PICKLE has no value\n")
}
}