2 * Copyright © 2007 Ryan Lortie
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * See the included COPYING file for more information.
17 start (GMarkupParseContext *context,
18 const char *element_name,
19 const char **attribute_names,
20 const char **attribute_values,
24 GString *string = user_data;
27 #define collect(...) \
28 g_markup_collect_attributes (element_name, attribute_names, \
29 attribute_values, error, __VA_ARGS__, \
30 G_MARKUP_COLLECT_INVALID)
31 #define BOOL G_MARKUP_COLLECT_BOOLEAN
32 #define OPTBOOL G_MARKUP_COLLECT_BOOLEAN | G_MARKUP_COLLECT_OPTIONAL
33 #define TRI G_MARKUP_COLLECT_TRISTATE
34 #define STR G_MARKUP_COLLECT_STRING
35 #define STRDUP G_MARKUP_COLLECT_STRDUP
36 #define OPTSTR G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL
37 #define OPTDUP G_MARKUP_COLLECT_STRDUP | G_MARKUP_COLLECT_OPTIONAL
38 #define n(x) ((x)?(x):"(null)")
40 if (strcmp (element_name, "bool") == 0)
42 gboolean mb = 2, ob = 2, tri = 2;
44 result = collect (BOOL, "mb", &mb,
49 (mb == FALSE && ob == FALSE && tri != TRUE && tri != FALSE));
51 if (tri != FALSE && tri != TRUE)
54 g_string_append_printf (string, "<bool(%d) %d %d %d>",
58 else if (strcmp (element_name, "str") == 0)
63 result = collect (STR, "cm", &cm,
69 (cm == NULL && am == NULL && ao == NULL && co == NULL));
71 g_string_append_printf (string, "<str(%d) %s %s %s %s>",
72 result, n (cm), n (am), n (ao), n (co));
79 static GMarkupParser parser = { start };
85 GMarkupError error_code;
86 const char *error_info;
89 static struct test tests[] =
91 { "<bool mb='y'>", "<bool(1) 1 0 -1>",
92 G_MARKUP_ERROR_PARSE, "'bool'" },
94 { "<bool mb='false'/>", "<bool(1) 0 0 -1>" },
95 { "<bool mb='true'/>", "<bool(1) 1 0 -1>" },
96 { "<bool mb='t' ob='f' tri='1'/>", "<bool(1) 1 0 1>" },
97 { "<bool mb='y' ob='n' tri='0'/>", "<bool(1) 1 0 0>" },
99 { "<bool ob='y'/>", "<bool(0) 0 0 -1>",
100 G_MARKUP_ERROR_MISSING_ATTRIBUTE, "'mb'" },
102 { "<bool mb='y' mb='y'/>", "<bool(0) 0 0 -1>",
103 G_MARKUP_ERROR_INVALID_CONTENT, "'mb'" },
105 { "<bool mb='y' tri='y' tri='n'/>", "<bool(0) 0 0 -1>",
106 G_MARKUP_ERROR_INVALID_CONTENT, "'tri'" },
108 { "<str cm='x' am='y'/>", "<str(1) x y (null) (null)>" },
110 { "<str am='x' co='y'/>", "<str(0) (null) (null) (null) (null)>",
111 G_MARKUP_ERROR_MISSING_ATTRIBUTE, "'cm'" },
113 { "<str am='x'/>", "<str(0) (null) (null) (null) (null)>",
114 G_MARKUP_ERROR_MISSING_ATTRIBUTE, "'cm'" },
116 { "<str am='x' cm='x' am='y'/>", "<str(0) (null) (null) (null) (null)>",
117 G_MARKUP_ERROR_INVALID_CONTENT, "'am'" },
119 { "<str am='x' qm='y' cm='x'/>", "<str(0) (null) (null) (null) (null)>",
120 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE, "'qm'" },
122 { "<str am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' cm='x'/>", "<str(0) (null) (null) (null) (null)>",
123 G_MARKUP_ERROR_INVALID_CONTENT, "'am'" },
125 { "<str cm='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x' am='x'/>", "<str(0) (null) (null) (null) (null)>",
126 G_MARKUP_ERROR_INVALID_CONTENT, "'am'" },
128 { "<str a='x' b='x' c='x' d='x' e='x' f='x' g='x' h='x' i='x' j='x' k='x' l='x' m='x' n='x' o='x' p='x' q='x' r='x' s='x' t='x' u='x' v='x' w='x' x='x' y='x' z='x' aa='x' bb='x' cc='x' dd='x' ee='x' ff='x' gg='x' hh='x' ii='x' jj='x' kk='x' ll='x' mm='x' nn='x' oo='x' pp='x' qq='x' rr='x' ss='x' tt='x' uu='x' vv='x' ww='x' xx='x' yy='x' zz='x' am='x' cm='x'/>",
129 "<str(0) (null) (null) (null) (null)>",
130 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE, "'a'" },
132 { "<bool mb='ja'/>", "<bool(0) 0 0 -1>",
133 G_MARKUP_ERROR_INVALID_CONTENT, "'mb'" },
135 { "<bool mb='nein'/>", "<bool(0) 0 0 -1>",
136 G_MARKUP_ERROR_INVALID_CONTENT, "'mb'" }
140 test_collect (gconstpointer d)
142 const struct test *test = d;
144 GMarkupParseContext *ctx;
145 GError *error = NULL;
149 string = g_string_new ("");
150 ctx = g_markup_parse_context_new (&parser, 0, string, NULL);
151 result = g_markup_parse_context_parse (ctx,
155 result = g_markup_parse_context_end_parse (ctx, &error);
159 g_assert_no_error (error);
160 g_assert_cmpint (test->error_code, ==, 0);
161 g_assert_cmpstr (test->result, ==, string->str);
165 g_assert_error (error, G_MARKUP_ERROR, test->error_code);
168 g_markup_parse_context_free (ctx);
169 g_string_free (string, TRUE);
170 g_clear_error (&error);
173 #define XML "<element a='1' b='2' c='3'/>"
176 start_element (GMarkupParseContext *context,
177 const gchar *element_name,
178 const gchar **attribute_names,
179 const gchar **attribute_values,
183 /* Omitting "c" attribute intentionally to trigger crash. */
184 g_markup_collect_attributes (element_name,
188 G_MARKUP_COLLECT_STRING, "a", NULL,
189 G_MARKUP_COLLECT_STRING, "b", NULL,
190 G_MARKUP_COLLECT_INVALID);
193 static GMarkupParser cleanup_parser = {
200 GMarkupParseContext *context;
202 if (!g_test_undefined ())
205 context = g_markup_parse_context_new (&cleanup_parser, 0, NULL, NULL);
206 g_markup_parse_context_parse (context, XML, -1, NULL);
208 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
209 "g_markup_parse_context_end_parse: assertion 'context->state != STATE_ERROR' failed");
210 g_markup_parse_context_end_parse (context, NULL);
211 g_test_assert_expected_messages ();
213 g_markup_parse_context_free (context);
217 main (int argc, char **argv)
222 g_test_init (&argc, &argv, NULL);
224 for (i = 0; i < G_N_ELEMENTS (tests); i++)
226 path = g_strdup_printf ("/markup/collect/%d", i);
227 g_test_add_data_func (path, &tests[i], test_collect);
231 g_test_add_func ("/markup/collect/cleanup", test_cleanup);
233 return g_test_run ();