From daf43b0ea708c34b670e90c295077eaa6c5c79d1 Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Sun, 25 Mar 2018 11:30:36 -0400 Subject: [PATCH] Convert error manager test to kotlin --- .../kotlin/function/builtin/special/LAMBDA.kt | 2 +- src/main/kotlin/table/FunctionTable.kt | 1 - .../environment/RuntimeEnvironmentTest.kt | 30 ++-- src/test/kotlin/error/ErrorManagerTest.java | 164 ------------------ src/test/kotlin/error/ErrorManagerTest.kt | 148 ++++++++++++++++ 5 files changed, 164 insertions(+), 181 deletions(-) delete mode 100644 src/test/kotlin/error/ErrorManagerTest.java create mode 100644 src/test/kotlin/error/ErrorManagerTest.kt diff --git a/src/main/kotlin/function/builtin/special/LAMBDA.kt b/src/main/kotlin/function/builtin/special/LAMBDA.kt index 36baeca..5b625bd 100644 --- a/src/main/kotlin/function/builtin/special/LAMBDA.kt +++ b/src/main/kotlin/function/builtin/special/LAMBDA.kt @@ -40,7 +40,7 @@ class LAMBDA(name: String) : LispSpecialFunction() { return Cons(Symbol("LAMBDA"), argumentList) } - companion object Lambda { + companion object Lambda { fun isLambdaExpression(sexpr: SExpression): Boolean { if (sexpr.isCons) { diff --git a/src/main/kotlin/table/FunctionTable.kt b/src/main/kotlin/table/FunctionTable.kt index dcf53d9..f9a00bc 100644 --- a/src/main/kotlin/table/FunctionTable.kt +++ b/src/main/kotlin/table/FunctionTable.kt @@ -50,7 +50,6 @@ import function.builtin.special.PROGN import function.builtin.special.QUOTE import function.builtin.special.RECUR import function.builtin.special.SETQ -import java.text.MessageFormat.format import java.util.HashMap import java.util.HashSet diff --git a/src/test/kotlin/environment/RuntimeEnvironmentTest.kt b/src/test/kotlin/environment/RuntimeEnvironmentTest.kt index 776bbf3..2e1a398 100644 --- a/src/test/kotlin/environment/RuntimeEnvironmentTest.kt +++ b/src/test/kotlin/environment/RuntimeEnvironmentTest.kt @@ -3,11 +3,11 @@ package environment import error.ErrorManager import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.Assertions.assertThrows import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import org.junit.jupiter.api.TestInstance import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS -import org.junit.jupiter.api.Assertions.assertThrows import java.util.HashSet import java.util.function.Function @@ -33,35 +33,35 @@ class RuntimeEnvironmentTest { } @Test - fun assignInputName() { + fun `assign an input name`() { RuntimeEnvironment.inputName = "test" assertThat(RuntimeEnvironment.inputName).isEqualTo("test") } @Test - fun assignInput() { + fun `assign input`() { RuntimeEnvironment.input = System.`in` assertThat(RuntimeEnvironment.input).isEqualTo(System.`in`) } @Test - fun assignOutput() { + fun `assign output`() { RuntimeEnvironment.output = System.out assertThat(RuntimeEnvironment.output).isEqualTo(System.out) } @Test - fun assignErrorOutput() { + fun `assign error output`() { RuntimeEnvironment.errorOutput = System.err assertThat(RuntimeEnvironment.errorOutput).isEqualTo(System.err) } @Test - fun assignErrorManager() { + fun `assign an error manager`() { val errorManager = ErrorManager() RuntimeEnvironment.errorManager = errorManager @@ -69,14 +69,14 @@ class RuntimeEnvironmentTest { } @Test - fun assignPath() { + fun `assign a path`() { RuntimeEnvironment.path = "testpath/" assertThat(RuntimeEnvironment.path).isEqualTo("testpath/") } @Test - fun assignTerminationFunction() { + fun `assign a termination function`() { RuntimeEnvironment.terminationFunction = Runnable { indicatorSet!!.add(TERMINATED_SUCCESSFULLY) } RuntimeEnvironment.terminateSuccessfully() @@ -84,7 +84,7 @@ class RuntimeEnvironmentTest { } @Test - fun assignErrorTerminationFunction() { + fun `assign an error termination function`() { RuntimeEnvironment.errorTerminationFunction = Runnable { indicatorSet!!.add(TERMINATED_EXCEPTIONALLY) } RuntimeEnvironment.terminateExceptionally() @@ -92,42 +92,42 @@ class RuntimeEnvironmentTest { } @Test - fun assignPromptDecorator() { + fun `assing a prompt decorator`() { RuntimeEnvironment.promptDecorator = Function { s -> "[$s]" } assertThat(RuntimeEnvironment.decoratePrompt("test")).isEqualTo("[test]") } @Test - fun assignValueOutputDecorator() { + fun `assign a value output decorator`() { RuntimeEnvironment.valueOutputDecorator = Function { s -> "($s)" } assertThat(RuntimeEnvironment.decorateValueOutput("test")).isEqualTo("(test)") } @Test - fun assignWarningOutputDecorator() { + fun `assign a warning output decorator`() { RuntimeEnvironment.warningOutputDecorator = Function { s -> "|$s|" } assertThat(RuntimeEnvironment.decorateWarningOutput("test")).isEqualTo("|test|") } @Test - fun assignErrorOutputDecorator() { + fun `assign an error output decorator`() { RuntimeEnvironment.errorOutputDecorator = Function { s -> "{$s}" } assertThat(RuntimeEnvironment.decorateErrorOutput("test")).isEqualTo("{test}") } @Test - fun assignCriticalOutputDecorator() { + fun `assign a critical output decorator`() { RuntimeEnvironment.criticalOutputDecorator = Function { s -> "/$s/" } assertThat(RuntimeEnvironment.decorateCriticalOutput("test")).isEqualTo("/test/") } @Test - fun resetWorks() { + fun `reset works`() { RuntimeEnvironment.inputName = "test" RuntimeEnvironment.input = System.`in` RuntimeEnvironment.output = System.out diff --git a/src/test/kotlin/error/ErrorManagerTest.java b/src/test/kotlin/error/ErrorManagerTest.java deleted file mode 100644 index 2c30a6e..0000000 --- a/src/test/kotlin/error/ErrorManagerTest.java +++ /dev/null @@ -1,164 +0,0 @@ -package error; - -import environment.RuntimeEnvironment; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.util.HashSet; -import java.util.Set; - -import static error.Severity.CRITICAL; -import static error.Severity.ERROR; -import static error.Severity.WARNING; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -public class ErrorManagerTest { - - private static final String TERMINATED = "terminated"; - private static final String MESSAGE = "message"; - - private Set indicatorSet; - private ByteArrayOutputStream errorOutputStream; - private ByteArrayOutputStream outputStream; - private RuntimeEnvironment environment; - - public ErrorManagerTest() { - this.environment = RuntimeEnvironment.INSTANCE; - } - - private ErrorManager createErrorManagerWithIndicators() { - environment.setErrorTerminationFunction(() -> indicatorSet.add(TERMINATED)); - environment.setErrorOutput(new PrintStream(errorOutputStream)); - environment.setOutput(new PrintStream(outputStream)); - environment.setWarningOutputDecorator(s -> s); - environment.setErrorOutputDecorator(s -> s); - environment.setCriticalOutputDecorator(s -> s); - - return new ErrorManager(); - } - - private LispException createLispException(Severity severity) { - return new LispException() { - - private static final long serialVersionUID = 1L; - - @Override - public Severity getSeverity() { - return severity; - } - - @Override - public String getMessage() { - return MESSAGE; - } - }; - } - - private void assertTerminated() { - assertTrue(indicatorSet.contains(TERMINATED)); - } - - private void assertNotTerminated() { - assertFalse(indicatorSet.contains(TERMINATED)); - } - - private void assertWarningMessageNotWritten() { - assertTrue(outputStream.toByteArray().length == 0); - } - - private void assertWarningMessageWritten() { - assertTrue(outputStream.toByteArray().length > 0); - } - - private void assertErrorMessageNotWritten() { - assertTrue(errorOutputStream.toByteArray().length == 0); - } - - private void assertErrorMessageWritten() { - assertTrue(errorOutputStream.toByteArray().length > 0); - } - - @Before - public void setUp() { - indicatorSet = new HashSet<>(); - errorOutputStream = new ByteArrayOutputStream(); - outputStream = new ByteArrayOutputStream(); - environment.reset(); - } - - @After - public void tearDown() { - environment.reset(); - } - - @Test - public void givenCriticalExceptionSeverity_RunsProvidedTerminationFunction() { - ErrorManager errorManager = createErrorManagerWithIndicators(); - - errorManager.handle(createLispException(CRITICAL)); - assertTerminated(); - } - - @Test - public void givenWarningExceptionSeverity_DoesNotRunProvidedTerminationFunction() { - ErrorManager errorManager = createErrorManagerWithIndicators(); - - errorManager.handle(createLispException(WARNING)); - assertNotTerminated(); - } - - @Test - public void givenErrorExceptionSeverity_DoesNotRunProvidedTerminationFunction() { - ErrorManager errorManager = createErrorManagerWithIndicators(); - - errorManager.handle(createLispException(ERROR)); - assertNotTerminated(); - } - - @Test - public void usesOutputToDisplayWarningMessage() { - ErrorManager errorManager = createErrorManagerWithIndicators(); - - errorManager.handle(createLispException(WARNING)); - assertNotTerminated(); - assertErrorMessageNotWritten(); - assertWarningMessageWritten(); - } - - @Test - public void usesErrorOutputToDisplayErrorMessage() { - ErrorManager errorManager = createErrorManagerWithIndicators(); - - errorManager.handle(createLispException(ERROR)); - assertNotTerminated(); - assertWarningMessageNotWritten(); - assertErrorMessageWritten(); - } - - @Test - public void noMessageDisplayedBeforeError() { - createErrorManagerWithIndicators(); - - assertNotTerminated(); - assertErrorMessageNotWritten(); - } - - @Test - public void usesOutputFunctionToDisplayMessages_WithTermination() { - ErrorManager errorManager = createErrorManagerWithIndicators(); - - errorManager.handle(createLispException(CRITICAL)); - assertTerminated(); - assertErrorMessageWritten(); - } - - @Test - public void severityEnumCoverage() { - for (Severity severity : Severity.values()) - Severity.valueOf(severity.toString()); - } -} diff --git a/src/test/kotlin/error/ErrorManagerTest.kt b/src/test/kotlin/error/ErrorManagerTest.kt new file mode 100644 index 0000000..fd48e70 --- /dev/null +++ b/src/test/kotlin/error/ErrorManagerTest.kt @@ -0,0 +1,148 @@ +package error + +import environment.RuntimeEnvironment +import error.Severity.CRITICAL +import error.Severity.ERROR +import error.Severity.WARNING +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 java.io.ByteArrayOutputStream +import java.io.PrintStream +import java.util.HashSet +import java.util.function.Function + +class ErrorManagerTest { + + companion object { + private const val TERMINATED = "terminated" + private const val MESSAGE = "message" + } + + private lateinit var indicatorSet: MutableSet + private lateinit var errorOutputStream: ByteArrayOutputStream + private lateinit var outputStream: ByteArrayOutputStream + + private fun createErrorManagerWithIndicators(): ErrorManager { + RuntimeEnvironment.errorTerminationFunction = Runnable { indicatorSet.add(TERMINATED) } + RuntimeEnvironment.errorOutput = PrintStream(errorOutputStream) + RuntimeEnvironment.output = PrintStream(outputStream) + RuntimeEnvironment.warningOutputDecorator = Function { s -> s } + RuntimeEnvironment.errorOutputDecorator = Function { s -> s } + RuntimeEnvironment.criticalOutputDecorator = Function { s -> s } + + return ErrorManager() + } + + private fun createLispException(severity: Severity) = object : LispException() { + override val severity = severity + + override val message + get() = MESSAGE + } + + private fun assertTerminated() { + assertThat(indicatorSet).contains(TERMINATED) + } + + private fun assertNotTerminated() { + assertThat(indicatorSet).doesNotContain(TERMINATED) + } + + private fun assertWarningMessageNotWritten() { + assertThat(outputStream.toByteArray()).isEmpty() + } + + private fun assertWarningMessageWritten() { + assertThat(outputStream.toByteArray()).isNotEmpty() + } + + private fun assertErrorMessageNotWritten() { + assertThat(errorOutputStream.toByteArray()).isEmpty() + } + + private fun assertErrorMessageWritten() { + assertThat(errorOutputStream.toByteArray()).isNotEmpty() + } + + @BeforeEach + fun setUp() { + indicatorSet = HashSet() + errorOutputStream = ByteArrayOutputStream() + outputStream = ByteArrayOutputStream() + RuntimeEnvironment.reset() + } + + @AfterEach + fun tearDown() { + RuntimeEnvironment.reset() + } + + @Test + fun `terminates on a critical exception`() { + val errorManager = createErrorManagerWithIndicators() + + errorManager.handle(createLispException(CRITICAL)) + assertTerminated() + } + + @Test + fun `does not terminate on a warning`() { + val errorManager = createErrorManagerWithIndicators() + + errorManager.handle(createLispException(WARNING)) + assertNotTerminated() + } + + @Test + fun `does not terminate on a non-critical exception`() { + val errorManager = createErrorManagerWithIndicators() + + errorManager.handle(createLispException(ERROR)) + assertNotTerminated() + } + + @Test + fun `output is used to display a warning message`() { + val errorManager = createErrorManagerWithIndicators() + + errorManager.handle(createLispException(WARNING)) + assertNotTerminated() + assertErrorMessageNotWritten() + assertWarningMessageWritten() + } + + @Test + fun `error output is used to display an error message`() { + val errorManager = createErrorManagerWithIndicators() + + errorManager.handle(createLispException(ERROR)) + assertNotTerminated() + assertWarningMessageNotWritten() + assertErrorMessageWritten() + } + + @Test + fun `no message is displayed before an error`() { + createErrorManagerWithIndicators() + + assertNotTerminated() + assertErrorMessageNotWritten() + } + + @Test + fun `uses output function to display messages with termination`() { + val errorManager = createErrorManagerWithIndicators() + + errorManager.handle(createLispException(CRITICAL)) + assertTerminated() + assertErrorMessageWritten() + } + + @Test + fun `severity enum test coverage`() { + for (severity in Severity.values()) + Severity.valueOf(severity.toString()) + } +}