added new scanner test from #501654, by Patrick Hulin with various
author09:27:42 Tim Janik <timj@imendio.com>
Thu, 6 Dec 2007 08:32:03 +0000 (08:32 +0000)
committerTim Janik <timj@src.gnome.org>
Thu, 6 Dec 2007 08:32:03 +0000 (08:32 +0000)
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

ChangeLog
tests/Makefile.am
tests/scannerapi.c [new file with mode: 0644]

index 7cef980..ca9bb36 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+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.
+
 2007-12-05 17:58:18  Tim Janik  <timj@imendio.com>
 
        * glib/gtester.c: added -m=thorough support to gtester.
index 11da3ad..7454e6a 100644 (file)
@@ -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 (file)
index 0000000..d33fed9
--- /dev/null
@@ -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();
+}