Debian Codegraph @

tests/xml_reporter_tests.c

raw symbols SCIP
1 #include <cgreen/cgreen.h>
2 #include <cgreen/breadcrumb.h>
3 #include <cgreen/messaging.h>
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <unistd.h>
8
9 #ifdef __cplusplus
10 using namespace cgreen;
11 #endif
12
13
14 #include "xml_reporter_internal.h"
15
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 char* pointer_to_substring(char* haystack, const char* needle) {
37 return strstr(haystack,needle);
38 }
39
40 static char* pointer_to_second_substring(char* haystack, const char* needle) {
41 return strstr(strstr(haystack,needle)+1,needle);
42 }
43
44 static int mocked_printf(FILE *file, const char *format, ...) {
45 char buffer[10000];
46 va_list ap;
47 va_start(ap, format);
48 vsnprintf(buffer, sizeof(buffer), format, ap);
49 va_end(ap);
50
51 (void)file;
52 output = concat(output, buffer);
53 return strlen(output);
54 }
55
56 static TestReporter *reporter;
57
58 static void setup_xml_reporter_tests(void) {
59 reporter = create_xml_reporter("PREFIX");
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(667);
65
66 clear_output();
67 set_xml_reporter_printer(reporter, mocked_printf);
68 }
69
70 static void teardown_xml_reporter_tests(void) {
71 //bad mojo when running tests in same process, as destroy_reporter also sets
72 //context.reporter = NULL, thus breaking the next test to run
73 destroy_reporter(reporter);
74 if (NULL != output) {
75 free(output);
76 //need to set output to NULL to avoid second free in
77 //subsequent call to setup_xml_reporter_tests->clear_output
78 //when running tests in same process
79 output = NULL;
80 }
81 }
82
83
84 Describe(XmlReporter);
85 BeforeEach(XmlReporter) {
86 setup_xml_reporter_tests();
87 }
88 AfterEach(XmlReporter) {
89 teardown_xml_reporter_tests();
90 }
91
92
93 Ensure(XmlReporter, will_report_beginning_of_suite) {
94 reporter->start_suite(reporter, "suite_name", 2);
95 assert_that(output, begins_with_string("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n<testsuite name=\"suite_name"));
96 }
97
98
99 Ensure(XmlReporter, will_report_beginning_and_successful_finishing_of_passing_test) {
100 va_list null_arguments;
101 memset(&null_arguments, 0, sizeof(null_arguments));
102
103 reporter->start_test(reporter, "test_name");
104 assert_that(output, begins_with_string("<testcase classname=\"\""));
105 assert_that(output, contains_string("name=\"test_name\""));
106
107 clear_output();
108
109 reporter->show_pass(reporter, "file", 2, "test_name", null_arguments);
110 assert_that(strlen(output), is_equal_to(0));
111
112 reporter->finish_test(reporter, "filename", line, NULL);
113 assert_that(output, contains_string("</testcase>"));
114 }
115
116
117 Ensure(XmlReporter, will_report_a_failing_test) {
118 va_list null_arguments;
119 memset(&null_arguments, 0, sizeof(null_arguments));
120
121 reporter->start_test(reporter, "test_name");
122 reporter->show_fail(reporter, "file", 2, "test_name", null_arguments);
123 reporter->finish_test(reporter, "filename", line, NULL);
124
125 assert_that(output, contains_string("<failure message=\"test_name\">"));
126 assert_that(output, contains_string("<location file=\"file\" line=\"2\"/>"));
127 assert_that(strstr(output, "time="), is_less_than(strstr(output, "<failure")));
128 }
129
130 Ensure(XmlReporter, will_report_a_failing_test_only_once) {
131 va_list null_arguments;
132 memset(&null_arguments, 0, sizeof(null_arguments));
133
134 reporter->start_test(reporter, "test_name");
135 reporter->show_fail(reporter, "file", 2, "test_failure_message", null_arguments);
136 reporter->show_fail(reporter, "file", 2, "other_message", null_arguments);
137 reporter->finish_test(reporter, "filename", line, NULL);
138
139 assert_that(pointer_to_substring(output, "<failure message=\"test_failure_message\">"), is_not_null);
140 assert_that(pointer_to_substring(output, "<failure message=\"other_message\">"), is_not_null);
141 assert_that(pointer_to_second_substring(output, "<failure message=\"test_failure_message\">"), is_null);
142 }
143
144
145 Ensure(XmlReporter, will_report_finishing_of_suite) {
146 reporter->start_suite(reporter, "suite_name", 1);
147 reporter->finish_suite(reporter, "filename", line);
148
149 assert_that(output, contains_string("</testsuite>"));
150 }
151
152
153 Ensure(XmlReporter, will_mark_ignored_test_as_skipped) {
154 const int line = 666;
155
156 reporter->start_suite(reporter, "suite_name", 1);
157 reporter->start_test(reporter, "skipped_test_name");
158 send_reporter_skipped_notification(reporter);
159 reporter->finish_test(reporter, "filename", line, "message");
160 reporter->finish_suite(reporter, "filename", line);
161
162 assert_that(output, contains_string("<skipped />"));
163 assert_that(strstr(output, "time="), is_less_than(strstr(output, "<skipped")));
164 }
165
166
167 Ensure(XmlReporter, will_report_non_finishing_test) {
168 const int line = 666;
169
170 reporter->start_suite(reporter, "suite_name", 1);
171 reporter->start_test(reporter, "test_name");
172 send_reporter_exception_notification(reporter);
173 reporter->finish_test(reporter, "filename", line, "message");
174 reporter->finish_suite(reporter, "filename", line);
175
176 assert_that(output, contains_string("error type=\"Fatal\""));
177 assert_that(output, contains_string("message=\"message\""));
178 }
179
180 Ensure(XmlReporter, will_report_time_correctly_for_non_finishing_test) {
181 const int line = 666;
182
183 reporter->start_suite(reporter, "suite_name", 1);
184 reporter->start_test(reporter, "test_name");
185 send_reporter_exception_notification(reporter);
186 reporter->finish_test(reporter, "filename", line, "message");
187 reporter->finish_suite(reporter, "filename", line);
188
189 assert_that(output, contains_string("name=\"test_name\" time=\""));
190 }
191
192
193 TestSuite *xml_reporter_tests(void) {
194 TestSuite *suite = create_test_suite();
195 set_setup(suite, setup_xml_reporter_tests);
196
197 add_test_with_context(suite, XmlReporter, will_report_beginning_of_suite);
198 add_test_with_context(suite, XmlReporter, will_report_beginning_and_successful_finishing_of_passing_test);
199 add_test_with_context(suite, XmlReporter, will_report_a_failing_test);
200 add_test_with_context(suite, XmlReporter, will_report_a_failing_test_only_once);
201 add_test_with_context(suite, XmlReporter, will_mark_ignored_test_as_skipped);
202 add_test_with_context(suite, XmlReporter, will_report_finishing_of_suite);
203 add_test_with_context(suite, XmlReporter, will_report_non_finishing_test);
204 add_test_with_context(suite, XmlReporter, will_report_time_correctly_for_non_finishing_test);
205
206 set_teardown(suite, teardown_xml_reporter_tests);
207 return suite;
208 }