tests/test-parser.c
author Jayson Vantuyl <jvantuyl@engineyard.com>
Wed, 25 Mar 2009 11:28:18 -0700
changeset 596 9096d2549372
parent 518 cdd6a0c5b439
child 664 f57b1b61e1fe
permissions -rw-r--r--
Removed Dangerous Instructions
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
517
6fabea75bea7 Make the emacs header changes to tests and examples
Mikael Hallendal <micke@imendio.com>
parents: 400
diff changeset
     1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
170
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
     2
/*
397
5e2ad0e0704a Migrated test suite to GLib unit test framework
Mikael Hallendal <micke@imendio.com>
parents: 171
diff changeset
     3
 * Copyright (C) 2006-2008 Imendio AB
170
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
     4
 *
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
     6
 * modify it under the terms of the GNU Lesser General Public License as
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
     7
 * published by the Free Software Foundation; either version 2 of the
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
     8
 * License, or (at your option) any later version.
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
     9
 *
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    13
 * Lesser General Public License for more details.
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    14
 *
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    15
 * You should have received a copy of the GNU Lesser General Public
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    16
 * License along with this program; if not, write to the
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    17
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    18
 * Boston, MA 02111-1307, USA.
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    19
 */
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    20
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    21
#include <stdlib.h>
171
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    22
#include <glib.h>
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    23
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    24
#include "loudmouth/lm-parser.h"
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    25
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    26
static GSList *
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    27
get_files (const gchar *prefix) 
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    28
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    29
    GSList      *list = NULL;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    30
    GError      *error = NULL;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    31
    GDir        *dir;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    32
    const gchar *name;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    33
    
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    34
    dir = g_dir_open (PARSER_TEST_DIR, 0, &error);
171
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    35
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    36
    if (!dir) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    37
        g_error ("Failed to open test file directory '%s' due to: %s",
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    38
                 PARSER_TEST_DIR, error->message);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    39
        g_clear_error (&error);
171
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    40
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    41
        return NULL;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    42
    }
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    43
    
171
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    44
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    45
    while ((name = g_dir_read_name (dir))) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    46
        /* Find *.xml */
171
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    47
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    48
        if (g_str_has_prefix (name, prefix) &&
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    49
            g_str_has_suffix (name, ".xml")) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    50
            gchar *file_path;
171
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    51
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    52
            file_path = g_strconcat (PARSER_TEST_DIR, "/", name, 
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    53
                                     NULL);
171
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    54
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    55
            list = g_slist_prepend (list, file_path);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    56
        }
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    57
    }
171
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    58
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    59
    g_dir_close (dir);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    60
    g_clear_error (&error);
171
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    61
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    62
    return list;
171
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    63
}
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    64
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    65
static void
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    66
test_parser_with_file (const gchar *file_path, gboolean is_valid)
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    67
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    68
    LmParser *parser;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    69
    gchar    *file_contents;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    70
    GError   *error = NULL;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    71
    gsize     length;
171
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    72
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    73
    parser = lm_parser_new (NULL, NULL, NULL);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    74
    if (!g_file_get_contents (file_path,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    75
                              &file_contents, &length, 
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    76
                              &error)) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    77
        g_error ("Couldn't read file '%s': %s",
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    78
                 file_path, error->message);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    79
        g_clear_error (&error);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    80
        return;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    81
    }
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    82
        
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    83
    g_assert (lm_parser_parse (parser, file_contents) == is_valid);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    84
    lm_parser_free (parser);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    85
    g_free (file_contents);
171
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    86
}
170
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    87
397
5e2ad0e0704a Migrated test suite to GLib unit test framework
Mikael Hallendal <micke@imendio.com>
parents: 171
diff changeset
    88
static void
5e2ad0e0704a Migrated test suite to GLib unit test framework
Mikael Hallendal <micke@imendio.com>
parents: 171
diff changeset
    89
test_valid_suite ()
170
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    90
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    91
    GSList *list, *l;
170
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    92
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    93
    list = get_files ("valid");
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    94
    for (l = list; l; l = l->next) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    95
        test_parser_with_file ((const gchar *) l->data, TRUE);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    96
        g_free (l->data);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    97
    }
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    98
    g_slist_free (list);
171
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    99
}
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
   100
397
5e2ad0e0704a Migrated test suite to GLib unit test framework
Mikael Hallendal <micke@imendio.com>
parents: 171
diff changeset
   101
static void
5e2ad0e0704a Migrated test suite to GLib unit test framework
Mikael Hallendal <micke@imendio.com>
parents: 171
diff changeset
   102
test_invalid_suite ()
171
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
   103
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   104
    GSList *list, *l;
171
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
   105
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   106
    list = get_files ("invalid");
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   107
    for (l = list; l; l = l->next) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   108
        test_parser_with_file ((const gchar *) l->data, FALSE);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   109
        g_free (l->data);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   110
    }
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   111
    g_slist_free (list);
170
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   112
}
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   113
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   114
int 
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   115
main (int argc, char **argv)
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   116
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   117
    g_test_init (&argc, &argv, NULL);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   118
    
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   119
    g_test_add_func ("/parser/valid_suite", test_valid_suite);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   120
    g_test_add_func ("/parser/invalid/suite", test_invalid_suite);
171
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
   121
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   122
    return g_test_run ();
170
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   123
}
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   124