From 5d7ba5b35f98b720fd92b2fd70cba770cc97a568 Mon Sep 17 00:00:00 2001 From: Janos Kutscherauer Date: Sat, 7 Mar 2015 22:29:56 +0100 Subject: [PATCH] Added a colored outputter to embUnit. The outputter is called ColorTextOutputter and can be enabled by defining OUTPUT=COLORTEXT. The colored outputter behaves just like the TextOutputter, but displays successful tests in GREEN and failed tests in RED. The summary message is also in GREEN/RED, but is slightly different from the TextOutputter. Also: * Added fancy simple none-verbose color-outputter for EmbUnit. The outputter outputs a simple statistics line in GREEN or RED, according to the test success or failure. (Also, the ColorTextOutputter was adjusted to use the statistics output of this new ColorOutputter.) The new outputter can be activated with OUTPUT=COLOR. * Added a single character "." output for the simple color outputter and replaced the color codes by macros. --- sys/Makefile.include | 4 ++ sys/embunit/ColorOutputter.c | 75 ++++++++++++++++++++++++ sys/embunit/ColorTextColors.h | 57 ++++++++++++++++++ sys/embunit/ColorTextOutputter.c | 64 ++++++++++++++++++++ sys/include/embUnit.h | 14 ++++- sys/include/embUnit/ColorOutputter.h | 45 ++++++++++++++ sys/include/embUnit/ColorTextOutputter.h | 30 ++++++++++ tests/unittests/README.md | 2 + 8 files changed, 288 insertions(+), 3 deletions(-) create mode 100644 sys/embunit/ColorOutputter.c create mode 100644 sys/embunit/ColorTextColors.h create mode 100644 sys/embunit/ColorTextOutputter.c create mode 100644 sys/include/embUnit/ColorOutputter.h create mode 100644 sys/include/embUnit/ColorTextOutputter.h diff --git a/sys/Makefile.include b/sys/Makefile.include index de8b66de52..f2238f8b4e 100644 --- a/sys/Makefile.include +++ b/sys/Makefile.include @@ -81,5 +81,9 @@ ifneq (,$(filter embunit,$(USEMODULE))) CFLAGS += -DOUTPUT=OUTPUT_TEXT else ifeq ($(OUTPUT),COMPILER) CFLAGS += -DOUTPUT=OUTPUT_COMPILER + else ifeq ($(OUTPUT),COLORTEXT) + CFLAGS += -DOUTPUT=OUTPUT_COLORTEXT + else ifeq ($(OUTPUT),COLOR) + CFLAGS += -DOUTPUT=OUTPUT_COLOR endif endif diff --git a/sys/embunit/ColorOutputter.c b/sys/embunit/ColorOutputter.c new file mode 100644 index 0000000000..3c489e1562 --- /dev/null +++ b/sys/embunit/ColorOutputter.c @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2015 Janos Kutscherauer + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +#include +#include "ColorOutputter.h" +#include "ColorTextColors.h" + +static void ColorOutputter_printHeader(OutputterRef self) +{ + (void) self; +} + +static void ColorOutputter_printStartTest(OutputterRef self, TestRef test) +{ + (void) self; + (void) test; +} + +static void ColorOutputter_printEndTest(OutputterRef self, TestRef test) +{ + (void) self; + (void) test; +} + +static void ColorOutputter_printSuccessful(OutputterRef self, TestRef test, int runCount) +{ + (void) self; + (void) test; + (void) runCount; + printf(CGREEN "." CDEFAULT); +} + +static void ColorOutputter_printFailure(OutputterRef self, TestRef test, char *msg, int line, + char *file, int runCount) +{ + (void) self; + (void) runCount; + printf("\n" CRED "FAILED %s (%s:%d) %s" CDEFAULT "\n", Test_name(test), file, line, msg); +} + +void ColorOutputter_printStatistics(OutputterRef self, TestResultRef result) +{ + (void) self; + if (result->failureCount) { + printf("\n" BGRED SBOLD "FAILED" SDEFAULT " (%d of %d failed)", result->failureCount, + result->runCount); + } + else { + printf("\n" BGGREEN SBOLD "OK" SDEFAULT " (%d tests)", result->runCount); + } + printf(LINEFILL BGDEFAULT "\n"); +} + +static const OutputterImplement ColorOutputterImplement = { + (OutputterPrintHeaderFunction) ColorOutputter_printHeader, + (OutputterPrintStartTestFunction) ColorOutputter_printStartTest, + (OutputterPrintEndTestFunction) ColorOutputter_printEndTest, + (OutputterPrintSuccessfulFunction) ColorOutputter_printSuccessful, + (OutputterPrintFailureFunction) ColorOutputter_printFailure, + (OutputterPrintStatisticsFunction) ColorOutputter_printStatistics +}; + +static const Outputter ColorOutputter = { + (OutputterImplementRef) &ColorOutputterImplement +}; + +OutputterRef ColorOutputter_outputter(void) +{ + return (OutputterRef) &ColorOutputter; +} diff --git a/sys/embunit/ColorTextColors.h b/sys/embunit/ColorTextColors.h new file mode 100644 index 0000000000..335dccc0ab --- /dev/null +++ b/sys/embunit/ColorTextColors.h @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2015 Janos Kutscherauer + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @{ + * + * @file + * + * @author Janos Kutscherauer + */ +#ifndef __COLORTEXTCOLORS_H__ +#define __COLORTEXTCOLORS_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @name Terminal color definitions + * C: text color + * + * CDEFAULT: default text color + * + * BG: background color + * + * BGDEFAULT: default background color + * + * S