Convert error manager test to kotlin

This commit is contained in:
Mike Cifelli 2018-03-25 11:30:36 -04:00
parent 26709a7f9e
commit daf43b0ea7
5 changed files with 164 additions and 181 deletions

View File

@ -50,7 +50,6 @@ import function.builtin.special.PROGN
import function.builtin.special.QUOTE import function.builtin.special.QUOTE
import function.builtin.special.RECUR import function.builtin.special.RECUR
import function.builtin.special.SETQ import function.builtin.special.SETQ
import java.text.MessageFormat.format
import java.util.HashMap import java.util.HashMap
import java.util.HashSet import java.util.HashSet

View File

@ -3,11 +3,11 @@ package environment
import error.ErrorManager import error.ErrorManager
import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions.assertThrows
import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS
import org.junit.jupiter.api.Assertions.assertThrows
import java.util.HashSet import java.util.HashSet
import java.util.function.Function import java.util.function.Function
@ -33,35 +33,35 @@ class RuntimeEnvironmentTest {
} }
@Test @Test
fun assignInputName() { fun `assign an input name`() {
RuntimeEnvironment.inputName = "test" RuntimeEnvironment.inputName = "test"
assertThat(RuntimeEnvironment.inputName).isEqualTo("test") assertThat(RuntimeEnvironment.inputName).isEqualTo("test")
} }
@Test @Test
fun assignInput() { fun `assign input`() {
RuntimeEnvironment.input = System.`in` RuntimeEnvironment.input = System.`in`
assertThat(RuntimeEnvironment.input).isEqualTo(System.`in`) assertThat(RuntimeEnvironment.input).isEqualTo(System.`in`)
} }
@Test @Test
fun assignOutput() { fun `assign output`() {
RuntimeEnvironment.output = System.out RuntimeEnvironment.output = System.out
assertThat(RuntimeEnvironment.output).isEqualTo(System.out) assertThat(RuntimeEnvironment.output).isEqualTo(System.out)
} }
@Test @Test
fun assignErrorOutput() { fun `assign error output`() {
RuntimeEnvironment.errorOutput = System.err RuntimeEnvironment.errorOutput = System.err
assertThat(RuntimeEnvironment.errorOutput).isEqualTo(System.err) assertThat(RuntimeEnvironment.errorOutput).isEqualTo(System.err)
} }
@Test @Test
fun assignErrorManager() { fun `assign an error manager`() {
val errorManager = ErrorManager() val errorManager = ErrorManager()
RuntimeEnvironment.errorManager = errorManager RuntimeEnvironment.errorManager = errorManager
@ -69,14 +69,14 @@ class RuntimeEnvironmentTest {
} }
@Test @Test
fun assignPath() { fun `assign a path`() {
RuntimeEnvironment.path = "testpath/" RuntimeEnvironment.path = "testpath/"
assertThat(RuntimeEnvironment.path).isEqualTo("testpath/") assertThat(RuntimeEnvironment.path).isEqualTo("testpath/")
} }
@Test @Test
fun assignTerminationFunction() { fun `assign a termination function`() {
RuntimeEnvironment.terminationFunction = Runnable { indicatorSet!!.add(TERMINATED_SUCCESSFULLY) } RuntimeEnvironment.terminationFunction = Runnable { indicatorSet!!.add(TERMINATED_SUCCESSFULLY) }
RuntimeEnvironment.terminateSuccessfully() RuntimeEnvironment.terminateSuccessfully()
@ -84,7 +84,7 @@ class RuntimeEnvironmentTest {
} }
@Test @Test
fun assignErrorTerminationFunction() { fun `assign an error termination function`() {
RuntimeEnvironment.errorTerminationFunction = Runnable { indicatorSet!!.add(TERMINATED_EXCEPTIONALLY) } RuntimeEnvironment.errorTerminationFunction = Runnable { indicatorSet!!.add(TERMINATED_EXCEPTIONALLY) }
RuntimeEnvironment.terminateExceptionally() RuntimeEnvironment.terminateExceptionally()
@ -92,42 +92,42 @@ class RuntimeEnvironmentTest {
} }
@Test @Test
fun assignPromptDecorator() { fun `assing a prompt decorator`() {
RuntimeEnvironment.promptDecorator = Function { s -> "[$s]" } RuntimeEnvironment.promptDecorator = Function { s -> "[$s]" }
assertThat(RuntimeEnvironment.decoratePrompt("test")).isEqualTo("[test]") assertThat(RuntimeEnvironment.decoratePrompt("test")).isEqualTo("[test]")
} }
@Test @Test
fun assignValueOutputDecorator() { fun `assign a value output decorator`() {
RuntimeEnvironment.valueOutputDecorator = Function { s -> "($s)" } RuntimeEnvironment.valueOutputDecorator = Function { s -> "($s)" }
assertThat(RuntimeEnvironment.decorateValueOutput("test")).isEqualTo("(test)") assertThat(RuntimeEnvironment.decorateValueOutput("test")).isEqualTo("(test)")
} }
@Test @Test
fun assignWarningOutputDecorator() { fun `assign a warning output decorator`() {
RuntimeEnvironment.warningOutputDecorator = Function { s -> "|$s|" } RuntimeEnvironment.warningOutputDecorator = Function { s -> "|$s|" }
assertThat(RuntimeEnvironment.decorateWarningOutput("test")).isEqualTo("|test|") assertThat(RuntimeEnvironment.decorateWarningOutput("test")).isEqualTo("|test|")
} }
@Test @Test
fun assignErrorOutputDecorator() { fun `assign an error output decorator`() {
RuntimeEnvironment.errorOutputDecorator = Function { s -> "{$s}" } RuntimeEnvironment.errorOutputDecorator = Function { s -> "{$s}" }
assertThat(RuntimeEnvironment.decorateErrorOutput("test")).isEqualTo("{test}") assertThat(RuntimeEnvironment.decorateErrorOutput("test")).isEqualTo("{test}")
} }
@Test @Test
fun assignCriticalOutputDecorator() { fun `assign a critical output decorator`() {
RuntimeEnvironment.criticalOutputDecorator = Function { s -> "/$s/" } RuntimeEnvironment.criticalOutputDecorator = Function { s -> "/$s/" }
assertThat(RuntimeEnvironment.decorateCriticalOutput("test")).isEqualTo("/test/") assertThat(RuntimeEnvironment.decorateCriticalOutput("test")).isEqualTo("/test/")
} }
@Test @Test
fun resetWorks() { fun `reset works`() {
RuntimeEnvironment.inputName = "test" RuntimeEnvironment.inputName = "test"
RuntimeEnvironment.input = System.`in` RuntimeEnvironment.input = System.`in`
RuntimeEnvironment.output = System.out RuntimeEnvironment.output = System.out

View File

@ -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<String> 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());
}
}

View File

@ -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<String>
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())
}
}