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 of version 2.1 of the GNU Lesser General Public
8 * License as published by the Free Software Foundation.
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
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 * Author: Dodji Seketeli
21 * See COPYRIGHTS file for copyrights information.
25 #include "cr-test-utils.h"
30 *Some test facilities for the #CROMParser class.
33 CRDocHandler *gv_test_handler = { 0 };
35 const guchar *gv_term_buf = "106";
37 const guchar *gv_decl_buf = "toto: tutu, tata";
39 const guchar *gv_decl_list_buf = "toto: titi; prop1:val1 ; prop2:val2;";
41 const guchar *gv_ruleset_buf = "s1 > s2 {toto: tutu, tata} ";
43 const guchar *gv_at_media_buf =
44 "@media print, toto {" " BODY { font-size: 10pt }" " }";
46 const guchar *gv_at_page_buf = "@page { size :8.5in 11in; margin: 2cm }";
48 const guchar *gv_at_charset_buf = "@charset \"ISO-8859-1\" ; ";
50 const guchar *gv_at_font_face_buf =
52 " font-family: \"Robson Celtic\";"
53 " src: url(\"http://site/fonts/rob-celt\")" "}";
55 const guchar *gv_at_import_buf = "@import \"subs.css\";";
57 static void display_help (char *prg_name);
59 static void display_about (char *prg_name);
61 static enum CRStatus test_cr_parser_parse (guchar * a_file_uri);
64 *Displays the usage of the test
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_help (char *prg_name)
73 g_print ("usage: %s <file-to-parse>\n", prg_name);
74 g_print ("\t <file-to-parse>: the file to parse\n");
76 g_print ("Tests the cr_parser_parse () method.\n");
77 g_print ("Tests the parsing following the css core syntax\n");
78 g_print ("Returns OK if the status is CR_OK, KO otherwise\n");
83 *Displays the about text.
84 *@param a_argc the argc variable passed to the main function.
85 *@param a_argv the argv variable passed to the main function.
88 display_about (char *prg_name)
91 g_print ("%s is a libcroco CROMParser class test program.\n",
93 g_print ("%s Parses a file and builds a CSS object model", prg_name);
94 g_print ("It should run on GNU compliants systems.\n");
96 g_print ("Initial author: Dodji Seketeli <dodji@seketeli.org>.\n");
101 *The test of the cr_input_read_byte() method.
102 *Reads the each byte of a_file_uri using the
103 *cr_input_read_byte() method. Each byte is send to
105 *@param a_file_uri the file to read.
106 *@return CR_OK upon successfull completion of the
107 *function, an error code otherwise.
110 test_cr_parser_parse (guchar * a_file_uri)
112 enum CRStatus status = CR_OK;
113 CROMParser *parser = NULL;
114 CRStyleSheet *stylesheet = NULL;
116 g_return_val_if_fail (a_file_uri, CR_BAD_PARAM_ERROR);
118 parser = cr_om_parser_new (NULL);
119 status = cr_om_parser_parse_file (parser, a_file_uri, CR_ASCII,
121 if (status == CR_OK && stylesheet) {
122 cr_stylesheet_dump (stylesheet, stdout);
123 cr_stylesheet_destroy (stylesheet);
125 cr_om_parser_destroy (parser);
131 test_cr_statement_at_page_rule_parse_from_buf (void)
133 CRStatement *stmt = NULL ;
135 stmt = cr_statement_at_page_rule_parse_from_buf
136 (gv_at_page_buf, CR_UTF_8) ;
140 cr_statement_destroy (stmt) ;
145 test_cr_term_parse_expression_from_buf (void)
147 guchar *tmp_str = NULL;
150 term = cr_term_parse_expression_from_buf (gv_term_buf, CR_UTF_8);
154 tmp_str = cr_term_to_string (term);
156 cr_term_destroy (term);
167 test_cr_declaration_parse (void)
169 guchar *tmp_str = NULL;
170 CRDeclaration *decl = NULL;
172 decl = cr_declaration_parse_from_buf (NULL, gv_decl_buf, CR_UTF_8);
175 tmp_str = cr_declaration_to_string (decl, 2);
178 cr_declaration_destroy (decl);
190 test_cr_declaration_parse_list (void)
193 guchar *tmp_str = NULL;
194 CRDeclaration *decl = NULL,
197 decl = cr_declaration_parse_list_from_buf (gv_decl_list_buf,
201 str = g_string_new (NULL);
202 for (cur_decl = decl; cur_decl; cur_decl = cur_decl->next) {
203 tmp_str = cr_declaration_to_string (cur_decl, 2);
205 g_string_append_printf (str, "%s;", tmp_str);
212 cr_declaration_destroy (decl);
216 g_string_free (str, TRUE);
224 test_cr_statement_ruleset_parse (void)
226 CRStatement *stmt = NULL;
228 stmt = cr_statement_ruleset_parse_from_buf (gv_ruleset_buf, CR_UTF_8);
229 g_return_val_if_fail (stmt, CR_ERROR);
232 cr_statement_destroy (stmt);
239 test_cr_statement_at_media_rule_parse (void)
241 CRStatement *stmt = NULL;
243 stmt = cr_statement_at_media_rule_parse_from_buf (gv_at_media_buf,
245 g_return_val_if_fail (stmt, CR_ERROR);
248 cr_statement_destroy (stmt);
257 test_cr_statement_at_charset_rule_parse (void)
259 CRStatement *stmt = NULL;
261 stmt = cr_statement_at_charset_rule_parse_from_buf (gv_at_charset_buf,
263 g_return_val_if_fail (stmt, CR_ERROR);
265 cr_statement_destroy (stmt);
273 test_cr_statement_font_face_rule_parse_from_buf (void)
275 CRStatement *stmt = NULL;
277 stmt = cr_statement_font_face_rule_parse_from_buf
278 (gv_at_font_face_buf, CR_UTF_8);
279 g_return_val_if_fail (stmt, CR_ERROR);
281 cr_statement_destroy (stmt);
289 test_cr_statement_at_import_rule_parse_from_buf (void)
291 CRStatement *stmt = NULL;
293 stmt = cr_statement_at_import_rule_parse_from_buf (gv_at_import_buf,
295 g_return_val_if_fail (stmt, CR_ERROR);
297 cr_statement_destroy (stmt);
305 test_cr_statement_parse_from_buf (void)
307 CRStatement *stmt = NULL;
309 stmt = cr_statement_parse_from_buf (gv_ruleset_buf, CR_UTF_8);
311 cr_statement_destroy (stmt);
316 stmt = cr_statement_parse_from_buf (gv_at_media_buf, CR_UTF_8);
318 cr_statement_destroy (stmt);
323 stmt = cr_statement_parse_from_buf (gv_at_page_buf, CR_UTF_8);
325 cr_statement_destroy (stmt);
330 stmt = cr_statement_parse_from_buf (gv_at_charset_buf, CR_UTF_8);
332 cr_statement_destroy (stmt);
337 stmt = cr_statement_parse_from_buf (gv_at_font_face_buf, CR_UTF_8);
339 cr_statement_destroy (stmt);
344 stmt = cr_statement_parse_from_buf (gv_at_import_buf, CR_UTF_8);
346 cr_statement_destroy (stmt);
356 *The entry point of the testing routine.
359 main (int argc, char **argv)
361 struct Options options;
362 enum CRStatus status = CR_OK;
364 status = test_cr_term_parse_expression_from_buf ();
365 if (status != CR_OK) {
366 g_print ("\ntest \"cr_term_parse_expression_from_buf failed\"");
368 status = test_cr_declaration_parse ();
369 if (status != CR_OK) {
370 g_print ("\n test \"cr_declaration_parse() failed\"\n");
373 status = test_cr_declaration_parse_list ();
374 if (status != CR_OK) {
375 g_print ("\ntest cr_declaration_parse_list() failed\n");
377 status = test_cr_statement_ruleset_parse ();
378 if (status != CR_OK) {
379 g_print ("\ntest cr_statement_ruleset_parse() failed\n");
382 status = test_cr_statement_at_media_rule_parse ();
383 if (status != CR_OK) {
384 g_print ("\ntest cr_statement_at_media_rule_parse() failed\n");
386 test_cr_statement_at_page_rule_parse_from_buf ();
387 if (status != CR_OK) {
388 g_print ("\ntest cr_statement_at_page_rule_parse() failed\n");
392 status = test_cr_statement_at_charset_rule_parse ();
393 if (status != CR_OK) {
394 g_print ("\ntest cr_statement_at_charset_rule_parse() failed\n");
397 status = test_cr_statement_font_face_rule_parse_from_buf ();
398 if (status != CR_OK) {
399 g_print ("\ntest cr_statement_font_face_rule_parse_from_buf() failed\n");
402 test_cr_statement_at_import_rule_parse_from_buf ();
403 if (status != CR_OK) {
404 g_print ("\ntest cr_statement_at_import_rule() failed\n");
407 status = test_cr_statement_parse_from_buf ();
408 if (status != CR_OK) {
409 g_print ("\ntest cr_statement_parse_from_buf() failed\n");
412 cr_test_utils_parse_cmd_line (argc, argv, &options);
414 if (options.display_help == TRUE) {
415 display_help (argv[0]);
419 if (options.display_about == TRUE) {
420 display_about (argv[0]);
424 if (options.files_list == NULL) {
425 display_help (argv[0]);
429 status = test_cr_parser_parse (options.files_list[0]);
430 if (status != CR_OK) {