Update PCUT to newest version
[helenos.git] / uspace / lib / pcut / src / report / xml.c
blob5b6223a5029f3462f21f3776667fad1a83aa98a2
1 /*
2 * Copyright (c) 2013 Vojtech Horky
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 /** @file
31 * Reporting routines for XML output (non-standard).
34 #include "../internal.h"
35 #include "report.h"
36 #ifndef __helenos__
37 #include <string.h>
38 #endif
39 #include <stdio.h>
41 /** Counter of all run tests. */
42 static int test_counter;
44 /** Counter for tests in a current suite. */
45 static int tests_in_suite;
47 /** Counter of failed tests in current suite. */
48 static int failed_tests_in_suite;
50 /** Initialize the XML output.
52 * @param all_items Start of the list with all items.
54 static void xml_init(pcut_item_t *all_items) {
55 printf("<?xml version=\"1.0\"?>\n");
57 int tests_total = pcut_count_tests(all_items);
58 test_counter = 0;
60 printf("<report tests-total=\"%d\">\n", tests_total);
63 /** Report that a suite was started.
65 * @param suite Suite that just started.
67 static void xml_suite_start(pcut_item_t *suite) {
68 tests_in_suite = 0;
69 failed_tests_in_suite = 0;
71 printf("\t<suite name=\"%s\">\n", suite->suite.name);
74 /** Report that a suite was completed.
76 * @param suite Suite that just ended.
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);
83 /** Report that a test was started.
85 * We do nothing - all handling is done after the test completes.
87 * @param test Test that is started.
89 static void xml_test_start(pcut_item_t *test) {
90 PCUT_UNUSED(test);
92 tests_in_suite++;
93 test_counter++;
96 /** Print the buffer as a CDATA into given element.
98 * @param message Message to print.
99 * @param element_name Wrapping XML element name.
101 static void print_by_lines(const char *message, const char *element_name) {
102 if ((message == NULL) || (message[0] == 0)) {
103 return;
106 printf("\t\t\t<%s><![CDATA[", element_name);
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');
115 if (message[0] != 0) {
116 printf("%s\n", message);
119 printf("]]></%s>\n", element_name);
122 /** Report a completed test.
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).
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;
135 if (outcome != TEST_OUTCOME_PASS) {
136 failed_tests_in_suite++;
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;
155 printf("\t\t<testcase name=\"%s\" status=\"%s\">\n", test_name,
156 status_str);
158 print_by_lines(error_message, "error-message");
159 print_by_lines(teardown_error_message, "error-message");
161 print_by_lines(extra_output, "standard-output");
163 printf("\t\t</testcase><!-- %s -->\n", test_name);
166 /** Report testing done. */
167 static void xml_done() {
168 printf("</report>\n");
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