1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
26 #include <gio/gunixoutputstream.h>
33 print_uri (gchar
*orig
, YelpUri
*uri
, GOutputStream
*stream
)
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
:
46 case YELP_URI_DOCUMENT_TYPE_MALLARD
:
49 case YELP_URI_DOCUMENT_TYPE_MAN
:
52 case YELP_URI_DOCUMENT_TYPE_INFO
:
55 case YELP_URI_DOCUMENT_TYPE_TEXT
:
58 case YELP_URI_DOCUMENT_TYPE_HTML
:
61 case YELP_URI_DOCUMENT_TYPE_XHTML
:
64 case YELP_URI_DOCUMENT_TYPE_HELP_LIST
:
67 case YELP_URI_DOCUMENT_TYPE_NOT_FOUND
:
70 case YELP_URI_DOCUMENT_TYPE_EXTERNAL
:
73 case YELP_URI_DOCUMENT_TYPE_ERROR
:
76 case YELP_URI_DOCUMENT_TYPE_UNRESOLVED
:
80 g_assert_not_reached ();
84 out
= g_strdup_printf ("DOCUMENT TYPE: %s\n", type
);
85 g_output_stream_write (stream
, out
, strlen (out
), NULL
, NULL
);
88 tmp
= yelp_uri_get_document_uri (uri
);
90 out
= g_strdup_printf ("DOCUMENT URI: %s\n", tmp
);
91 g_output_stream_write (stream
, out
, strlen (out
), NULL
, NULL
);
96 tmp
= yelp_uri_get_canonical_uri (uri
);
98 out
= g_strdup_printf ("CANONICAL URI: %s\n", tmp
);
99 g_output_stream_write (stream
, out
, strlen (out
), NULL
, NULL
);
104 file
= yelp_uri_get_file (uri
);
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
);
111 g_object_unref (file
);
114 tmpv
= yelp_uri_get_search_path (uri
);
117 for (i
= 0; tmpv
[i
]; i
++) {
119 out
= g_strdup_printf ("SEARCH PATH: %s\n", tmpv
[i
]);
121 out
= g_strdup_printf (" %s\n", tmpv
[i
]);
122 g_output_stream_write (stream
, out
, strlen (out
), NULL
, NULL
);
128 tmp
= yelp_uri_get_page_id (uri
);
130 out
= g_strdup_printf ("PAGE ID: %s\n", tmp
);
131 g_output_stream_write (stream
, out
, strlen (out
), NULL
, NULL
);
136 tmp
= yelp_uri_get_frag_id (uri
);
138 out
= g_strdup_printf ("FRAG ID: %s\n", tmp
);
139 g_output_stream_write (stream
, out
, strlen (out
), NULL
, NULL
);
144 g_output_stream_write (stream
, "\0", 1, NULL
, NULL
);
147 static void run_test (gconstpointer data
)
149 GFileInputStream
*stream
;
150 gchar contents
[1024];
152 gchar
*curi
, *newline
;
154 GFile
*file
= G_FILE (data
);
156 GOutputStream
*outstream
;
159 stream
= g_file_read (file
, NULL
, NULL
);
160 g_assert (g_input_stream_read_all (G_INPUT_STREAM (stream
),
161 contents
, 1024, &bytes
,
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]);
168 uri
= yelp_uri_new_relative (uri
, uriv
[1]);
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
));
181 g_assert (!strncmp (out
, contents
, bytes
));
185 run_all_tests (int argc
, char **argv
)
189 GFileEnumerator
*children
;
192 dir
= g_file_new_for_path ("uri");
193 children
= g_file_enumerate_children (dir
,
194 G_FILE_ATTRIBUTE_STANDARD_NAME
,
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"))
201 list
= g_list_insert_sorted (list
, (gchar
*) name
, (GCompareFunc
) strcmp
);
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
);
209 list
= g_list_delete_link (list
, list
);
212 return g_test_run ();
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
;
230 g_log_set_always_fatal (G_LOG_LEVEL_ERROR
| G_LOG_LEVEL_CRITICAL
);
233 g_test_init (&argc
, &argv
, NULL
);
234 return run_all_tests (argc
, argv
);
238 parent
= yelp_uri_new (argv
[1]);
239 uri
= yelp_uri_new_relative (parent
, argv
[2]);
241 uri
= yelp_uri_new (argv
[1]);
245 /* We leak orig if argc > 2. I don't care. */
247 orig
= g_strconcat (argv
[1], " ", argv
[2], NULL
);
250 g_signal_connect (uri
, "resolved", G_CALLBACK (uri_resolved
), orig
);
251 yelp_uri_resolve (uri
);
254 g_object_unref (parent
);
258 loop
= g_main_loop_new (NULL
, FALSE
);
259 g_main_loop_run (loop
);