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