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