2 * Copyright (c) 2013 Vojtech Horky
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
31 * Reporting routines for XML output (non-standard).
34 #include "../internal.h"
38 #pragma warning(push, 0)
43 #pragma warning(push, 0)
48 /** Counter of all run tests. */
49 static int test_counter
;
51 /** Counter for tests in a current suite. */
52 static int tests_in_suite
;
54 /** Counter of failed tests in current suite. */
55 static int failed_tests_in_suite
;
57 /** Initialize the XML output.
59 * @param all_items Start of the list with all items.
61 static void xml_init(pcut_item_t
*all_items
) {
62 int tests_total
= pcut_count_tests(all_items
);
65 printf("<?xml version=\"1.0\"?>\n");
66 printf("<report tests-total=\"%d\">\n", tests_total
);
69 /** Report that a suite was started.
71 * @param suite Suite that just started.
73 static void xml_suite_start(pcut_item_t
*suite
) {
75 failed_tests_in_suite
= 0;
77 printf("\t<suite name=\"%s\">\n", suite
->name
);
80 /** Report that a suite was completed.
82 * @param suite Suite that just ended.
84 static void xml_suite_done(pcut_item_t
*suite
) {
85 printf("\t</suite><!-- %s: %d / %d -->\n", suite
->name
,
86 failed_tests_in_suite
, tests_in_suite
);
89 /** Report that a test was started.
91 * We do nothing - all handling is done after the test completes.
93 * @param test Test that is started.
95 static void xml_test_start(pcut_item_t
*test
) {
102 /** Print the buffer as a CDATA into given element.
104 * @param message Message to print.
105 * @param element_name Wrapping XML element name.
107 static void print_by_lines(const char *message
, const char *element_name
) {
108 char *next_line_start
;
110 if ((message
== NULL
) || (message
[0] == 0)) {
114 printf("\t\t\t<%s><![CDATA[", element_name
);
116 next_line_start
= pcut_str_find_char(message
, '\n');
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');
123 if (message
[0] != 0) {
124 printf("%s\n", message
);
127 printf("]]></%s>\n", element_name
);
130 /** Report a completed test.
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).
138 static void xml_test_done(pcut_item_t
*test
, int outcome
,
139 const char *error_message
, const char *teardown_error_message
,
140 const char *extra_output
) {
141 const char *test_name
= test
->name
;
142 const char *status_str
= NULL
;
144 if (outcome
!= PCUT_OUTCOME_PASS
) {
145 failed_tests_in_suite
++;
149 case PCUT_OUTCOME_PASS
:
152 case PCUT_OUTCOME_FAIL
:
156 status_str
= "error";
160 printf("\t\t<testcase name=\"%s\" status=\"%s\">\n", test_name
,
163 print_by_lines(error_message
, "error-message");
164 print_by_lines(teardown_error_message
, "error-message");
166 print_by_lines(extra_output
, "standard-output");
168 printf("\t\t</testcase><!-- %s -->\n", test_name
);
171 /** Report testing done. */
172 static void xml_done(void) {
173 printf("</report>\n");
177 pcut_report_ops_t pcut_report_xml
= {
179 xml_suite_start
, xml_suite_done
,
180 xml_test_start
, xml_test_done