Back to home page

Enduro/X

 
 

    


0001 #include <cgreen/cute_reporter.h>
0002 #include <cgreen/reporter.h>
0003 #include <cgreen/breadcrumb.h>
0004 #include <stdlib.h>
0005 #include <stdio.h>
0006 #include <string.h>
0007 
0008 #ifdef __cplusplus
0009 namespace cgreen {
0010 #endif
0011 
0012 typedef struct {
0013     Printer *printer;
0014     int error_count;    /* For status within the test case process */
0015     int previous_error; /* For status outside the test case process */
0016 } CuteMemo;
0017 
0018 static void cute_reporter_suite_started(TestReporter *reporter,
0019         const char *name, const int number_of_tests);
0020 static void cute_reporter_testcase_started(TestReporter *reporter,
0021         const char *name);
0022 static void assert_failed(TestReporter *reporter, const char *file, int line,
0023         const char *message, va_list arguments);
0024 static void assert_passed(TestReporter *reporter, const char *file, int line,
0025         const char *message, va_list arguments);
0026 static void testcase_failed_to_complete(TestReporter *reporter,
0027         const char *file, int line, const char *message, va_list arguments);
0028 static void cute_reporter_testcase_finished(TestReporter *reporter,
0029         const char *filename, int line, const char *message);
0030 static void cute_reporter_suite_finished(TestReporter *reporter,
0031         const char *filename, int line);
0032 
0033 void set_cute_printer(TestReporter *reporter, Printer *printer) {
0034     CuteMemo *memo = (CuteMemo *) reporter->memo;
0035     memo->printer = printer;
0036 }
0037 
0038 TestReporter *create_cute_reporter(void) {
0039     CuteMemo *memo;
0040     TestReporter *reporter;
0041 
0042     reporter = create_reporter();
0043     if (reporter == NULL) {
0044         return NULL;
0045     }
0046 
0047     memo = (CuteMemo *) malloc(sizeof(CuteMemo) + 100);
0048     if (memo == NULL) {
0049         destroy_reporter(reporter);
0050         return NULL;
0051     }
0052 
0053     memo->printer = printf;
0054 
0055     reporter->start_suite = &cute_reporter_suite_started;
0056     reporter->start_test = &cute_reporter_testcase_started;
0057     reporter->show_fail = &assert_failed;
0058     reporter->show_pass = &assert_passed;
0059     reporter->show_incomplete = &testcase_failed_to_complete;
0060     reporter->finish_test = &cute_reporter_testcase_finished;
0061     reporter->finish_suite = &cute_reporter_suite_finished;
0062     reporter->memo = memo;
0063 
0064     return reporter;
0065 }
0066 
0067 static void cute_reporter_suite_started(TestReporter *reporter,
0068         const char *name, const int number_of_tests) {
0069     CuteMemo *memo = (CuteMemo *) reporter->memo;
0070     reporter_start(reporter, name);
0071     memo->printer("#beginning %s %d\n", name, number_of_tests);
0072 }
0073 
0074 static void cute_reporter_testcase_started(TestReporter *reporter,
0075         const char *name) {
0076     CuteMemo *memo = (CuteMemo *) reporter->memo;
0077     memo->error_count = reporter->failures + reporter->exceptions;
0078     memo->previous_error = 0;
0079     reporter_start(reporter, name);
0080     memo->printer("#starting %s\n", name);
0081 }
0082 
0083 static void cute_reporter_testcase_finished(TestReporter *reporter, const char *filename, int line, const char *message) {
0084     CuteMemo *memo = (CuteMemo *) reporter->memo;
0085     const char *name = get_current_from_breadcrumb((CgreenBreadcrumb *)reporter->breadcrumb);
0086 
0087     reporter_finish(reporter, filename, line, message);
0088     if (memo->error_count == reporter->failures + reporter->exceptions) {
0089         memo->printer("#success %s OK\n", name);
0090     }
0091 }
0092 
0093 static void cute_reporter_suite_finished(TestReporter *reporter,
0094         const char *filename, int line) {
0095     CuteMemo *memo = (CuteMemo *) reporter->memo;
0096     const char *name = get_current_from_breadcrumb((CgreenBreadcrumb *)reporter->breadcrumb);
0097     reporter_finish(reporter, filename, line, NULL);
0098 
0099     memo->printer("#ending %s", name);
0100     if (get_breadcrumb_depth((CgreenBreadcrumb *) reporter->breadcrumb) == 0) {
0101         memo->printer(": %d pass%s, %d failure%s, %d exception%s.\n",
0102                 reporter->passes, reporter->passes == 1 ? "" : "es",
0103                 reporter->failures, reporter->failures == 1 ? "" : "s",
0104                 reporter->exceptions, reporter->exceptions == 1 ? "" : "s");
0105     } else
0106         memo->printer("\n");
0107 }
0108 
0109 static void assert_failed(TestReporter *reporter, const char *file, int line,
0110         const char *message, va_list arguments) {
0111     CuteMemo *memo = (CuteMemo *) reporter->memo;
0112     if (!memo->previous_error) {
0113         char buffer[1000];
0114         memo->printer("#failure %s",
0115                 get_current_from_breadcrumb(
0116                         (CgreenBreadcrumb *) reporter->breadcrumb));
0117         memo->printer(" %s:%d ", file, line);
0118         vsprintf(buffer, (message == NULL ? "Problem" : message), arguments);
0119         memo->printer("%s\n", buffer);
0120         memo->previous_error = 1;
0121     }
0122 }
0123 
0124 static void assert_passed(TestReporter *reporter, const char *file, int line,
0125         const char *message, va_list arguments) {
0126     (void) reporter;
0127     (void) file;
0128     (void) line;
0129     (void) message;
0130     (void) arguments;
0131 }
0132 
0133 static void testcase_failed_to_complete(TestReporter *reporter,
0134         const char *file, int line, const char *message, va_list arguments) {
0135     /* TODO: add additional message to output */
0136     CuteMemo *memo;
0137     (void)file;
0138     (void)line;
0139     (void)message;
0140     (void)arguments;
0141 
0142     memo = (CuteMemo *) reporter->memo;
0143     memo->printer("#error %s failed to complete\n",
0144             get_current_from_breadcrumb((CgreenBreadcrumb *)reporter->breadcrumb));
0145 }
0146 
0147 #ifdef __cplusplus
0148 } // namespace cgreen
0149 #endif
0150 
0151 /* vim: set ts=4 sw=4 et cindent: */