AL: Use IKVM.Reflection to get custom attributes from the template assembly,
[mono.git] / eglib / test / markup.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <glib.h>
4 #include "test.h"
5
6 #define do_bad_test(s) do { char *r = markup_test (s); if (r == NULL) return FAILED ("Failed on test " # s); else g_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)
8
9 static char *
10 markup_test (const char *s)
11 {
12         GMarkupParser *parser = g_new0 (GMarkupParser, 1);
13         GMarkupParseContext *context;
14         GError *error = NULL;
15         
16         context = g_markup_parse_context_new (parser, 0, 0, 0);
17
18         g_markup_parse_context_parse (context, s, strlen (s), &error);
19         g_markup_parse_context_free (context);
20
21         if (error != NULL){
22                 char *msg = g_strdup (error->message);
23                 g_error_free (error);
24
25                 g_free (parser);
26                 return msg;
27         }
28         g_free (parser);
29         return NULL;
30 }
31
32 RESULT
33 invalid_documents (void)
34 {
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>");
42         
43         return OK;
44 }
45
46 RESULT
47 valid_documents (void)
48 {
49         /* These should fail */
50         do_ok_test ("<a>");
51         do_ok_test ("<a a=\"b\">");
52         
53         return OK;
54 }
55
56 /*
57  * This is a test for the kind of files that the code in mono/domain.c
58  * parses;  This code comes from Mono
59  */
60 typedef struct {
61         GSList *supported_runtimes;
62         char *required_runtime;
63         int configuration_count;
64         int startup_count;
65 } AppConfigInfo;
66
67 static char *
68 get_attribute_value (const gchar **attribute_names,
69                      const gchar **attribute_values,
70                      const char *att_name)
71 {
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]);
76         }
77         return NULL;
78 }
79
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)
87 {
88         AppConfigInfo* app_config = (AppConfigInfo*) user_data;
89
90         if (strcmp (element_name, "configuration") == 0) {
91                 app_config->configuration_count++;
92                 return;
93         }
94         if (strcmp (element_name, "startup") == 0) {
95                 app_config->startup_count++;
96                 return;
97         }
98
99         if (app_config->configuration_count != 1 || app_config->startup_count != 1)
100                 return;
101
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);
107         }
108 }
109
110 static void
111 end_element   (GMarkupParseContext *context,
112                const gchar         *element_name,
113                gpointer             user_data,
114                GError             **error)
115 {
116         AppConfigInfo* app_config = (AppConfigInfo*) user_data;
117         
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--;
122         }
123 }
124
125 static const GMarkupParser
126 mono_parser = {
127         start_element,
128         end_element,
129         NULL,
130         NULL,
131         NULL
132 };
133
134 AppConfigInfo *
135 domain_test (char *text)
136 {
137         AppConfigInfo *app_config = g_new0 (AppConfigInfo, 1);
138         GMarkupParseContext *context;
139         
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);
143         }
144         g_markup_parse_context_free (context);
145
146         return app_config;
147 }
148
149 void
150 domain_free (AppConfigInfo *info)
151 {
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);
157         }
158         g_slist_free (info->supported_runtimes);
159         g_free (info);
160 }
161
162 RESULT
163 mono_domain (void)
164 {
165         AppConfigInfo *info;
166
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);
173
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);
180
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");
185                 
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);
193
194         return NULL;
195 }
196
197 RESULT
198 mcs_config (void)
199 {
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>");
201
202 }
203
204 RESULT
205 xml_parse (void)
206 {
207         return markup_test ("<?xml version=\"1.0\" encoding=\"utf-8\"?><a></a>");
208 }
209
210 RESULT
211 machine_config (void)
212 {
213         char *data;
214         gsize size;
215         
216         if (g_file_get_contents ("../../data/net_1_1/machine.config", &data, &size, NULL)){
217                 return markup_test (data);
218         }
219         printf ("Ignoring this test\n");
220         return NULL;
221 }
222
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}
231 };
232
233 DEFINE_TEST_GROUP_INIT(markup_tests_init, markup_tests)
234