Fixed typo in Czech translation
[yelp.git] / tests / test-uri.c
blob0f50abfd6a3e1e64c6ed54f5c8d332f285b8a8a4
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * Copyright (C) 2009 Shaun McCance <shaunm@gnome.org
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program 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 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 * Author: Shaun McCance <shaunm@gnome.org>
21 #include <config.h>
22 #include <stdio.h>
23 #include <string.h>
25 #include <gio/gio.h>
26 #include <gio/gunixoutputstream.h>
28 #include "yelp-uri.h"
30 GMainLoop *loop;
32 static void
33 print_uri (gchar *orig, YelpUri *uri, GOutputStream *stream)
35 GFile *file;
36 const gchar *type = NULL;
37 gchar *tmp, **tmpv, *out;
39 g_output_stream_write (stream, orig, strlen (orig), NULL, NULL);
40 g_output_stream_write (stream, "\n", 1, NULL, NULL);
42 switch (yelp_uri_get_document_type (uri)) {
43 case YELP_URI_DOCUMENT_TYPE_DOCBOOK:
44 type = "DOCBOOK";
45 break;
46 case YELP_URI_DOCUMENT_TYPE_MALLARD:
47 type = "MALLARD";
48 break;
49 case YELP_URI_DOCUMENT_TYPE_MAN:
50 type = "MAN";
51 break;
52 case YELP_URI_DOCUMENT_TYPE_INFO:
53 type = "INFO";
54 break;
55 case YELP_URI_DOCUMENT_TYPE_TEXT:
56 type = "TEXT";
57 break;
58 case YELP_URI_DOCUMENT_TYPE_HTML:
59 type = "HTML";
60 break;
61 case YELP_URI_DOCUMENT_TYPE_XHTML:
62 type = "XHTML";
63 break;
64 case YELP_URI_DOCUMENT_TYPE_HELP_LIST:
65 type = "TOC";
66 break;
67 case YELP_URI_DOCUMENT_TYPE_NOT_FOUND:
68 type = "NOT FOUND";
69 break;
70 case YELP_URI_DOCUMENT_TYPE_EXTERNAL:
71 type = "EXTERNAL";
72 break;
73 case YELP_URI_DOCUMENT_TYPE_ERROR:
74 type = "ERROR";
75 break;
76 case YELP_URI_DOCUMENT_TYPE_UNRESOLVED:
77 type = "UNRESOLVED";
78 break;
79 default:
80 g_assert_not_reached ();
81 break;
84 out = g_strdup_printf ("DOCUMENT TYPE: %s\n", type);
85 g_output_stream_write (stream, out, strlen (out), NULL, NULL);
86 g_free (out);
88 tmp = yelp_uri_get_document_uri (uri);
89 if (tmp) {
90 out = g_strdup_printf ("DOCUMENT URI: %s\n", tmp);
91 g_output_stream_write (stream, out, strlen (out), NULL, NULL);
92 g_free (out);
93 g_free (tmp);
96 tmp = yelp_uri_get_canonical_uri (uri);
97 if (tmp) {
98 out = g_strdup_printf ("CANONICAL URI: %s\n", tmp);
99 g_output_stream_write (stream, out, strlen (out), NULL, NULL);
100 g_free (out);
101 g_free (tmp);
104 file = yelp_uri_get_file (uri);
105 if (file) {
106 tmp = g_file_get_uri (file);
107 out = g_strdup_printf ("FILE URI: %s\n", tmp);
108 g_output_stream_write (stream, out, strlen (out), NULL, NULL);
109 g_free (out);
110 g_free (tmp);
111 g_object_unref (file);
114 tmpv = yelp_uri_get_search_path (uri);
115 if (tmpv) {
116 int i;
117 for (i = 0; tmpv[i]; i++) {
118 if (i == 0)
119 out = g_strdup_printf ("SEARCH PATH: %s\n", tmpv[i]);
120 else
121 out = g_strdup_printf (" %s\n", tmpv[i]);
122 g_output_stream_write (stream, out, strlen (out), NULL, NULL);
123 g_free (out);
125 g_strfreev (tmpv);
128 tmp = yelp_uri_get_page_id (uri);
129 if (tmp) {
130 out = g_strdup_printf ("PAGE ID: %s\n", tmp);
131 g_output_stream_write (stream, out, strlen (out), NULL, NULL);
132 g_free (out);
133 g_free (tmp);
136 tmp = yelp_uri_get_frag_id (uri);
137 if (tmp) {
138 out = g_strdup_printf ("FRAG ID: %s\n", tmp);
139 g_output_stream_write (stream, out, strlen (out), NULL, NULL);
140 g_free (out);
141 g_free (tmp);
144 g_output_stream_write (stream, "\0", 1, NULL, NULL);
147 static void run_test (gconstpointer data)
149 GFileInputStream *stream;
150 gchar contents[1024];
151 gsize bytes;
152 gchar *curi, *newline;
153 gchar **uriv;
154 GFile *file = G_FILE (data);
155 YelpUri *uri;
156 GOutputStream *outstream;
157 gchar *out;
159 stream = g_file_read (file, NULL, NULL);
160 g_assert (g_input_stream_read_all (G_INPUT_STREAM (stream),
161 contents, 1024, &bytes,
162 NULL, NULL));
163 newline = strchr (contents, '\n');
164 curi = g_strndup (contents, newline - contents);
165 uriv = g_strsplit (curi, " ", 2);
166 uri = yelp_uri_new (uriv[0]);
167 if (uriv[1] != NULL)
168 uri = yelp_uri_new_relative (uri, uriv[1]);
169 g_strfreev (uriv);
171 yelp_uri_resolve (uri);
173 while (!yelp_uri_is_resolved (uri))
174 while (g_main_context_pending (NULL))
175 g_main_context_iteration (NULL, FALSE);
177 outstream = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
178 print_uri (curi, uri, outstream);
179 out = (gchar *) g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (outstream));
180 g_free (curi);
181 g_assert (!strncmp (out, contents, bytes));
184 static int
185 run_all_tests (int argc, char **argv)
187 GFile *dir, *file;
188 GFileInfo *info;
189 GFileEnumerator *children;
190 GList *list = NULL;
192 dir = g_file_new_for_path ("uri");
193 children = g_file_enumerate_children (dir,
194 G_FILE_ATTRIBUTE_STANDARD_NAME,
195 0, NULL, NULL);
196 while ((info = g_file_enumerator_next_file (children, NULL, NULL))) {
197 const gchar *name = g_file_info_get_attribute_byte_string (info,
198 G_FILE_ATTRIBUTE_STANDARD_NAME);
199 if (!g_str_has_suffix (name, ".test"))
200 continue;
201 list = g_list_insert_sorted (list, (gchar *) name, (GCompareFunc) strcmp);
204 while (list) {
205 gchar *test_id = g_strconcat ("/", list->data, NULL);
206 file = g_file_get_child (dir, list->data);
207 g_test_add_data_func (test_id, file, run_test);
208 g_free (test_id);
209 list = g_list_delete_link (list, list);
212 return g_test_run ();
215 static void
216 uri_resolved (YelpUri *uri, gchar *orig)
218 GOutputStream *stream = g_unix_output_stream_new (1, FALSE);
219 print_uri (orig, uri, stream);
220 g_object_unref (uri);
221 g_main_loop_quit (loop);
225 main (int argc, char **argv)
227 YelpUri *parent = NULL;
228 YelpUri *uri = NULL;
230 g_log_set_always_fatal (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
232 if (argc < 2) {
233 g_test_init (&argc, &argv, NULL);
234 return run_all_tests (argc, argv);
236 else {
237 if (argc > 2) {
238 parent = yelp_uri_new (argv[1]);
239 uri = yelp_uri_new_relative (parent, argv[2]);
240 } else {
241 uri = yelp_uri_new (argv[1]);
243 if (uri) {
244 gchar *orig;
245 /* We leak orig if argc > 2. I don't care. */
246 if (argc > 2)
247 orig = g_strconcat (argv[1], " ", argv[2], NULL);
248 else
249 orig = argv[1];
250 g_signal_connect (uri, "resolved", G_CALLBACK (uri_resolved), orig);
251 yelp_uri_resolve (uri);
253 if (parent) {
254 g_object_unref (parent);
258 loop = g_main_loop_new (NULL, FALSE);
259 g_main_loop_run (loop);
261 return 0;