Move doc comments inline.
[glib.git] / tests / shell-test.c
blob3762cb4cb19e974857f498bd155c993cbaca1d3b
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
27 #undef G_DISABLE_ASSERT
28 #undef G_LOG_DOMAIN
30 #include <glib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
36 typedef struct _TestResult TestResult;
38 struct _TestResult
40 gint argc;
41 const gchar **argv;
44 static const gchar *
45 test_command_lines[] =
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 /* 12 */ "foo \"c:\\\\\"",
60 NULL
63 static const gchar *result0[] = { "foo", "bar", NULL };
64 static const gchar *result1[] = { "foo", "bar", NULL };
65 static const gchar *result2[] = { "foo", "bar", NULL };
66 static const gchar *result3[] = { "foo", "", "bar", NULL };
67 static const gchar *result4[] = { "foo", "barbazblahfoo'blahboo", NULL };
68 static const gchar *result5[] = { "foo", "blah", "foo", "bar", "baz", NULL };
69 static const gchar *result6[] = { "foo", " spaces more spaces lots of spaces in this ", NULL };
70 static const gchar *result7[] = { "foo", "bar", NULL };
71 static const gchar *result8[] = { "foo", "", "", NULL };
72 static const gchar *result9[] = { "foo", "\"", "la", "la", "la", NULL };
73 static const gchar *result10[] = { "foo", " foo", "woo", "woo ", NULL };
74 static const gchar *result11[] = { "foo", "yada yada $\"", NULL };
75 static const gchar *result12[] = { "foo", "c:\\", NULL };
77 static const TestResult
78 correct_results[] =
80 { G_N_ELEMENTS (result0) - 1, result0 },
81 { G_N_ELEMENTS (result1) - 1, result1 },
82 { G_N_ELEMENTS (result2) - 1, result2 },
83 { G_N_ELEMENTS (result3) - 1, result3 },
84 { G_N_ELEMENTS (result4) - 1, result4 },
85 { G_N_ELEMENTS (result5) - 1, result5 },
86 { G_N_ELEMENTS (result6) - 1, result6 },
87 { G_N_ELEMENTS (result7) - 1, result7 },
88 { G_N_ELEMENTS (result8) - 1, result8 },
89 { G_N_ELEMENTS (result9) - 1, result9 },
90 { G_N_ELEMENTS (result10) - 1, result10 },
91 { G_N_ELEMENTS (result11) - 1, result11 },
92 { G_N_ELEMENTS (result12) - 1, result12 }
95 static void
96 print_test (const gchar *cmdline, gint argc, gchar **argv,
97 const TestResult *result)
99 gint i;
101 fprintf (stderr, "Command line was: '%s'\n", cmdline);
103 fprintf (stderr, "Expected result (%d args):\n", result->argc);
105 i = 0;
106 while (result->argv[i])
108 fprintf (stderr, " %3d '%s'\n", i, result->argv[i]);
109 ++i;
112 fprintf (stderr, "Actual result (%d args):\n", argc);
114 i = 0;
115 while (argv[i])
117 fprintf (stderr, " %3d '%s'\n", i, argv[i]);
118 ++i;
122 static void
123 do_argv_test (const gchar *cmdline, const TestResult *result)
125 gint argc;
126 gchar **argv;
127 GError *err;
128 gint i;
130 err = NULL;
131 if (!g_shell_parse_argv (cmdline, &argc, &argv, &err))
133 fprintf (stderr, "Error parsing command line that should work fine: %s\n",
134 err->message);
136 exit (1);
139 if (argc != result->argc)
141 fprintf (stderr, "Expected and actual argc don't match\n");
142 print_test (cmdline, argc, argv, result);
143 exit (1);
146 i = 0;
147 while (argv[i])
149 if (strcmp (argv[i], result->argv[i]) != 0)
151 fprintf (stderr, "Expected and actual arg %d do not match\n", i);
152 print_test (cmdline, argc, argv, result);
153 exit (1);
156 ++i;
159 if (argv[i] != NULL)
161 fprintf (stderr, "argv didn't get NULL-terminated\n");
162 exit (1);
166 static void
167 run_tests (void)
169 gint i;
171 i = 0;
172 while (test_command_lines[i])
174 do_argv_test (test_command_lines[i], &correct_results[i]);
175 ++i;
179 static gboolean any_test_failed = FALSE;
181 #define CHECK_STRING_RESULT(expression, expected_value) \
182 check_string_result (#expression, __FILE__, __LINE__, expression, expected_value)
184 static void
185 check_string_result (const char *expression,
186 const char *file_name,
187 int line_number,
188 char *result,
189 const char *expected)
191 gboolean match;
193 if (expected == NULL)
194 match = result == NULL;
195 else
196 match = result != NULL && strcmp (result, expected) == 0;
198 if (!match)
200 if (!any_test_failed)
201 fprintf (stderr, "\n");
203 fprintf (stderr, "FAIL: check failed in %s, line %d\n", file_name, line_number);
204 fprintf (stderr, " evaluated: %s\n", expression);
205 fprintf (stderr, " expected: %s\n", expected == NULL ? "NULL" : expected);
206 fprintf (stderr, " got: %s\n", result == NULL ? "NULL" : result);
208 any_test_failed = TRUE;
211 g_free (result);
214 static char *
215 test_shell_unquote (const char *str)
217 char *result;
218 GError *error;
220 error = NULL;
221 result = g_shell_unquote (str, &error);
222 if (error == NULL)
223 return result;
225 /* Leaks the error, which is no big deal and easy to fix if we
226 * decide it matters.
229 if (error->domain != G_SHELL_ERROR)
230 return g_strdup ("error in domain other than G_SHELL_ERROR");
232 /* It would be nice to check the error message too, but that's
233 * localized, so it's too much of a pain.
235 switch (error->code)
237 case G_SHELL_ERROR_BAD_QUOTING:
238 return g_strdup ("G_SHELL_ERROR_BAD_QUOTING");
239 case G_SHELL_ERROR_EMPTY_STRING:
240 return g_strdup ("G_SHELL_ERROR_EMPTY_STRING");
241 case G_SHELL_ERROR_FAILED:
242 return g_strdup ("G_SHELL_ERROR_FAILED");
243 default:
244 return g_strdup ("bad error code in G_SHELL_ERROR domain");
249 main (int argc,
250 char *argv[])
252 run_tests ();
254 CHECK_STRING_RESULT (g_shell_quote (""), "''");
255 CHECK_STRING_RESULT (g_shell_quote ("a"), "'a'");
256 CHECK_STRING_RESULT (g_shell_quote ("("), "'('");
257 CHECK_STRING_RESULT (g_shell_quote ("'"), "''\\'''");
258 CHECK_STRING_RESULT (g_shell_quote ("'a"), "''\\''a'");
259 CHECK_STRING_RESULT (g_shell_quote ("a'"), "'a'\\'''");
260 CHECK_STRING_RESULT (g_shell_quote ("a'a"), "'a'\\''a'");
262 CHECK_STRING_RESULT (test_shell_unquote (""), "");
263 CHECK_STRING_RESULT (test_shell_unquote ("a"), "a");
264 CHECK_STRING_RESULT (test_shell_unquote ("'a'"), "a");
265 CHECK_STRING_RESULT (test_shell_unquote ("'('"), "(");
266 CHECK_STRING_RESULT (test_shell_unquote ("''\\'''"), "'");
267 CHECK_STRING_RESULT (test_shell_unquote ("''\\''a'"), "'a");
268 CHECK_STRING_RESULT (test_shell_unquote ("'a'\\'''"), "a'");
269 CHECK_STRING_RESULT (test_shell_unquote ("'a'\\''a'"), "a'a");
271 CHECK_STRING_RESULT (test_shell_unquote ("\\\\"), "\\");
272 CHECK_STRING_RESULT (test_shell_unquote ("\\\n"), "");
274 CHECK_STRING_RESULT (test_shell_unquote ("'\\''"), "G_SHELL_ERROR_BAD_QUOTING");
276 #if defined (_MSC_VER) && (_MSC_VER <= 1200)
277 /* using \x22 instead of \" to work around a msvc 5.0, 6.0 compiler bug */
278 CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\x22\""), "\"");
279 #else
280 CHECK_STRING_RESULT (test_shell_unquote ("\"\\\"\""), "\"");
281 #endif
283 CHECK_STRING_RESULT (test_shell_unquote ("\""), "G_SHELL_ERROR_BAD_QUOTING");
284 CHECK_STRING_RESULT (test_shell_unquote ("'"), "G_SHELL_ERROR_BAD_QUOTING");
286 CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\\\""), "\\");
287 CHECK_STRING_RESULT (test_shell_unquote ("\x22\\`\""), "`");
288 CHECK_STRING_RESULT (test_shell_unquote ("\x22\\$\""), "$");
289 CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\n\""), "\n");
291 CHECK_STRING_RESULT (test_shell_unquote ("\"\\'\""), "\\'");
292 CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\r\""), "\\\r");
293 CHECK_STRING_RESULT (test_shell_unquote ("\x22\\n\""), "\\n");
295 return any_test_failed ? 1 : 0;