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