| 1 | #include <cgreen/reporter.h> |
| 2 | #include <cgreen/messaging.h> |
| 3 | #include <cgreen/breadcrumb.h> |
| 4 | #include <stdio.h> |
| 5 | #include <string.h> |
| 6 | #include <stdarg.h> |
| 7 | #include <stdbool.h> |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | |
| 11 | enum { pass = 1, fail, skipped ,completion, exception }; |
| 12 | enum { FINISH_NOTIFICATION_RECEIVED = 0, FINISH_TEST_SKIPPED, FINISH_NOTIFICATION_NOT_RECEIVED }; |
| 13 | |
| 14 | struct TestContext_ { |
| 15 | TestReporter *reporter; |
| 16 | }; |
| 17 | typedef struct TestContext_ TestContext; |
| 18 | |
| 19 | static TestContext context; |
| 20 | |
| 21 | static void show_pass(TestReporter *reporter, const char *file, int line, |
| 22 | const char *message, va_list arguments); |
| 23 | static void show_skip(TestReporter *reporter, const char *file, int line); |
| 24 | static void show_fail(TestReporter *reporter, const char *file, int line, |
| 25 | const char *message, va_list arguments); |
| 26 | static void show_incomplete(TestReporter *reporter, const char *file, int line, |
| 27 | const char *message, va_list arguments); |
| 28 | static void assert_true(TestReporter *reporter, const char *file, int line, |
| 29 | int result, const char *message, ...); |
| 30 | static int read_reporter_results(TestReporter *reporter); |
| 31 | |
| 32 | TestReporter *get_test_reporter(void) { |
| 33 | return context.reporter; |
| 34 | } |
| 35 | |
| 36 | void setup_reporting(TestReporter *reporter) { |
| 37 | reporter->ipc = start_cgreen_messaging(45); |
| 38 | context.reporter = reporter; |
| 39 | } |
| 40 | |
| 41 | void set_reporter_options(TestReporter *reporter, void *options) { |
| 42 | /* We should really copy the options locally to avoid caller to |
| 43 | free the area, but we don't know the size of it... */ |
| 44 | reporter->options = options; |
| 45 | } |
| 46 | |
| 47 | TestReporter *create_reporter(void) { |
| 48 | CgreenBreadcrumb *breadcrumb; |
| 49 | TestReporter *reporter = (TestReporter *) malloc(sizeof(TestReporter)); |
| 50 | if (reporter == NULL) { |
| 51 | return NULL; |
| 52 | } |
| 53 | |
| 54 | breadcrumb = create_breadcrumb(); |
| 55 | if (breadcrumb == NULL) { |
| 56 | free(reporter); |
| 57 | return NULL; |
| 58 | } |
| 59 | |
| 60 | reporter->destroy = &destroy_reporter; |
| 61 | reporter->start_suite = &reporter_start_suite; |
| 62 | reporter->start_test = &reporter_start_test; |
| 63 | reporter->show_pass = &show_pass; |
| 64 | reporter->show_skip = &show_skip; |
| 65 | reporter->show_fail = &show_fail; |
| 66 | reporter->show_incomplete = &show_incomplete; |
| 67 | reporter->assert_true = &assert_true; |
| 68 | reporter->finish_test = &reporter_finish_test; |
| 69 | reporter->finish_suite = &reporter_finish_suite; |
| 70 | reporter->passes = 0; |
| 71 | reporter->skips = 0; |
| 72 | reporter->failures = 0; |
| 73 | reporter->exceptions = 0; |
| 74 | reporter->duration = 0; |
| 75 | reporter->total_passes = 0; |
| 76 | reporter->total_skips = 0; |
| 77 | reporter->total_failures = 0; |
| 78 | reporter->total_exceptions = 0; |
| 79 | reporter->total_duration = 0; |
| 80 | reporter->breadcrumb = breadcrumb; |
| 81 | reporter->memo = NULL; |
| 82 | reporter->options = NULL; |
| 83 | return reporter; |
| 84 | } |
| 85 | |
| 86 | void destroy_reporter(TestReporter *reporter) { |
| 87 | destroy_breadcrumb((CgreenBreadcrumb *)reporter->breadcrumb); |
| 88 | destroy_memo((TestReportMemo *)reporter->memo); |
| 89 | free(reporter); |
| 90 | // hack to allow destroy_reporter to be called in reporter_tests when |
| 91 | // tests are running in same process |
| 92 | if (context.reporter == reporter) |
| 93 | { |
| 94 | context.reporter = NULL; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void destroy_memo(TestReportMemo *memo) { |
| 99 | if (NULL != memo) { |
| 100 | free(memo); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void reporter_start_test(TestReporter *reporter, const char *name) { |
| 105 | push_breadcrumb((CgreenBreadcrumb *) reporter->breadcrumb, name); |
| 106 | } |
| 107 | |
| 108 | void reporter_start_suite(TestReporter *reporter, const char *name, const int count) { |
| 109 | (void) count; |
| 110 | push_breadcrumb((CgreenBreadcrumb *) reporter->breadcrumb, name); |
| 111 | } |
| 112 | |
| 113 | void reporter_finish_test(TestReporter *reporter, const char *filename, int line, const char *message) { |
| 114 | int status = read_reporter_results(reporter); |
| 115 | |
| 116 | if (status == FINISH_TEST_SKIPPED) { |
| 117 | (*reporter->show_skip)(reporter, filename, line); |
| 118 | } else if (status == FINISH_NOTIFICATION_NOT_RECEIVED) { |
| 119 | va_list no_arguments; |
| 120 | memset(&no_arguments, 0, sizeof(va_list)); |
| 121 | reporter->exceptions++; |
| 122 | (*reporter->show_incomplete)(reporter, filename, line, message, no_arguments); |
| 123 | } |
| 124 | |
| 125 | pop_breadcrumb((CgreenBreadcrumb *)reporter->breadcrumb); |
| 126 | } |
| 127 | |
| 128 | void reporter_finish_suite(TestReporter *reporter, const char *filename, int line) { |
| 129 | (void)filename; |
| 130 | (void)line; |
| 131 | |
| 132 | read_reporter_results(reporter); |
| 133 | pop_breadcrumb((CgreenBreadcrumb *)reporter->breadcrumb); |
| 134 | |
| 135 | } |
| 136 | |
| 137 | void add_reporter_result(TestReporter *reporter, int result) { |
| 138 | send_cgreen_message(reporter->ipc, result ? pass : fail); |
| 139 | } |
| 140 | |
| 141 | void send_reporter_exception_notification(TestReporter *reporter) { |
| 142 | send_cgreen_message(reporter->ipc, exception); |
| 143 | } |
| 144 | |
| 145 | void send_reporter_skipped_notification(TestReporter *reporter) { |
| 146 | send_cgreen_message(reporter->ipc, skipped); |
| 147 | } |
| 148 | |
| 149 | void send_reporter_completion_notification(TestReporter *reporter) { |
| 150 | send_cgreen_message(reporter->ipc, completion); |
| 151 | } |
| 152 | |
| 153 | static void show_pass(TestReporter *reporter, const char *file, int line, |
| 154 | const char *message, va_list arguments) { |
| 155 | (void)reporter; |
| 156 | (void)file; |
| 157 | (void)line; |
| 158 | (void)message; |
| 159 | (void)arguments; |
| 160 | } |
| 161 | |
| 162 | static void show_skip(TestReporter *reporter, const char *file, int line) { |
| 163 | (void)reporter; |
| 164 | (void)file; |
| 165 | (void)line; |
| 166 | } |
| 167 | |
| 168 | static void show_fail(TestReporter *reporter, const char *file, int line, |
| 169 | const char *message, va_list arguments) { |
| 170 | (void)reporter; |
| 171 | (void)file; |
| 172 | (void)line; |
| 173 | (void)message; |
| 174 | (void)arguments; |
| 175 | } |
| 176 | |
| 177 | static void show_incomplete(TestReporter *reporter, const char *file, int line, |
| 178 | const char *message, va_list arguments) { |
| 179 | (void)reporter; |
| 180 | (void)file; |
| 181 | (void)line; |
| 182 | (void)message; |
| 183 | (void)arguments; |
| 184 | } |
| 185 | |
| 186 | static void assert_true(TestReporter *reporter, const char *file, int line, |
| 187 | int result, const char *message, ...) { |
| 188 | va_list arguments; |
| 189 | memset(&arguments, 0, sizeof(va_list)); |
| 190 | |
| 191 | va_start(arguments, message); |
| 192 | if (result) { |
| 193 | (*reporter->show_pass)(reporter, file, line, message, arguments); |
| 194 | } else { |
| 195 | (*reporter->show_fail)(reporter, file, line, message, arguments); |
| 196 | } |
| 197 | |
| 198 | add_reporter_result(reporter, result); |
| 199 | va_end(arguments); |
| 200 | } |
| 201 | |
| 202 | static int read_reporter_results(TestReporter *reporter) { |
| 203 | int result; |
| 204 | while ((result = receive_cgreen_message(reporter->ipc)) > 0) { |
| 205 | if (result == pass) { |
| 206 | reporter->passes++; |
| 207 | } else if (result == skipped) { |
| 208 | reporter->skips++; |
| 209 | return FINISH_TEST_SKIPPED; |
| 210 | } else if (result == fail) { |
| 211 | reporter->failures++; |
| 212 | } else if (result == exception) { |
| 213 | reporter->exceptions++; |
| 214 | } else if (result == completion) { |
| 215 | /* TODO: this should always be the last message; if it's not, there's a bad race */ |
| 216 | return FINISH_NOTIFICATION_RECEIVED; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | return FINISH_NOTIFICATION_NOT_RECEIVED; |
| 221 | } |
| 222 | |
| 223 | /* vim: set ts=4 sw=4 et cindent: */ |