2 * Copyright (C) 2007 Patrick Hulin
3 * Copyright (C) 2007 Imendio AB
6 * SPDX-License-Identifier: LGPL-2.1-or-later
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 /* GScanner fixture */
31 scanner_fixture_setup (ScannerFixture *fix,
32 gconstpointer test_data)
34 fix->scanner = g_scanner_new (NULL);
35 g_assert (fix->scanner != NULL);
38 scanner_fixture_teardown (ScannerFixture *fix,
39 gconstpointer test_data)
41 g_assert (fix->scanner != NULL);
42 g_scanner_destroy (fix->scanner);
46 scanner_msg_func (GScanner *scanner,
50 g_assert_cmpstr (message, ==, "test");
54 test_scanner_warn (ScannerFixture *fix,
55 gconstpointer test_data)
57 fix->scanner->msg_handler = scanner_msg_func;
58 g_scanner_warn (fix->scanner, "test");
62 test_scanner_error (ScannerFixture *fix,
63 gconstpointer test_data)
65 if (g_test_subprocess ())
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);
73 g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
74 g_test_trap_assert_passed ();
75 g_test_trap_assert_stderr ("*scanner-error-message-test*");
79 check_keys (gpointer key,
83 g_assert_cmpint (GPOINTER_TO_INT (value), ==, g_ascii_strtoull (key, NULL, 0));
87 test_scanner_symbols (ScannerFixture *fix,
88 gconstpointer test_data)
93 g_scanner_set_scope (fix->scanner, 1);
95 for (i = 0; i < 10; i++)
96 g_scanner_scope_add_symbol (fix->scanner,
98 g_ascii_dtostr (buf, 2, (gdouble)i),
100 g_scanner_scope_foreach_symbol (fix->scanner, 1, check_keys, NULL);
101 g_assert_cmpint (GPOINTER_TO_INT (g_scanner_lookup_symbol (fix->scanner, "5")), ==, 5);
102 g_scanner_scope_remove_symbol (fix->scanner, 1, "5");
103 g_assert (g_scanner_lookup_symbol (fix->scanner, "5") == NULL);
105 g_assert_cmpint (GPOINTER_TO_INT (g_scanner_scope_lookup_symbol (fix->scanner, 1, "4")), ==, 4);
106 g_assert_cmpint (GPOINTER_TO_INT (g_scanner_scope_lookup_symbol (fix->scanner, 1, "5")), ==, 0);
110 test_scanner_tokens (ScannerFixture *fix,
111 gconstpointer test_data)
113 gchar buf[] = "(\t\n\r\\){}";
114 const gint buflen = strlen (buf);
115 gchar tokbuf[] = "(\\){}";
116 const gsize tokbuflen = strlen (tokbuf);
119 g_scanner_input_text (fix->scanner, buf, buflen);
121 g_assert_cmpint (g_scanner_cur_token (fix->scanner), ==, G_TOKEN_NONE);
122 g_scanner_get_next_token (fix->scanner);
123 g_assert_cmpint (g_scanner_cur_token (fix->scanner), ==, tokbuf[0]);
124 g_assert_cmpint (g_scanner_cur_line (fix->scanner), ==, 1);
126 for (i = 1; i < tokbuflen; i++)
127 g_assert_cmpint (g_scanner_get_next_token (fix->scanner), ==, tokbuf[i]);
128 g_assert_cmpint (g_scanner_get_next_token (fix->scanner), ==, G_TOKEN_EOF);
136 g_test_init (&argc, &argv, NULL);
138 g_test_add ("/scanner/warn", ScannerFixture, 0, scanner_fixture_setup, test_scanner_warn, scanner_fixture_teardown);
139 g_test_add ("/scanner/error", ScannerFixture, 0, scanner_fixture_setup, test_scanner_error, scanner_fixture_teardown);
140 g_test_add ("/scanner/symbols", ScannerFixture, 0, scanner_fixture_setup, test_scanner_symbols, scanner_fixture_teardown);
141 g_test_add ("/scanner/tokens", ScannerFixture, 0, scanner_fixture_setup, test_scanner_tokens, scanner_fixture_teardown);