1 /* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset:8 -*- */
4 * This file is part of The Croco Library
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms
8 * of version 2.1 of the GNU Lesser General Public
9 * License as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the
17 * GNU Lesser General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * See COPYRIGHTS file for copyright information.
27 #include "cr-test-utils.h"
28 #include "cr-parser.h"
32 *Some test facilities for the #CRParser class.
35 CRDocHandler *gv_test_handler = { 0 };
38 display_help (char *prg_name);
41 display_about (char *prg_name);
44 test_cr_parser_parse (guchar * a_file_uri);
47 *Displays the usage of the test
49 *@param a_argc the argc variable passed to the main function.
50 *@param a_argv the argv variable passed to the main function.
53 display_help (char *prg_name)
55 fprintf (stdout, "\n\n");
56 fprintf (stdout, "usage: %s <file-to-parse>\n", prg_name);
57 fprintf (stdout, "\t <file-to-parse>: the file to parse\n");
58 fprintf (stdout, "\n\n");
59 fprintf (stdout, "Tests the cr_parser_parse () method.\n");
60 fprintf (stdout, "Returns OK if the status is CR_OK, KO otherwise\n");
61 fprintf (stdout, "\n\n");
65 *Displays the about text.
66 *@param a_argc the argc variable passed to the main function.
67 *@param a_argv the argv variable passed to the main function.
70 display_about (char *prg_name)
72 fprintf (stdout, "\n\n");
73 fprintf (stdout, "%s is a libcroco CRParser class test program.\n",
75 fprintf (stdout, "It should run on GNU compliants systems.\n");
76 fprintf (stdout, "\n\n");
78 "Initial author: Dodji Seketeli <dodji@seketeli.org>.\n");
79 fprintf (stdout, "\n\n");
82 /***************************
83 *Some SAC document handlers
85 ***************************/
88 test_start_document (CRDocHandler * a_handler)
90 g_return_if_fail (a_handler);
92 fprintf (stdout, "***************\n");
93 fprintf (stdout, "start_document\n");
94 fprintf (stdout, "***************\n\n");
98 test_end_document (CRDocHandler * a_handler)
100 g_return_if_fail (a_handler);
102 fprintf (stdout, "***************\n");
103 fprintf (stdout, "end_document\n");
104 fprintf (stdout, "***************\n\n");
108 test_import_style (CRDocHandler * a_handler,
109 GList * a_media_list,
111 CRString * a_uri_default_ns,
112 CRParsingLocation *a_location)
114 g_return_if_fail (a_handler) ;
116 fprintf (stdout, "****************\n");
117 fprintf (stdout, "import_style\n");
122 fprintf (stdout, "\nmedia list:\n");
123 fprintf (stdout, "-------------\n");
125 for (cur = a_media_list; cur; cur = cur->next) {
129 (((CRString *) cur->data)->stryng->str,
130 ((CRString *) cur->data)->stryng->len);
133 fprintf (stdout, str);
134 fprintf (stdout, "\n");
141 fprintf (stdout, "\ndefault namespace:\n");
142 fprintf (stdout, "--------------------\n");
144 if (a_uri_default_ns) {
145 guchar *str = cr_string_dup2
148 fprintf (stdout, str);
149 fprintf (stdout, "\n");
155 fprintf (stdout, "******************\n\n");
156 a_uri = NULL; /*keep compiler happy */
160 test_namespace_declaration (CRDocHandler * a_handler,
163 CRParsingLocation *a_location)
165 g_return_if_fail (a_handler);
167 fprintf (stdout, "***************\n");
168 fprintf (stdout, "namespace_declaration:\n");
171 guchar *prefix = NULL;
173 prefix = cr_string_dup2 (a_prefix) ;
175 fprintf (stdout, "prefix: %s\n", prefix);
183 uri = cr_string_dup2 (a_uri) ;
185 fprintf (stdout, "uri: %s\n", uri);
190 fprintf (stdout, "\n");
192 fprintf (stdout, "***************\n\n");
197 test_comment (CRDocHandler * a_handler,
198 CRString * a_comment)
200 g_return_if_fail (a_handler);
202 fprintf (stdout, "***************\n");
203 fprintf (stdout, "comment:\n");
205 guchar *comment = NULL;
207 comment = cr_string_dup2 (a_comment);
210 fprintf (stdout, "\n/*----------------------\n");
211 fprintf (stdout, "%s\n", comment);
212 fprintf (stdout, "-------------------------*/\n");
217 fprintf (stdout, "***************\n\n");
221 test_start_selector (CRDocHandler * a_handler,
222 CRSelector * a_selector_list)
224 g_return_if_fail (a_handler);
226 fprintf (stdout, "***************\n");
227 fprintf (stdout, "start_selector\n");
229 if (a_selector_list) {
230 cr_selector_dump (a_selector_list, stdout);
231 fprintf (stdout, "\n");
234 fprintf (stdout, "***************\n\n");
238 test_end_selector (CRDocHandler * a_handler,
239 CRSelector * a_selector_list)
241 g_return_if_fail (a_handler);
243 fprintf (stdout, "***************\n");
244 fprintf (stdout, "end_selector\n");
246 if (a_selector_list) {
247 cr_selector_dump (a_selector_list, stdout);
248 fprintf (stdout, "\n");
251 fprintf (stdout, "***************\n\n");
255 test_property (CRDocHandler * a_handler,
258 gboolean a_important)
260 g_return_if_fail (a_handler);
262 fprintf (stdout, "***************\n");
263 fprintf (stdout, "property\n");
267 && a_name->stryng->str) {
268 guchar *name = g_strndup
269 (a_name->stryng->str,
270 a_name->stryng->len);
273 fprintf (stdout, name);
276 fprintf (stdout, ": ");
277 cr_term_dump (a_expr, stdout);
283 fprintf (stdout, "\n");
285 fprintf (stdout, "***************\n\n");
289 test_start_font_face (CRDocHandler * a_handler,
290 CRParsingLocation *a_location)
292 g_return_if_fail (a_handler);
294 fprintf (stdout, "***************\n");
295 fprintf (stdout, "start_font_face\n");
296 fprintf (stdout, "***************\n\n");
300 test_end_font_face (CRDocHandler * a_handler)
302 g_return_if_fail (a_handler);
304 fprintf (stdout, "***************\n");
305 fprintf (stdout, "end_font_face\n");
306 fprintf (stdout, "***************\n\n");
311 test_start_media (CRDocHandler * a_handler,
312 GList * a_media_list,
313 CRParsingLocation *a_location)
315 g_return_if_fail (a_handler);
317 fprintf (stdout, "***************\n");
318 fprintf (stdout, "start_media\n");
322 guchar *medium = NULL;
324 for (cur = a_media_list; cur; cur = cur->next) {
325 if (cur->data == NULL)
327 medium = cr_string_dup2
328 ((CRString *) cur->data);
331 fprintf (stdout, "medium: %s\n", medium);
338 fprintf (stdout, "***************\n\n");
342 test_end_media (CRDocHandler * a_handler,
343 GList * a_media_list)
345 g_return_if_fail (a_handler);
347 fprintf (stdout, "***************\n");
348 fprintf (stdout, "end_media\n");
352 guchar *medium = NULL;
354 for (cur = a_media_list; cur; cur = cur->next) {
355 if (cur->data == NULL)
358 medium = g_strndup (((CRString *) cur->data)->stryng->str,
359 ((CRString *) cur->data)->stryng->len);
362 fprintf (stdout, "medium: %s\n", medium);
370 fprintf (stdout, "***************\n\n");
374 test_start_page (CRDocHandler * a_handler,
376 CRString * a_pseudo_page,
377 CRParsingLocation *a_location)
382 g_return_if_fail (a_handler);
384 fprintf (stdout, "***************\n");
385 fprintf (stdout, "start_page\n");
388 name = cr_string_dup2 (a_name) ;
391 pseudo_page = cr_string_dup2 (a_pseudo_page);
394 fprintf (stdout, "%s", name);
397 fprintf (stdout, ": %s\n", pseudo_page);
399 fprintf (stdout, "***************\n\n");
405 g_free (pseudo_page);
411 test_end_page (CRDocHandler * a_handler,
413 CRString * a_pseudo_page)
418 g_return_if_fail (a_handler);
420 fprintf (stdout, "***************\n");
421 fprintf (stdout, "end_page\n");
424 name = cr_string_dup2 (a_name) ;
427 pseudo_page = cr_string_dup2 (a_pseudo_page) ;
430 fprintf (stdout, "%s", name);
433 fprintf (stdout, ": %s\n", pseudo_page);
436 fprintf (stdout, "***************\n\n");
442 g_free (pseudo_page);
448 test_ignorable_at_rule (CRDocHandler * a_handler,
453 g_return_if_fail (a_handler);
455 fprintf (stdout, "*********************\n");
456 fprintf (stdout, "ignorable_at_rule\n");
459 name = cr_string_dup2 (a_name);
462 fprintf (stdout, "%s\n", name);
464 fprintf (stdout, "*********************\n\n");
468 init_test_sac_handler (CRDocHandler * a_handler)
470 a_handler->start_document = test_start_document;
471 a_handler->end_document = test_end_document;
472 a_handler->import_style = test_import_style;
473 a_handler->namespace_declaration = test_namespace_declaration;
474 a_handler->comment = test_comment;
475 a_handler->start_selector = test_start_selector;
476 a_handler->end_selector = test_end_selector;
477 a_handler->property = test_property;
478 a_handler->start_font_face = test_start_font_face;
479 a_handler->end_font_face = test_end_font_face;
480 a_handler->start_media = test_start_media;
481 a_handler->end_media = test_end_media;
482 a_handler->start_page = test_start_page;
483 a_handler->end_page = test_end_page;
484 a_handler->ignorable_at_rule = test_ignorable_at_rule;
487 /***************************
488 *END of TEST SAC document
490 ***************************/
493 *The test of the cr_input_read_byte() method.
494 *Reads the each byte of a_file_uri using the
495 *cr_input_read_byte() method. Each byte is send to
497 *@param a_file_uri the file to read.
498 *@return CR_OK upon successfull completion of the
499 *function, an error code otherwise.
502 test_cr_parser_parse (guchar * a_file_uri)
504 enum CRStatus status = CR_OK;
505 CRParser *parser = NULL;
507 g_return_val_if_fail (a_file_uri, CR_BAD_PARAM_ERROR);
509 gv_test_handler = cr_doc_handler_new ();
510 init_test_sac_handler (gv_test_handler);
512 parser = cr_parser_new (NULL);
514 status = cr_parser_set_sac_handler (parser, gv_test_handler);
516 if (status != CR_OK) {
517 cr_parser_destroy (parser);
518 g_return_val_if_fail (status == CR_OK, CR_ERROR);
521 status = cr_parser_parse_file (parser, a_file_uri, CR_ASCII);
523 cr_parser_destroy (parser);
525 gv_test_handler = NULL;
531 *The entry point of the testing routine.
534 main (int argc, char **argv)
536 struct Options options;
537 enum CRStatus status = CR_OK;
539 cr_test_utils_parse_cmd_line (argc, argv, &options);
541 if (options.display_help == TRUE) {
542 display_help (argv[0]);
546 if (options.display_about == TRUE) {
547 display_about (argv[0]);
551 if (options.files_list == NULL) {
552 display_help (argv[0]);
556 status = test_cr_parser_parse (options.files_list[0]);
558 if (status != CR_OK) {
559 fprintf (stdout, "KO\n");