check: Import version 0.9.14
[platform/upstream/gstreamer.git] / libs / gst / check / libcheck / check_print.c
1 /*
2  * Check: a unit test framework for C
3  * Copyright (C) 2001, 2002 Arien Malec
4  *
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.
9  *
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.
14  *
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,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #include "../lib/libcompat.h"
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include "check.h"
28 #include "check_list.h"
29 #include "check_impl.h"
30 #include "check_str.h"
31 #include "check_print.h"
32
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);
37
38
39 void
40 srunner_print (SRunner * sr, enum print_output print_mode)
41 {
42   srunner_fprint (stdout, sr, print_mode);
43 }
44
45 void
46 srunner_fprint (FILE * file, SRunner * sr, enum print_output print_mode)
47 {
48   if (print_mode == CK_ENV) {
49     print_mode = get_env_printmode ();
50   }
51
52   srunner_fprint_summary (file, sr, print_mode);
53   srunner_fprint_results (file, sr, print_mode);
54 }
55
56 static void
57 srunner_fprint_summary (FILE * file, SRunner * sr, enum print_output print_mode)
58 {
59 #if ENABLE_SUBUNIT
60   if (print_mode == CK_SUBUNIT)
61     return;
62 #endif
63
64   if (print_mode >= CK_MINIMAL) {
65     char *str;
66
67     str = sr_stat_str (sr);
68     fprintf (file, "%s\n", str);
69     free (str);
70   }
71   return;
72 }
73
74 static void
75 srunner_fprint_results (FILE * file, SRunner * sr, enum print_output print_mode)
76 {
77   List *resultlst;
78
79 #if ENABLE_SUBUNIT
80   if (print_mode == CK_SUBUNIT)
81     return;
82 #endif
83
84   resultlst = sr->resultlst;
85
86   for (check_list_front (resultlst); !check_list_at_end (resultlst);
87       check_list_advance (resultlst)) {
88     TestResult *tr = (TestResult *) check_list_val (resultlst);
89
90     tr_fprint (file, tr, print_mode);
91   }
92   return;
93 }
94
95 void
96 fprint_xml_esc (FILE * file, const char *str)
97 {
98   for (; *str != '\0'; str++) {
99
100     switch (*str) {
101
102         /* handle special characters that must be escaped */
103       case '"':
104         fputs ("&quot;", file);
105         break;
106       case '\'':
107         fputs ("&apos;", file);
108         break;
109       case '<':
110         fputs ("&lt;", file);
111         break;
112       case '>':
113         fputs ("&gt;", file);
114         break;
115       case '&':
116         fputs ("&amp;", file);
117         break;
118
119         /* regular characters, print as is */
120       default:
121         fputc (*str, file);
122         break;
123     }
124   }
125 }
126
127 void
128 tr_fprint (FILE * file, TestResult * tr, enum print_output print_mode)
129 {
130   if (print_mode == CK_ENV) {
131     print_mode = get_env_printmode ();
132   }
133
134   if ((print_mode >= CK_VERBOSE && tr->rtype == CK_PASS) ||
135       (tr->rtype != CK_PASS && print_mode >= CK_NORMAL)) {
136     char *trstr = tr_str (tr);
137
138     fprintf (file, "%s\n", trstr);
139     free (trstr);
140   }
141 }
142
143 void
144 tr_xmlprint (FILE * file, TestResult * tr,
145     enum print_output print_mode CK_ATTRIBUTE_UNUSED)
146 {
147   char result[10];
148   char *path_name = NULL;
149   char *file_name = NULL;
150   char *slash = NULL;
151
152   switch (tr->rtype) {
153     case CK_PASS:
154       snprintf (result, sizeof (result), "%s", "success");
155       break;
156     case CK_FAILURE:
157       snprintf (result, sizeof (result), "%s", "failure");
158       break;
159     case CK_ERROR:
160       snprintf (result, sizeof (result), "%s", "error");
161       break;
162     case CK_TEST_RESULT_INVALID:
163     default:
164       abort ();
165       break;
166   }
167
168   if (tr->file) {
169     slash = strrchr (tr->file, '/');
170     if (slash == NULL) {
171       slash = strrchr (tr->file, '\\');
172     }
173
174     if (slash == NULL) {
175       path_name = strdup (".");
176       file_name = tr->file;
177     } else {
178       path_name = strdup (tr->file);
179       path_name[slash - tr->file] = 0;  /* Terminate the temporary string. */
180       file_name = slash + 1;
181     }
182   }
183
184
185   fprintf (file, "    <test result=\"%s\">\n", result);
186   fprintf (file, "      <path>%s</path>\n",
187       (path_name == NULL ? "" : path_name));
188   fprintf (file, "      <fn>%s:%d</fn>\n",
189       (file_name == NULL ? "" : file_name), tr->line);
190   fprintf (file, "      <id>%s</id>\n", tr->tname);
191   fprintf (file, "      <iteration>%d</iteration>\n", tr->iter);
192   fprintf (file, "      <duration>%d.%06d</duration>\n",
193       tr->duration < 0 ? -1 : tr->duration / US_PER_SEC,
194       tr->duration < 0 ? 0 : tr->duration % US_PER_SEC);
195   fprintf (file, "      <description>");
196   fprint_xml_esc (file, tr->tcname);
197   fprintf (file, "</description>\n");
198   fprintf (file, "      <message>");
199   fprint_xml_esc (file, tr->msg);
200   fprintf (file, "</message>\n");
201   fprintf (file, "    </test>\n");
202
203   free (path_name);
204 }
205
206 enum print_output
207 get_env_printmode (void)
208 {
209   char *env = getenv ("CK_VERBOSITY");
210
211   if (env == NULL)
212     return CK_NORMAL;
213   if (strcmp (env, "silent") == 0)
214     return CK_SILENT;
215   if (strcmp (env, "minimal") == 0)
216     return CK_MINIMAL;
217   if (strcmp (env, "verbose") == 0)
218     return CK_VERBOSE;
219   return CK_NORMAL;
220 }