tests/test-parser.c
author Mikael Berthe <mikael@lilotux.net>
Mon, 25 Jan 2021 20:59:00 +0100
changeset 740 f1bb5e7e9477
parent 690 7ccf2113ec5f
permissions -rw-r--r--
Bump version to 1.5.4
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
690
7ccf2113ec5f Update the postal address of the FSF
Frank Zschockelt <lm@freakysoft.de>
parents: 664
diff changeset
    16
 * License along with this program; if not, see <https://www.gnu.org/licenses>
170
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    17
 */
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    18
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    19
#include <stdlib.h>
171
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    20
#include <glib.h>
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    21
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    22
#include "loudmouth/lm-parser.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
static GSList *
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    25
get_files (const gchar *prefix)
171
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    26
{
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
    27
    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
    28
    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
    29
    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
    30
    const gchar *name;
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    31
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
    32
    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
    33
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
    34
    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
    35
        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
    36
                 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
    37
        g_clear_error (&error);
171
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    38
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
    39
        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
    40
    }
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    41
171
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    42
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
    43
    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
    44
        /* Find *.xml */
171
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    45
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
    46
        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
    47
            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
    48
            gchar *file_path;
171
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    49
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    50
            file_path = g_strconcat (PARSER_TEST_DIR, "/", name,
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
    51
                                     NULL);
171
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    52
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
    53
            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
    54
        }
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
    }
171
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    56
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
    57
    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
    58
    g_clear_error (&error);
171
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    59
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
    60
    return list;
171
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    61
}
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    62
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    63
static void
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    64
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
    65
{
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
    66
    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
    67
    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
    68
    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
    69
    gsize     length;
171
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    70
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
    71
    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
    72
    if (!g_file_get_contents (file_path,
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    73
                              &file_contents, &length,
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
    74
                              &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
    75
        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
    76
                 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
    77
        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
    78
        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
    79
    }
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    80
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
    81
    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
    82
    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
    83
    g_free (file_contents);
171
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    84
}
170
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    85
397
5e2ad0e0704a Migrated test suite to GLib unit test framework
Mikael Hallendal <micke@imendio.com>
parents: 171
diff changeset
    86
static void
5e2ad0e0704a Migrated test suite to GLib unit test framework
Mikael Hallendal <micke@imendio.com>
parents: 171
diff changeset
    87
test_valid_suite ()
170
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
    88
{
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
    89
    GSList *list, *l;
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
    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
    92
    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
    93
        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
    94
        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
    95
    }
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_slist_free (list);
171
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    97
}
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
    98
397
5e2ad0e0704a Migrated test suite to GLib unit test framework
Mikael Hallendal <micke@imendio.com>
parents: 171
diff changeset
    99
static void
5e2ad0e0704a Migrated test suite to GLib unit test framework
Mikael Hallendal <micke@imendio.com>
parents: 171
diff changeset
   100
test_invalid_suite ()
171
118ca6c4520e 2006-09-10 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 170
diff changeset
   101
{
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
   102
    GSList *list, *l;
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
    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
   105
    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
   106
        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
   107
        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
   108
    }
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_slist_free (list);
170
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   110
}
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   111
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   112
int
170
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   113
main (int argc, char **argv)
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   114
{
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
   115
    g_test_init (&argc, &argv, NULL);
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
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_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
   118
    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
   119
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
   120
    return g_test_run ();
170
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   121
}
bba590bac876 2006-09-08 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
diff changeset
   122