Cleaned up the ErrorManager and associated unit tests
This commit is contained in:
parent
25c0b5813b
commit
a137e41a14
|
@ -10,10 +10,9 @@ public class ErrorManager {
|
|||
|
||||
public static final int CRITICAL_LEVEL = 3;
|
||||
|
||||
public static final String ANSI_RESET = "\u001B[0m";
|
||||
public static final String ANSI_RED = "\u001B[31m";
|
||||
public static final String ANSI_YELLOW = "\u001B[33m";
|
||||
public static final String ANSI_PURPLE = "\u001B[35m";
|
||||
private static final String ANSI_RESET = "\u001B[0m";
|
||||
private static final String ANSI_RED = "\u001B[31m";
|
||||
private static final String ANSI_PURPLE = "\u001B[35m";
|
||||
|
||||
private Runnable systemTerminatingFunction;
|
||||
private Consumer<String> outputFunction;
|
||||
|
@ -24,15 +23,20 @@ public class ErrorManager {
|
|||
}
|
||||
|
||||
public void generateError(LispException lispException) {
|
||||
String color = (lispException.getSeverity() >= CRITICAL_LEVEL) ? ANSI_PURPLE : ANSI_RED;
|
||||
String formattedMessage = MessageFormat.format("{0}error: {1}{2}\n", color, lispException.getMessage(),
|
||||
ANSI_RESET);
|
||||
outputFunction.accept(formatMessage(lispException));
|
||||
|
||||
outputFunction.accept(formattedMessage);
|
||||
|
||||
if (lispException.getSeverity() >= CRITICAL_LEVEL) {
|
||||
if (isCritical(lispException))
|
||||
systemTerminatingFunction.run();
|
||||
}
|
||||
}
|
||||
|
||||
private String formatMessage(LispException lispException) {
|
||||
String color = isCritical(lispException) ? ANSI_PURPLE : ANSI_RED;
|
||||
|
||||
return MessageFormat.format("{0}error: {1}{2}\n", color, lispException.getMessage(), ANSI_RESET);
|
||||
}
|
||||
|
||||
private boolean isCritical(LispException lispException) {
|
||||
return lispException.getSeverity() >= CRITICAL_LEVEL;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import java.util.HashSet;
|
|||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ErrorManagerTester {
|
||||
|
@ -14,7 +15,9 @@ public class ErrorManagerTester {
|
|||
private static final String TERMINATED = "terminated";
|
||||
private static final String MESSAGE = "message";
|
||||
|
||||
private ErrorManager createErrorManagerWithIndicators(Set<String> indicatorSet) {
|
||||
private Set<String> indicatorSet;
|
||||
|
||||
private ErrorManager createErrorManagerWithIndicators() {
|
||||
Runnable terminationFunction = new Runnable() {
|
||||
|
||||
@Override
|
||||
|
@ -50,11 +53,15 @@ public class ErrorManagerTester {
|
|||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.indicatorSet = new HashSet<>();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCriticalExceptionSeverity_RunsProvidedTerminationFunction() {
|
||||
Set<String> indicatorSet = new HashSet<>();
|
||||
ErrorManager errorManager = createErrorManagerWithIndicators(indicatorSet);
|
||||
ErrorManager errorManager = createErrorManagerWithIndicators();
|
||||
|
||||
errorManager.generateError(createLispException(ErrorManager.CRITICAL_LEVEL));
|
||||
assertTrue(indicatorSet.contains(TERMINATED));
|
||||
|
@ -62,8 +69,7 @@ public class ErrorManagerTester {
|
|||
|
||||
@Test
|
||||
public void givenNonCriticalExceptionSeverity_DoesNotRunProvidedTerminationFunction() {
|
||||
Set<String> indicatorSet = new HashSet<>();
|
||||
ErrorManager errorManager = createErrorManagerWithIndicators(indicatorSet);
|
||||
ErrorManager errorManager = createErrorManagerWithIndicators();
|
||||
|
||||
errorManager.generateError(createLispException(0));
|
||||
assertFalse(indicatorSet.contains(TERMINATED));
|
||||
|
@ -71,8 +77,7 @@ public class ErrorManagerTester {
|
|||
|
||||
@Test
|
||||
public void noMessageDisplayedBeforeError() {
|
||||
Set<String> indicatorSet = new HashSet<>();
|
||||
createErrorManagerWithIndicators(indicatorSet);
|
||||
createErrorManagerWithIndicators();
|
||||
|
||||
assertFalse(indicatorSet.contains(TERMINATED));
|
||||
assertFalse(indicatorSet.contains(MESSAGE));
|
||||
|
@ -80,8 +85,7 @@ public class ErrorManagerTester {
|
|||
|
||||
@Test
|
||||
public void usesOutputFunctionToDisplayMessages_NoTermination() {
|
||||
Set<String> indicatorSet = new HashSet<>();
|
||||
ErrorManager errorManager = createErrorManagerWithIndicators(indicatorSet);
|
||||
ErrorManager errorManager = createErrorManagerWithIndicators();
|
||||
|
||||
errorManager.generateError(createLispException(0));
|
||||
assertFalse(indicatorSet.contains(TERMINATED));
|
||||
|
@ -90,8 +94,7 @@ public class ErrorManagerTester {
|
|||
|
||||
@Test
|
||||
public void usesOutputFunctionToDisplayMessages_WithTermination() {
|
||||
Set<String> indicatorSet = new HashSet<>();
|
||||
ErrorManager errorManager = createErrorManagerWithIndicators(indicatorSet);
|
||||
ErrorManager errorManager = createErrorManagerWithIndicators();
|
||||
|
||||
errorManager.generateError(createLispException(ErrorManager.CRITICAL_LEVEL));
|
||||
assertTrue(indicatorSet.contains(TERMINATED));
|
||||
|
|
Loading…
Reference in New Issue