LCOV - code coverage report
Current view: top level - glib/glib/tests - scannerapi.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 62 62 100.0 %
Date: 2024-04-23 05:16:05 Functions: 9 9 100.0 %
Branches: 6 6 100.0 %

           Branch data     Line data    Source code
       1                 :            : /* Gtk+ object tests
       2                 :            :  * Copyright (C) 2007 Patrick Hulin
       3                 :            :  * Copyright (C) 2007 Imendio AB
       4                 :            :  * Authors: Tim Janik
       5                 :            :  *
       6                 :            :  * SPDX-License-Identifier: LGPL-2.1-or-later
       7                 :            :  *
       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.
      12                 :            :  *
      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.
      17                 :            :  *
      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/>.
      20                 :            :  */
      21                 :            : #include <glib.h>
      22                 :            : #include <string.h>
      23                 :            : #include <stdlib.h>
      24                 :            : 
      25                 :            : 
      26                 :            : /* GScanner fixture */
      27                 :            : typedef struct {
      28                 :            :   GScanner *scanner;
      29                 :            : } ScannerFixture;
      30                 :            : static void
      31                 :          5 : scanner_fixture_setup (ScannerFixture *fix,
      32                 :            :                        gconstpointer   test_data)
      33                 :            : {
      34                 :          5 :   fix->scanner = g_scanner_new (NULL);
      35                 :          5 :   g_assert (fix->scanner != NULL);
      36                 :          5 : }
      37                 :            : static void
      38                 :          4 : scanner_fixture_teardown (ScannerFixture *fix,
      39                 :            :                           gconstpointer   test_data)
      40                 :            : {
      41                 :          4 :   g_assert (fix->scanner != NULL);
      42                 :          4 :   g_scanner_destroy (fix->scanner);
      43                 :          4 : }
      44                 :            : 
      45                 :            : static void
      46                 :          1 : scanner_msg_func (GScanner *scanner,
      47                 :            :                   gchar    *message,
      48                 :            :                   gboolean  error)
      49                 :            : {
      50                 :          1 :   g_assert_cmpstr (message, ==, "test");
      51                 :          1 : }
      52                 :            : 
      53                 :            : static void
      54                 :          1 : test_scanner_warn (ScannerFixture *fix,
      55                 :            :                    gconstpointer   test_data)
      56                 :            : {
      57                 :          1 :   fix->scanner->msg_handler = scanner_msg_func;
      58                 :          1 :   g_scanner_warn (fix->scanner, "test");
      59                 :          1 : }
      60                 :            : 
      61                 :            : static void
      62                 :          2 : test_scanner_error (ScannerFixture *fix,
      63                 :            :                     gconstpointer   test_data)
      64                 :            : {
      65         [ +  + ]:          2 :   if (g_test_subprocess ())
      66                 :            :     {
      67                 :          1 :       int pe = fix->scanner->parse_errors;
      68                 :          1 :       g_scanner_error (fix->scanner, "scanner-error-message-test");
      69                 :          1 :       g_assert_cmpint (fix->scanner->parse_errors, ==, pe + 1);
      70                 :          1 :       exit (0);
      71                 :            :     }
      72                 :            : 
      73                 :          1 :   g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
      74                 :          1 :   g_test_trap_assert_passed ();
      75                 :          1 :   g_test_trap_assert_stderr ("*scanner-error-message-test*");
      76                 :          1 : }
      77                 :            : 
      78                 :            : static void
      79                 :         10 : check_keys (gpointer key,
      80                 :            :             gpointer value,
      81                 :            :             gpointer user_data)
      82                 :            : {
      83                 :         10 :   g_assert_cmpint (GPOINTER_TO_INT (value), ==, g_ascii_strtoull (key, NULL, 0));
      84                 :         10 : }
      85                 :            : 
      86                 :            : static void
      87                 :          1 : test_scanner_symbols (ScannerFixture *fix,
      88                 :            :                       gconstpointer   test_data)
      89                 :            : {
      90                 :            :   gint i;
      91                 :            :   gchar buf[2];
      92                 :            : 
      93                 :          1 :   g_scanner_set_scope (fix->scanner, 1);
      94                 :            : 
      95         [ +  + ]:         11 :   for (i = 0; i < 10; i++)
      96                 :         10 :     g_scanner_scope_add_symbol (fix->scanner,
      97                 :            :                                 1,
      98                 :         10 :                                 g_ascii_dtostr (buf, 2, (gdouble)i),
      99                 :         10 :                                 GINT_TO_POINTER (i));
     100                 :          1 :   g_scanner_scope_foreach_symbol (fix->scanner, 1, check_keys, NULL);
     101                 :          1 :   g_assert_cmpint (GPOINTER_TO_INT (g_scanner_lookup_symbol (fix->scanner, "5")), ==, 5);
     102                 :          1 :   g_scanner_scope_remove_symbol (fix->scanner, 1, "5");
     103                 :          1 :   g_assert (g_scanner_lookup_symbol (fix->scanner, "5") == NULL);
     104                 :            : 
     105                 :          1 :   g_assert_cmpint (GPOINTER_TO_INT (g_scanner_scope_lookup_symbol (fix->scanner, 1, "4")), ==, 4);
     106                 :          1 :   g_assert_cmpint (GPOINTER_TO_INT (g_scanner_scope_lookup_symbol (fix->scanner, 1, "5")), ==, 0);
     107                 :          1 : }
     108                 :            : 
     109                 :            : static void
     110                 :          1 : test_scanner_tokens (ScannerFixture *fix,
     111                 :            :                      gconstpointer   test_data)
     112                 :            : {
     113                 :          1 :   gchar buf[] = "(\t\n\r\\){}";
     114                 :          1 :   const gint buflen = strlen (buf);
     115                 :          1 :   gchar tokbuf[] = "(\\){}";
     116                 :          1 :   const gsize tokbuflen = strlen (tokbuf);
     117                 :            :   gsize i;
     118                 :            : 
     119                 :          1 :   g_scanner_input_text (fix->scanner, buf, buflen);
     120                 :            : 
     121                 :          1 :   g_assert_cmpint (g_scanner_cur_token (fix->scanner), ==, G_TOKEN_NONE);
     122                 :          1 :   g_scanner_get_next_token (fix->scanner);
     123                 :          1 :   g_assert_cmpint (g_scanner_cur_token (fix->scanner), ==, tokbuf[0]);
     124                 :          1 :   g_assert_cmpint (g_scanner_cur_line (fix->scanner), ==, 1);
     125                 :            : 
     126         [ +  + ]:          5 :   for (i = 1; i < tokbuflen; i++)
     127                 :          4 :     g_assert_cmpint (g_scanner_get_next_token (fix->scanner), ==, tokbuf[i]);
     128                 :          1 :   g_assert_cmpint (g_scanner_get_next_token (fix->scanner), ==, G_TOKEN_EOF);
     129                 :          1 :   return;
     130                 :            : }
     131                 :            : 
     132                 :            : int
     133                 :          2 : main (int   argc,
     134                 :            :       char *argv[])
     135                 :            : {
     136                 :          2 :   g_test_init (&argc, &argv, NULL);
     137                 :            : 
     138                 :          2 :   g_test_add ("/scanner/warn", ScannerFixture, 0, scanner_fixture_setup, test_scanner_warn, scanner_fixture_teardown);
     139                 :          2 :   g_test_add ("/scanner/error", ScannerFixture, 0, scanner_fixture_setup, test_scanner_error, scanner_fixture_teardown);
     140                 :          2 :   g_test_add ("/scanner/symbols", ScannerFixture, 0, scanner_fixture_setup, test_scanner_symbols, scanner_fixture_teardown);
     141                 :          2 :   g_test_add ("/scanner/tokens", ScannerFixture, 0, scanner_fixture_setup, test_scanner_tokens, scanner_fixture_teardown);
     142                 :            : 
     143                 :          2 :   return g_test_run();
     144                 :            : }

Generated by: LCOV version 1.14