diff options
author | 09:27:42 Tim Janik <timj@imendio.com> | 2007-12-06 08:32:03 +0000 |
---|---|---|
committer | Tim Janik <timj@src.gnome.org> | 2007-12-06 08:32:03 +0000 |
commit | 871897db961d7228da6b844eaa0b6f6636771c24 (patch) | |
tree | c0dfa31e3ff9702a5150ef0247ed46aef184dd3f /tests | |
parent | 66f1845caaf91954d85d7b1d8e44c892e11ffb73 (diff) |
added new scanner test from #501654, by Patrick Hulin with various
2007-12-06 09:27:42 Tim Janik <timj@imendio.com>
* tests/scannerapi.c: added new scanner test from #501654, by
Patrick Hulin with various modifications.
reworked coding style, adapted to new testing framework, fixed
token parser test and use a forked sub process to test
g_scanner_error() output messages.
svn path=/trunk/; revision=6055
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile.am | 7 | ||||
-rw-r--r-- | tests/scannerapi.c | 139 |
2 files changed, 145 insertions, 1 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index 11da3ad33..7454e6a0f 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -65,7 +65,7 @@ endif if ENABLE_TIMELOOP timeloop = timeloop timeloop-closure endif -noinst_PROGRAMS = \ +noinst_PROGRAMS = $(TEST_PROGS) \ testglib \ testgdate \ testgdateparser \ @@ -74,6 +74,11 @@ noinst_PROGRAMS = \ $(timeloop) \ errorcheck-mutex-test +TEST_PROGS += scannerapi +scannerapi_SOURCES = scannerapi.c +scannerapi_LDADD = $(progs_ldadd) + + testglib_LDADD = $(libglib) patterntest_LDADD = $(libglib) testgdate_LDADD = $(libglib) diff --git a/tests/scannerapi.c b/tests/scannerapi.c new file mode 100644 index 000000000..d33fed917 --- /dev/null +++ b/tests/scannerapi.c @@ -0,0 +1,139 @@ +/* Gtk+ object tests + * Copyright (C) 2007 Patrick Hulin + * Copyright (C) 2007 Imendio AB + * Authors: Tim Janik + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ +#include <glib.h> +#include <string.h> +#include <stdlib.h> + + +/* GScanner fixture */ +typedef struct { + GScanner *scanner; +} ScannerFixture; +static void +scanner_fixture_setup (ScannerFixture *fix, + gconstpointer test_data) +{ + fix->scanner = g_scanner_new (NULL); + g_assert (fix->scanner != NULL); +} +static void +scanner_fixture_teardown (ScannerFixture *fix, + gconstpointer test_data) +{ + g_assert (fix->scanner != NULL); + g_scanner_destroy (fix->scanner); +} + +static void +scanner_msg_func (GScanner *scanner, + gchar *message, + gboolean error) +{ + g_assert_cmpstr (message, ==, "test"); +} + +static void +test_scanner_warn (ScannerFixture *fix, + gconstpointer test_data) +{ + fix->scanner->msg_handler = scanner_msg_func; + g_scanner_warn (fix->scanner, "test"); +} + +static void +test_scanner_error (ScannerFixture *fix, + gconstpointer test_data) +{ + if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR)) + { + int pe = fix->scanner->parse_errors; + g_scanner_error (fix->scanner, "scanner-error-message-test"); + g_assert_cmpint (fix->scanner->parse_errors, ==, pe + 1); + exit (0); + } + g_test_trap_assert_passed(); + g_test_trap_assert_stderr ("*scanner-error-message-test*"); +} + +static void +check_keys (gpointer key, + gpointer value, + gpointer user_data) +{ + g_assert_cmpint (GPOINTER_TO_INT (value), ==, g_ascii_strtoull (key, NULL, 0)); +} + +static void +test_scanner_symbols (ScannerFixture *fix, + gconstpointer test_data) +{ + gint i; + gchar buf[2]; + + g_scanner_set_scope (fix->scanner, 1); + + for (i = 0; i < 10; i++) + g_scanner_scope_add_symbol (fix->scanner, + 1, + g_ascii_dtostr (buf, 2, (gdouble)i), + GINT_TO_POINTER (i)); + g_scanner_scope_foreach_symbol (fix->scanner, 1, check_keys, NULL); + g_scanner_scope_remove_symbol (fix->scanner, 1, "5"); + g_assert_cmpint (GPOINTER_TO_INT (g_scanner_scope_lookup_symbol (fix->scanner, 1, "4")), ==, 4); + g_assert_cmpint (GPOINTER_TO_INT (g_scanner_scope_lookup_symbol (fix->scanner, 1, "5")), ==, 0); +} + +static void +test_scanner_tokens (ScannerFixture *fix, + gconstpointer test_data) +{ + gchar buf[] = "(\t\n\r\\){}"; + const gint buflen = strlen (buf); + gchar tokbuf[] = "(\\){}"; + const gint tokbuflen = strlen (tokbuf); + guint i; + + g_scanner_input_text (fix->scanner, buf, buflen); + + g_assert_cmpint (g_scanner_cur_token (fix->scanner), ==, G_TOKEN_NONE); + g_scanner_get_next_token (fix->scanner); + g_assert_cmpint (g_scanner_cur_token (fix->scanner), ==, tokbuf[0]); + g_assert_cmpint (g_scanner_cur_line (fix->scanner), ==, 1); + + for (i = 1; i < tokbuflen; i++) + g_assert_cmpint (g_scanner_get_next_token (fix->scanner), ==, tokbuf[i]); + g_assert_cmpint (g_scanner_get_next_token (fix->scanner), ==, G_TOKEN_EOF); + return; +} + +int +main (int argc, + char *argv[]) +{ + g_test_init (&argc, &argv, NULL); + + g_test_add ("/scanner/warn", ScannerFixture, 0, scanner_fixture_setup, test_scanner_warn, scanner_fixture_teardown); + g_test_add ("/scanner/error", ScannerFixture, 0, scanner_fixture_setup, test_scanner_error, scanner_fixture_teardown); + g_test_add ("/scanner/symbols", ScannerFixture, 0, scanner_fixture_setup, test_scanner_symbols, scanner_fixture_teardown); + g_test_add ("/scanner/tokens", ScannerFixture, 0, scanner_fixture_setup, test_scanner_tokens, scanner_fixture_teardown); + + return g_test_run(); +} |