[01579ad] | 1 | /*
|
---|
| 2 | * Copyright (c) 2013 Vojtech Horky
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @file
|
---|
| 30 | *
|
---|
| 31 | * Reporting routines for XML output (non-standard).
|
---|
| 32 | */
|
---|
| 33 |
|
---|
| 34 | #include "../internal.h"
|
---|
| 35 | #include "report.h"
|
---|
| 36 | #ifndef __helenos__
|
---|
| 37 | #include <string.h>
|
---|
| 38 | #endif
|
---|
| 39 | #include <stdio.h>
|
---|
| 40 |
|
---|
| 41 | /** Counter of all run tests. */
|
---|
| 42 | static int test_counter;
|
---|
| 43 |
|
---|
| 44 | /** Counter for tests in a current suite. */
|
---|
| 45 | static int tests_in_suite;
|
---|
| 46 |
|
---|
| 47 | /** Counter of failed tests in current suite. */
|
---|
| 48 | static int failed_tests_in_suite;
|
---|
| 49 |
|
---|
[134ac5d] | 50 | /** Initialize the XML output.
|
---|
| 51 | *
|
---|
| 52 | * @param all_items Start of the list with all items.
|
---|
| 53 | */
|
---|
[01579ad] | 54 | static void xml_init(pcut_item_t *all_items) {
|
---|
| 55 | printf("<?xml version=\"1.0\"?>\n");
|
---|
| 56 |
|
---|
| 57 | int tests_total = pcut_count_tests(all_items);
|
---|
| 58 | test_counter = 0;
|
---|
| 59 |
|
---|
| 60 | printf("<report tests-total=\"%d\">\n", tests_total);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[134ac5d] | 63 | /** Report that a suite was started.
|
---|
| 64 | *
|
---|
| 65 | * @param suite Suite that just started.
|
---|
| 66 | */
|
---|
[01579ad] | 67 | static void xml_suite_start(pcut_item_t *suite) {
|
---|
| 68 | tests_in_suite = 0;
|
---|
| 69 | failed_tests_in_suite = 0;
|
---|
| 70 |
|
---|
| 71 | printf("\t<suite name=\"%s\">\n", suite->suite.name);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[134ac5d] | 74 | /** Report that a suite was completed.
|
---|
| 75 | *
|
---|
| 76 | * @param suite Suite that just ended.
|
---|
| 77 | */
|
---|
[01579ad] | 78 | static void xml_suite_done(pcut_item_t *suite) {
|
---|
| 79 | printf("\t</suite><!-- %s: %d / %d -->\n", suite->suite.name,
|
---|
| 80 | failed_tests_in_suite, tests_in_suite);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /** Report that a test was started.
|
---|
| 84 | *
|
---|
| 85 | * We do nothing - all handling is done after the test completes.
|
---|
| 86 | *
|
---|
| 87 | * @param test Test that is started.
|
---|
| 88 | */
|
---|
| 89 | static void xml_test_start(pcut_item_t *test) {
|
---|
| 90 | PCUT_UNUSED(test);
|
---|
| 91 |
|
---|
| 92 | tests_in_suite++;
|
---|
| 93 | test_counter++;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | /** Print the buffer as a CDATA into given element.
|
---|
| 97 | *
|
---|
| 98 | * @param message Message to print.
|
---|
| 99 | * @param element_name Wrapping XML element name.
|
---|
| 100 | */
|
---|
| 101 | static void print_by_lines(const char *message, const char *element_name) {
|
---|
| 102 | if ((message == NULL) || (message[0] == 0)) {
|
---|
| 103 | return;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | printf("\t\t\t<%s><![CDATA[", element_name);
|
---|
| 107 |
|
---|
| 108 | char *next_line_start = pcut_str_find_char(message, '\n');
|
---|
| 109 | while (next_line_start != NULL) {
|
---|
| 110 | next_line_start[0] = 0;
|
---|
| 111 | printf("%s\n", message);
|
---|
| 112 | message = next_line_start + 1;
|
---|
| 113 | next_line_start = pcut_str_find_char(message, '\n');
|
---|
| 114 | }
|
---|
| 115 | if (message[0] != 0) {
|
---|
| 116 | printf("%s\n", message);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | printf("]]></%s>\n", element_name);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[134ac5d] | 122 | /** Report a completed test.
|
---|
| 123 | *
|
---|
| 124 | * @param test Test that just finished.
|
---|
| 125 | * @param outcome Outcome of the test.
|
---|
| 126 | * @param error_message Buffer with error message.
|
---|
| 127 | * @param teardown_error_message Buffer with error message from a tear-down function.
|
---|
| 128 | * @param extra_output Extra output from the test (stdout).
|
---|
| 129 | */
|
---|
[01579ad] | 130 | static void xml_test_done(pcut_item_t *test, int outcome,
|
---|
| 131 | const char *error_message, const char *teardown_error_message,
|
---|
| 132 | const char *extra_output) {
|
---|
| 133 | const char *test_name = test->test.name;
|
---|
| 134 |
|
---|
| 135 | if (outcome != TEST_OUTCOME_PASS) {
|
---|
| 136 | failed_tests_in_suite++;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | const char *status_str = NULL;
|
---|
| 140 | switch (outcome) {
|
---|
| 141 | case TEST_OUTCOME_PASS:
|
---|
| 142 | status_str = "pass";
|
---|
| 143 | break;
|
---|
| 144 | case TEST_OUTCOME_FAIL:
|
---|
| 145 | status_str = "fail";
|
---|
| 146 | break;
|
---|
| 147 | case TEST_OUTCOME_ERROR:
|
---|
| 148 | status_str = "error";
|
---|
| 149 | break;
|
---|
| 150 | default:
|
---|
| 151 | /* Shall not get here. */
|
---|
| 152 | break;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | printf("\t\t<testcase name=\"%s\" status=\"%s\">\n", test_name,
|
---|
| 156 | status_str);
|
---|
| 157 |
|
---|
| 158 | print_by_lines(error_message, "error-message");
|
---|
| 159 | print_by_lines(teardown_error_message, "error-message");
|
---|
| 160 |
|
---|
| 161 | print_by_lines(extra_output, "standard-output");
|
---|
| 162 |
|
---|
| 163 | printf("\t\t</testcase><!-- %s -->\n", test_name);
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | /** Report testing done. */
|
---|
| 167 | static void xml_done() {
|
---|
| 168 | printf("</report>\n");
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 |
|
---|
| 172 | pcut_report_ops_t pcut_report_xml = {
|
---|
| 173 | .init = xml_init,
|
---|
| 174 | .done = xml_done,
|
---|
| 175 | .suite_start = xml_suite_start,
|
---|
| 176 | .suite_done = xml_suite_done,
|
---|
| 177 | .test_start = xml_test_start,
|
---|
| 178 | .test_done = xml_test_done
|
---|
| 179 | };
|
---|