Add MSVC-specific text by Hans Breuer.
[platform/upstream/glib.git] / tests / shell-test.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GLib Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #undef G_DISABLE_ASSERT
28 #undef G_LOG_DOMAIN
29
30 #include <glib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34
35
36 typedef struct _TestResult TestResult;
37
38 struct _TestResult
39 {
40   gint argc;
41   const gchar **argv;
42 };
43
44 static const gchar *
45 test_command_lines[] =
46 {
47   /*  0 */ "foo bar",
48   /*  1 */ "foo 'bar'",
49   /*  2 */ "foo \"bar\"",
50   /*  3 */ "foo '' 'bar'",
51   /*  4 */ "foo \"bar\"'baz'blah'foo'\\''blah'\"boo\"",
52   /*  5 */ "foo \t \tblah\tfoo\t\tbar  baz",
53   /*  6 */ "foo '    spaces more spaces lots of     spaces in this   '  \t",
54   /*  7 */ "foo \\\nbar",
55   /*  8 */ "foo '' ''",
56   /*  9 */ "foo \\\" la la la",
57   /* 10 */ "foo \\ foo woo woo\\ ",
58   /* 11 */ "foo \"yada yada \\$\\\"\"",
59   NULL
60 };
61
62 static const gchar *result0[] = { "foo", "bar", NULL };
63 static const gchar *result1[] = { "foo", "bar", NULL };
64 static const gchar *result2[] = { "foo", "bar", NULL };
65 static const gchar *result3[] = { "foo", "", "bar", NULL };
66 static const gchar *result4[] = { "foo", "barbazblahfoo'blahboo", NULL };
67 static const gchar *result5[] = { "foo", "blah", "foo", "bar", "baz", NULL };
68 static const gchar *result6[] = { "foo", "    spaces more spaces lots of     spaces in this   ", NULL };
69 static const gchar *result7[] = { "foo", "bar", NULL };
70 static const gchar *result8[] = { "foo", "", "", NULL };
71 static const gchar *result9[] = { "foo", "\"", "la", "la", "la", NULL };
72 static const gchar *result10[] = { "foo", " foo", "woo", "woo ", NULL };
73 static const gchar *result11[] = { "foo", "yada yada $\"", NULL };
74
75 static const TestResult
76 correct_results[] =
77 {
78   { G_N_ELEMENTS (result0) - 1, result0 },
79   { G_N_ELEMENTS (result1) - 1, result1 },
80   { G_N_ELEMENTS (result2) - 1, result2 },
81   { G_N_ELEMENTS (result3) - 1, result3 },
82   { G_N_ELEMENTS (result4) - 1, result4 },
83   { G_N_ELEMENTS (result5) - 1, result5 },
84   { G_N_ELEMENTS (result6) - 1, result6 },
85   { G_N_ELEMENTS (result7) - 1, result7 },
86   { G_N_ELEMENTS (result8) - 1, result8 },
87   { G_N_ELEMENTS (result9) - 1, result9 },
88   { G_N_ELEMENTS (result10) - 1, result10 },
89   { G_N_ELEMENTS (result11) - 1, result11 }
90 };
91
92 static void
93 print_test (const gchar *cmdline, gint argc, gchar **argv,
94             const TestResult *result)
95 {
96   gint i;
97   
98   fprintf (stderr, "Command line was: '%s'\n", cmdline);
99
100   fprintf (stderr, "Expected result (%d args):\n", result->argc);
101   
102   i = 0;
103   while (result->argv[i])
104     {
105       fprintf (stderr, " %3d '%s'\n", i, result->argv[i]);
106       ++i;
107     }  
108
109   fprintf (stderr, "Actual result (%d args):\n", argc);
110   
111   i = 0;
112   while (argv[i])
113     {
114       fprintf (stderr, " %3d '%s'\n", i, argv[i]);
115       ++i;
116     }
117 }
118
119 static void
120 do_argv_test (const gchar *cmdline, const TestResult *result)
121 {
122   gint argc;
123   gchar **argv;
124   GError *err;
125   gint i;
126
127   err = NULL;
128   if (!g_shell_parse_argv (cmdline, &argc, &argv, &err))
129     {
130       fprintf (stderr, "Error parsing command line that should work fine: %s\n",
131                err->message);
132       
133       exit (1);
134     }
135   
136   if (argc != result->argc)
137     {
138       fprintf (stderr, "Expected and actual argc don't match\n");
139       print_test (cmdline, argc, argv, result);
140       exit (1);
141     }
142
143   i = 0;
144   while (argv[i])
145     {
146       if (strcmp (argv[i], result->argv[i]) != 0)
147         {
148           fprintf (stderr, "Expected and actual arg %d do not match\n", i);
149           print_test (cmdline, argc, argv, result);
150           exit (1);
151         }
152       
153       ++i;
154     }
155
156   if (argv[i] != NULL)
157     {
158       fprintf (stderr, "argv didn't get NULL-terminated\n");
159       exit (1);
160     }
161 }
162
163 static void
164 run_tests (void)
165 {
166   gint i;
167   
168   i = 0;
169   while (test_command_lines[i])
170     {
171       do_argv_test (test_command_lines[i], &correct_results[i]);
172       ++i;
173     }
174 }
175
176 static gboolean any_test_failed = FALSE;
177
178 #define CHECK_STRING_RESULT(expression, expected_value) \
179  check_string_result (#expression, __FILE__, __LINE__, expression, expected_value)
180
181 static void
182 check_string_result (const char *expression,
183                      const char *file_name,
184                      int line_number,
185                      char *result,
186                      const char *expected)
187 {
188   gboolean match;
189   
190   if (expected == NULL)
191     match = result == NULL;
192   else
193     match = result != NULL && strcmp (result, expected) == 0;
194   
195   if (!match)
196     {
197       if (!any_test_failed)
198         fprintf (stderr, "\n");
199       
200       fprintf (stderr, "FAIL: check failed in %s, line %d\n", file_name, line_number);
201       fprintf (stderr, "      evaluated: %s\n", expression);
202       fprintf (stderr, "       expected: %s\n", expected == NULL ? "NULL" : expected);
203       fprintf (stderr, "            got: %s\n", result == NULL ? "NULL" : result);
204       
205       any_test_failed = TRUE;
206     }
207   
208   g_free (result);
209 }
210
211 static char *
212 test_shell_unquote (const char *str)
213 {
214   char *result;
215   GError *error;
216
217   error = NULL;
218   result = g_shell_unquote (str, &error);
219   if (error == NULL)
220     return result;
221
222   /* Leaks the error, which is no big deal and easy to fix if we
223    * decide it matters.
224    */
225
226   if (error->domain != G_SHELL_ERROR)
227     return g_strdup ("error in domain other than G_SHELL_ERROR");
228
229   /* It would be nice to check the error message too, but that's
230    * localized, so it's too much of a pain.
231    */
232   switch (error->code)
233     {
234     case G_SHELL_ERROR_BAD_QUOTING:
235       return g_strdup ("G_SHELL_ERROR_BAD_QUOTING");
236     case G_SHELL_ERROR_EMPTY_STRING:
237       return g_strdup ("G_SHELL_ERROR_EMPTY_STRING");
238     case G_SHELL_ERROR_FAILED:
239       return g_strdup ("G_SHELL_ERROR_FAILED");
240     default:
241       return g_strdup ("bad error code in G_SHELL_ERROR domain");
242     }
243 }
244
245 int
246 main (int   argc,
247       char *argv[])
248 {
249   run_tests ();
250   
251   CHECK_STRING_RESULT (g_shell_quote (""), "''");
252   CHECK_STRING_RESULT (g_shell_quote ("a"), "'a'");
253   CHECK_STRING_RESULT (g_shell_quote ("("), "'('");
254   CHECK_STRING_RESULT (g_shell_quote ("'"), "''\\'''");
255   CHECK_STRING_RESULT (g_shell_quote ("'a"), "''\\''a'");
256   CHECK_STRING_RESULT (g_shell_quote ("a'"), "'a'\\'''");
257   CHECK_STRING_RESULT (g_shell_quote ("a'a"), "'a'\\''a'");
258   
259   CHECK_STRING_RESULT (test_shell_unquote (""), "");
260   CHECK_STRING_RESULT (test_shell_unquote ("a"), "a");
261   CHECK_STRING_RESULT (test_shell_unquote ("'a'"), "a");
262   CHECK_STRING_RESULT (test_shell_unquote ("'('"), "(");
263   CHECK_STRING_RESULT (test_shell_unquote ("''\\'''"), "'");
264   CHECK_STRING_RESULT (test_shell_unquote ("''\\''a'"), "'a");
265   CHECK_STRING_RESULT (test_shell_unquote ("'a'\\'''"), "a'");
266   CHECK_STRING_RESULT (test_shell_unquote ("'a'\\''a'"), "a'a");
267
268   CHECK_STRING_RESULT (test_shell_unquote ("\\\\"), "\\");
269   CHECK_STRING_RESULT (test_shell_unquote ("\\\n"), "");
270
271   CHECK_STRING_RESULT (test_shell_unquote ("'\\''"), "G_SHELL_ERROR_BAD_QUOTING");
272
273 #if defined (_MSC_VER) && (_MSC_VER <= 1200)
274   /* using \x22 instead of \" to work around a msvc 5.0, 6.0 compiler bug */
275   CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\x22\""), "\"");
276 #else
277   CHECK_STRING_RESULT (test_shell_unquote ("\"\\\"\""), "\"");
278 #endif
279
280   CHECK_STRING_RESULT (test_shell_unquote ("\""), "G_SHELL_ERROR_BAD_QUOTING");
281   CHECK_STRING_RESULT (test_shell_unquote ("'"), "G_SHELL_ERROR_BAD_QUOTING");
282
283   CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\\\""), "\\");
284   CHECK_STRING_RESULT (test_shell_unquote ("\x22\\`\""), "`");
285   CHECK_STRING_RESULT (test_shell_unquote ("\x22\\$\""), "$");
286   CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\n\""), "\n");
287
288   CHECK_STRING_RESULT (test_shell_unquote ("\"\\'\""), "\\'");
289   CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\r\""), "\\\r");
290   CHECK_STRING_RESULT (test_shell_unquote ("\x22\\n\""), "\\n");
291
292   return any_test_failed ? 1 : 0;
293 }