Debian Codegraph @

tests/cute_reporter_tests.c

raw symbols SCIP
1
2 #include <cgreen/breadcrumb.h>
3 #include <cgreen/cgreen.h>
4 #include <cgreen/messaging.h>
5
6 #include <stdio.h>
7 #include <string.h>
8
9 #include <cgreen/cute_reporter.h>
10 #include "cute_reporter_internal.h"
11
12
13 #ifdef __cplusplus
14 using namespace cgreen;
15 #endif
16
17 static const int line=666;
18 static char *output = NULL;
19
20 static void clear_output(void)
21 {
22 if (NULL != output) {
23 free(output);
24 }
25
26 output = (char*)malloc(1);
27 *output = '\0';
28 }
29
30 static char *concat(char *output, char *buffer) {
31 output = (char *) realloc(output, strlen(output)+strlen(buffer)+1);
32 strcat(output, buffer);
33 return output;
34 }
35
36 static int mocked_vprintf(const char *format, va_list arguments) {
37 char buffer[10000];
38
39 vsnprintf(buffer, sizeof(buffer), format, arguments);
40
41 output = concat(output, buffer);
42 return strlen(output);
43 }
44
45 static int mocked_printf(const char *format, ...) {
46 int result = 0;
47
48 va_list ap;
49 va_start(ap, format);
50 result = mocked_vprintf(format, ap);
51 va_end(ap);
52
53 return result;
54 }
55
56 static TestReporter *reporter;
57
58 static void setup_cute_reporter_tests(void) {
59 reporter = create_cute_reporter();
60
61 // We can not use setup_reporting() since we are running
62 // inside a test suite which needs the real reporting
63 // So we'll have to set up the messaging explicitly
64 reporter->ipc = start_cgreen_messaging(666);
65
66 clear_output();
67 set_cute_reporter_printer(reporter, mocked_printf);
68 set_cute_reporter_vprinter(reporter, mocked_vprintf);
69 }
70
71 static void teardown_cute_reporter_tests(void) {
72 //bad mojo when running tests in same process, as destroy_reporter also sets
73 //context.reporter = NULL, thus breaking the next test to run
74 destroy_reporter(reporter);
75 if (NULL != output) {
76 free(output);
77 //need to set output to NULL to avoid second free in subsequent call to setup_cute_reporter_tests->clear_output
78 //when running tests in same process
79 output = NULL;
80 }
81 }
82
83 static void assert_no_output(void) {
84 assert_that(strlen(output), is_equal_to(0));
85 }
86
87 Describe(CuteReporter);
88 BeforeEach(CuteReporter) {
89 setup_cute_reporter_tests();
90 }
91 AfterEach(CuteReporter) {
92 teardown_cute_reporter_tests();
93 }
94
95 Ensure(CuteReporter, will_report_beginning_of_suite) {
96 reporter->start_suite(reporter, "suite_name", 2);
97 assert_that(output, begins_with_string("#beginning"));
98 assert_that(output, contains_string("suite_name"));
99 }
100
101 Ensure(CuteReporter, will_report_beginning_and_successful_finishing_of_test) {
102 va_list arguments;
103
104 reporter->start_test(reporter, "test_name");
105 assert_that(output, begins_with_string("#starting"));
106 assert_that(output, contains_string("test_name"));
107
108 clear_output();
109
110 memset(&arguments, 0, sizeof(va_list));
111 reporter->show_pass(reporter, "file", 2, "test_name", arguments);
112 assert_no_output();
113
114 // Must indicate test case completion before calling finish_test()
115 send_reporter_completion_notification(reporter);
116 reporter->finish_test(reporter, "filename", line, NULL);
117 assert_that(output, begins_with_string("#success"));
118 assert_that(output, contains_string("test_name"));
119 }
120
121 Ensure(CuteReporter, will_report_failing_of_test_only_once) {
122 va_list arguments;
123
124 reporter->start_test(reporter, "test_name");
125
126 clear_output();
127 reporter->failures++; // Simulating a failed assert
128 memset(&arguments, 0, sizeof(va_list));
129 reporter->show_fail(reporter, "file", 2, "test_name", arguments);
130 assert_that(output, begins_with_string("#failure"));
131 assert_that(output, contains_string("test_name"));
132
133 clear_output();
134 reporter->failures++; // Simulating another failed assert
135 reporter->show_fail(reporter, "file", 2, "test_name", arguments);
136 assert_no_output();
137
138 // Must indicate test case completion before calling finish_test()
139 send_reporter_completion_notification(reporter);
140 reporter->finish_test(reporter, "filename", line, NULL);
141 assert_no_output();
142 }
143
144 static void wrapped_show_fail(TestReporter *reporter, const char *file, int line,
145 const char *message, ...) {
146 va_list arguments;
147 va_start(arguments, message);
148 reporter->show_fail(reporter, file, line, message, arguments);
149 va_end(arguments);
150 }
151
152 Ensure(CuteReporter, will_use_arguments_for_show_fail) {
153 reporter->start_test(reporter, "test_name");
154
155 clear_output();
156 reporter->failures++; // Simulating a failed assert
157 wrapped_show_fail(reporter, "file", 2, "test_case %i provides %s", 42, "the answer");
158 assert_that(output, begins_with_string("#failure"));
159 assert_that(output, contains_string("test_case 42 provides the answer"));
160
161 clear_output();
162
163 // Must indicate test case completion before calling finish_test()
164 send_reporter_completion_notification(reporter);
165 reporter->finish_test(reporter, "filename", line, NULL);
166 assert_no_output();
167 }
168
169 Ensure(CuteReporter, will_report_finishing_of_suite) {
170 // Must indicate test suite completion before calling finish_suite()
171 reporter->start_suite(reporter, "suite_name", 1);
172
173 clear_output();
174
175 send_reporter_completion_notification(reporter);
176 reporter->finish_suite(reporter, "filename", line);
177
178 assert_that(output, begins_with_string("#ending"));
179 assert_that(output, contains_string("suite_name"));
180 }
181
182 Ensure(CuteReporter, will_report_non_finishing_test) {
183 const int line = 666;
184 reporter->start_suite(reporter, "suite_name", 1);
185 clear_output();
186
187 send_reporter_exception_notification(reporter);
188 reporter->finish_suite(reporter, "filename", line);
189
190 assert_that(output, begins_with_string("#error"));
191 assert_that(output, contains_string("failed to complete"));
192 }
193
194 TestSuite *cute_reporter_tests(void) {
195 TestSuite *suite = create_test_suite();
196 set_setup(suite, setup_cute_reporter_tests);
197
198 add_test_with_context(suite, CuteReporter, will_report_beginning_of_suite);
199 add_test_with_context(suite, CuteReporter, will_report_beginning_and_successful_finishing_of_test);
200 add_test_with_context(suite, CuteReporter, will_report_failing_of_test_only_once);
201 add_test_with_context(suite, CuteReporter, will_report_finishing_of_suite);
202 add_test_with_context(suite, CuteReporter, will_report_non_finishing_test);
203
204 set_teardown(suite, teardown_cute_reporter_tests);
205 return suite;
206 }