1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
4 * This file is part of The Croco Library
6 * Copyright (C) 2002-2003 Dodji Seketeli <dodji@seketeli.org>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of version 2.1 of the GNU Lesser General Public
10 * License as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the 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
24 #include "cr-test-utils.h"
29 *Some test facilities for the #CRParser class.
32 CRDocHandler *gv_test_handler = { 0 };
34 const guchar *xml_content =
37 "<E1><E1-1>text1</E1-1></E1>"
38 "<E2 attr2=\"val2\">text2</E2>"
39 "<E3 attr3=\"val3_1 val3_2 val3_3\">text3</E3>"
40 "<E4 attr4=\"val4_1-val4_2-val4_3\">text4</E4>"
41 "<E5 class=\"class5\">text5</E5>"
42 "<E6 id=\"id6\">text6</E6>"
43 "<E7 lang=\"fr\">text7</E7>" "</document>";
46 display_help (char *prg_name);
49 display_about (char *prg_name);
51 test_sel_eng (guchar * a_file_uri);
54 walk_xml_tree_and_lookup_rules (CRSelEng * a_sel_eng,
55 CRStyleSheet * a_sheet, xmlNode * a_node);
58 *Displays the usage of the test
60 *@param a_argc the argc variable passed to the main function.
61 *@param a_argv the argv variable passed to the main function.
64 display_help (char *prg_name)
67 g_print ("usage: %s <file-to-parse>\n", prg_name);
68 g_print ("\t <file-to-parse>: the file to parse\n");
70 g_print ("Test the selection engine");
71 g_print ("Returns OK if the status is CR_OK, KO otherwise\n");
76 *Displays the about text.
77 *@param a_argc the argc variable passed to the main function.
78 *@param a_argv the argv variable passed to the main function.
81 display_about (char *prg_name)
84 g_print ("%s is a libcroco CROMParser class test program.\n",
86 g_print ("%s Parses a file and builds a CSS object model", prg_name);
87 g_print ("It should run on GNU compliants systems.\n");
89 g_print ("Initial author: Dodji Seketeli <dodji@seketeli.org>.\n");
94 walk_xml_tree_and_lookup_rules (CRSelEng * a_sel_eng,
95 CRStyleSheet * a_sheet, xmlNode * a_node)
97 CRStatement **stmts_tab = NULL;
100 enum CRStatus status = CR_OK;
102 xmlNode *cur_node = a_node;
104 for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
105 status = cr_sel_eng_get_matched_rulesets
106 (a_sel_eng, a_sheet, cur_node, &stmts_tab, &tab_len);
108 if (status == CR_OK && tab_len) {
109 g_print ("'''''''''''''''''''''''''\n");
110 g_print ("xml start element: %s\n", cur_node->name);
112 for (i = 0; i < tab_len; i++) {
115 cr_statement_dump (stmts_tab[i],
119 g_print ("\n\nxml end element: %s\n", cur_node->name);
120 g_print ("'''''''''''''''''''''''''\n");
127 if (cur_node->children) {
128 walk_xml_tree_and_lookup_rules
129 (a_sel_eng, a_sheet, cur_node->children);
135 *The test of the cr_input_read_byte() method.
136 *Reads the each byte of a_file_uri using the
137 *cr_input_read_byte() method. Each byte is send to
139 *@param a_file_uri the file to read.
140 *@return CR_OK upon successfull completion of the
141 *function, an error code otherwise.
144 test_sel_eng (guchar * a_file_uri)
146 enum CRStatus status = CR_OK;
147 CROMParser *parser = NULL;
148 CRStyleSheet *stylesheet = NULL;
149 xmlDoc *xml_doc = NULL;
150 xmlNode *cur_node = NULL;
151 CRSelEng *selection_engine = NULL;
153 g_return_val_if_fail (a_file_uri, CR_BAD_PARAM_ERROR);
155 parser = cr_om_parser_new (NULL);
156 status = cr_om_parser_parse_file (parser, a_file_uri, CR_ASCII,
158 if (status != CR_OK || !stylesheet) {
159 cr_utils_trace_info ("Could not parse xml content");
163 xml_doc = xmlParseMemory (xml_content, strlen (xml_content));
165 cr_utils_trace_info ("Could not parse xml content");
170 selection_engine = cr_sel_eng_new ();
172 cur_node = xml_doc->children;
174 walk_xml_tree_and_lookup_rules (selection_engine,
175 stylesheet, cur_node);
178 cr_om_parser_destroy (parser);
183 xmlFreeDoc (xml_doc);
188 cr_stylesheet_destroy (stylesheet);
191 if (selection_engine) {
192 cr_sel_eng_destroy (selection_engine) ;
193 selection_engine = NULL ;
201 cr_om_parser_destroy (parser);
206 xmlFreeDoc (xml_doc);
211 cr_stylesheet_destroy (stylesheet);
220 *The entry point of the testing routine.
223 main (int argc, char **argv)
225 struct Options options;
226 enum CRStatus status = CR_OK;
228 cr_test_utils_parse_cmd_line (argc, argv, &options);
230 if (options.display_help == TRUE) {
231 display_help (argv[0]);
235 if (options.display_about == TRUE) {
236 display_about (argv[0]);
240 if (options.files_list == NULL) {
241 display_help (argv[0]);
245 status = test_sel_eng (options.files_list[0]);
247 if (status != CR_OK) {