summaryrefslogtreecommitdiff
path: root/glib/tests/markup.c
blob: 8533f061989d957d44ee27bccb707c1bdefc12b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* Unit tests for GMarkup
 * Copyright (C) 2013 Red Hat, Inc.
 *
 * SPDX-License-Identifier: LicenseRef-old-glib-tests
 *
 * This work is provided "as is"; redistribution and modification
 * in whole or in part, in any medium, physical or electronic is
 * permitted without restriction.
 *
 * This work 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.
 *
 * In no event shall the authors or contributors be liable for any
 * direct, indirect, incidental, special, exemplary, or consequential
 * damages (including, but not limited to, procurement of substitute
 * goods or services; loss of use, data, or profits; or business
 * interruption) however caused and on any theory of liability, whether
 * in contract, strict liability, or tort (including negligence or
 * otherwise) arising in any way out of the use of this software, even
 * if advised of the possibility of such damage.
 *
 * Author: Matthias Clasen
 */

#include "glib.h"

typedef struct {
  GSList *stack;
} ParseData;

static void
start (GMarkupParseContext  *context,
       const gchar          *element_name,
       const gchar         **attribute_names,
       const gchar         **attribute_values,
       gpointer              user_data,
       GError              **error)
{
  ParseData *data = user_data;

  data->stack = g_slist_prepend (data->stack, g_strdup (element_name));
}

static void
end (GMarkupParseContext  *context,
     const gchar          *element_name,
     gpointer              user_data,
     GError              **error)
{
  ParseData *data = user_data;
  const GSList *stack;
  const GSList *s1, *s2;
  GSList *s;
  
  stack = g_markup_parse_context_get_element_stack (context);
  for (s1 = stack, s2 = data->stack; s1 && s2; s1 = s1->next, s2 = s2->next)
    g_assert_cmpstr (s1->data, ==, s2->data);
  g_assert (s1 == NULL && s2 == NULL);

  s = data->stack;
  data->stack = data->stack->next;
  s->next = NULL;
  g_slist_free_full (s, g_free);
}

const gchar content[] = 
 "<e1><foo><bar></bar> bla <l>fff</l></foo></e1>";

static void
test_markup_stack (void)
{
  GMarkupParser parser = {
    start,
    end,
    NULL,
    NULL,
    NULL
  };
  GMarkupParseContext *context;
  ParseData data = { NULL };
  gboolean res;
  GError *error = NULL;

  context = g_markup_parse_context_new (&parser, G_MARKUP_DEFAULT_FLAGS, &data, NULL);
  res = g_markup_parse_context_parse (context, content, -1, &error);
  g_assert (res);
  g_assert_no_error (error);
  g_markup_parse_context_free (context);
}

int
main (int argc, char *argv[])
{
  g_test_init (&argc, &argv, NULL);

  g_test_add_func ("/markup/stack", test_markup_stack);

  return g_test_run ();
}