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.1 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, see <http://www.gnu.org/licenses/>.
18 */
19#include <glib.h>
20#include <string.h>
21#include <stdlib.h>
22
23
24/* GScanner fixture */
25typedef struct {
26 GScanner *scanner;
27} ScannerFixture;
28static void
29scanner_fixture_setup (ScannerFixture *fix,
30 gconstpointer test_data)
31{
32 fix->scanner = g_scanner_new (NULL);
33 g_assert (fix->scanner != NULL);
34}
35static void
36scanner_fixture_teardown (ScannerFixture *fix,
37 gconstpointer test_data)
38{
39 g_assert (fix->scanner != NULL);
40 g_scanner_destroy (scanner: fix->scanner);
41}
42
43static void
44scanner_msg_func (GScanner *scanner,
45 gchar *message,
46 gboolean error)
47{
48 g_assert_cmpstr (message, ==, "test");
49}
50
51static void
52test_scanner_warn (ScannerFixture *fix,
53 gconstpointer test_data)
54{
55 fix->scanner->msg_handler = scanner_msg_func;
56 g_scanner_warn (scanner: fix->scanner, format: "test");
57}
58
59static void
60test_scanner_error (ScannerFixture *fix,
61 gconstpointer test_data)
62{
63 if (g_test_subprocess ())
64 {
65 int pe = fix->scanner->parse_errors;
66 g_scanner_error (scanner: fix->scanner, format: "scanner-error-message-test");
67 g_assert_cmpint (fix->scanner->parse_errors, ==, pe + 1);
68 exit (status: 0);
69 }
70
71 g_test_trap_subprocess (NULL, usec_timeout: 0, test_flags: 0);
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 (scanner: fix->scanner, scope_id: 1);
92
93 for (i = 0; i < 10; i++)
94 g_scanner_scope_add_symbol (scanner: fix->scanner,
95 scope_id: 1,
96 symbol: g_ascii_dtostr (buffer: buf, buf_len: 2, d: (gdouble)i),
97 GINT_TO_POINTER (i));
98 g_scanner_scope_foreach_symbol (scanner: fix->scanner, scope_id: 1, func: check_keys, NULL);
99 g_assert_cmpint (GPOINTER_TO_INT (g_scanner_lookup_symbol (fix->scanner, "5")), ==, 5);
100 g_scanner_scope_remove_symbol (scanner: fix->scanner, scope_id: 1, symbol: "5");
101 g_assert (g_scanner_lookup_symbol (fix->scanner, "5") == NULL);
102
103 g_assert_cmpint (GPOINTER_TO_INT (g_scanner_scope_lookup_symbol (fix->scanner, 1, "4")), ==, 4);
104 g_assert_cmpint (GPOINTER_TO_INT (g_scanner_scope_lookup_symbol (fix->scanner, 1, "5")), ==, 0);
105}
106
107static void
108test_scanner_tokens (ScannerFixture *fix,
109 gconstpointer test_data)
110{
111 gchar buf[] = "(\t\n\r\\){}";
112 const gint buflen = strlen (s: buf);
113 gchar tokbuf[] = "(\\){}";
114 const gsize tokbuflen = strlen (s: tokbuf);
115 gsize i;
116
117 g_scanner_input_text (scanner: fix->scanner, text: buf, text_len: buflen);
118
119 g_assert_cmpint (g_scanner_cur_token (fix->scanner), ==, G_TOKEN_NONE);
120 g_scanner_get_next_token (scanner: fix->scanner);
121 g_assert_cmpint (g_scanner_cur_token (fix->scanner), ==, tokbuf[0]);
122 g_assert_cmpint (g_scanner_cur_line (fix->scanner), ==, 1);
123
124 for (i = 1; i < tokbuflen; i++)
125 g_assert_cmpint (g_scanner_get_next_token (fix->scanner), ==, tokbuf[i]);
126 g_assert_cmpint (g_scanner_get_next_token (fix->scanner), ==, G_TOKEN_EOF);
127 return;
128}
129
130int
131main (int argc,
132 char *argv[])
133{
134 g_test_init (argc: &argc, argv: &argv, NULL);
135
136 g_test_add ("/scanner/warn", ScannerFixture, 0, scanner_fixture_setup, test_scanner_warn, scanner_fixture_teardown);
137 g_test_add ("/scanner/error", ScannerFixture, 0, scanner_fixture_setup, test_scanner_error, scanner_fixture_teardown);
138 g_test_add ("/scanner/symbols", ScannerFixture, 0, scanner_fixture_setup, test_scanner_symbols, scanner_fixture_teardown);
139 g_test_add ("/scanner/tokens", ScannerFixture, 0, scanner_fixture_setup, test_scanner_tokens, scanner_fixture_teardown);
140
141 return g_test_run();
142}
143

source code of gtk/subprojects/glib/glib/tests/scannerapi.c