source: mainline/uspace/lib/pcut/src/report/xml.c@ 2bff9860

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2bff9860 was 01579ad, checked in by Vojtech Horky <vojtechhorky@…>, 12 years ago

Start work on PCUT integration

PCUT is a simple library for (hopefully) easier unit testing.
See https://github.com/vhotspur/pcut for more details.

  • Property mode set to 100644
File size: 4.5 KB
Line 
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. */
42static int test_counter;
43
44/** Counter for tests in a current suite. */
45static int tests_in_suite;
46
47/** Counter of failed tests in current suite. */
48static int failed_tests_in_suite;
49
50/** Initialize the XML output. */
51static void xml_init(pcut_item_t *all_items) {
52 printf("<?xml version=\"1.0\"?>\n");
53
54 int tests_total = pcut_count_tests(all_items);
55 test_counter = 0;
56
57 printf("<report tests-total=\"%d\">\n", tests_total);
58}
59
60/** Report that a suite was started. */
61static void xml_suite_start(pcut_item_t *suite) {
62 tests_in_suite = 0;
63 failed_tests_in_suite = 0;
64
65 printf("\t<suite name=\"%s\">\n", suite->suite.name);
66}
67
68/** Report that a suite was completed. */
69static void xml_suite_done(pcut_item_t *suite) {
70 printf("\t</suite><!-- %s: %d / %d -->\n", suite->suite.name,
71 failed_tests_in_suite, tests_in_suite);
72}
73
74/** Report that a test was started.
75 *
76 * We do nothing - all handling is done after the test completes.
77 *
78 * @param test Test that is started.
79 */
80static void xml_test_start(pcut_item_t *test) {
81 PCUT_UNUSED(test);
82
83 tests_in_suite++;
84 test_counter++;
85}
86
87/** Print the buffer as a CDATA into given element.
88 *
89 * @param message Message to print.
90 * @param element_name Wrapping XML element name.
91 */
92static void print_by_lines(const char *message, const char *element_name) {
93 if ((message == NULL) || (message[0] == 0)) {
94 return;
95 }
96
97 printf("\t\t\t<%s><![CDATA[", element_name);
98
99 char *next_line_start = pcut_str_find_char(message, '\n');
100 while (next_line_start != NULL) {
101 next_line_start[0] = 0;
102 printf("%s\n", message);
103 message = next_line_start + 1;
104 next_line_start = pcut_str_find_char(message, '\n');
105 }
106 if (message[0] != 0) {
107 printf("%s\n", message);
108 }
109
110 printf("]]></%s>\n", element_name);
111}
112
113/** Report a completed test. */
114static void xml_test_done(pcut_item_t *test, int outcome,
115 const char *error_message, const char *teardown_error_message,
116 const char *extra_output) {
117 const char *test_name = test->test.name;
118
119 if (outcome != TEST_OUTCOME_PASS) {
120 failed_tests_in_suite++;
121 }
122
123 const char *status_str = NULL;
124 switch (outcome) {
125 case TEST_OUTCOME_PASS:
126 status_str = "pass";
127 break;
128 case TEST_OUTCOME_FAIL:
129 status_str = "fail";
130 break;
131 case TEST_OUTCOME_ERROR:
132 status_str = "error";
133 break;
134 default:
135 /* Shall not get here. */
136 break;
137 }
138
139 printf("\t\t<testcase name=\"%s\" status=\"%s\">\n", test_name,
140 status_str);
141
142 print_by_lines(error_message, "error-message");
143 print_by_lines(teardown_error_message, "error-message");
144
145 print_by_lines(extra_output, "standard-output");
146
147 printf("\t\t</testcase><!-- %s -->\n", test_name);
148}
149
150/** Report testing done. */
151static void xml_done() {
152 printf("</report>\n");
153}
154
155
156pcut_report_ops_t pcut_report_xml = {
157 .init = xml_init,
158 .done = xml_done,
159 .suite_start = xml_suite_start,
160 .suite_done = xml_suite_done,
161 .test_start = xml_test_start,
162 .test_done = xml_test_done
163};
Note: See TracBrowser for help on using the repository browser.