2010-04-16 Sebastien Pouliot <sebastien@ximian.com>
[mono/afaerber.git] / eglib / test / markup.c
blob2d80fb819571a621bc9903471d6b42923c302b37
1 #include <stdio.h>
2 #include <string.h>
3 #include <glib.h>
4 #include "test.h"
6 #define do_bad_test(s) do { char *r = markup_test (s); if (r == NULL) return FAILED ("Failed on test " # s); else free (r); } while (0)
7 #define do_ok_test(s) do { char *r = markup_test (s); if (r != NULL) return FAILED ("Could not parse valid " # s); } while (0)
9 static char *
10 markup_test (const char *s)
12 GMarkupParser *parser = g_new0 (GMarkupParser, 1);
13 GMarkupParseContext *context;
14 GError *error = NULL;
16 context = g_markup_parse_context_new (parser, 0, 0, 0);
18 g_markup_parse_context_parse (context, s, strlen (s), &error);
19 g_markup_parse_context_free (context);
21 if (error != NULL){
22 char *msg = g_strdup (error->message);
23 g_error_free (error);
25 g_free (parser);
26 return msg;
28 g_free (parser);
29 return NULL;
32 RESULT
33 invalid_documents (void)
35 /* These should fail */
36 do_bad_test ("<1>");
37 do_bad_test ("<a<");
38 do_bad_test ("</a>");
39 do_bad_test ("<a b>");
40 do_bad_test ("<a b=>");
41 do_bad_test ("<a b=c>");
43 return OK;
46 RESULT
47 valid_documents (void)
49 /* These should fail */
50 do_ok_test ("<a>");
51 do_ok_test ("<a a=\"b\">");
53 return OK;
57 * This is a test for the kind of files that the code in mono/domain.c
58 * parses; This code comes from Mono
60 typedef struct {
61 GSList *supported_runtimes;
62 char *required_runtime;
63 int configuration_count;
64 int startup_count;
65 } AppConfigInfo;
67 static char *
68 get_attribute_value (const gchar **attribute_names,
69 const gchar **attribute_values,
70 const char *att_name)
72 int n;
73 for (n=0; attribute_names[n] != NULL; n++) {
74 if (strcmp (attribute_names[n], att_name) == 0)
75 return g_strdup (attribute_values[n]);
77 return NULL;
80 static void
81 start_element (GMarkupParseContext *context,
82 const gchar *element_name,
83 const gchar **attribute_names,
84 const gchar **attribute_values,
85 gpointer user_data,
86 GError **error)
88 AppConfigInfo* app_config = (AppConfigInfo*) user_data;
90 if (strcmp (element_name, "configuration") == 0) {
91 app_config->configuration_count++;
92 return;
94 if (strcmp (element_name, "startup") == 0) {
95 app_config->startup_count++;
96 return;
99 if (app_config->configuration_count != 1 || app_config->startup_count != 1)
100 return;
102 if (strcmp (element_name, "requiredRuntime") == 0) {
103 app_config->required_runtime = get_attribute_value (attribute_names, attribute_values, "version");
104 } else if (strcmp (element_name, "supportedRuntime") == 0) {
105 char *version = get_attribute_value (attribute_names, attribute_values, "version");
106 app_config->supported_runtimes = g_slist_append (app_config->supported_runtimes, version);
110 static void
111 end_element (GMarkupParseContext *context,
112 const gchar *element_name,
113 gpointer user_data,
114 GError **error)
116 AppConfigInfo* app_config = (AppConfigInfo*) user_data;
118 if (strcmp (element_name, "configuration") == 0) {
119 app_config->configuration_count--;
120 } else if (strcmp (element_name, "startup") == 0) {
121 app_config->startup_count--;
125 static const GMarkupParser
126 mono_parser = {
127 start_element,
128 end_element,
129 NULL,
130 NULL,
131 NULL
134 AppConfigInfo *
135 domain_test (char *text)
137 AppConfigInfo *app_config = g_new0 (AppConfigInfo, 1);
138 GMarkupParseContext *context;
140 context = g_markup_parse_context_new (&mono_parser, 0, app_config, NULL);
141 if (g_markup_parse_context_parse (context, text, strlen (text), NULL)) {
142 g_markup_parse_context_end_parse (context, NULL);
144 g_markup_parse_context_free (context);
146 return app_config;
149 void
150 domain_free (AppConfigInfo *info)
152 GSList *l;
153 if (info->required_runtime)
154 g_free (info->required_runtime);
155 for (l = info->supported_runtimes; l != NULL; l = l->next){
156 g_free (l->data);
158 g_slist_free (info->supported_runtimes);
159 g_free (info);
162 RESULT
163 mono_domain (void)
165 AppConfigInfo *info;
167 info = domain_test ("<configuration><!--hello--><startup><!--world--><requiredRuntime version=\"v1\"><!--r--></requiredRuntime></startup></configuration>");
168 if (info->required_runtime == NULL)
169 return FAILED ("No required runtime section");
170 if (strcmp (info->required_runtime, "v1") != 0)
171 return FAILED ("Got a runtime version %s, expected v1", info->required_runtime);
172 domain_free (info);
174 info = domain_test ("<configuration><startup><requiredRuntime version=\"v1\"/><!--comment--></configuration><!--end-->");
175 if (info->required_runtime == NULL)
176 return FAILED ("No required runtime section on auto-close section");
177 if (strcmp (info->required_runtime, "v1") != 0)
178 return FAILED ("Got a runtime version %s, expected v1", info->required_runtime);
179 domain_free (info);
181 info = domain_test ("<!--start--><configuration><startup><supportedRuntime version=\"v1\"/><!--middle--><supportedRuntime version=\"v2\"/></startup></configuration>");
182 if ((strcmp ((char*)info->supported_runtimes->data, "v1") == 0)){
183 if (info->supported_runtimes->next == NULL)
184 return FAILED ("Expected 2 supported runtimes");
186 if ((strcmp ((char*)info->supported_runtimes->next->data, "v2") != 0))
187 return FAILED ("Expected v1, v2, got %s", info->supported_runtimes->next->data);
188 if (info->supported_runtimes->next->next != NULL)
189 return FAILED ("Expected v1, v2, got more");
190 } else
191 return FAILED ("Expected `v1', got %s", info->supported_runtimes->data);
192 domain_free (info);
194 return NULL;
197 RESULT
198 mcs_config (void)
200 return markup_test ("<configuration>\r\n <system.diagnostics>\r\n <trace autoflush=\"true\" indentsize=\"4\">\r\n <listeners>\r\n <add name=\"compilerLogListener\" type=\"System.Diagnostics.TextWriterTraceListener,System\"/> </listeners> </trace> </system.diagnostics> </configuration>");
204 RESULT
205 xml_parse (void)
207 return markup_test ("<?xml version=\"1.0\" encoding=\"utf-8\"?><a></a>");
210 RESULT
211 machine_config (void)
213 char *data;
214 gsize size;
216 if (g_file_get_contents ("../../data/net_1_1/machine.config", &data, &size, NULL)){
217 return markup_test (data);
219 printf ("Ignoring this test\n");
220 return NULL;
223 static Test markup_tests [] = {
224 {"invalid_documents", invalid_documents},
225 {"good_documents", valid_documents},
226 {"mono_domain", mono_domain},
227 {"mcs_config", mcs_config},
228 {"xml_parse", xml_parse},
229 {"machine_config", machine_config},
230 {NULL, NULL}
233 DEFINE_TEST_GROUP_INIT(markup_tests_init, markup_tests)