tests/test-parser.c
changeset 664 f57b1b61e1fe
parent 518 cdd6a0c5b439
child 690 7ccf2113ec5f
equal deleted inserted replaced
663:3697251ef911 664:f57b1b61e1fe
    22 #include <glib.h>
    22 #include <glib.h>
    23 
    23 
    24 #include "loudmouth/lm-parser.h"
    24 #include "loudmouth/lm-parser.h"
    25 
    25 
    26 static GSList *
    26 static GSList *
    27 get_files (const gchar *prefix) 
    27 get_files (const gchar *prefix)
    28 {
    28 {
    29     GSList      *list = NULL;
    29     GSList      *list = NULL;
    30     GError      *error = NULL;
    30     GError      *error = NULL;
    31     GDir        *dir;
    31     GDir        *dir;
    32     const gchar *name;
    32     const gchar *name;
    33     
    33 
    34     dir = g_dir_open (PARSER_TEST_DIR, 0, &error);
    34     dir = g_dir_open (PARSER_TEST_DIR, 0, &error);
    35 
    35 
    36     if (!dir) {
    36     if (!dir) {
    37         g_error ("Failed to open test file directory '%s' due to: %s",
    37         g_error ("Failed to open test file directory '%s' due to: %s",
    38                  PARSER_TEST_DIR, error->message);
    38                  PARSER_TEST_DIR, error->message);
    39         g_clear_error (&error);
    39         g_clear_error (&error);
    40 
    40 
    41         return NULL;
    41         return NULL;
    42     }
    42     }
    43     
    43 
    44 
    44 
    45     while ((name = g_dir_read_name (dir))) {
    45     while ((name = g_dir_read_name (dir))) {
    46         /* Find *.xml */
    46         /* Find *.xml */
    47 
    47 
    48         if (g_str_has_prefix (name, prefix) &&
    48         if (g_str_has_prefix (name, prefix) &&
    49             g_str_has_suffix (name, ".xml")) {
    49             g_str_has_suffix (name, ".xml")) {
    50             gchar *file_path;
    50             gchar *file_path;
    51 
    51 
    52             file_path = g_strconcat (PARSER_TEST_DIR, "/", name, 
    52             file_path = g_strconcat (PARSER_TEST_DIR, "/", name,
    53                                      NULL);
    53                                      NULL);
    54 
    54 
    55             list = g_slist_prepend (list, file_path);
    55             list = g_slist_prepend (list, file_path);
    56         }
    56         }
    57     }
    57     }
    70     GError   *error = NULL;
    70     GError   *error = NULL;
    71     gsize     length;
    71     gsize     length;
    72 
    72 
    73     parser = lm_parser_new (NULL, NULL, NULL);
    73     parser = lm_parser_new (NULL, NULL, NULL);
    74     if (!g_file_get_contents (file_path,
    74     if (!g_file_get_contents (file_path,
    75                               &file_contents, &length, 
    75                               &file_contents, &length,
    76                               &error)) {
    76                               &error)) {
    77         g_error ("Couldn't read file '%s': %s",
    77         g_error ("Couldn't read file '%s': %s",
    78                  file_path, error->message);
    78                  file_path, error->message);
    79         g_clear_error (&error);
    79         g_clear_error (&error);
    80         return;
    80         return;
    81     }
    81     }
    82         
    82 
    83     g_assert (lm_parser_parse (parser, file_contents) == is_valid);
    83     g_assert (lm_parser_parse (parser, file_contents) == is_valid);
    84     lm_parser_free (parser);
    84     lm_parser_free (parser);
    85     g_free (file_contents);
    85     g_free (file_contents);
    86 }
    86 }
    87 
    87 
   109         g_free (l->data);
   109         g_free (l->data);
   110     }
   110     }
   111     g_slist_free (list);
   111     g_slist_free (list);
   112 }
   112 }
   113 
   113 
   114 int 
   114 int
   115 main (int argc, char **argv)
   115 main (int argc, char **argv)
   116 {
   116 {
   117     g_test_init (&argc, &argv, NULL);
   117     g_test_init (&argc, &argv, NULL);
   118     
   118 
   119     g_test_add_func ("/parser/valid_suite", test_valid_suite);
   119     g_test_add_func ("/parser/valid_suite", test_valid_suite);
   120     g_test_add_func ("/parser/invalid/suite", test_invalid_suite);
   120     g_test_add_func ("/parser/invalid/suite", test_invalid_suite);
   121 
   121 
   122     return g_test_run ();
   122     return g_test_run ();
   123 }
   123 }