glib/tests/scannerapi.c
changeset 18 47c74d1534e1
equal deleted inserted replaced
0:e4d67989cc36 18:47c74d1534e1
       
     1 /* Gtk+ object tests
       
     2  * Copyright (C) 2007 Patrick Hulin
       
     3  * Copyright (C) 2007 Imendio AB
       
     4  * Authors: Tim Janik
       
     5  * Portions copyright (c) 2009 Nokia Corporation.  All rights reserved.
       
     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 #ifdef __SYMBIAN32__
       
    25 #include "mrt2_glib2_test.h"
       
    26 #endif /*__SYMBIAN32__*/
       
    27 
       
    28 /* GScanner fixture */
       
    29 typedef struct {
       
    30   GScanner *scanner;
       
    31 } ScannerFixture;
       
    32 static void
       
    33 scanner_fixture_setup (ScannerFixture *fix,
       
    34                        gconstpointer   test_data)
       
    35 {
       
    36   fix->scanner = g_scanner_new (NULL);
       
    37   g_assert (fix->scanner != NULL);
       
    38 }
       
    39 static void
       
    40 scanner_fixture_teardown (ScannerFixture *fix,
       
    41                           gconstpointer   test_data)
       
    42 {
       
    43   g_assert (fix->scanner != NULL);
       
    44   g_scanner_destroy (fix->scanner);
       
    45 }
       
    46 
       
    47 static void
       
    48 scanner_msg_func (GScanner *scanner,
       
    49                   gchar    *message,
       
    50                   gboolean  error)
       
    51 {
       
    52   g_assert_cmpstr (message, ==, "test");
       
    53 }
       
    54 
       
    55 static void
       
    56 test_scanner_warn (ScannerFixture *fix,
       
    57                    gconstpointer   test_data)
       
    58 {
       
    59   fix->scanner->msg_handler = scanner_msg_func;
       
    60   g_scanner_warn (fix->scanner, "test");
       
    61 }
       
    62 
       
    63 static void
       
    64 test_scanner_error (ScannerFixture *fix,
       
    65                     gconstpointer   test_data)
       
    66 {
       
    67   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
       
    68     {
       
    69       int pe = fix->scanner->parse_errors;
       
    70       g_scanner_error (fix->scanner, "scanner-error-message-test");
       
    71       g_assert_cmpint (fix->scanner->parse_errors, ==, pe + 1);
       
    72       exit (0);
       
    73     }
       
    74   g_test_trap_assert_passed();
       
    75   g_test_trap_assert_stderr ("*scanner-error-message-test*");
       
    76 }
       
    77 
       
    78 static void
       
    79 check_keys (gpointer key,
       
    80             gpointer value,
       
    81             gpointer user_data)
       
    82 {
       
    83   g_assert_cmpint (GPOINTER_TO_INT (value), ==, g_ascii_strtoull (key, NULL, 0));
       
    84 }
       
    85 
       
    86 static void
       
    87 test_scanner_symbols (ScannerFixture *fix,
       
    88                       gconstpointer   test_data)
       
    89 {
       
    90   gint i;
       
    91   gchar buf[2];
       
    92 
       
    93   g_scanner_set_scope (fix->scanner, 1);
       
    94 
       
    95   for (i = 0; i < 10; i++)
       
    96     g_scanner_scope_add_symbol (fix->scanner,
       
    97                                 1,
       
    98                                 g_ascii_dtostr (buf, 2, (gdouble)i),
       
    99                                 GINT_TO_POINTER (i));
       
   100   g_scanner_scope_foreach_symbol (fix->scanner, 1, check_keys, NULL);
       
   101   g_scanner_scope_remove_symbol (fix->scanner, 1, "5");
       
   102   g_assert_cmpint (GPOINTER_TO_INT (g_scanner_scope_lookup_symbol (fix->scanner, 1, "4")), ==, 4);
       
   103   g_assert_cmpint (GPOINTER_TO_INT (g_scanner_scope_lookup_symbol (fix->scanner, 1, "5")), ==, 0);
       
   104 }
       
   105 
       
   106 static void
       
   107 test_scanner_tokens (ScannerFixture *fix,
       
   108                      gconstpointer   test_data)
       
   109 {
       
   110   gchar buf[] = "(\t\n\r\\){}";
       
   111   const gint buflen = strlen (buf);
       
   112   gchar tokbuf[] = "(\\){}";
       
   113   const gint tokbuflen = strlen (tokbuf);
       
   114   guint i;
       
   115 
       
   116   g_scanner_input_text (fix->scanner, buf, buflen);
       
   117 
       
   118   g_assert_cmpint (g_scanner_cur_token (fix->scanner), ==, G_TOKEN_NONE);
       
   119   g_scanner_get_next_token (fix->scanner);
       
   120   g_assert_cmpint (g_scanner_cur_token (fix->scanner), ==, tokbuf[0]);
       
   121   g_assert_cmpint (g_scanner_cur_line (fix->scanner), ==, 1);
       
   122 
       
   123   for (i = 1; i < tokbuflen; i++)
       
   124     g_assert_cmpint (g_scanner_get_next_token (fix->scanner), ==, tokbuf[i]);
       
   125   g_assert_cmpint (g_scanner_get_next_token (fix->scanner), ==, G_TOKEN_EOF);
       
   126   return;
       
   127 }
       
   128 
       
   129 int
       
   130 main (int   argc,
       
   131       char *argv[])
       
   132 {
       
   133 #ifdef __SYMBIAN32__
       
   134   g_log_set_handler (NULL,  G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
       
   135   g_set_print_handler(mrtPrintHandler);
       
   136   #endif /*__SYMBIAN32__*/
       
   137   g_test_init (&argc, &argv, NULL);
       
   138 
       
   139   g_test_add ("/scanner/warn", ScannerFixture, 0, scanner_fixture_setup, test_scanner_warn, scanner_fixture_teardown);
       
   140   g_test_add ("/scanner/error", ScannerFixture, 0, scanner_fixture_setup, test_scanner_error, scanner_fixture_teardown);
       
   141   g_test_add ("/scanner/symbols", ScannerFixture, 0, scanner_fixture_setup, test_scanner_symbols, scanner_fixture_teardown);
       
   142   g_test_add ("/scanner/tokens", ScannerFixture, 0, scanner_fixture_setup, test_scanner_tokens, scanner_fixture_teardown);
       
   143 #if __SYMBIAN32__
       
   144   testResultXml("scannerapi");
       
   145   #endif /* EMULATOR */
       
   146   return g_test_run();
       
   147 }