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

source code of gtk/subprojects/glib/glib/tests/markup.c