| 1 | #include <cgreen/cgreen.h> |
| 2 | #include <cgreen/breadcrumb.h> |
| 3 | #include <cgreen/messaging.h> |
| 4 | #include "cgreen_value_internal.h" |
| 5 | #include "constraint_internal.h" |
| 6 | |
| 7 | #include <stdio.h> |
| 8 | #include <string.h> |
| 9 | #include <unistd.h> |
| 10 | #include <stdint.h> |
| 11 | |
| 12 | #include <libxml/tree.h> |
| 13 | |
| 14 | #ifdef __cplusplus |
| 15 | using namespace cgreen; |
| 16 | #endif |
| 17 | |
| 18 | |
| 19 | #include "libxml_reporter_internal.h" |
| 20 | |
| 21 | #define XMLSTRING(x) (BAD_CAST x) |
| 22 | |
| 23 | static const int line=666; |
| 24 | static xmlChar *output = NULL; |
| 25 | |
| 26 | static void clear_output(void) |
| 27 | { |
| 28 | if (NULL != output) { |
| 29 | xmlFree(output); |
| 30 | } |
| 31 | output = NULL; |
| 32 | } |
| 33 | |
| 34 | static int mocked_xmlout(xmlDocPtr doc) { |
| 35 | if (output) { |
| 36 | xmlFree(output); |
| 37 | } |
| 38 | |
| 39 | xmlDocDumpMemoryEnc(doc, &output, NULL, "UTF-8"); |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | static TestReporter *reporter; |
| 44 | |
| 45 | static void setup_xml_reporter_tests(void) { |
| 46 | reporter = create_libxml_reporter("PREFIX"); |
| 47 | |
| 48 | // We can not use setup_reporting() since we are running |
| 49 | // inside a test suite which needs the real reporting |
| 50 | // So we'll have to set up the messaging explicitly |
| 51 | reporter->ipc = start_cgreen_messaging(667); |
| 52 | |
| 53 | clear_output(); |
| 54 | set_libxml_reporter_printer(reporter, mocked_xmlout); |
| 55 | } |
| 56 | |
| 57 | static void teardown_xml_reporter_tests(void) { |
| 58 | //bad mojo when running tests in same process, as destroy_reporter also sets |
| 59 | //context.reporter = NULL, thus breaking the next test to run |
| 60 | destroy_reporter(reporter); |
| 61 | if (NULL != output) { |
| 62 | free(output); |
| 63 | //need to set output to NULL to avoid second free in |
| 64 | //subsequent call to setup_xml_reporter_tests->clear_output |
| 65 | //when running tests in same process |
| 66 | output = NULL; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | static xmlChar* getAttribute(xmlNodePtr node, const xmlChar* name) { |
| 71 | xmlAttr *attr = node->properties; |
| 72 | while (attr) { |
| 73 | if (xmlStrEqual(attr->name, name)) { |
| 74 | return xmlNodeGetContent((xmlNode*)attr); |
| 75 | } |
| 76 | attr = attr->next; |
| 77 | } |
| 78 | |
| 79 | return NULL; |
| 80 | } |
| 81 | |
| 82 | static bool hasAttribute(xmlNodePtr node, const xmlChar* name) { |
| 83 | xmlAttr *attr = node->properties; |
| 84 | while (attr) { |
| 85 | if (xmlStrEqual(attr->name, name)) { |
| 86 | return true; |
| 87 | } |
| 88 | attr = attr->next; |
| 89 | } |
| 90 | |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | struct xmlNode_has_attribute_equal_to { |
| 95 | xmlChar *attr, *value; |
| 96 | }; |
| 97 | |
| 98 | static bool compare_xmlNode_has_attribute_equal_to(Constraint *constraint, |
| 99 | CgreenValue actual) { |
| 100 | xmlNodePtr actualNode = (xmlNodePtr)actual.value.pointer_value; |
| 101 | struct xmlNode_has_attribute_equal_to *expected = |
| 102 | (struct xmlNode_has_attribute_equal_to*)constraint->expected_value.value.pointer_value; |
| 103 | xmlChar* actualValue = getAttribute(actualNode, expected->attr); |
| 104 | |
| 105 | bool ret = xmlStrEqual(actualValue, expected->value); |
| 106 | xmlFree(actualValue); |
| 107 | return ret; |
| 108 | } |
| 109 | |
| 110 | static char *failure_message_xmlNode_has_attribute_equal_to( |
| 111 | Constraint *constraint, const char *actual_string, intptr_t actual_value) { |
| 112 | struct xmlNode_has_attribute_equal_to *expected = |
| 113 | (struct xmlNode_has_attribute_equal_to*)constraint->expected_value.value.pointer_value; |
| 114 | xmlChar* actualValue = getAttribute((xmlNodePtr)actual_value, expected->attr); |
| 115 | |
| 116 | const char *message_template = "Expected attribute [%s] of [%s] to [equal] [%s]\n" |
| 117 | "\tactual value:\t\t[%s]\n" |
| 118 | "\texpected to equal:\t[%s]\n"; |
| 119 | size_t msglen = xmlStrlen(expected->attr) + strlen(actual_string) + |
| 120 | strlen(constraint->expected_value_name) + xmlStrlen(actualValue) + |
| 121 | xmlStrlen(expected->value) + strlen(message_template); |
| 122 | char *message = (char*)malloc(msglen); |
| 123 | if (!message) { |
| 124 | xmlFree(actualValue); |
| 125 | return NULL; |
| 126 | } |
| 127 | |
| 128 | if (snprintf(message, msglen, message_template, |
| 129 | expected->attr, actual_string, constraint->expected_value_name, |
| 130 | actualValue, expected->value) >= (ssize_t)msglen) { |
| 131 | xmlFree(actualValue); |
| 132 | free(message); |
| 133 | return NULL; |
| 134 | } |
| 135 | |
| 136 | xmlFree(actualValue); |
| 137 | return message; |
| 138 | } |
| 139 | |
| 140 | static void destroy_xmlNode_has_attribute_equal_to(Constraint* constraint) { |
| 141 | struct xmlNode_has_attribute_equal_to *expected = |
| 142 | (struct xmlNode_has_attribute_equal_to*)constraint->expected_value.value.pointer_value; |
| 143 | xmlFree(expected->attr); |
| 144 | xmlFree(expected->value); |
| 145 | free(expected); |
| 146 | destroy_empty_constraint(constraint); |
| 147 | } |
| 148 | |
| 149 | static Constraint *create_xmlNode_has_attribute_equal_to(const xmlChar* attr, |
| 150 | const xmlChar *value, |
| 151 | const char* expected_value_name) |
| 152 | { |
| 153 | Constraint *constraint = create_constraint(); |
| 154 | if (!constraint) |
| 155 | return NULL; |
| 156 | |
| 157 | struct xmlNode_has_attribute_equal_to *expected = |
| 158 | (struct xmlNode_has_attribute_equal_to *)malloc(sizeof(struct xmlNode_has_attribute_equal_to)); |
| 159 | if (!expected) { |
| 160 | constraint->destroy(constraint); |
| 161 | return NULL; |
| 162 | } |
| 163 | |
| 164 | expected->attr = xmlStrdup(attr); |
| 165 | expected->value = xmlStrdup(value); |
| 166 | |
| 167 | constraint->expected_value = make_cgreen_pointer_value(expected); |
| 168 | constraint->expected_value_name = strdup(expected_value_name); |
| 169 | constraint->type = CGREEN_STRING_COMPARER_CONSTRAINT; |
| 170 | |
| 171 | constraint->compare = &compare_xmlNode_has_attribute_equal_to; |
| 172 | constraint->failure_message = &failure_message_xmlNode_has_attribute_equal_to; |
| 173 | constraint->name = "have attribute with value"; |
| 174 | constraint->size_of_expected_value = sizeof(intptr_t); |
| 175 | constraint->destroy = &destroy_xmlNode_has_attribute_equal_to; |
| 176 | |
| 177 | return constraint; |
| 178 | } |
| 179 | |
| 180 | #define xmlnode_has_attribute_equal_to(attr, value) \ |
| 181 | create_xmlNode_has_attribute_equal_to(XMLSTRING(attr), XMLSTRING(value), #value) |
| 182 | |
| 183 | static xmlNodePtr xmlnode_find_sibling(xmlNodePtr node, const char *name) |
| 184 | { |
| 185 | while (node) { |
| 186 | if (xmlStrEqual(node->name, XMLSTRING(name))) |
| 187 | break; |
| 188 | node = xmlNextElementSibling(node); |
| 189 | } |
| 190 | return node; |
| 191 | } |
| 192 | |
| 193 | Describe(LibXmlReporter); |
| 194 | BeforeEach(LibXmlReporter) { |
| 195 | setup_xml_reporter_tests(); |
| 196 | } |
| 197 | AfterEach(LibXmlReporter) { |
| 198 | teardown_xml_reporter_tests(); |
| 199 | } |
| 200 | |
| 201 | |
| 202 | Ensure(LibXmlReporter, will_report_beginning_of_suite) { |
| 203 | reporter->start_suite(reporter, "suite_name", 2); |
| 204 | reporter->finish_suite(reporter, "filename", line); |
| 205 | |
| 206 | xmlDocPtr doc = xmlParseDoc(output); |
| 207 | assert_that(doc, is_not_null); |
| 208 | |
| 209 | xmlNodePtr testsuite = xmlDocGetRootElement(doc); |
| 210 | assert_that(testsuite->name, is_equal_to_string("testsuite")); |
| 211 | assert_that(xmlChildElementCount(testsuite), is_equal_to(0)); |
| 212 | assert_that(testsuite, xmlnode_has_attribute_equal_to("name", "suite_name")); |
| 213 | xmlFreeDoc(doc); |
| 214 | } |
| 215 | |
| 216 | |
| 217 | static void reporter_show_pass_vargs(TestReporter *reporter, const char *name, int line, |
| 218 | const char *format, ...) |
| 219 | { |
| 220 | va_list vargs; |
| 221 | va_start(vargs, format); |
| 222 | reporter->show_pass(reporter, name, line, format, vargs); |
| 223 | va_end(vargs); |
| 224 | } |
| 225 | |
| 226 | Ensure(LibXmlReporter, will_report_beginning_and_successful_finishing_of_passing_test) { |
| 227 | reporter->start_suite(reporter, "suite_name", 2); |
| 228 | reporter->start_test(reporter, "test_name"); |
| 229 | reporter_show_pass_vargs(reporter, "file", 2, "test_name"); |
| 230 | send_reporter_completion_notification(reporter); |
| 231 | reporter->finish_test(reporter, "filename", line, NULL); |
| 232 | reporter->finish_suite(reporter, "filename", line); |
| 233 | |
| 234 | xmlDocPtr doc = xmlParseDoc(output); |
| 235 | assert_that(doc, is_not_null); |
| 236 | |
| 237 | xmlNodePtr testsuite = xmlDocGetRootElement(doc); |
| 238 | assert_that(xmlChildElementCount(testsuite), is_equal_to(1)); |
| 239 | |
| 240 | xmlNodePtr testcase = xmlFirstElementChild(testsuite); |
| 241 | assert_that(testcase->name, is_equal_to_string("testcase")); |
| 242 | assert_that(xmlChildElementCount(testcase), is_equal_to(0)); |
| 243 | assert_that(testcase, xmlnode_has_attribute_equal_to("name", "test_name")); |
| 244 | assert_that(testcase, xmlnode_has_attribute_equal_to("classname", "suite_name")); |
| 245 | } |
| 246 | |
| 247 | |
| 248 | static void reporter_show_fail_vargs(TestReporter *reporter, const char *name, int line, |
| 249 | const char *format, ...) |
| 250 | { |
| 251 | va_list vargs; |
| 252 | va_start(vargs, format); |
| 253 | reporter->show_fail(reporter, name, line, format, vargs); |
| 254 | va_end(vargs); |
| 255 | } |
| 256 | |
| 257 | Ensure(LibXmlReporter, will_report_a_failing_test) { |
| 258 | reporter->start_suite(reporter, "suite_name", 2); |
| 259 | reporter->start_test(reporter, "test_name"); |
| 260 | reporter_show_fail_vargs(reporter, "file", 2, "test_name"); |
| 261 | send_reporter_completion_notification(reporter); |
| 262 | reporter->finish_test(reporter, "filename", line, NULL); |
| 263 | reporter->finish_suite(reporter, "filename", line); |
| 264 | |
| 265 | xmlDocPtr doc = xmlParseDoc(output); |
| 266 | assert_that(doc, is_not_null); |
| 267 | |
| 268 | xmlNodePtr testsuite = xmlDocGetRootElement(doc); |
| 269 | assert_that(xmlChildElementCount(testsuite), is_equal_to(1)); |
| 270 | |
| 271 | xmlNodePtr testcase = xmlFirstElementChild(testsuite); |
| 272 | assert_that(testcase->name, is_equal_to_string("testcase")); |
| 273 | assert_that(xmlChildElementCount(testcase), is_equal_to(1)); |
| 274 | assert_that(hasAttribute(testcase, XMLSTRING("time")), is_true); |
| 275 | |
| 276 | xmlNodePtr failure = xmlFirstElementChild(testcase); |
| 277 | assert_that(failure->name, is_equal_to_string("failure")); |
| 278 | assert_that(xmlChildElementCount(failure), is_equal_to(1)); |
| 279 | assert_that(failure, xmlnode_has_attribute_equal_to("message", "test_name")); |
| 280 | |
| 281 | xmlNodePtr location = xmlFirstElementChild(failure); |
| 282 | assert_that(location->name, is_equal_to_string("location")); |
| 283 | assert_that(xmlChildElementCount(location), is_equal_to(0)); |
| 284 | assert_that(location, xmlnode_has_attribute_equal_to("file", "file")); |
| 285 | assert_that(location, xmlnode_has_attribute_equal_to("line", "2")); |
| 286 | } |
| 287 | |
| 288 | Ensure(LibXmlReporter, will_report_a_failing_test_only_once) { |
| 289 | va_list null_arguments; |
| 290 | memset(&null_arguments, 0, sizeof(null_arguments)); |
| 291 | |
| 292 | reporter->start_suite(reporter, "suite_name", 2); |
| 293 | reporter->start_test(reporter, "test_name"); |
| 294 | reporter->show_fail(reporter, "file", 2, "test_failure_message", null_arguments); |
| 295 | reporter->show_fail(reporter, "file", 2, "other_message", null_arguments); |
| 296 | reporter->finish_test(reporter, "filename", line, NULL); |
| 297 | reporter->finish_suite(reporter, "filename", line); |
| 298 | |
| 299 | xmlDocPtr doc = xmlParseDoc(output); |
| 300 | xmlNodePtr testsuite = xmlDocGetRootElement(doc); |
| 301 | xmlNodePtr testcase = xmlFirstElementChild(testsuite); |
| 302 | |
| 303 | static const char FAILURE[] = "failure"; |
| 304 | xmlNodePtr failure = xmlnode_find_sibling(xmlFirstElementChild(testcase), FAILURE); |
| 305 | |
| 306 | assert_that(failure->name, is_equal_to_string(FAILURE)); |
| 307 | assert_that(failure, xmlnode_has_attribute_equal_to("message", "test_failure_message")); |
| 308 | |
| 309 | failure = xmlnode_find_sibling(xmlNextElementSibling(failure), FAILURE); |
| 310 | assert_that(failure, xmlnode_has_attribute_equal_to("message", "other_message")); |
| 311 | |
| 312 | failure = xmlnode_find_sibling(xmlNextElementSibling(failure), FAILURE); |
| 313 | assert_that(failure, is_null); |
| 314 | } |
| 315 | |
| 316 | Ensure(LibXmlReporter, will_mark_ignored_test_as_skipped) { |
| 317 | const int line = 666; |
| 318 | |
| 319 | reporter->start_suite(reporter, "suite_name", 1); |
| 320 | reporter->start_test(reporter, "skipped_test_name"); |
| 321 | send_reporter_skipped_notification(reporter); |
| 322 | reporter->finish_test(reporter, "filename", line, "message"); |
| 323 | reporter->finish_suite(reporter, "filename", line); |
| 324 | |
| 325 | xmlDocPtr doc = xmlParseDoc(output); |
| 326 | assert_that(doc, is_not_null); |
| 327 | |
| 328 | xmlNodePtr testsuite = xmlDocGetRootElement(doc); |
| 329 | assert_that(xmlChildElementCount(testsuite), is_equal_to(1)); |
| 330 | |
| 331 | xmlNodePtr testcase = xmlFirstElementChild(testsuite); |
| 332 | assert_that(testcase->name, is_equal_to_string("testcase")); |
| 333 | assert_that(xmlChildElementCount(testcase), is_equal_to(1)); |
| 334 | assert_that(hasAttribute(testcase, XMLSTRING("time")), is_true); |
| 335 | |
| 336 | xmlNodePtr skipped = xmlFirstElementChild(testcase); |
| 337 | assert_that(skipped->name, is_equal_to_string("skipped")); |
| 338 | assert_that(xmlChildElementCount(skipped), is_equal_to(0)); |
| 339 | } |
| 340 | |
| 341 | |
| 342 | Ensure(LibXmlReporter, will_report_non_finishing_test) { |
| 343 | const int line = 666; |
| 344 | |
| 345 | reporter->start_suite(reporter, "suite_name", 1); |
| 346 | reporter->start_test(reporter, "test_name"); |
| 347 | send_reporter_exception_notification(reporter); |
| 348 | reporter->finish_test(reporter, "filename", line, "message"); |
| 349 | reporter->finish_suite(reporter, "filename", line); |
| 350 | |
| 351 | xmlDocPtr doc = xmlParseDoc(output); |
| 352 | assert_that(doc, is_not_null); |
| 353 | |
| 354 | xmlNodePtr testsuite = xmlDocGetRootElement(doc); |
| 355 | assert_that(xmlChildElementCount(testsuite), is_equal_to(1)); |
| 356 | |
| 357 | xmlNodePtr testcase = xmlFirstElementChild(testsuite); |
| 358 | assert_that(testcase->name, is_equal_to_string("testcase")); |
| 359 | assert_that(xmlChildElementCount(testcase), is_equal_to(1)); |
| 360 | assert_that(hasAttribute(testcase, XMLSTRING("time")), is_true); |
| 361 | |
| 362 | xmlNodePtr error = xmlFirstElementChild(testcase); |
| 363 | assert_that(error->name, is_equal_to_string("error")); |
| 364 | assert_that(error, xmlnode_has_attribute_equal_to("type", "Fatal")); |
| 365 | assert_that(error, xmlnode_has_attribute_equal_to("message", "message")); |
| 366 | } |
| 367 | |
| 368 | Ensure(LibXmlReporter, will_report_time_correctly_for_non_finishing_test) { |
| 369 | const int line = 666; |
| 370 | |
| 371 | reporter->start_suite(reporter, "suite_name", 1); |
| 372 | reporter->start_test(reporter, "test_name"); |
| 373 | send_reporter_exception_notification(reporter); |
| 374 | reporter->finish_test(reporter, "filename", line, "message"); |
| 375 | reporter->finish_suite(reporter, "filename", line); |
| 376 | |
| 377 | assert_that(output, contains_string("name=\"test_name\"")); |
| 378 | assert_that(output, contains_string(" time=\"")); |
| 379 | } |
| 380 | |
| 381 | TestSuite *libxml_reporter_tests(void) { |
| 382 | TestSuite *suite = create_test_suite(); |
| 383 | set_setup(suite, setup_xml_reporter_tests); |
| 384 | |
| 385 | add_test_with_context(suite, LibXmlReporter, will_report_beginning_of_suite); |
| 386 | add_test_with_context(suite, LibXmlReporter, will_report_beginning_and_successful_finishing_of_passing_test); |
| 387 | add_test_with_context(suite, LibXmlReporter, will_report_a_failing_test); |
| 388 | add_test_with_context(suite, LibXmlReporter, will_report_a_failing_test_only_once); |
| 389 | add_test_with_context(suite, LibXmlReporter, will_mark_ignored_test_as_skipped); |
| 390 | add_test_with_context(suite, LibXmlReporter, will_report_non_finishing_test); |
| 391 | add_test_with_context(suite, LibXmlReporter, will_report_time_correctly_for_non_finishing_test); |
| 392 | |
| 393 | set_teardown(suite, teardown_xml_reporter_tests); |
| 394 | return suite; |
| 395 | } |