"Initial commit to Gerrit"
[profile/ivi/libcroco.git] / tests / test2-main.c
1 /* -*- Mode: C; indent-tabs-mode:nil; c-basic-offset:8 -*- */
2
3 /*
4  * This file is part of The Croco Library
5  *
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.
10  *
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.
15  *
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
20  * USA
21  *
22  * See COPYRIGHTS file for copyright information.
23  */
24
25 #include <stdio.h>
26 #include <string.h>
27 #include "cr-test-utils.h"
28 #include "cr-parser.h"
29
30 /**
31  *@file
32  *Some test facilities for the #CRParser class.
33  */
34
35 CRDocHandler *gv_test_handler = { 0 };
36
37 static void
38   display_help (char *prg_name);
39
40 static void
41   display_about (char *prg_name);
42
43 static enum CRStatus
44   test_cr_parser_parse (guchar * a_file_uri);
45
46 /**
47  *Displays the usage of the test
48  *facility.
49  *@param a_argc the argc variable passed to the main function.
50  *@param a_argv the argv variable passed to the main function.
51  */
52 static void
53 display_help (char *prg_name)
54 {
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");
62 }
63
64 /**
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.
68  */
69 static void
70 display_about (char *prg_name)
71 {
72         fprintf (stdout, "\n\n");
73         fprintf (stdout, "%s is a libcroco CRParser class test program.\n",
74                  prg_name);
75         fprintf (stdout, "It should run on GNU compliants systems.\n");
76         fprintf (stdout, "\n\n");
77         fprintf (stdout,
78                  "Initial author: Dodji Seketeli <dodji@seketeli.org>.\n");
79         fprintf (stdout, "\n\n");
80 }
81
82 /***************************
83  *Some SAC document handlers
84  *for TEST PURPOSES.
85  ***************************/
86
87 static void
88 test_start_document (CRDocHandler * a_handler)
89 {
90         g_return_if_fail (a_handler);
91
92         fprintf (stdout, "***************\n");
93         fprintf (stdout, "start_document\n");
94         fprintf (stdout, "***************\n\n");
95 }
96
97 static void
98 test_end_document (CRDocHandler * a_handler)
99 {
100         g_return_if_fail (a_handler);
101
102         fprintf (stdout, "***************\n");
103         fprintf (stdout, "end_document\n");
104         fprintf (stdout, "***************\n\n");
105 }
106
107 static void
108 test_import_style (CRDocHandler * a_handler,
109                    GList * a_media_list, 
110                    CRString * a_uri,
111                    CRString * a_uri_default_ns,
112                    CRParsingLocation *a_location)
113 {
114         g_return_if_fail (a_handler) ;
115
116         fprintf (stdout, "****************\n");
117         fprintf (stdout, "import_style\n");
118
119         if (a_media_list) {
120                 GList *cur = NULL;
121
122                 fprintf (stdout, "\nmedia list:\n");
123                 fprintf (stdout, "-------------\n");
124
125                 for (cur = a_media_list; cur; cur = cur->next) {
126                         if (cur->data) {
127                                 guchar *str =
128                                         g_strndup
129                                         (((CRString *) cur->data)->stryng->str,
130                                          ((CRString *) cur->data)->stryng->len);
131
132                                 if (str) {
133                                         fprintf (stdout, str);
134                                         fprintf (stdout, "\n");
135                                         g_free (str);
136                                         str = NULL;
137                                 }
138                         }
139                 }
140
141                 fprintf (stdout, "\ndefault namespace:\n");
142                 fprintf (stdout, "--------------------\n");
143
144                 if (a_uri_default_ns) {
145                         guchar *str = cr_string_dup2 
146                                 (a_uri_default_ns) ;
147                         if (str) {
148                                 fprintf (stdout, str);
149                                 fprintf (stdout, "\n");
150                                 g_free (str);
151                                 str = NULL;
152                         }
153                 }
154         }
155         fprintf (stdout, "******************\n\n");
156         a_uri = NULL;           /*keep compiler happy */
157 }
158
159 static void
160 test_namespace_declaration (CRDocHandler * a_handler,
161                             CRString * a_prefix, 
162                             CRString * a_uri,
163                             CRParsingLocation *a_location)
164 {
165         g_return_if_fail (a_handler);
166
167         fprintf (stdout, "***************\n");
168         fprintf (stdout, "namespace_declaration:\n");
169
170         if (a_prefix) {
171                 guchar *prefix = NULL;
172
173                 prefix = cr_string_dup2 (a_prefix) ;
174                 if (prefix) {
175                         fprintf (stdout, "prefix: %s\n", prefix);
176                         g_free (prefix);
177                         prefix = NULL;
178                 }
179         }
180         if (a_uri) {
181                 guchar *uri = NULL;
182
183                 uri = cr_string_dup2 (a_uri) ;
184                 if (uri) {
185                         fprintf (stdout, "uri: %s\n", uri);
186                         g_free (uri);
187                         uri = NULL;
188                 }
189         }
190         fprintf (stdout, "\n");
191
192         fprintf (stdout, "***************\n\n");
193
194 }
195
196 static void
197 test_comment (CRDocHandler * a_handler, 
198               CRString * a_comment)
199 {
200         g_return_if_fail (a_handler);
201
202         fprintf (stdout, "***************\n");
203         fprintf (stdout, "comment:\n");
204         if (a_comment) {
205                 guchar *comment = NULL;
206
207                 comment = cr_string_dup2 (a_comment);
208
209                 if (comment) {
210                         fprintf (stdout, "\n/*----------------------\n");
211                         fprintf (stdout, "%s\n", comment);
212                         fprintf (stdout, "-------------------------*/\n");
213                         g_free (comment);
214                         comment = NULL;
215                 }
216         }
217         fprintf (stdout, "***************\n\n");
218 }
219
220 static void
221 test_start_selector (CRDocHandler * a_handler, 
222                      CRSelector * a_selector_list)
223 {
224         g_return_if_fail (a_handler);
225
226         fprintf (stdout, "***************\n");
227         fprintf (stdout, "start_selector\n");
228
229         if (a_selector_list) {
230                 cr_selector_dump (a_selector_list, stdout);
231                 fprintf (stdout, "\n");
232         }
233
234         fprintf (stdout, "***************\n\n");
235 }
236
237 static void
238 test_end_selector (CRDocHandler * a_handler, 
239                    CRSelector * a_selector_list)
240 {
241         g_return_if_fail (a_handler);
242
243         fprintf (stdout, "***************\n");
244         fprintf (stdout, "end_selector\n");
245
246         if (a_selector_list) {
247                 cr_selector_dump (a_selector_list, stdout);
248                 fprintf (stdout, "\n");
249         }
250
251         fprintf (stdout, "***************\n\n");
252 }
253
254 static void
255 test_property (CRDocHandler * a_handler, 
256                CRString * a_name,
257                CRTerm * a_expr, 
258                gboolean a_important)
259 {
260         g_return_if_fail (a_handler);
261
262         fprintf (stdout, "***************\n");
263         fprintf (stdout, "property\n");
264
265         if (a_name 
266             && a_name->stryng
267             && a_name->stryng->str) {
268                 guchar *name = g_strndup 
269                         (a_name->stryng->str, 
270                          a_name->stryng->len);
271
272                 if (name) {
273                         fprintf (stdout, name);
274                 }
275                 if (a_expr) {
276                         fprintf (stdout, ": ");
277                         cr_term_dump (a_expr, stdout);
278                 }
279                 if (name) {
280                         g_free (name);
281                         name = NULL;
282                 }
283                 fprintf (stdout, "\n");
284         }
285         fprintf (stdout, "***************\n\n");
286 }
287
288 static void
289 test_start_font_face (CRDocHandler * a_handler,
290                       CRParsingLocation *a_location)
291 {
292         g_return_if_fail (a_handler);
293
294         fprintf (stdout, "***************\n");
295         fprintf (stdout, "start_font_face\n");
296         fprintf (stdout, "***************\n\n");
297 }
298
299 static void
300 test_end_font_face (CRDocHandler * a_handler)
301 {
302         g_return_if_fail (a_handler);
303
304         fprintf (stdout, "***************\n");
305         fprintf (stdout, "end_font_face\n");
306         fprintf (stdout, "***************\n\n");
307
308 }
309
310 static void
311 test_start_media (CRDocHandler * a_handler, 
312                   GList * a_media_list,
313                   CRParsingLocation *a_location)
314 {
315         g_return_if_fail (a_handler);
316
317         fprintf (stdout, "***************\n");
318         fprintf (stdout, "start_media\n");
319
320         if (a_media_list) {
321                 GList *cur = NULL;
322                 guchar *medium = NULL;
323
324                 for (cur = a_media_list; cur; cur = cur->next) {
325                         if (cur->data == NULL)
326                                 continue;
327                         medium = cr_string_dup2 
328                                 ((CRString *) cur->data);
329                         if (medium == NULL)
330                                 continue;
331                         fprintf (stdout, "medium: %s\n", medium);
332                         if (medium) {
333                                 g_free (medium);
334                                 medium = NULL;
335                         }
336                 }
337         }
338         fprintf (stdout, "***************\n\n");
339 }
340
341 static void
342 test_end_media (CRDocHandler * a_handler, 
343                 GList * a_media_list)
344 {
345         g_return_if_fail (a_handler);
346
347         fprintf (stdout, "***************\n");
348         fprintf (stdout, "end_media\n");
349
350         if (a_media_list) {
351                 GList *cur = NULL;
352                 guchar *medium = NULL;
353
354                 for (cur = a_media_list; cur; cur = cur->next) {
355                         if (cur->data == NULL)
356                                 continue;
357
358                         medium = g_strndup (((CRString *) cur->data)->stryng->str,
359                                             ((CRString *) cur->data)->stryng->len);
360                         if (medium == NULL)
361                                 continue;
362                         fprintf (stdout, "medium: %s\n", medium);
363                         if (medium) {
364                                 g_free (medium);
365                                 medium = NULL;
366                         }
367                 }
368         }
369
370         fprintf (stdout, "***************\n\n");
371 }
372
373 static void
374 test_start_page (CRDocHandler * a_handler,
375                  CRString * a_name, 
376                  CRString * a_pseudo_page,
377                  CRParsingLocation *a_location)
378 {
379         guchar *name = NULL,
380                 *pseudo_page = NULL;
381
382         g_return_if_fail (a_handler);
383
384         fprintf (stdout, "***************\n");
385         fprintf (stdout, "start_page\n");
386
387         if (a_name) {
388                 name = cr_string_dup2 (a_name) ;
389         }
390         if (a_pseudo_page) {
391                 pseudo_page = cr_string_dup2 (a_pseudo_page);
392         }
393         if (name) {
394                 fprintf (stdout, "%s", name);
395         }
396         if (pseudo_page) {
397                 fprintf (stdout, ": %s\n", pseudo_page);
398         }
399         fprintf (stdout, "***************\n\n");
400         if (name) {
401                 g_free (name);
402                 name = NULL;
403         }
404         if (pseudo_page) {
405                 g_free (pseudo_page);
406                 pseudo_page = NULL;
407         }
408 }
409
410 static void
411 test_end_page (CRDocHandler * a_handler,
412                CRString * a_name, 
413                CRString * a_pseudo_page)
414 {
415         guchar *name = NULL,
416                 *pseudo_page = NULL;
417
418         g_return_if_fail (a_handler);
419
420         fprintf (stdout, "***************\n");
421         fprintf (stdout, "end_page\n");
422
423         if (a_name) {
424                 name = cr_string_dup2 (a_name) ;
425         }
426         if (a_pseudo_page) {
427                 pseudo_page = cr_string_dup2 (a_pseudo_page) ;
428         }
429         if (name) {
430                 fprintf (stdout, "%s", name);
431         }
432         if (pseudo_page) {
433                 fprintf (stdout, ": %s\n", pseudo_page);
434
435         }
436         fprintf (stdout, "***************\n\n");
437         if (name) {
438                 g_free (name);
439                 name = NULL;
440         }
441         if (pseudo_page) {
442                 g_free (pseudo_page);
443                 pseudo_page = NULL;
444         }
445 }
446
447 static void
448 test_ignorable_at_rule (CRDocHandler * a_handler, 
449                         CRString * a_name)
450 {
451         guchar *name = NULL;
452
453         g_return_if_fail (a_handler);
454
455         fprintf (stdout, "*********************\n");
456         fprintf (stdout, "ignorable_at_rule\n");
457
458         if (a_name) {
459                 name = cr_string_dup2 (a_name);
460         }
461         if (name) {
462                 fprintf (stdout, "%s\n", name);
463         }
464         fprintf (stdout, "*********************\n\n");
465 }
466
467 static void
468 init_test_sac_handler (CRDocHandler * a_handler)
469 {
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;
485 }
486
487 /***************************
488  *END of TEST SAC document
489  *handlers.
490  ***************************/
491
492 /**
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
496  *stdout.
497  *@param a_file_uri the file to read.
498  *@return CR_OK upon successfull completion of the
499  *function, an error code otherwise.
500  */
501 static enum CRStatus
502 test_cr_parser_parse (guchar * a_file_uri)
503 {
504         enum CRStatus status = CR_OK;
505         CRParser *parser = NULL;
506
507         g_return_val_if_fail (a_file_uri, CR_BAD_PARAM_ERROR);
508
509         gv_test_handler = cr_doc_handler_new ();
510         init_test_sac_handler (gv_test_handler);
511
512         parser = cr_parser_new (NULL);
513
514         status = cr_parser_set_sac_handler (parser, gv_test_handler);
515
516         if (status != CR_OK) {
517                 cr_parser_destroy (parser);
518                 g_return_val_if_fail (status == CR_OK, CR_ERROR);
519         }
520
521         status = cr_parser_parse_file (parser, a_file_uri, CR_ASCII);
522
523         cr_parser_destroy (parser);
524
525         gv_test_handler = NULL;
526
527         return status;
528 }
529
530 /**
531  *The entry point of the testing routine.
532  */
533 int
534 main (int argc, char **argv)
535 {
536         struct Options options;
537         enum CRStatus status = CR_OK;
538
539         cr_test_utils_parse_cmd_line (argc, argv, &options);
540
541         if (options.display_help == TRUE) {
542                 display_help (argv[0]);
543                 return 0;
544         }
545
546         if (options.display_about == TRUE) {
547                 display_about (argv[0]);
548                 return 0;
549         }
550
551         if (options.files_list == NULL) {
552                 display_help (argv[0]);
553                 return 0;
554         }
555
556         status = test_cr_parser_parse (options.files_list[0]);
557
558         if (status != CR_OK) {
559                 fprintf (stdout, "KO\n");
560         }
561
562         return 0;
563 }