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.
16 start (GMarkupParseContext *context,
17 const char *element_name,
18 const char **attribute_names,
19 const char **attribute_values,
23 GString *string = user_data;
26 #define collect(...) \
27 g_markup_collect_attributes (element_name, attribute_names, \
28 attribute_values, error, __VA_ARGS__, \
29 G_MARKUP_COLLECT_INVALID)
30 #define BOOL G_MARKUP_COLLECT_BOOLEAN
31 #define OPTBOOL G_MARKUP_COLLECT_BOOLEAN | G_MARKUP_COLLECT_OPTIONAL
32 #define TRI G_MARKUP_COLLECT_TRISTATE
33 #define STR G_MARKUP_COLLECT_STRING
34 #define STRDUP G_MARKUP_COLLECT_STRDUP
35 #define OPTSTR G_MARKUP_COLLECT_STRING | G_MARKUP_COLLECT_OPTIONAL
36 #define OPTDUP G_MARKUP_COLLECT_STRDUP | G_MARKUP_COLLECT_OPTIONAL
37 #define n(x) ((x)?(x):"(null)")
39 if (strcmp (element_name, "bool") == 0)
41 gboolean mb = 2, ob = 2, tri = 2;
43 result = collect (BOOL, "mb", &mb,
48 (mb == FALSE && ob == FALSE && tri != TRUE && tri != FALSE));
50 if (tri != FALSE && tri != TRUE)
53 g_string_append_printf (string, "<bool(%d) %d %d %d>",
57 else if (strcmp (element_name, "str") == 0)
62 result = collect (STR, "cm", &cm,
68 (cm == NULL && am == NULL && ao == NULL && co == NULL));
70 g_string_append_printf (string, "<str(%d) %s %s %s %s>",
71 result, n (cm), n (am), n (ao), n (co));
78 static GMarkupParser parser = { start };
84 GMarkupError error_code;
85 const char *error_info;
88 static struct test tests[] =
90 { "<bool mb='y'>", "<bool(1) 1 0 -1>",
91 G_MARKUP_ERROR_PARSE, "'bool'" },
93 { "<bool mb='false'/>", "<bool(1) 0 0 -1>" },
94 { "<bool mb='true'/>", "<bool(1) 1 0 -1>" },
95 { "<bool mb='t' ob='f' tri='1'/>", "<bool(1) 1 0 1>" },
96 { "<bool mb='y' ob='n' tri='0'/>", "<bool(1) 1 0 0>" },
98 { "<bool ob='y'/>", "<bool(0) 0 0 -1>",
99 G_MARKUP_ERROR_MISSING_ATTRIBUTE, "'mb'" },
101 { "<bool mb='y' mb='y'/>", "<bool(0) 0 0 -1>",
102 G_MARKUP_ERROR_INVALID_CONTENT, "'mb'" },
104 { "<bool mb='y' tri='y' tri='n'/>", "<bool(0) 0 0 -1>",
105 G_MARKUP_ERROR_INVALID_CONTENT, "'tri'" },
107 { "<str cm='x' am='y'/>", "<str(1) x y (null) (null)>" },
109 { "<str am='x' co='y'/>", "<str(0) (null) (null) (null) (null)>",
110 G_MARKUP_ERROR_MISSING_ATTRIBUTE, "'cm'" },
112 { "<str am='x'/>", "<str(0) (null) (null) (null) (null)>",
113 G_MARKUP_ERROR_MISSING_ATTRIBUTE, "'cm'" },
115 { "<str am='x' cm='x' am='y'/>", "<str(0) (null) (null) (null) (null)>",
116 G_MARKUP_ERROR_INVALID_CONTENT, "'am'" },
118 { "<str am='x' qm='y' cm='x'/>", "<str(0) (null) (null) (null) (null)>",
119 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE, "'qm'" },
121 { "<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)>",
122 G_MARKUP_ERROR_INVALID_CONTENT, "'am'" },
124 { "<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)>",
125 G_MARKUP_ERROR_INVALID_CONTENT, "'am'" },
127 { "<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'/>",
128 "<str(0) (null) (null) (null) (null)>",
129 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE, "'a'" },
131 { "<bool mb='ja'/>", "<bool(0) 0 0 -1>",
132 G_MARKUP_ERROR_INVALID_CONTENT, "'mb'" },
134 { "<bool mb='nein'/>", "<bool(0) 0 0 -1>",
135 G_MARKUP_ERROR_INVALID_CONTENT, "'mb'" }
139 main (int argc, char **argv)
141 gboolean verbose = FALSE;
146 if (argc != 2 || strcmp (argv[1], "-v") != 0)
148 g_print ("error: call with no arguments or '-v' for verbose\n");
155 for (i = 0; i < G_N_ELEMENTS (tests); i++)
157 GMarkupParseContext *ctx;
158 GError *error = NULL;
162 string = g_string_new ("");
163 ctx = g_markup_parse_context_new (&parser, 0, string, NULL);
164 result = g_markup_parse_context_parse (ctx,
168 result = g_markup_parse_context_end_parse (ctx, &error);
171 g_print ("%d: %s:\n (error %d, \"%s\")\n %s\n\n",
172 i, tests[i].document,
173 error ? error->code : 0,
174 error ? error->message : "(no error)",
180 g_error ("parser successful but error is set: "
181 "%s(%d) '%s'", g_quark_to_string (error->domain),
182 error->code, error->message);
184 if (tests[i].error_code != 0)
185 g_error ("parser succeeded on test %d ('%s') but "
186 "we expected a failure with code %d\n", i,
187 tests[i].document, tests[i].error_code);
191 if (error->domain != G_MARKUP_ERROR)
192 g_error ("error occured on test %d ('%s') but is not in "
193 "the GMarkupError domain, but rather '%s'", i,
194 tests[i].document, g_quark_to_string (error->domain));
196 if (error->code != tests[i].error_code)
197 g_error ("failure expected with test %d ('%s') but it "
198 "has error code %d (we expected code %d)", i,
199 tests[i].document, error->code, tests[i].error_code);
201 if (strstr (error->message, tests[i].error_info) == NULL)
202 g_error ("failure message on test %d ('%s') fails "
203 "to mention '%s' in the error message", i,
204 tests[i].document, tests[i].error_info);
207 if (strcmp (tests[i].result, string->str) != 0)
208 g_error ("result on test %d ('%s') expected to be '%s' "
209 "but came out as '%s'", i, tests[i].document,
210 tests[i].result, string->str);
212 g_markup_parse_context_free (ctx);
213 g_string_free (string, TRUE);
214 g_clear_error (&error);
218 g_print ("\n*** all tests passed ***\n\n");