2010-05-14 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / eglib / test / shell.c
blob38252cd2e0e196db8ddd90c53d435fa5ec0829bd
1 #include <glib.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include "test.h"
6 RESULT
7 test_shell_argv1 ()
9 GError *error;
10 gint argc;
11 gchar **argv;
12 gboolean ret;
14 /* The next line prints a critical error and returns FALSE
15 ret = g_shell_parse_argv (NULL, NULL, NULL, NULL);
17 ret = g_shell_parse_argv ("", NULL, NULL, NULL);
18 if (ret)
19 return FAILED ("1. It should return FALSE");
21 ret = g_shell_parse_argv ("hola", NULL, NULL, NULL);
22 if (!ret)
23 return FAILED ("2. It should return TRUE");
25 argc = 0;
26 ret = g_shell_parse_argv ("hola", &argc, NULL, NULL);
27 if (!ret)
28 return FAILED ("3. It should return TRUE");
29 if (argc != 1)
30 return FAILED ("4. argc was %d", argc);
32 argc = 0;
33 ret = g_shell_parse_argv ("hola bola", &argc, NULL, NULL);
34 if (!ret)
35 return FAILED ("5. It should return TRUE");
36 if (argc != 2)
37 return FAILED ("6. argc was %d", argc);
39 argc = 0;
40 ret = g_shell_parse_argv ("hola bola", &argc, &argv, NULL);
41 if (!ret)
42 return FAILED ("7. It should return TRUE");
43 if (argc != 2)
44 return FAILED ("8. argc was %d", argc);
45 if (strcmp (argv [0], "hola"))
46 return FAILED ("9. argv[0] was %s", argv [0]);
47 if (strcmp (argv [1], "bola"))
48 return FAILED ("10. argv[1] was %s", argv [1]);
50 g_strfreev (argv);
51 argv = NULL;
52 argc = 0;
53 error = NULL;
54 ret = g_shell_parse_argv ("hola 'bola'", &argc, &argv, &error);
55 if (!ret)
56 return FAILED ("11. It should return TRUE");
57 if (argc != 2)
58 return FAILED ("12. argc was %d", argc);
59 if (strcmp (argv [0], "hola"))
60 return FAILED ("13. argv[0] was %s", argv [0]);
61 if (strcmp (argv [1], "bola"))
62 return FAILED ("14. argv[1] was %s", argv [1]);
63 if (error != NULL)
64 return FAILED ("15. error is not null");
67 g_strfreev (argv);
68 argv = NULL;
69 argc = 0;
70 error = NULL;
71 ret = g_shell_parse_argv ("hola '' 'bola'", &argc, &argv, &error);
72 if (!ret)
73 return FAILED ("16. It should return TRUE");
74 if (argc != 3)
75 return FAILED ("17. argc was %d", argc);
76 if (strcmp (argv [0], "hola"))
77 return FAILED ("18. argv[0] was %s", argv [0]);
78 if (strcmp (argv [1], ""))
79 return FAILED ("19. argv[2] was %s", argv [1]);
80 if (strcmp (argv [2], "bola"))
81 return FAILED ("19. argv[2] was %s", argv [2]);
82 if (error != NULL)
83 return FAILED ("20. error is not null");
85 g_strfreev (argv);
86 argv = NULL;
87 argc = 0;
88 return OK;
91 RESULT
92 test_shell_argv2 ()
94 GError *error;
95 gint argc;
96 gchar **argv;
97 gboolean ret;
99 argv = NULL;
100 argc = 0;
101 error = NULL;
102 ret = g_shell_parse_argv ("hola \"bola\"", &argc, &argv, &error);
103 if (!ret)
104 return FAILED ("1. It should return TRUE");
105 if (argc != 2)
106 return FAILED ("2. argc was %d", argc);
107 if (strcmp (argv [0], "hola"))
108 return FAILED ("3. argv[0] was %s", argv [0]);
109 if (strcmp (argv [1], "bola"))
110 return FAILED ("4. argv[1] was %s", argv [1]);
111 if (error != NULL)
112 return FAILED ("5. error is not null");
114 g_strfreev (argv);
115 argv = NULL;
116 argc = 0;
117 error = NULL;
118 ret = g_shell_parse_argv ("hola \"\" \"bola \"", &argc, &argv, &error);
119 if (!ret)
120 return FAILED ("6. It should return TRUE");
121 if (argc != 3)
122 return FAILED ("7. argc was %d", argc);
123 if (strcmp (argv [0], "hola"))
124 return FAILED ("8. argv[0] was %s", argv [0]);
125 if (strcmp (argv [1], ""))
126 return FAILED ("9. argv[2] was %s", argv [1]);
127 if (strcmp (argv [2], "bola "))
128 return FAILED ("10. argv[2] was %s", argv [2]);
129 if (error != NULL)
130 return FAILED ("11. error is not null");
132 g_strfreev (argv);
133 argv = NULL;
134 argc = 0;
135 error = NULL;
136 ret = g_shell_parse_argv ("hola\n\t \"\t\" \"bola \"", &argc, &argv, &error);
137 if (!ret)
138 return FAILED ("10. It should return TRUE");
139 if (argc != 3)
140 return FAILED ("11. argc was %d", argc);
141 if (strcmp (argv [0], "hola"))
142 return FAILED ("12. argv[0] was %s", argv [0]);
143 if (strcmp (argv [1], "\t"))
144 return FAILED ("13. argv[2] was %s", argv [1]);
145 if (strcmp (argv [2], "bola "))
146 return FAILED ("14. argv[2] was %s", argv [2]);
147 if (error != NULL)
148 return FAILED ("15. error is not null");
150 g_strfreev (argv);
151 argv = NULL;
152 argc = 0;
153 error = NULL;
154 ret = g_shell_parse_argv ("hola\n\t \\\n \"\t\" \"bola \"", &argc, &argv, &error);
155 if (!ret)
156 return FAILED ("16. It should return TRUE");
157 if (argc != 3)
158 return FAILED ("17. argc was %d", argc);
159 if (strcmp (argv [0], "hola"))
160 return FAILED ("18. argv[0] was %s", argv [0]);
161 if (strcmp (argv [1], "\t"))
162 return FAILED ("19. argv[2] was %s", argv [1]);
163 if (strcmp (argv [2], "bola "))
164 return FAILED ("20. argv[2] was %s", argv [2]);
165 if (error != NULL)
166 return FAILED ("21. error is not null");
168 g_strfreev (argv);
169 return OK;
172 RESULT
173 test_shell_argv3 ()
175 GError *error;
176 gint argc;
177 gchar **argv;
178 gboolean ret;
180 argv = NULL;
181 argc = 0;
182 error = NULL;
183 ret = g_shell_parse_argv ("hola \"bola", &argc, &argv, &error);
184 if (ret)
185 return FAILED ("1. It should return FALSE");
186 if (argc != 0)
187 return FAILED ("2. argc was %d", argc);
188 if (argv != NULL)
189 return FAILED ("3. argv[0] was %s", argv [0]);
190 if (error == NULL)
191 return FAILED ("4. error is null");
193 /* Text ended before matching quote was found for ". (The text was 'hola "bola') */
194 g_error_free (error);
195 error = NULL;
196 ret = g_shell_parse_argv ("hola \\\"bola", &argc, &argv, &error);
197 if (!ret)
198 return FAILED ("5. It should return TRUE");
199 if (argc != 2)
200 return FAILED ("6. argc was %d", argc);
201 if (strcmp (argv [0], "hola"))
202 return FAILED ("18. argv[0] was %s", argv [0]);
203 if (strcmp (argv [1], "\"bola"))
204 return FAILED ("18. argv[1] was %s", argv [1]);
205 if (error != NULL)
206 return FAILED ("8. error is not null");
208 g_strfreev (argv);
209 argv = NULL;
210 argc = 0;
211 ret = g_shell_parse_argv ("hola \"\n\\'bola\"", &argc, &argv, &error);
212 if (!ret)
213 return FAILED ("9. It should return TRUE. %s", error->message);
214 if (argc != 2)
215 return FAILED ("10. argc was %d", argc);
216 if (strcmp (argv [0], "hola"))
217 return FAILED ("11. argv[0] was %s", argv [0]);
218 if (strcmp (argv [1], "\n\\'bola"))
219 return FAILED ("12. argv[1] was %s", argv [1]);
220 if (error != NULL)
221 return FAILED ("13. error is not null");
223 g_strfreev (argv);
224 argv = NULL;
225 argc = 0;
226 return OK;
229 RESULT
230 test_quote ()
232 if (strcmp (g_shell_quote ("foo"), "'foo'"))
233 return FAILED ("Should return 'foo'");
235 if (strcmp (g_shell_quote ("foo'bar"), "'foo'\\''bar'"))
236 return FAILED ("Should return 'foo'\\''bar'");
238 if (strcmp (g_shell_quote ("foo bar"), "'foo bar'"))
239 return FAILED ("Should return 'foo bar'");
240 return OK;
243 static Test shell_tests [] = {
244 {"g_shell_parse_argv1", test_shell_argv1},
245 {"g_shell_parse_argv2", test_shell_argv2},
246 {"g_shell_parse_argv3", test_shell_argv3},
247 {"g_shell_quote", test_quote},
248 {NULL, NULL}
251 DEFINE_TEST_GROUP_INIT(shell_tests_init, shell_tests)