[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 | int tests_total = pcut_count_tests(all_items);
|
---|
| 56 | test_counter = 0;
|
---|
| 57 |
|
---|
[9b20126] | 58 | printf("<?xml version=\"1.0\"?>\n");
|
---|
[01579ad] | 59 | printf("<report tests-total=\"%d\">\n", tests_total);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[134ac5d] | 62 | /** Report that a suite was started.
|
---|
| 63 | *
|
---|
| 64 | * @param suite Suite that just started.
|
---|
| 65 | */
|
---|
[01579ad] | 66 | static void xml_suite_start(pcut_item_t *suite) {
|
---|
| 67 | tests_in_suite = 0;
|
---|
| 68 | failed_tests_in_suite = 0;
|
---|
| 69 |
|
---|
[9b20126] | 70 | printf("\t<suite name=\"%s\">\n", suite->name);
|
---|
[01579ad] | 71 | }
|
---|
| 72 |
|
---|
[134ac5d] | 73 | /** Report that a suite was completed.
|
---|
| 74 | *
|
---|
| 75 | * @param suite Suite that just ended.
|
---|
| 76 | */
|
---|
[01579ad] | 77 | static void xml_suite_done(pcut_item_t *suite) {
|
---|
[9b20126] | 78 | printf("\t</suite><!-- %s: %d / %d -->\n", suite->name,
|
---|
[01579ad] | 79 | failed_tests_in_suite, tests_in_suite);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | /** Report that a test was started.
|
---|
| 83 | *
|
---|
| 84 | * We do nothing - all handling is done after the test completes.
|
---|
| 85 | *
|
---|
| 86 | * @param test Test that is started.
|
---|
| 87 | */
|
---|
| 88 | static void xml_test_start(pcut_item_t *test) {
|
---|
| 89 | PCUT_UNUSED(test);
|
---|
| 90 |
|
---|
| 91 | tests_in_suite++;
|
---|
| 92 | test_counter++;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | /** Print the buffer as a CDATA into given element.
|
---|
| 96 | *
|
---|
| 97 | * @param message Message to print.
|
---|
| 98 | * @param element_name Wrapping XML element name.
|
---|
| 99 | */
|
---|
| 100 | static void print_by_lines(const char *message, const char *element_name) {
|
---|
[9b20126] | 101 | char *next_line_start;
|
---|
| 102 |
|
---|
[01579ad] | 103 | if ((message == NULL) || (message[0] == 0)) {
|
---|
| 104 | return;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | printf("\t\t\t<%s><![CDATA[", element_name);
|
---|
| 108 |
|
---|
[9b20126] | 109 | next_line_start = pcut_str_find_char(message, '\n');
|
---|
[01579ad] | 110 | while (next_line_start != NULL) {
|
---|
| 111 | next_line_start[0] = 0;
|
---|
| 112 | printf("%s\n", message);
|
---|
| 113 | message = next_line_start + 1;
|
---|
| 114 | next_line_start = pcut_str_find_char(message, '\n');
|
---|
| 115 | }
|
---|
| 116 | if (message[0] != 0) {
|
---|
| 117 | printf("%s\n", message);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | printf("]]></%s>\n", element_name);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[134ac5d] | 123 | /** Report a completed test.
|
---|
| 124 | *
|
---|
| 125 | * @param test Test that just finished.
|
---|
| 126 | * @param outcome Outcome of the test.
|
---|
| 127 | * @param error_message Buffer with error message.
|
---|
| 128 | * @param teardown_error_message Buffer with error message from a tear-down function.
|
---|
| 129 | * @param extra_output Extra output from the test (stdout).
|
---|
| 130 | */
|
---|
[01579ad] | 131 | static void xml_test_done(pcut_item_t *test, int outcome,
|
---|
| 132 | const char *error_message, const char *teardown_error_message,
|
---|
| 133 | const char *extra_output) {
|
---|
[9b20126] | 134 | const char *test_name = test->name;
|
---|
| 135 | const char *status_str = NULL;
|
---|
[01579ad] | 136 |
|
---|
| 137 | if (outcome != TEST_OUTCOME_PASS) {
|
---|
| 138 | failed_tests_in_suite++;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | switch (outcome) {
|
---|
| 142 | case TEST_OUTCOME_PASS:
|
---|
| 143 | status_str = "pass";
|
---|
| 144 | break;
|
---|
| 145 | case TEST_OUTCOME_FAIL:
|
---|
| 146 | status_str = "fail";
|
---|
| 147 | break;
|
---|
| 148 | case TEST_OUTCOME_ERROR:
|
---|
| 149 | status_str = "error";
|
---|
| 150 | break;
|
---|
| 151 | default:
|
---|
| 152 | /* Shall not get here. */
|
---|
| 153 | break;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | printf("\t\t<testcase name=\"%s\" status=\"%s\">\n", test_name,
|
---|
| 157 | status_str);
|
---|
| 158 |
|
---|
| 159 | print_by_lines(error_message, "error-message");
|
---|
| 160 | print_by_lines(teardown_error_message, "error-message");
|
---|
| 161 |
|
---|
| 162 | print_by_lines(extra_output, "standard-output");
|
---|
| 163 |
|
---|
| 164 | printf("\t\t</testcase><!-- %s -->\n", test_name);
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | /** Report testing done. */
|
---|
[9b20126] | 168 | static void xml_done(void) {
|
---|
[01579ad] | 169 | printf("</report>\n");
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 |
|
---|
| 173 | pcut_report_ops_t pcut_report_xml = {
|
---|
[9b20126] | 174 | xml_init, xml_done,
|
---|
| 175 | xml_suite_start, xml_suite_done,
|
---|
| 176 | xml_test_start, xml_test_done
|
---|
[01579ad] | 177 | };
|
---|