| 1 | #include <cgreen/cute_reporter.h> |
| 2 | #include <cgreen/breadcrumb.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <stdio.h> |
| 5 | #include <string.h> |
| 6 | |
| 7 | #include "cute_reporter_internal.h" |
| 8 | |
| 9 | #ifdef __ANDROID__ |
| 10 | #include "cgreen/internal/android_headers/androidcompat.h" |
| 11 | #endif // #ifdef __ANDROID__ |
| 12 | |
| 13 | typedef struct { |
| 14 | CutePrinter *printer; |
| 15 | CuteVPrinter *vprinter; |
| 16 | int error_count; // For status within the test case process |
| 17 | int previous_error; // For status outside the test case process |
| 18 | } CuteMemo; |
| 19 | |
| 20 | static void cute_start_suite(TestReporter *reporter, |
| 21 | const char *name, const int number_of_tests); |
| 22 | static void cute_start_test(TestReporter *reporter, |
| 23 | const char *name); |
| 24 | static void show_fail(TestReporter *reporter, const char *file, int line, |
| 25 | const char *message, va_list arguments); |
| 26 | static void show_pass(TestReporter *reporter, const char *file, int line, |
| 27 | const char *message, va_list arguments); |
| 28 | static void cute_failed_to_complete(TestReporter *reporter, |
| 29 | const char *file, int line, const char *message, va_list arguments); |
| 30 | static void cute_finish_test(TestReporter *reporter, |
| 31 | const char *filename, int line, const char *message); |
| 32 | static void cute_finish_suite(TestReporter *reporter, |
| 33 | const char *filename, int line); |
| 34 | |
| 35 | |
| 36 | void set_cute_reporter_printer(TestReporter *reporter, CutePrinter *new_printer) { |
| 37 | CuteMemo *memo = (CuteMemo *)reporter->memo; |
| 38 | memo->printer = new_printer; |
| 39 | } |
| 40 | |
| 41 | void set_cute_reporter_vprinter(TestReporter *reporter, CuteVPrinter *new_vprinter) { |
| 42 | CuteMemo *memo = (CuteMemo *)reporter->memo; |
| 43 | memo->vprinter = new_vprinter; |
| 44 | } |
| 45 | |
| 46 | TestReporter *create_cute_reporter(void) { |
| 47 | CuteMemo *memo; |
| 48 | TestReporter *reporter; |
| 49 | |
| 50 | reporter = create_reporter(); |
| 51 | if (reporter == NULL) { |
| 52 | return NULL; |
| 53 | } |
| 54 | |
| 55 | memo = (CuteMemo *) malloc(sizeof(CuteMemo) + 100); |
| 56 | if (memo == NULL) { |
| 57 | destroy_reporter(reporter); |
| 58 | return NULL; |
| 59 | } |
| 60 | reporter->memo = memo; |
| 61 | |
| 62 | set_cute_reporter_printer(reporter, printf); |
| 63 | set_cute_reporter_vprinter(reporter, vprintf); |
| 64 | |
| 65 | reporter->start_suite = &cute_start_suite; |
| 66 | reporter->start_test = &cute_start_test; |
| 67 | reporter->show_fail = &show_fail; |
| 68 | reporter->show_pass = &show_pass; |
| 69 | reporter->show_incomplete = &cute_failed_to_complete; |
| 70 | reporter->finish_test = &cute_finish_test; |
| 71 | reporter->finish_suite = &cute_finish_suite; |
| 72 | reporter->memo = memo; |
| 73 | |
| 74 | return reporter; |
| 75 | } |
| 76 | |
| 77 | static void cute_start_suite(TestReporter *reporter, |
| 78 | const char *name, const int number_of_tests) { |
| 79 | reporter->passes = 0; |
| 80 | reporter->failures = 0; |
| 81 | reporter->skips = 0; |
| 82 | reporter->exceptions = 0; |
| 83 | |
| 84 | CuteMemo *memo = (CuteMemo *)reporter->memo; |
| 85 | reporter_start_test(reporter, name); |
| 86 | memo->printer("#beginning %s %d\n", name, number_of_tests); |
| 87 | } |
| 88 | |
| 89 | static void cute_start_test(TestReporter *reporter, |
| 90 | const char *name) { |
| 91 | CuteMemo *memo = (CuteMemo *) reporter->memo; |
| 92 | memo->error_count = reporter->failures + reporter->exceptions; |
| 93 | memo->previous_error = 0; |
| 94 | reporter_start_test(reporter, name); |
| 95 | memo->printer("#starting %s\n", name); |
| 96 | } |
| 97 | |
| 98 | static void cute_finish_test(TestReporter *reporter, const char *filename, int line, const char *message) { |
| 99 | CuteMemo *memo = (CuteMemo *) reporter->memo; |
| 100 | const char *name = get_current_from_breadcrumb((CgreenBreadcrumb *)reporter->breadcrumb); |
| 101 | |
| 102 | reporter_finish_test(reporter, filename, line, message); |
| 103 | if (memo->error_count == reporter->failures + reporter->exceptions) { |
| 104 | memo->printer("#success %s OK\n", name); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | static void cute_finish_suite(TestReporter *reporter, const char *filename, int line) { |
| 109 | const char *name = get_current_from_breadcrumb((CgreenBreadcrumb *)reporter->breadcrumb); |
| 110 | CuteMemo *memo = (CuteMemo *)reporter->memo; |
| 111 | |
| 112 | reporter_finish_test(reporter, filename, line, NULL); |
| 113 | |
| 114 | reporter->total_passes += reporter->passes; |
| 115 | reporter->total_failures += reporter->failures; |
| 116 | reporter->total_skips += reporter->skips; |
| 117 | reporter->total_exceptions += reporter->exceptions; |
| 118 | |
| 119 | memo->printer("#ending %s", name); |
| 120 | if (get_breadcrumb_depth((CgreenBreadcrumb *) reporter->breadcrumb) == 0) { |
| 121 | memo->printer(": %d pass%s, %d failure%s, %d exception%s, %d ms.\n", |
| 122 | reporter->total_passes, reporter->total_passes == 1 ? "" : "es", |
| 123 | reporter->total_failures, reporter->total_failures == 1 ? "" : "s", |
| 124 | reporter->total_exceptions, reporter->total_exceptions == 1 ? "" : "s", |
| 125 | reporter->total_duration); |
| 126 | } else |
| 127 | memo->printer("\n"); |
| 128 | } |
| 129 | |
| 130 | static void show_fail(TestReporter *reporter, const char *file, int line, |
| 131 | const char *message, va_list arguments) { |
| 132 | CuteMemo *memo = (CuteMemo *) reporter->memo; |
| 133 | if (!memo->previous_error) { |
| 134 | memo->printer("#failure %s", |
| 135 | get_current_from_breadcrumb((CgreenBreadcrumb *) reporter->breadcrumb)); |
| 136 | memo->printer(" %s:%d ", file, line); |
| 137 | if (message == NULL) { |
| 138 | memo->printer("<FATAL: NULL for failure message>"); |
| 139 | } else { |
| 140 | memo->vprinter(message, arguments); |
| 141 | } |
| 142 | memo->printer("\n"); |
| 143 | memo->previous_error = 1; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | static void show_pass(TestReporter *reporter, const char *file, int line, |
| 148 | const char *message, va_list arguments) { |
| 149 | (void) reporter; |
| 150 | (void) file; |
| 151 | (void) line; |
| 152 | (void) message; |
| 153 | (void) arguments; |
| 154 | } |
| 155 | |
| 156 | static void cute_failed_to_complete(TestReporter *reporter, |
| 157 | const char *file, int line, const char *message, va_list arguments) { |
| 158 | CuteMemo *memo = (CuteMemo *)reporter->memo; |
| 159 | |
| 160 | /* TODO: add additional message to output */ |
| 161 | (void)file; |
| 162 | (void)line; |
| 163 | (void)message; |
| 164 | (void)arguments; |
| 165 | |
| 166 | memo->printer("#error %s failed to complete\n", |
| 167 | get_current_from_breadcrumb((CgreenBreadcrumb *)reporter->breadcrumb)); |
| 168 | } |
| 169 | /* vim: set ts=4 sw=4 et cindent: */ |