Commit 691315e6e544ae3966906113bf87c576ff157afb

Edward Thomson 2020-06-03T15:47:42

clar: add an output abstraction layer Add an output abstraction layer, with a single output format, "clap", the clar protocol, which is the current output format for clar.

diff --git a/tests/clar.c b/tests/clar.c
index 73ac993..070447a 100644
--- a/tests/clar.c
+++ b/tests/clar.c
@@ -140,6 +140,8 @@ static struct {
 	int tests_ran;
 	int suites_ran;
 
+	enum cl_output_format output_format;
+
 	int report_errors_only;
 	int exit_on_error;
 	int report_suite_names;
diff --git a/tests/clar.h b/tests/clar.h
index 20ff4c8..8bcba35 100644
--- a/tests/clar.h
+++ b/tests/clar.h
@@ -16,6 +16,10 @@ enum cl_test_status {
 	CL_TEST_NOTRUN,
 };
 
+enum cl_output_format {
+	CL_OUTPUT_CLAP,
+};
+
 /** Setup clar environment */
 void clar_test_init(int argc, char *argv[]);
 int clar_test_run(void);
diff --git a/tests/clar/print.h b/tests/clar/print.h
index 5c8857c..0b27f1c 100644
--- a/tests/clar/print.h
+++ b/tests/clar/print.h
@@ -1,12 +1,13 @@
+/* clap: clar protocol, the traditional clar output format */
 
-static void clar_print_cl_init(int test_count, int suite_count, const char *suite_names)
+static void clar_print_clap_init(int test_count, int suite_count, const char *suite_names)
 {
 	(void)test_count;
 	printf("Loaded %d suites: %s\n", (int)suite_count, suite_names);
 	printf("Started (test status codes: OK='.' FAILURE='F' SKIPPED='S')\n");
 }
 
-static void clar_print_cl_shutdown(int test_count, int suite_count, int error_count)
+static void clar_print_clap_shutdown(int test_count, int suite_count, int error_count)
 {
 	(void)test_count;
 	(void)suite_count;
@@ -16,7 +17,7 @@ static void clar_print_cl_shutdown(int test_count, int suite_count, int error_co
 	clar_report_all();
 }
 
-static void clar_print_cl_error(int num, const struct clar_report *report, const struct clar_error *error)
+static void clar_print_clap_error(int num, const struct clar_report *report, const struct clar_error *error)
 {
 	printf("  %d) Failure:\n", num);
 
@@ -35,7 +36,7 @@ static void clar_print_cl_error(int num, const struct clar_report *report, const
 	fflush(stdout);
 }
 
-static void clar_print_cl_ontest(const char *test_name, int test_number, enum cl_test_status status)
+static void clar_print_clap_ontest(const char *test_name, int test_number, enum cl_test_status status)
 {
 	(void)test_name;
 	(void)test_number;
@@ -50,7 +51,7 @@ static void clar_print_cl_ontest(const char *test_name, int test_number, enum cl
 	fflush(stdout);
 }
 
-static void clar_print_cl_onsuite(const char *suite_name, int suite_index)
+static void clar_print_clap_onsuite(const char *suite_name, int suite_index)
 {
 	if (_clar.report_suite_names)
 		printf("\n%s", suite_name);
@@ -58,40 +59,52 @@ static void clar_print_cl_onsuite(const char *suite_name, int suite_index)
 	(void)suite_index;
 }
 
-static void clar_print_cl_onabort(const char *fmt, va_list arg)
+static void clar_print_clap_onabort(const char *fmt, va_list arg)
 {
 	vfprintf(stderr, fmt, arg);
 }
 
+/* indirection between protocol output selection */
+
+#define PRINT(FN, args...) do { \
+		switch (_clar.output_format) { \
+			case CL_OUTPUT_CLAP: \
+				clar_print_clap_##FN (args); \
+				break; \
+			default: \
+				abort(); \
+		} \
+	} while (0)
+
 static void clar_print_init(int test_count, int suite_count, const char *suite_names)
 {
-	clar_print_cl_init(test_count, suite_count, suite_names);
+	PRINT(init, test_count, suite_count, suite_names);
 }
 
 static void clar_print_shutdown(int test_count, int suite_count, int error_count)
 {
-	clar_print_cl_shutdown(test_count, suite_count, error_count);
+	PRINT(shutdown, test_count, suite_count, error_count);
 }
 
 static void clar_print_error(int num, const struct clar_report *report, const struct clar_error *error)
 {
-	clar_print_cl_error(num, report, error);
+	PRINT(error, num, report, error);
 }
 
 static void clar_print_ontest(const char *test_name, int test_number, enum cl_test_status status)
 {
-	clar_print_cl_ontest(test_name, test_number, status);
+	PRINT(ontest, test_name, test_number, status);
 }
 
 static void clar_print_onsuite(const char *suite_name, int suite_index)
 {
-	clar_print_cl_onsuite(suite_name, suite_index);
+	PRINT(onsuite, suite_name, suite_index);
 }
 
 static void clar_print_onabort(const char *msg, ...)
 {
 	va_list argp;
 	va_start(argp, msg);
-	clar_print_cl_onabort(msg, argp);
+	PRINT(onabort, msg, argp);
 	va_end(argp);
 }