Merge branch 'non-atomicity-of-g_file_set_contents' into 'master'
[glib.git] / glib / tests / shell.c
blobc3cab105a79b1e19077a114c819cd341c259488c
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.1 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, see <http://www.gnu.org/licenses/>.
19 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GLib Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GLib at ftp://ftp.gtk.org/pub/gtk/.
25 #undef G_DISABLE_ASSERT
26 #undef G_LOG_DOMAIN
28 #include <glib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
34 typedef struct _CmdlineTest CmdlineTest;
36 struct _CmdlineTest
38 const gchar *cmdline;
39 gint argc;
40 const gchar *argv[10];
41 gint error_code;
44 static CmdlineTest cmdline_tests[] =
46 { "foo bar", 2, { "foo", "bar", NULL }, -1 },
47 { "foo 'bar'", 2, { "foo", "bar", NULL }, -1 },
48 { "foo \"bar\"", 2, { "foo", "bar", NULL }, -1 },
49 { "foo '' 'bar'", 3, { "foo", "", "bar", NULL }, -1 },
50 { "foo \"bar\"'baz'blah'foo'\\''blah'\"boo\"", 2, { "foo", "barbazblahfoo'blahboo", NULL }, -1 },
51 { "foo \t \tblah\tfoo\t\tbar baz", 5, { "foo", "blah", "foo", "bar", "baz", NULL }, -1 },
52 { "foo ' spaces more spaces lots of spaces in this ' \t", 2, { "foo", " spaces more spaces lots of spaces in this ", NULL }, -1 },
53 { "foo \\\nbar", 2, { "foo", "bar", NULL }, -1 },
54 { "foo '' ''", 3, { "foo", "", "", NULL }, -1 },
55 { "foo \\\" la la la", 5, { "foo", "\"", "la", "la", "la", NULL }, -1 },
56 { "foo \\ foo woo woo\\ ", 4, { "foo", " foo", "woo", "woo ", NULL }, -1 },
57 { "foo \"yada yada \\$\\\"\"", 2, { "foo", "yada yada $\"", NULL }, -1 },
58 { "foo \"c:\\\\\"", 2, { "foo", "c:\\", NULL }, -1 },
59 { "foo # bla bla bla\n bar", 2, { "foo", "bar", NULL }, -1 },
60 { "foo a#b", 2, { "foo", "a#b", NULL }, -1 },
61 { "#foo", 0, { NULL }, G_SHELL_ERROR_EMPTY_STRING },
62 { "foo bar \\", 0, { NULL }, G_SHELL_ERROR_BAD_QUOTING },
63 { "foo 'bar baz", 0, { NULL }, G_SHELL_ERROR_BAD_QUOTING },
64 { "foo '\"bar\" baz", 0, { NULL }, G_SHELL_ERROR_BAD_QUOTING },
65 { "", 0, { NULL }, G_SHELL_ERROR_EMPTY_STRING },
66 { " ", 0, { NULL }, G_SHELL_ERROR_EMPTY_STRING },
67 { "# foo bar", 0, { NULL }, G_SHELL_ERROR_EMPTY_STRING },
68 {"foo '/bar/summer'\\''09 tours.pdf'", 2, {"foo", "/bar/summer'09 tours.pdf", NULL}, -1}
71 static gboolean
72 strv_equal (gchar **a, gchar **b)
74 gint i;
76 if (g_strv_length (a) != g_strv_length (b))
77 return FALSE;
79 for (i = 0; a[i]; i++)
80 if (g_strcmp0 (a[i], b[i]) != 0)
81 return FALSE;
83 return TRUE;
86 static void
87 do_cmdline_test (gconstpointer d)
89 const CmdlineTest *test = d;
90 gint argc;
91 gchar **argv;
92 GError *err;
93 gboolean res;
95 err = NULL;
96 g_printerr ("test cmdline: %s\n", test->cmdline);
97 res = g_shell_parse_argv (test->cmdline, &argc, &argv, &err);
98 if (test->error_code == -1)
100 g_assert (res);
101 g_assert_cmpint (argc, ==, test->argc);
102 g_assert (strv_equal (argv, (gchar **)test->argv));
103 g_assert_no_error (err);
105 else
107 g_assert (!res);
108 g_assert_error (err, G_SHELL_ERROR, test->error_code);
111 if (err)
112 g_error_free (err);
113 if (res)
114 g_strfreev (argv);
117 typedef struct _QuoteTest QuoteTest;
119 struct _QuoteTest
121 const gchar *in;
122 const gchar *out;
125 static QuoteTest quote_tests[] =
127 { "", "''" },
128 { "a", "'a'" },
129 { "(", "'('" },
130 { "'", "''\\'''" },
131 { "'a", "''\\''a'" },
132 { "a'", "'a'\\'''" },
133 { "a'a", "'a'\\''a'" }
136 static void
137 do_quote_test (gconstpointer d)
139 const QuoteTest *test = d;
140 gchar *out;
142 out = g_shell_quote (test->in);
143 g_assert_cmpstr (out, ==, test->out);
144 g_free (out);
147 typedef struct _UnquoteTest UnquoteTest;
149 struct _UnquoteTest
151 const gchar *in;
152 const gchar *out;
153 gint error_code;
156 static UnquoteTest unquote_tests[] =
158 { "", "", -1 },
159 { "a", "a", -1 },
160 { "'a'", "a", -1 },
161 { "'('", "(", -1 },
162 { "''\\'''", "'", -1 },
163 { "''\\''a'", "'a", -1 },
164 { "'a'\\'''", "a'", -1 },
165 { "'a'\\''a'", "a'a", -1 },
166 { "\\\\", "\\", -1 },
167 { "\\\n", "", -1 },
168 { "'\\''", NULL, G_SHELL_ERROR_BAD_QUOTING },
169 { "\"\\\"\"", "\"", -1 },
170 { "\"", NULL, G_SHELL_ERROR_BAD_QUOTING },
171 { "'", NULL, G_SHELL_ERROR_BAD_QUOTING },
172 { "\x22\\\\\"", "\\", -1 },
173 { "\x22\\`\"", "`", -1 },
174 { "\x22\\$\"", "$", -1 },
175 { "\x22\\\n\"", "\n", -1 },
176 { "\"\\'\"", "\\'", -1 },
177 { "\x22\\\r\"", "\\\r", -1 },
178 { "\x22\\n\"", "\\n", -1 }
181 static void
182 do_unquote_test (gconstpointer d)
184 const UnquoteTest *test = d;
185 gchar *out;
186 GError *error;
188 error = NULL;
189 out = g_shell_unquote (test->in, &error);
190 g_assert_cmpstr (out, ==, test->out);
191 if (test->error_code == -1)
192 g_assert_no_error (error);
193 else
194 g_assert_error (error, G_SHELL_ERROR, test->error_code);
196 g_free (out);
197 if (error)
198 g_error_free (error);
202 main (int argc, char *argv[])
204 gint i;
205 gchar *path;
207 g_test_init (&argc, &argv, NULL);
209 for (i = 0; i < G_N_ELEMENTS (cmdline_tests); i++)
211 path = g_strdup_printf ("/shell/cmdline/%d", i);
212 g_test_add_data_func (path, &cmdline_tests[i], do_cmdline_test);
213 g_free (path);
216 for (i = 0; i < G_N_ELEMENTS (quote_tests); i++)
218 path = g_strdup_printf ("/shell/quote/%d", i);
219 g_test_add_data_func (path, &quote_tests[i], do_quote_test);
220 g_free (path);
223 for (i = 0; i < G_N_ELEMENTS (unquote_tests); i++)
225 path = g_strdup_printf ("/shell/unquote/%d", i);
226 g_test_add_data_func (path, &unquote_tests[i], do_unquote_test);
227 g_free (path);
230 return g_test_run ();