1/* Gtk+ object tests
2 * Copyright (C) 2007 Patrick Hulin
3 * Copyright (C) 2007 Imendio AB
4 * Authors: Tim Janik
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library 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 GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21#include <glib.h>
22#include <string.h>
23#include <stdlib.h>
24
25
26/* GScanner fixture */
27typedef struct {
28  GScanner *scanner;
29} ScannerFixture;
30static void
31scanner_fixture_setup (ScannerFixture *fix,
32                       gconstpointer   test_data)
33{
34  fix->scanner = g_scanner_new (NULL);
35  g_assert (fix->scanner != NULL);
36}
37static void
38scanner_fixture_teardown (ScannerFixture *fix,
39                          gconstpointer   test_data)
40{
41  g_assert (fix->scanner != NULL);
42  g_scanner_destroy (fix->scanner);
43}
44
45static void
46scanner_msg_func (GScanner *scanner,
47                  gchar    *message,
48                  gboolean  error)
49{
50  g_assert_cmpstr (message, ==, "test");
51}
52
53static void
54test_scanner_warn (ScannerFixture *fix,
55                   gconstpointer   test_data)
56{
57  fix->scanner->msg_handler = scanner_msg_func;
58  g_scanner_warn (fix->scanner, "test");
59}
60
61static void
62test_scanner_error (ScannerFixture *fix,
63                    gconstpointer   test_data)
64{
65  if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
66    {
67      int pe = fix->scanner->parse_errors;
68      g_scanner_error (fix->scanner, "scanner-error-message-test");
69      g_assert_cmpint (fix->scanner->parse_errors, ==, pe + 1);
70      exit (0);
71    }
72  g_test_trap_assert_passed();
73  g_test_trap_assert_stderr ("*scanner-error-message-test*");
74}
75
76static void
77check_keys (gpointer key,
78            gpointer value,
79            gpointer user_data)
80{
81  g_assert_cmpint (GPOINTER_TO_INT (value), ==, g_ascii_strtoull (key, NULL, 0));
82}
83
84static void
85test_scanner_symbols (ScannerFixture *fix,
86                      gconstpointer   test_data)
87{
88  gint i;
89  gchar buf[2];
90
91  g_scanner_set_scope (fix->scanner, 1);
92
93  for (i = 0; i < 10; i++)
94    g_scanner_scope_add_symbol (fix->scanner,
95                                1,
96                                g_ascii_dtostr (buf, 2, (gdouble)i),
97                                GINT_TO_POINTER (i));
98  g_scanner_scope_foreach_symbol (fix->scanner, 1, check_keys, NULL);
99  g_scanner_scope_remove_symbol (fix->scanner, 1, "5");
100  g_assert_cmpint (GPOINTER_TO_INT (g_scanner_scope_lookup_symbol (fix->scanner, 1, "4")), ==, 4);
101  g_assert_cmpint (GPOINTER_TO_INT (g_scanner_scope_lookup_symbol (fix->scanner, 1, "5")), ==, 0);
102}
103
104static void
105test_scanner_tokens (ScannerFixture *fix,
106                     gconstpointer   test_data)
107{
108  gchar buf[] = "(\t\n\r\\){}";
109  const gint buflen = strlen (buf);
110  gchar tokbuf[] = "(\\){}";
111  const gint tokbuflen = strlen (tokbuf);
112  guint i;
113
114  g_scanner_input_text (fix->scanner, buf, buflen);
115
116  g_assert_cmpint (g_scanner_cur_token (fix->scanner), ==, G_TOKEN_NONE);
117  g_scanner_get_next_token (fix->scanner);
118  g_assert_cmpint (g_scanner_cur_token (fix->scanner), ==, tokbuf[0]);
119  g_assert_cmpint (g_scanner_cur_line (fix->scanner), ==, 1);
120
121  for (i = 1; i < tokbuflen; i++)
122    g_assert_cmpint (g_scanner_get_next_token (fix->scanner), ==, tokbuf[i]);
123  g_assert_cmpint (g_scanner_get_next_token (fix->scanner), ==, G_TOKEN_EOF);
124  return;
125}
126
127int
128main (int   argc,
129      char *argv[])
130{
131  g_test_init (&argc, &argv, NULL);
132
133  g_test_add ("/scanner/warn", ScannerFixture, 0, scanner_fixture_setup, test_scanner_warn, scanner_fixture_teardown);
134  g_test_add ("/scanner/error", ScannerFixture, 0, scanner_fixture_setup, test_scanner_error, scanner_fixture_teardown);
135  g_test_add ("/scanner/symbols", ScannerFixture, 0, scanner_fixture_setup, test_scanner_symbols, scanner_fixture_teardown);
136  g_test_add ("/scanner/tokens", ScannerFixture, 0, scanner_fixture_setup, test_scanner_tokens, scanner_fixture_teardown);
137
138  return g_test_run();
139}
140