1 #include <cgreen/cgreen.h>
2 #include <cgreen/cute_reporter.h>
3 #include <cgreen/messaging.h>
4
5 #include <stdio.h>
6 #include <string.h>
7
8 static char *output;
clear_output()9 static void clear_output()
10 {
11 output = (char*)malloc(1);
12 *output = '\0';
13 }
14
concat(char * output,char * buffer)15 static char *concat(char *output, char *buffer) {
16 output = realloc(output, strlen(output)+strlen(buffer)+1);
17 strcat(output, buffer);
18 return output;
19 }
20
mocked_printf(const char * format,...)21 static int mocked_printf(const char *format, ...) {
22 char buffer[10000];
23 va_list ap;
24 va_start(ap, format);
25 vsprintf(buffer, format, ap);
26 va_end(ap);
27
28 output = concat(output, buffer);
29 return strlen(output);
30 }
31
strpos(char * whole,char * part)32 int strpos(char *whole, char *part) {
33 int pos;
34 if (strlen(whole) >= strlen(part))
35 for (pos = 0; pos < strlen(whole)-strlen(part); pos++) {
36 if (strncmp(&whole[pos], part, strlen(part)) == 0)
37 return pos;
38 }
39 return -1;
40 }
41
42 TestReporter *reporter;
43
setup_cute_reporter_tests()44 static void setup_cute_reporter_tests() {
45 reporter = create_cute_reporter();
46
47 // We can not use setup_reporting() since we are running
48 // inside an test suite which needs the real reporting
49 // So we'll have to set up the messaging explicitly
50 reporter->ipc = start_cgreen_messaging(666);
51
52 clear_output();
53 set_cute_printer(reporter, mocked_printf);
54 }
55
56
assert_no_output()57 static void assert_no_output() {
58 assert_equal(strlen(output), 0);
59 }
60
assert_output_starts_with(char * string)61 static void assert_output_starts_with(char *string) {
62 assert_equal(strpos(output, string), 0);
63 }
64
assert_output_contains(char * string)65 static void assert_output_contains(char *string) {
66 assert_true(strpos(output, string) > 0);
67 }
68
will_report_beginning_of_suite()69 Ensure will_report_beginning_of_suite() {
70 reporter->start_suite(reporter, "suite_name", 2);
71 assert_output_starts_with("#beginning");
72 assert_output_contains("suite_name");
73 }
74
will_report_beginning_and_successful_finishing_of_test()75 Ensure will_report_beginning_and_successful_finishing_of_test() {
76 reporter->start_test(reporter, "test_name");
77 assert_output_starts_with("#starting");
78 assert_output_contains("test_name");
79
80 clear_output();
81 reporter->show_pass(reporter, "file", 2, "test_name", "");
82 assert_no_output();
83
84 // Must indicate test case completion before calling finish_test()
85 send_reporter_completion_notification(reporter);
86 reporter->finish_test(reporter, "test_name");
87 assert_output_starts_with("#success");
88 assert_output_contains("test_name");
89 }
90
will_report_failing_of_test_only_once()91 Ensure will_report_failing_of_test_only_once() {
92 reporter->start_test(reporter, "test_name");
93
94 clear_output();
95 reporter->failures++; // Simulating a failed assert
96 reporter->show_fail(reporter, "file", 2, "test_name", "");
97 assert_output_starts_with("#failure");
98 assert_output_contains("test_name");
99
100 clear_output();
101 reporter->failures++; // Simulating another failed assert
102 reporter->show_fail(reporter, "file", 2, "test_name", "");
103 assert_no_output();
104
105 // Must indicate test case completion before calling finish_test()
106 send_reporter_completion_notification(reporter);
107 reporter->finish_test(reporter, "test_name");
108 assert_no_output();
109 }
110
will_report_finishing_of_suite()111 Ensure will_report_finishing_of_suite() {
112 // Must indicate test suite completion before calling finish_suite()
113 send_reporter_completion_notification(reporter);
114 reporter->finish_suite(reporter, "suite_name");
115 assert_output_starts_with("#ending");
116 assert_output_contains("suite_name");
117 }
118
cute_reporter_tests()119 TestSuite *cute_reporter_tests() {
120 TestSuite *suite = create_test_suite();
121 setup(suite, setup_cute_reporter_tests);
122
123 add_test(suite, will_report_beginning_of_suite);
124 add_test(suite, will_report_beginning_and_successful_finishing_of_test);
125 add_test(suite, will_report_failing_of_test_only_once);
126 add_test(suite, will_report_finishing_of_suite);
127 return suite;
128 }
129