| 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 | #include <cgreen/cdash_reporter.h> |
| 9 | #include "src/cdash_reporter_internal.h" |
| 10 | |
| 11 | #ifdef __cplusplus |
| 12 | using namespace cgreen; |
| 13 | #endif |
| 14 | |
| 15 | static const int line=666; |
| 16 | static char *output = NULL; |
| 17 | |
| 18 | static void clear_output(void) |
| 19 | { |
| 20 | if (NULL != output) { |
| 21 | free(output); |
| 22 | } |
| 23 | |
| 24 | output = (char*)malloc(1); |
| 25 | *output = '\0'; |
| 26 | } |
| 27 | |
| 28 | static char *concat(char *output, char *buffer) { |
| 29 | output = (char *) realloc(output, strlen(output)+strlen(buffer)+1); |
| 30 | strcat(output, buffer); |
| 31 | return output; |
| 32 | } |
| 33 | |
| 34 | static int mocked_vprinter(FILE *stream, const char *format, va_list ap) { |
| 35 | (void)stream; /* Unused */ |
| 36 | |
| 37 | char buffer[10000]; |
| 38 | vsnprintf(buffer, sizeof(buffer), format, ap); |
| 39 | |
| 40 | output = concat(output, buffer); |
| 41 | return strlen(output); |
| 42 | } |
| 43 | |
| 44 | static int mocked_printer(FILE *stream, const char *format, ...) { |
| 45 | int result = 0; |
| 46 | |
| 47 | va_list ap; |
| 48 | va_start(ap, format); |
| 49 | result = mocked_vprinter(stream, format, ap); |
| 50 | va_end(ap); |
| 51 | |
| 52 | return result; |
| 53 | } |
| 54 | |
| 55 | static TestReporter *reporter; |
| 56 | |
| 57 | static CDashInfo info; |
| 58 | |
| 59 | static void setup_cdash_reporter_tests(void) { |
| 60 | reporter = create_cdash_reporter(&info); |
| 61 | |
| 62 | // We can not use setup_reporting() since we are running |
| 63 | // inside a test suite which needs the real reporting |
| 64 | // So we'll have to set up the messaging explicitly |
| 65 | reporter->ipc = start_cgreen_messaging(666); |
| 66 | |
| 67 | clear_output(); |
| 68 | set_cdash_reporter_printer(reporter, mocked_printer); |
| 69 | set_cdash_reporter_vprinter(reporter, mocked_vprinter); |
| 70 | } |
| 71 | |
| 72 | static void teardown_cdash_reporter_tests(void) { |
| 73 | reporter->destroy(reporter); |
| 74 | |
| 75 | //bad mojo when running tests in same process, as destroy_reporter also sets |
| 76 | //context.reporter = NULL, thus breaking the next test to run |
| 77 | if (NULL != output) { |
| 78 | free(output); |
| 79 | //need to set output to NULL to avoid second free in subsequent call to setup_cdash_reporter_tests->clear_output |
| 80 | //when running tests in same process |
| 81 | output = NULL; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | |
| 86 | Describe(CDashReporter); |
| 87 | BeforeEach(CDashReporter) { |
| 88 | setup_cdash_reporter_tests(); |
| 89 | } |
| 90 | AfterEach(CDashReporter) { |
| 91 | teardown_cdash_reporter_tests(); |
| 92 | } |
| 93 | |
| 94 | Ensure(CDashReporter, will_report_nothing_for_suites) { |
| 95 | reporter->start_suite(reporter, "suite_name", 2); |
| 96 | reporter->finish_suite(reporter, "filename", line); |
| 97 | } |
| 98 | |
| 99 | Ensure(CDashReporter, will_report_passed_for_test_with_one_pass) { |
| 100 | va_list arguments; |
| 101 | |
| 102 | reporter->start_test(reporter, "test_name"); |
| 103 | |
| 104 | memset(&arguments, 0, sizeof(va_list)); |
| 105 | reporter->show_pass(reporter, "file", 2, "test_name", arguments); |
| 106 | |
| 107 | // Must indicate test case completion before calling finish_test() |
| 108 | send_reporter_completion_notification(reporter); |
| 109 | reporter->finish_test(reporter, "filename", line, NULL); |
| 110 | |
| 111 | assert_that(output, contains_string("<Test Status=\"passed\">")); |
| 112 | } |
| 113 | |
| 114 | Ensure(CDashReporter, will_report_failed_once_for_each_fail) { |
| 115 | va_list arguments; |
| 116 | |
| 117 | reporter->start_test(reporter, "test_name"); |
| 118 | |
| 119 | reporter->failures++; // Simulating a failed assert |
| 120 | memset(&arguments, 0, sizeof(va_list)); |
| 121 | reporter->show_fail(reporter, "file", 2, "test_name", arguments); |
| 122 | |
| 123 | assert_that(output, contains_string("<Test Status=\"failed\">")); |
| 124 | assert_that(output, contains_string("<Name>test_name</Name>")); |
| 125 | assert_that(output, contains_string("<FullName>file</FullName>")); |
| 126 | assert_that(output, contains_string("<FullCommandLine>at [file] line [2]</FullCommandLine>")); |
| 127 | |
| 128 | clear_output(); |
| 129 | |
| 130 | reporter->failures++; // Simulating another failed assert |
| 131 | reporter->show_fail(reporter, "file", 3, "test_name", arguments); |
| 132 | assert_that(output, contains_string("<Test Status=\"failed\">")); |
| 133 | assert_that(output, contains_string("<Name>test_name</Name>")); |
| 134 | assert_that(output, contains_string("<FullCommandLine>at [file] line [3]</FullCommandLine>")); |
| 135 | |
| 136 | clear_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_that(output, is_equal_to_string("")); |
| 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(CDashReporter, will_use_arguments_for_show_fail) { |
| 153 | reporter->start_test(reporter, "test_name"); |
| 154 | |
| 155 | reporter->failures++; // Simulating a failed assert |
| 156 | wrapped_show_fail(reporter, "file", 2, "test_case %i is %s", 5, "alive"); |
| 157 | |
| 158 | assert_that(output, contains_string("<Value>test_case 5 is alive</Value>")); |
| 159 | |
| 160 | clear_output(); |
| 161 | |
| 162 | // Must indicate test case completion before calling finish_test() |
| 163 | send_reporter_completion_notification(reporter); |
| 164 | reporter->finish_test(reporter, "filename", line, NULL); |
| 165 | assert_that(output, is_equal_to_string("")); |
| 166 | } |
| 167 | |
| 168 | Ensure(CDashReporter, will_report_non_finishing_test) { |
| 169 | const int line = 666; |
| 170 | reporter->start_suite(reporter, "suite_name", 1); |
| 171 | |
| 172 | send_reporter_exception_notification(reporter); |
| 173 | reporter->finish_suite(reporter, "filename", line); |
| 174 | |
| 175 | assert_that(output, contains_string("<Test Status=\"incomplete\">")); |
| 176 | } |
| 177 | |
| 178 | TestSuite *cdash_reporter_tests(void) { |
| 179 | TestSuite *suite = create_test_suite(); |
| 180 | set_setup(suite, setup_cdash_reporter_tests); |
| 181 | |
| 182 | add_test_with_context(suite, CDashReporter, will_report_nothing_for_suites); |
| 183 | add_test_with_context(suite, CDashReporter, will_report_passed_for_test_with_one_pass); |
| 184 | add_test_with_context(suite, CDashReporter, will_report_failed_once_for_each_fail); |
| 185 | add_test_with_context(suite, CDashReporter, will_report_non_finishing_test); |
| 186 | |
| 187 | set_teardown(suite, teardown_cdash_reporter_tests); |
| 188 | return suite; |
| 189 | } |