LCOV - code coverage report
Current view: top level - glib/glib/tests - markup.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 29 29 100.0 %
Date: 2024-04-23 05:16:05 Functions: 4 4 100.0 %
Branches: 3 4 75.0 %

           Branch data     Line data    Source code
       1                 :            : /* Unit tests for GMarkup
       2                 :            :  * Copyright (C) 2013 Red Hat, Inc.
       3                 :            :  *
       4                 :            :  * SPDX-License-Identifier: LicenseRef-old-glib-tests
       5                 :            :  *
       6                 :            :  * This work is provided "as is"; redistribution and modification
       7                 :            :  * in whole or in part, in any medium, physical or electronic is
       8                 :            :  * permitted without restriction.
       9                 :            :  *
      10                 :            :  * This work is distributed in the hope that it will be useful,
      11                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      12                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
      13                 :            :  *
      14                 :            :  * In no event shall the authors or contributors be liable for any
      15                 :            :  * direct, indirect, incidental, special, exemplary, or consequential
      16                 :            :  * damages (including, but not limited to, procurement of substitute
      17                 :            :  * goods or services; loss of use, data, or profits; or business
      18                 :            :  * interruption) however caused and on any theory of liability, whether
      19                 :            :  * in contract, strict liability, or tort (including negligence or
      20                 :            :  * otherwise) arising in any way out of the use of this software, even
      21                 :            :  * if advised of the possibility of such damage.
      22                 :            :  *
      23                 :            :  * Author: Matthias Clasen
      24                 :            :  */
      25                 :            : 
      26                 :            : #include "glib.h"
      27                 :            : 
      28                 :            : typedef struct {
      29                 :            :   GSList *stack;
      30                 :            : } ParseData;
      31                 :            : 
      32                 :            : static void
      33                 :          4 : start (GMarkupParseContext  *context,
      34                 :            :        const gchar          *element_name,
      35                 :            :        const gchar         **attribute_names,
      36                 :            :        const gchar         **attribute_values,
      37                 :            :        gpointer              user_data,
      38                 :            :        GError              **error)
      39                 :            : {
      40                 :          4 :   ParseData *data = user_data;
      41                 :            : 
      42                 :          4 :   data->stack = g_slist_prepend (data->stack, g_strdup (element_name));
      43                 :          4 : }
      44                 :            : 
      45                 :            : static void
      46                 :          4 : end (GMarkupParseContext  *context,
      47                 :            :      const gchar          *element_name,
      48                 :            :      gpointer              user_data,
      49                 :            :      GError              **error)
      50                 :            : {
      51                 :          4 :   ParseData *data = user_data;
      52                 :            :   const GSList *stack;
      53                 :            :   const GSList *s1, *s2;
      54                 :            :   GSList *s;
      55                 :            :   
      56                 :          4 :   stack = g_markup_parse_context_get_element_stack (context);
      57   [ +  +  +  - ]:         13 :   for (s1 = stack, s2 = data->stack; s1 && s2; s1 = s1->next, s2 = s2->next)
      58                 :          9 :     g_assert_cmpstr (s1->data, ==, s2->data);
      59                 :          4 :   g_assert (s1 == NULL && s2 == NULL);
      60                 :            : 
      61                 :          4 :   s = data->stack;
      62                 :          4 :   data->stack = data->stack->next;
      63                 :          4 :   s->next = NULL;
      64                 :          4 :   g_slist_free_full (s, g_free);
      65                 :          4 : }
      66                 :            : 
      67                 :            : const gchar content[] = 
      68                 :            :  "<e1><foo><bar></bar> bla <l>fff</l></foo></e1>";
      69                 :            : 
      70                 :            : static void
      71                 :          1 : test_markup_stack (void)
      72                 :            : {
      73                 :          1 :   GMarkupParser parser = {
      74                 :            :     start,
      75                 :            :     end,
      76                 :            :     NULL,
      77                 :            :     NULL,
      78                 :            :     NULL
      79                 :            :   };
      80                 :            :   GMarkupParseContext *context;
      81                 :          1 :   ParseData data = { NULL };
      82                 :            :   gboolean res;
      83                 :          1 :   GError *error = NULL;
      84                 :            : 
      85                 :          1 :   context = g_markup_parse_context_new (&parser, G_MARKUP_DEFAULT_FLAGS, &data, NULL);
      86                 :          1 :   res = g_markup_parse_context_parse (context, content, -1, &error);
      87                 :          1 :   g_assert (res);
      88                 :          1 :   g_assert_no_error (error);
      89                 :          1 :   g_markup_parse_context_free (context);
      90                 :          1 : }
      91                 :            : 
      92                 :            : int
      93                 :          1 : main (int argc, char *argv[])
      94                 :            : {
      95                 :          1 :   g_test_init (&argc, &argv, NULL);
      96                 :            : 
      97                 :          1 :   g_test_add_func ("/markup/stack", test_markup_stack);
      98                 :            : 
      99                 :          1 :   return g_test_run ();
     100                 :            : }

Generated by: LCOV version 1.14