Debian Codegraph @

tests/text_reporter_tests.c

raw symbols SCIP
1 #include <cgreen/breadcrumb.h>
2 #include <cgreen/cgreen.h>
3 #include <cgreen/messaging.h>
4
5 #include <stdio.h>
6 #include <string.h>
7
8 #ifdef __cplusplus
9 using namespace cgreen;
10 #endif
11
12
13 #include <cgreen/text_reporter.h>
14 #include "src/text_reporter_internal.h"
15
16
17
18 static const int line=666;
19 static char *output = NULL;
20
21 static void clear_output(void)
22 {
23 if (NULL != output) {
24 free(output);
25 }
26
27 output = (char*)malloc(1);
28 *output = '\0';
29 }
30
31 static char *concat(char *output, char *buffer) {
32 output = (char *) realloc(output, strlen(output)+strlen(buffer)+1);
33 strcat(output, buffer);
34 return output;
35 }
36
37 static int mocked_vprinter(const char *format, va_list ap) {
38 char buffer[10000];
39
40 vsnprintf(buffer, sizeof(buffer), format, ap);
41
42 output = concat(output, buffer);
43 return strlen(output);
44 }
45
46 static int mocked_printer(const char *format, ...) {
47 int result = 0;
48
49 va_list ap;
50 va_start(ap, format);
51 result = mocked_vprinter(format, ap);
52 va_end(ap);
53
54 return result;
55 }
56
57 static TestReporter *reporter;
58
59
60 static void text_reporter_tests_setup(void) {
61 reporter = create_text_reporter();
62
63 // We can not use setup_reporting() since we are running
64 // inside a test suite which needs the real reporting
65 // So we'll have to set up the messaging explicitly
66 reporter->ipc = start_cgreen_messaging(666);
67
68 clear_output();
69 set_text_reporter_printer(reporter, mocked_printer);
70 set_text_reporter_vprinter(reporter, mocked_vprinter);
71 }
72
73 static void text_reporter_tests_teardown(void) {
74 reporter->destroy(reporter);
75
76 // bad mojo when running tests in same process, as destroy_reporter
77 // also sets context.reporter = NULL, thus breaking the next test
78 // to run
79 if (output != NULL) {
80 free(output);
81 // need to set output to NULL to avoid second free in
82 // subsequent call to setup_*_reporter_tests->clear_output when
83 // running tests in same process
84 output = NULL;
85 }
86 }
87
88
89 Describe(TextReporter);
90 BeforeEach(TextReporter) {
91 text_reporter_tests_setup();
92 }
93 AfterEach(TextReporter) {
94 text_reporter_tests_teardown();
95 }
96
97 Ensure(TextReporter, will_report_beginning_and_end_of_suites) {
98 reporter->start_suite(reporter, "suite_name", 2);
99 reporter->finish_suite(reporter, "filename", line);
100 assert_that(output, begins_with_string("Running \"suite_name\" (2 tests)"));
101 assert_that(output, contains_string("Completed \"suite_name\""));
102 }
103
104
105 Ensure(TextReporter, will_report_passed_for_test_with_one_pass_on_completion) {
106 reporter->start_suite(reporter, "suite_name", 15);
107 reporter->start_test(reporter, "test_name");
108
109 (*reporter->assert_true)(reporter, "file", 2, true, "");
110
111 // Must indicate test case completion before calling finish_test()
112 send_reporter_completion_notification(reporter);
113 reporter->finish_test(reporter, "filename", line, NULL);
114 reporter->finish_suite(reporter, "filename", line);
115
116 assert_that(output, contains_string("Completed \"suite_name\": 1 pass"));
117 }
118
119 Ensure(TextReporter, will_report_no_asserts_for_suites_with_no_asserts) {
120 reporter->start_suite(reporter, "suite_name", 15);
121 reporter->start_test(reporter, "test_name");
122
123 // Must indicate test case completion before calling finish_test()
124 send_reporter_completion_notification(reporter);
125 reporter->finish_test(reporter, "filename", line, NULL);
126 reporter->finish_suite(reporter, "filename", line);
127
128 assert_that(output, contains_string("Completed \"suite_name\": No assertions"));
129 }
130
131
132 Ensure(TextReporter, will_report_failed_once_for_each_fail) {
133 va_list arguments;
134
135 reporter->start_test(reporter, "test_name");
136
137 reporter->failures++; // Simulating a failed assert
138 memset(&arguments, 0, sizeof(va_list));
139 reporter->show_fail(reporter, "file", 2, "test_name", arguments);
140
141 assert_that(output, contains_string("file:2: Failure"));
142 clear_output();
143
144 reporter->failures++; // Simulating another failed assert
145 reporter->show_fail(reporter, "file", 3, "test_name", arguments);
146 assert_that(output, contains_string("file:3: Failure:"));
147 clear_output();
148
149 // Must indicate test case completion before calling finish_test()
150 send_reporter_completion_notification(reporter);
151 reporter->finish_test(reporter, "filename", line, NULL);
152 assert_that(output, is_equal_to_string(""));
153 }
154
155 static void wrapped_show_fail(TestReporter *reporter, const char *file, int line,
156 const char *message, ...) {
157 va_list arguments;
158 va_start(arguments, message);
159 reporter->show_fail(reporter, file, line, message, arguments);
160 va_end(arguments);
161 }
162
163 Ensure(TextReporter, will_use_arguments_for_show_fail) {
164 reporter->start_test(reporter, "test_name");
165
166 reporter->failures++; // Simulating a failed assert
167 wrapped_show_fail(reporter, "file", 2, "test_case %s requires %i arguments", "Foo", 2);
168
169 assert_that(output, contains_string("file:2: Failure"));
170 assert_that(output, contains_string("test_case Foo requires 2 arguments"));
171
172 clear_output();
173
174 // Must indicate test case completion before calling finish_test()
175 send_reporter_completion_notification(reporter);
176 reporter->finish_test(reporter, "filename", line, NULL);
177 assert_that(output, is_equal_to_string(""));
178 }
179
180 Ensure(TextReporter, will_report_non_finishing_test) {
181 const int line = 666;
182 reporter->start_suite(reporter, "suite_name", 1);
183
184 send_reporter_exception_notification(reporter);
185 reporter->finish_suite(reporter, "filename", line);
186
187 assert_that(output, contains_string("1 exception"));
188 }
189
190 /* Issue #335: in quiet mode the final summary must still reveal skipped
191 (xEnsure) and failed tests, otherwise there is no indication of them. */
192 Ensure(TextReporter, quiet_mode_reports_skips_in_the_summary) {
193 TextReporterOptions quiet = {0};
194 quiet.quiet_mode = true;
195 set_reporter_options(reporter, &quiet);
196
197 reporter->start_suite(reporter, "suite_name", 2);
198
199 reporter->start_test(reporter, "a_pass");
200 (*reporter->assert_true)(reporter, "file", 2, true, "");
201 send_reporter_completion_notification(reporter);
202 reporter->finish_test(reporter, "filename", line, NULL);
203
204 reporter->start_test(reporter, "a_skip");
205 send_reporter_skipped_notification(reporter);
206 reporter->finish_test(reporter, "filename", line, NULL);
207
208 reporter->finish_suite(reporter, "filename", line);
209
210 assert_that(output, contains_string("1 pass"));
211 assert_that(output, contains_string("1 skipped"));
212 }
213
214 /* Issue #335: a context that only suspended tests shows "S" in the dot stream. */
215 Ensure(TextReporter, quiet_mode_marks_a_suspended_context_with_S) {
216 TextReporterOptions quiet = {0};
217 quiet.quiet_mode = true;
218 set_reporter_options(reporter, &quiet);
219
220 reporter->start_suite(reporter, "run", 1);
221 reporter->start_suite(reporter, "a_context", 1);
222 reporter->start_test(reporter, "a_skip");
223 send_reporter_skipped_notification(reporter);
224 reporter->finish_test(reporter, "filename", line, NULL);
225 reporter->finish_suite(reporter, "filename", line); /* nested context -> "S" */
226
227 assert_that(output, contains_string("S"));
228
229 reporter->finish_suite(reporter, "filename", line); /* top-level -> summary */
230 }
231
232 /* Issue #335: a context with both a failure and a skip shows the more severe
233 "F" in the stream, but the skip is still tallied in the summary. */
234 Ensure(TextReporter, quiet_mode_shows_F_but_still_tallies_a_skip_in_a_mixed_context) {
235 TextReporterOptions quiet = {0};
236 quiet.quiet_mode = true;
237 set_reporter_options(reporter, &quiet);
238
239 reporter->start_suite(reporter, "run", 1);
240 reporter->start_suite(reporter, "a_context", 2);
241
242 reporter->start_test(reporter, "a_skip");
243 send_reporter_skipped_notification(reporter);
244 reporter->finish_test(reporter, "filename", line, NULL);
245
246 reporter->start_test(reporter, "a_fail");
247 (*reporter->assert_true)(reporter, "file", 2, false, "boom");
248 send_reporter_completion_notification(reporter);
249 reporter->finish_test(reporter, "filename", line, NULL);
250
251 reporter->finish_suite(reporter, "filename", line); /* context -> "F", not "S" */
252 reporter->finish_suite(reporter, "filename", line); /* top-level -> summary */
253
254 assert_that(strstr(output, "S"), is_null); /* the stream char is F, not S */
255 assert_that(output, contains_string("skipped")); /* but the skip is still tallied */
256 }
257
258 TestSuite *text_reporter_tests(void) {
259 TestSuite *suite = create_test_suite();
260 set_setup(suite, text_reporter_tests_setup);
261
262 add_test_with_context(suite, TextReporter, will_report_beginning_and_end_of_suites);
263 add_test_with_context(suite, TextReporter, will_report_passed_for_test_with_one_pass_on_completion);
264 add_test_with_context(suite, TextReporter, will_report_failed_once_for_each_fail);
265 add_test_with_context(suite, TextReporter, will_report_non_finishing_test);
266 add_test_with_context(suite, TextReporter, quiet_mode_reports_skips_in_the_summary);
267 add_test_with_context(suite, TextReporter, quiet_mode_marks_a_suspended_context_with_S);
268 add_test_with_context(suite, TextReporter, quiet_mode_shows_F_but_still_tallies_a_skip_in_a_mixed_context);
269
270 set_teardown(suite, text_reporter_tests_teardown);
271 return suite;
272 }