From a137e41a1432ddac51718f3feab9d043bb145e82 Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Fri, 16 Dec 2016 13:12:13 -0500 Subject: [PATCH] Cleaned up the ErrorManager and associated unit tests --- src/error/ErrorManager.java | 26 +++++++++++++++----------- test/error/ErrorManagerTester.java | 25 ++++++++++++++----------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/error/ErrorManager.java b/src/error/ErrorManager.java index 5a069e5..72f06b0 100644 --- a/src/error/ErrorManager.java +++ b/src/error/ErrorManager.java @@ -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 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; } } diff --git a/test/error/ErrorManagerTester.java b/test/error/ErrorManagerTester.java index fe63912..7abb0de 100644 --- a/test/error/ErrorManagerTester.java +++ b/test/error/ErrorManagerTester.java @@ -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 indicatorSet) { + private Set 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 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 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 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 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 indicatorSet = new HashSet<>(); - ErrorManager errorManager = createErrorManagerWithIndicators(indicatorSet); + ErrorManager errorManager = createErrorManagerWithIndicators(); errorManager.generateError(createLispException(ErrorManager.CRITICAL_LEVEL)); assertTrue(indicatorSet.contains(TERMINATED));