tests: add a test for the previous commit
[platform/upstream/glib.git] / glib / tests / markup-collect.c
1 /* 
2  * Copyright © 2007 Ryan Lortie
3  * 
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.
8  * 
9  * See the included COPYING file for more information.
10  */
11
12 #include <stdlib.h>
13 #include <string.h>
14 #include <glib.h>
15
16 static void
17 start (GMarkupParseContext  *context,
18        const char           *element_name,
19        const char          **attribute_names,
20        const char          **attribute_values,
21        gpointer              user_data,
22        GError              **error)
23 {
24   GString *string = user_data;
25   gboolean result;
26
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)")
39
40   if (strcmp (element_name, "bool") == 0)
41     {
42       gboolean mb = 2, ob = 2, tri = 2;
43
44       result = collect (BOOL,    "mb", &mb,
45                         OPTBOOL, "ob", &ob,
46                         TRI,     "tri", &tri);
47
48       g_assert (result ||
49                 (mb == FALSE && ob == FALSE && tri != TRUE && tri != FALSE));
50
51       if (tri != FALSE && tri != TRUE)
52         tri = -1;
53
54       g_string_append_printf (string, "<bool(%d) %d %d %d>",
55                               result, mb, ob, tri);
56     }
57
58   else if (strcmp (element_name, "str") == 0)
59     {
60       const char *cm, *co;
61       char *am, *ao;
62
63       result = collect (STR,    "cm", &cm,
64                         STRDUP, "am", &am,
65                         OPTDUP, "ao", &ao,
66                         OPTSTR, "co", &co);
67
68       g_assert (result ||
69                 (cm == NULL && am == NULL && ao == NULL && co == NULL));
70
71       g_string_append_printf (string, "<str(%d) %s %s %s %s>",
72                               result, n (cm), n (am), n (ao), n (co));
73
74       g_free (am);
75       g_free (ao);
76     }
77 }
78
79 static GMarkupParser parser = { start };
80
81 struct test
82 {
83   const char   *document;
84   const char   *result;
85   GMarkupError  error_code;
86   const char   *error_info;
87 };
88
89 static struct test tests[] =
90 {
91   { "<bool mb='y'>", "<bool(1) 1 0 -1>",
92     G_MARKUP_ERROR_PARSE, "'bool'" },
93
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>" },
98
99   { "<bool ob='y'/>", "<bool(0) 0 0 -1>",
100     G_MARKUP_ERROR_MISSING_ATTRIBUTE, "'mb'" },
101
102   { "<bool mb='y' mb='y'/>", "<bool(0) 0 0 -1>",
103     G_MARKUP_ERROR_INVALID_CONTENT, "'mb'" },
104
105   { "<bool mb='y' tri='y' tri='n'/>", "<bool(0) 0 0 -1>",
106     G_MARKUP_ERROR_INVALID_CONTENT, "'tri'" },
107
108   { "<str cm='x' am='y'/>", "<str(1) x y (null) (null)>" },
109
110   { "<str am='x' co='y'/>", "<str(0) (null) (null) (null) (null)>",
111     G_MARKUP_ERROR_MISSING_ATTRIBUTE, "'cm'" },
112
113   { "<str am='x'/>", "<str(0) (null) (null) (null) (null)>",
114     G_MARKUP_ERROR_MISSING_ATTRIBUTE, "'cm'" },
115
116   { "<str am='x' cm='x' am='y'/>", "<str(0) (null) (null) (null) (null)>",
117     G_MARKUP_ERROR_INVALID_CONTENT, "'am'" },
118
119   { "<str am='x' qm='y' cm='x'/>", "<str(0) (null) (null) (null) (null)>",
120     G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE, "'qm'" },
121
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'" },
124
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'" },
127
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'" },
131
132   { "<bool mb='ja'/>", "<bool(0) 0 0 -1>",
133     G_MARKUP_ERROR_INVALID_CONTENT, "'mb'" },
134
135   { "<bool mb='nein'/>", "<bool(0) 0 0 -1>",
136     G_MARKUP_ERROR_INVALID_CONTENT, "'mb'" }
137 };
138
139 static void
140 test_collect (gconstpointer d)
141 {
142   const struct test *test = d;
143
144   GMarkupParseContext *ctx;
145   GError *error = NULL;
146   GString *string;
147   gboolean result;
148
149   string = g_string_new ("");
150   ctx = g_markup_parse_context_new (&parser, 0, string, NULL);
151   result = g_markup_parse_context_parse (ctx,
152                                          test->document,
153                                          -1, &error);
154   if (result)
155     result = g_markup_parse_context_end_parse (ctx, &error);
156
157   if (result)
158     {
159       g_assert_no_error (error);
160       g_assert_cmpint (test->error_code, ==, 0);
161       g_assert_cmpstr (test->result, ==, string->str);
162     }
163   else
164     {
165       g_assert_error (error, G_MARKUP_ERROR, test->error_code);
166     }
167
168   g_markup_parse_context_free (ctx);
169   g_string_free (string, TRUE);
170   g_clear_error (&error);
171 }
172
173 #define XML "<element a='1' b='2' c='3'/>"
174
175 static void
176 start_element (GMarkupParseContext  *context,
177                const gchar          *element_name,
178                const gchar         **attribute_names,
179                const gchar         **attribute_values,
180                gpointer              user_data,
181                GError              **error)
182 {
183   /* Omitting "c" attribute intentionally to trigger crash. */
184   g_markup_collect_attributes (element_name,
185                                attribute_names,
186                                attribute_values,
187                                error,
188                                G_MARKUP_COLLECT_STRING, "a", NULL,
189                                G_MARKUP_COLLECT_STRING, "b", NULL,
190                                G_MARKUP_COLLECT_INVALID);
191 }
192
193 static GMarkupParser cleanup_parser = {
194   start_element
195 };
196
197 static void
198 test_cleanup (void)
199 {
200   GMarkupParseContext *context;
201
202   if (!g_test_undefined ())
203     return;
204
205   context = g_markup_parse_context_new (&cleanup_parser, 0, NULL, NULL);
206   g_markup_parse_context_parse (context, XML, -1, NULL);
207
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 ();
212
213   g_markup_parse_context_free (context);
214 }
215
216 int
217 main (int argc, char **argv)
218 {
219   int i;
220   gchar *path;
221
222   g_test_init (&argc, &argv, NULL);
223
224   for (i = 0; i < G_N_ELEMENTS (tests); i++)
225     {
226       path = g_strdup_printf ("/markup/collect/%d", i);
227       g_test_add_data_func (path, &tests[i], test_collect);
228       g_free (path);
229     }
230
231   g_test_add_func ("/markup/collect/cleanup", test_cleanup);
232
233   return g_test_run ();
234 }