From: amalec Date: Sat, 30 Jun 2001 01:27:02 +0000 (+0000) Subject: Restructured printing to allow for logging function X-Git-Tag: 0.10.0~1095 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b93d96e5408bcccba9c93837b87abe8270fd590b;p=check Restructured printing to allow for logging function git-svn-id: svn+ssh://svn.code.sf.net/p/check/code/trunk@48 64e312b2-a51f-0410-8e61-82d0ca0eb02a --- diff --git a/check/src/Makefile.in b/check/src/Makefile.in index 5dad586..80ef9a9 100644 --- a/check/src/Makefile.in +++ b/check/src/Makefile.in @@ -71,7 +71,7 @@ lib_LIBRARIES = libcheck.a include_HEADERS = check.h check_log.h -libcheck_a_SOURCES = check.c check_run.c check.h check_impl.h check_msg.c check_msg.h chec_log.c check_log.h error.c error.h list.c list.h +libcheck_a_SOURCES = check.c check_run.c check.h check_impl.h check_msg.c check_msg.h check_log.c check_log.h error.c error.h list.c list.h CLEANFILES = *.*~ @@ -86,8 +86,8 @@ CPPFLAGS = @CPPFLAGS@ LDFLAGS = @LDFLAGS@ LIBS = @LIBS@ libcheck_a_LIBADD = -libcheck_a_OBJECTS = check.o check_run.o check_msg.o chec_log.o error.o \ -list.o +libcheck_a_OBJECTS = check.o check_run.o check_msg.o check_log.o \ +error.o list.o AR = ar CFLAGS = @CFLAGS@ COMPILE = $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) @@ -102,7 +102,7 @@ DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) $(TEXINFOS) $(EXTRA_DIST) TAR = gtar GZIP_ENV = --best -DEP_FILES = .deps/chec_log.P .deps/check.P .deps/check_msg.P \ +DEP_FILES = .deps/check.P .deps/check_log.P .deps/check_msg.P \ .deps/check_run.P .deps/error.P .deps/list.P SOURCES = $(libcheck_a_SOURCES) OBJECTS = $(libcheck_a_OBJECTS) diff --git a/check/src/check.h b/check/src/check.h index ceafeb5..2e4bbf2 100644 --- a/check/src/check.h +++ b/check/src/check.h @@ -207,14 +207,20 @@ TestResult **srunner_failures (SRunner *sr); TestResult **srunner_results (SRunner *sr); /* Printing */ +/*! Print the results contained in an SRunner + \param sr SRunner for which results are printed + \param print_mode Specification of print verbosity, constrainted to enum #print_verbosity +*/ +void srunner_print (SRunner *sr, int print_mode); + /*! Print a summary report of %passed, #checks, failures */ -void srunner_print_summary (SRunner *sr); +/* void srunner_print_summary (SRunner *sr); */ /*! \brief Print a detailed report of test results \param sr SRunner for which results are printed \param print_mode Specification of print verbosity, constrainted to enum #print_verbosity */ -void srunner_print_results (SRunner *sr, int print_mode); +/* void srunner_print_results (SRunner *sr, int print_mode); */ /*! @} */ diff --git a/check/src/check_impl.h b/check/src/check_impl.h index 2354076..b6b7ba1 100644 --- a/check/src/check_impl.h +++ b/check/src/check_impl.h @@ -48,5 +48,26 @@ struct TCase { SFun teardown; }; +typedef struct TestStats { + int n_checked; + int n_failed; + int n_errors; +} TestStats; + +struct TestResult { + int rtype; /* Type of result */ + char *file; /* File where the test occured */ + int line; /* Line number where the test occurred */ + char *tcname; /* Test case that generated the result */ + char *msg; /* Failure message */ +}; + +struct SRunner { + Suite *s; + TestStats *stats; + List *resultlst; + char *log_fname; +}; + #endif /* CHECK_IMPL_H */ diff --git a/check/src/check_log.h b/check/src/check_log.h index 6542006..3549880 100644 --- a/check/src/check_log.h +++ b/check/src/check_log.h @@ -1,5 +1,5 @@ #ifndef CHECK_LOG_H -#define CHEKC_LOG_H +#define CHECK_LOG_H /* Check: a unit test framework for C @@ -23,7 +23,10 @@ /*! \page check_log Check logging */ -/*! Set a log file to which to write during test running +/*! Set a log file to which to write during test running. + Log file setting is an initialize only operation -- it should be done + immediatly after SRunner creation, and the log file can't be changed + after being set. \param sr The SRunner for which to enable logging \param fname The file name to which to write the log */ @@ -35,4 +38,10 @@ void srunner_set_log (SRunner *sr, char *fname); */ int srunner_has_log (SRunner *sr); +/*! Return the name of the log file, or NULL if none + \param sr The SRunner to query + \return The current log file, or NULL if not logging +*/ +char *srunner_log_fname (SRunner *sr); + #endif /*CHECK_LOG_H*/ diff --git a/check/src/check_run.c b/check/src/check_run.c index a5b1869..0065309 100644 --- a/check/src/check_run.c +++ b/check/src/check_run.c @@ -22,31 +22,13 @@ #include #include #include +#include #include "error.h" #include "list.h" #include "check.h" #include "check_impl.h" #include "check_msg.h" -typedef struct TestStats { - int n_checked; - int n_failed; - int n_errors; -} TestStats; - -struct SRunner { - Suite *s; - TestStats *stats; - List *resultlst; -}; - -struct TestResult { - int rtype; /* Type of result */ - char *file; /* File where the test occured */ - int line; /* Line number where the test occurred */ - char *tcname; /* Test case that generated the result */ - char *msg; /* Failure message */ -}; static int percent_passed (TestStats *t); static void srunner_run_tcase (SRunner *sr, TCase *tc); @@ -57,7 +39,11 @@ static void receive_last_loc_info (int msqid, TestResult *tr); static void receive_failure_info (int msqid, int status, TestResult *tr); static List *srunner_resultlst (SRunner *sr); -static void tr_print (TestResult *tr); +static void srunner_printf (SRunner *sr, int target_mode, + int print_mode, char *fmt, ...); +static void srunner_print_summary (SRunner *sr, int print_mode); +static void srunner_print_results (SRunner *sr, int print_mode); +static void tr_print (SRunner *sr, TestResult *tr, int print_mode); static char *signal_msg (int sig); static char *exit_msg (int exitstatus); static int non_pass (int val); @@ -70,6 +56,7 @@ SRunner *srunner_create (Suite *s) sr->stats = emalloc (sizeof(TestStats)); /* freed in srunner_free */ sr->stats->n_checked = sr->stats->n_failed = sr->stats->n_errors = 0; sr->resultlst = list_create(); + sr->log_fname = NULL; return sr; } @@ -103,10 +90,8 @@ void srunner_run_all (SRunner *sr, int print_mode) if (print_mode < 0 || print_mode >= CRLAST) eprintf("Bad print_mode argument to srunner_run_all: %d", print_mode); - if (print_mode > CRSILENT) { - printf ("Running suite: %s\n", sr->s->name); - fflush (stdout); - } + srunner_printf (sr, CRMINIMAL, print_mode, + "Running suite: %s\n", sr->s->name); tcl = sr->s->tclst; @@ -114,9 +99,8 @@ void srunner_run_all (SRunner *sr, int print_mode) tc = list_val (tcl); srunner_run_tcase (sr, tc); } - if (print_mode >= CRMINIMAL) - srunner_print_summary (sr); - srunner_print_results (sr, print_mode); + + srunner_print (sr, print_mode); } static void srunner_add_failure (SRunner *sr, TestResult *tr) @@ -233,15 +217,38 @@ static TestResult *tfun_run (int msqid, char *tcname, TF *tfun) /* Printing */ -void srunner_print_summary (SRunner *sr) +static void srunner_printf (SRunner *sr, int target_mode, + int print_mode, char *fmt, ...) +{ + va_list args; + + if (print_mode >= target_mode) { + fflush(stdout); + + va_start(args, fmt); + vfprintf(stdout, fmt, args); + va_end(args); + fflush(stdout); + } +} + +void srunner_print (SRunner *sr, int print_mode) +{ + srunner_print_summary (sr, print_mode); + srunner_print_results (sr, print_mode); +} + +static void srunner_print_summary (SRunner *sr, int print_mode) { TestStats *ts = sr->stats; - printf ("%d%%: Checks: %d, Failures: %d, Errors: %d\n", - percent_passed (ts), ts->n_checked, ts->n_failed, ts->n_errors); + srunner_printf (sr, CRMINIMAL, print_mode, + "%d%%: Checks: %d, Failures: %d, Errors: %d\n", + percent_passed (ts), ts->n_checked, ts->n_failed, + ts->n_errors); return; } -void srunner_print_results (SRunner *sr, int print_mode) +static void srunner_print_results (SRunner *sr, int print_mode) { List *resultlst; if (print_mode < CRNORMAL) @@ -251,13 +258,7 @@ void srunner_print_results (SRunner *sr, int print_mode) for (list_front(resultlst); !list_at_end(resultlst); list_advance(resultlst)) { TestResult *tr = list_val(resultlst); - if (tr_rtype(tr) == CRPASS) - if (print_mode >= CRVERBOSE) - tr_print (tr); - else - ; - else - tr_print (tr); + tr_print (sr, tr, print_mode); } return; } @@ -358,13 +359,20 @@ static char *rtype_to_string (int rtype) } } -static void tr_print (TestResult *tr) +static void tr_print (SRunner *sr, TestResult *tr, int print_mode) { char *exact_msg; exact_msg = (tr->rtype == CRERROR) ? "(after this point) ": ""; - printf ("%s:%d:%s:%s: %s%s\n", tr->file, tr->line, - rtype_to_string(tr->rtype), tr->tcname, - exact_msg, tr->msg); + if (tr->rtype == CRPASS) + srunner_printf (sr, CRVERBOSE, print_mode, + "%s:%d:%s:%s: %s%s\n", tr->file, tr->line, + rtype_to_string(tr->rtype), tr->tcname, + exact_msg, tr->msg); + else + srunner_printf (sr, CRNORMAL, print_mode, + "%s:%d:%s:%s: %s%s\n", tr->file, tr->line, + rtype_to_string(tr->rtype), tr->tcname, + exact_msg, tr->msg); } static char *signal_msg (int signal) diff --git a/check/tests/check_check_log.c b/check/tests/check_check_log.c index 2843c00..d631af3 100644 --- a/check/tests/check_check_log.c +++ b/check/tests/check_check_log.c @@ -12,8 +12,33 @@ START_TEST(test_set_log) srunner_set_log (sr, "test_log"); fail_unless (srunner_has_log (sr), "SRunner not logging"); + fail_unless (strcmp(srunner_log_fname(sr), "test_log") == 0, + "Bad file name returned"); } -END_TEST +END_TEST + +START_TEST(test_no_set_log) +{ + Suite *s = suite_create("Suite"); + SRunner *sr = srunner_create(s); + + fail_unless (!srunner_has_log (sr), "SRunner not logging"); + fail_unless (srunner_log_fname(sr) == NULL, "Bad file name returned"); +} +END_TEST + +START_TEST(test_double_set_log) +{ + Suite *s = suite_create("Suite"); + SRunner *sr = srunner_create(s); + + srunner_set_log (sr, "test_log"); + srunner_set_log (sr, "test2_log"); + + fail_unless(strcmp(srunner_log_fname(sr), "test_log") == 0, + "Log file is initialize only and shouldn't be changeable once set"); +} +END_TEST; Suite *make_log_suite(void) { @@ -26,6 +51,8 @@ Suite *make_log_suite(void) suite_add_tcase(s, tc_core); tcase_add_test(tc_core, test_set_log); + tcase_add_test(tc_core, test_no_set_log); + tcase_add_test(tc_core, test_double_set_log); return s; } diff --git a/check/tests/test_output.sh b/check/tests/test_output.sh index dc4e54f..5464990 100755 --- a/check/tests/test_output.sh +++ b/check/tests/test_output.sh @@ -18,21 +18,21 @@ op1=`./ex_output CRMINIMAL` op2=`./ex_output CRNORMAL` op3=`./ex_output CRVERBOSE` -if [ "$t0" != x"$op0" ]; then - echo "Problem with ex_output CRSILENT"; - exit 1 -fi -if [ "$t1" != x"$op1" ]; then - echo "Problem with ex_output CRMINIMAL"; - exit 1 -fi -if [ "$t2" != x"$op2" ]; then - echo "Problem with ex_output CRNORMAL"; - exit 1 -fi -if [ "$t3" != x"$op3" ]; then - echo "Problem with ex_output CRVERBOSE"; - exit 1 -fi +test_output ( ) { + if [ "${1}" != "${2}" ]; then + echo "Problem with ex_output ${3}"; + echo "Expected:"; + echo "${1}"; + echo "Got:"; + echo "${2}"; + exit 1; + fi + +} + +test_output "$t0" x"$op0" "CRSILENT"; +test_output "$t1" x"$op1" "CRMINIMAL"; +test_output "$t2" x"$op2" "CRNORMAL"; +test_output "$t3" x"$op3" "CRVERBOSE"; exit 0