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