2 * Check: a unit test framework for C
3 * Copyright (C) 2001, 2002 Arien Malec
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 #include "libcompat/libcompat.h"
27 #include "internal-check.h"
28 #include "check_list.h"
29 #include "check_impl.h"
30 #include "check_str.h"
31 #include "check_print.h"
33 static void srunner_fprint_summary (FILE * file, SRunner * sr,
34 enum print_output print_mode);
35 static void srunner_fprint_results (FILE * file, SRunner * sr,
36 enum print_output print_mode);
40 srunner_print (SRunner * sr, enum print_output print_mode)
42 srunner_fprint (stdout, sr, print_mode);
46 srunner_fprint (FILE * file, SRunner * sr, enum print_output print_mode)
48 if (print_mode == CK_ENV) {
49 print_mode = get_env_printmode ();
52 srunner_fprint_summary (file, sr, print_mode);
53 srunner_fprint_results (file, sr, print_mode);
57 srunner_fprint_summary (FILE * file, SRunner * sr, enum print_output print_mode)
60 if (print_mode == CK_SUBUNIT)
64 if (print_mode >= CK_MINIMAL) {
67 str = sr_stat_str (sr);
68 fprintf (file, "%s\n", str);
75 srunner_fprint_results (FILE * file, SRunner * sr, enum print_output print_mode)
80 if (print_mode == CK_SUBUNIT)
84 resultlst = sr->resultlst;
86 for (check_list_front (resultlst); !check_list_at_end (resultlst);
87 check_list_advance (resultlst)) {
88 TestResult *tr = (TestResult *) check_list_val (resultlst);
90 tr_fprint (file, tr, print_mode);
96 fprint_xml_esc (FILE * file, const char *str)
98 /* The valid XML characters are as follows:
99 * #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
100 * Characters that are outside of ASCII must be encoded. Further, the
101 * following special characters:
103 * must be encoded. We assume that the incoming string may be a multibyte
107 for (; *str != '\0'; str++) {
110 /* handle special characters that must be escaped */
111 if (next == '"' || next == '\'' || next == '<' || next == '>'
115 fputs (""", file);
118 fputs ("'", file);
121 fputs ("<", file);
124 fputs (">", file);
127 fputs ("&", file);
131 /* printable ASCII */
132 else if (next >= ' ' && next <= '~') {
135 /* Non-printable character */
136 else if (next == 0x9 || next == 0xA || next == 0xD || next >= 0x20) {
137 fprintf (file, "&#x%X;", next);
139 /* If it did not get printed, it is not a valid XML character */
144 tr_fprint (FILE * file, TestResult * tr, enum print_output print_mode)
146 if (print_mode == CK_ENV) {
147 print_mode = get_env_printmode ();
150 if ((print_mode >= CK_VERBOSE && tr->rtype == CK_PASS) ||
151 (tr->rtype != CK_PASS && print_mode >= CK_NORMAL)) {
152 char *trstr = tr_str (tr);
154 fprintf (file, "%s\n", trstr);
160 tr_xmlprint (FILE * file, TestResult * tr,
161 enum print_output print_mode CK_ATTRIBUTE_UNUSED)
164 char *path_name = NULL;
165 char *file_name = NULL;
170 snprintf (result, sizeof (result), "%s", "success");
173 snprintf (result, sizeof (result), "%s", "failure");
176 snprintf (result, sizeof (result), "%s", "error");
178 case CK_TEST_RESULT_INVALID:
185 slash = strrchr (tr->file, '/');
187 slash = strrchr (tr->file, '\\');
191 path_name = strdup (".");
192 file_name = tr->file;
194 path_name = strdup (tr->file);
195 path_name[slash - tr->file] = 0; /* Terminate the temporary string. */
196 file_name = slash + 1;
201 fprintf (file, " <test result=\"%s\">\n", result);
202 fprintf (file, " <path>%s</path>\n",
203 (path_name == NULL ? "" : path_name));
204 fprintf (file, " <fn>%s:%d</fn>\n",
205 (file_name == NULL ? "" : file_name), tr->line);
206 fprintf (file, " <id>%s</id>\n", tr->tname);
207 fprintf (file, " <iteration>%d</iteration>\n", tr->iter);
208 fprintf (file, " <duration>%d.%06d</duration>\n",
209 tr->duration < 0 ? -1 : tr->duration / US_PER_SEC,
210 tr->duration < 0 ? 0 : tr->duration % US_PER_SEC);
211 fprintf (file, " <description>");
212 fprint_xml_esc (file, tr->tcname);
213 fprintf (file, "</description>\n");
214 fprintf (file, " <message>");
215 fprint_xml_esc (file, tr->msg);
216 fprintf (file, "</message>\n");
217 fprintf (file, " </test>\n");
223 get_env_printmode (void)
225 char *env = getenv ("CK_VERBOSITY");
229 if (strcmp (env, "silent") == 0)
231 if (strcmp (env, "minimal") == 0)
233 if (strcmp (env, "verbose") == 0)