forked from scream3r/java-simple-serial-connector
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add stylized unit test method names to maven output
- Loading branch information
Showing
7 changed files
with
109 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package jssc.common; | ||
|
||
public enum ConsoleColor { | ||
ANSI_RESET(0), | ||
ANSI_BLACK(30), | ||
ANSI_RED(31), | ||
ANSI_GREEN(32), | ||
ANSI_YELLOW(33), | ||
ANSI_BLUE(34), | ||
ANSI_PURPLE(35), | ||
ANSI_CYAN(36), | ||
ANSI_WHITE(37); | ||
|
||
String colorCode; | ||
ConsoleColor(int colorCode) { | ||
this.colorCode = "\u001B[" + colorCode + "m"; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return colorCode; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package jssc.common; | ||
|
||
import org.junit.runner.Description; | ||
|
||
import static jssc.common.ConsoleColor.*; | ||
|
||
/** | ||
* Utility class for coloring a console message similar to JUnit 4 | ||
*/ | ||
public enum ConsoleStyle { | ||
INFO, | ||
WARNING, | ||
ERROR; | ||
|
||
public String colorize(String message) { | ||
// e.g. [INFO] --- surefire:3.0.0-M4:test (default-test) @ jssc --- | ||
return String.format("%s --- %s @ %s", | ||
getPrefix(), | ||
styleMessage(ANSI_GREEN, "surefire:" + message), | ||
styleMessage(ANSI_CYAN, "jssc")); | ||
} | ||
|
||
public String colorize(Description description) { | ||
return colorize(description.getMethodName()); | ||
} | ||
|
||
private ConsoleColor getColor() { | ||
switch(this) { | ||
case ERROR: | ||
return ANSI_RED; | ||
case WARNING: | ||
return ANSI_YELLOW; | ||
case INFO: | ||
default: | ||
return ANSI_BLUE; | ||
} | ||
} | ||
|
||
private String styleSeverity() { | ||
return styleMessage(getColor(), name()); | ||
} | ||
|
||
private static String styleMessage(ConsoleColor color, String message) { | ||
return color + message + ANSI_RESET; | ||
} | ||
|
||
private String getPrefix() { | ||
return ANSI_RESET + "[" + styleSeverity() + "]"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package jssc.junit.rules; | ||
|
||
import org.junit.Rule; | ||
import org.junit.rules.*; | ||
import org.junit.runner.*; | ||
|
||
import static jssc.common.ConsoleStyle.*; | ||
|
||
/** | ||
* Adds the method name to the JUnit logs, useful for debugging | ||
*/ | ||
public class DisplayMethodNameRule { | ||
@Rule | ||
public TestWatcher testWatcher = new TestWatcher() { | ||
@Override | ||
protected void starting(Description description) { | ||
/*String nl = System.getProperty("line.separator"); | ||
System.out.println(String.format(nl + | ||
"-------------------------------------------------------" + nl + | ||
"METHOD: %s" + nl + | ||
"-------------------------------------------------------", | ||
description.getMethodName()));*/ | ||
System.out.println(INFO.colorize(description)); | ||
} | ||
}; | ||
} |