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
36 typedef struct _CmdlineTest CmdlineTest
;
42 const gchar
*argv
[10];
46 static CmdlineTest cmdline_tests
[] =
48 { "foo bar", 2, { "foo", "bar", NULL
}, -1 },
49 { "foo 'bar'", 2, { "foo", "bar", NULL
}, -1 },
50 { "foo \"bar\"", 2, { "foo", "bar", NULL
}, -1 },
51 { "foo '' 'bar'", 3, { "foo", "", "bar", NULL
}, -1 },
52 { "foo \"bar\"'baz'blah'foo'\\''blah'\"boo\"", 2, { "foo", "barbazblahfoo'blahboo", NULL
}, -1 },
53 { "foo \t \tblah\tfoo\t\tbar baz", 5, { "foo", "blah", "foo", "bar", "baz", NULL
}, -1 },
54 { "foo ' spaces more spaces lots of spaces in this ' \t", 2, { "foo", " spaces more spaces lots of spaces in this ", NULL
}, -1 },
55 { "foo \\\nbar", 2, { "foo", "bar", NULL
}, -1 },
56 { "foo '' ''", 3, { "foo", "", "", NULL
}, -1 },
57 { "foo \\\" la la la", 5, { "foo", "\"", "la", "la", "la", NULL
}, -1 },
58 { "foo \\ foo woo woo\\ ", 4, { "foo", " foo", "woo", "woo ", NULL
}, -1 },
59 { "foo \"yada yada \\$\\\"\"", 2, { "foo", "yada yada $\"", NULL
}, -1 },
60 { "foo \"c:\\\\\"", 2, { "foo", "c:\\", NULL
}, -1 },
61 { "foo # bla bla bla\n bar", 2, { "foo", "bar", NULL
}, -1 },
62 { "foo a#b", 2, { "foo", "a#b", NULL
}, -1 },
63 { "#foo", 0, { NULL
}, G_SHELL_ERROR_EMPTY_STRING
},
64 { "foo bar \\", 0, { NULL
}, G_SHELL_ERROR_BAD_QUOTING
},
65 { "foo 'bar baz", 0, { NULL
}, G_SHELL_ERROR_BAD_QUOTING
},
66 { "foo '\"bar\" baz", 0, { NULL
}, G_SHELL_ERROR_BAD_QUOTING
},
67 { "", 0, { NULL
}, G_SHELL_ERROR_EMPTY_STRING
},
68 { " ", 0, { NULL
}, G_SHELL_ERROR_EMPTY_STRING
},
69 { "# foo bar", 0, { NULL
}, G_SHELL_ERROR_EMPTY_STRING
},
70 {"foo '/bar/summer'\\''09 tours.pdf'", 2, {"foo", "/bar/summer'09 tours.pdf", NULL
}, -1}
74 strv_equal (gchar
**a
, gchar
**b
)
78 if (g_strv_length (a
) != g_strv_length (b
))
81 for (i
= 0; a
[i
]; i
++)
82 if (g_strcmp0 (a
[i
], b
[i
]) != 0)
89 do_cmdline_test (gconstpointer d
)
91 const CmdlineTest
*test
= d
;
98 g_print ("test cmdline: %s\n", test
->cmdline
);
99 res
= g_shell_parse_argv (test
->cmdline
, &argc
, &argv
, &err
);
100 if (test
->error_code
== -1)
103 g_assert_cmpint (argc
, ==, test
->argc
);
104 g_assert (strv_equal (argv
, (gchar
**)test
->argv
));
105 g_assert_no_error (err
);
110 g_assert_error (err
, G_SHELL_ERROR
, test
->error_code
);
119 typedef struct _QuoteTest QuoteTest
;
127 static QuoteTest quote_tests
[] =
133 { "'a", "''\\''a'" },
134 { "a'", "'a'\\'''" },
135 { "a'a", "'a'\\''a'" }
139 do_quote_test (gconstpointer d
)
141 const QuoteTest
*test
= d
;
144 out
= g_shell_quote (test
->in
);
145 g_assert_cmpstr (out
, ==, test
->out
);
149 typedef struct _UnquoteTest UnquoteTest
;
158 static UnquoteTest unquote_tests
[] =
164 { "''\\'''", "'", -1 },
165 { "''\\''a'", "'a", -1 },
166 { "'a'\\'''", "a'", -1 },
167 { "'a'\\''a'", "a'a", -1 },
168 { "\\\\", "\\", -1 },
170 { "'\\''", NULL
, G_SHELL_ERROR_BAD_QUOTING
},
171 { "\"\\\"\"", "\"", -1 },
172 { "\"", NULL
, G_SHELL_ERROR_BAD_QUOTING
},
173 { "'", NULL
, G_SHELL_ERROR_BAD_QUOTING
},
174 { "\x22\\\\\"", "\\", -1 },
175 { "\x22\\`\"", "`", -1 },
176 { "\x22\\$\"", "$", -1 },
177 { "\x22\\\n\"", "\n", -1 },
178 { "\"\\'\"", "\\'", -1 },
179 { "\x22\\\r\"", "\\\r", -1 },
180 { "\x22\\n\"", "\\n", -1 }
184 do_unquote_test (gconstpointer d
)
186 const UnquoteTest
*test
= d
;
191 out
= g_shell_unquote (test
->in
, &error
);
192 g_assert_cmpstr (out
, ==, test
->out
);
193 if (test
->error_code
== -1)
194 g_assert_no_error (error
);
196 g_assert_error (error
, G_SHELL_ERROR
, test
->error_code
);
200 g_error_free (error
);
204 main (int argc
, char *argv
[])
209 g_test_init (&argc
, &argv
, NULL
);
211 for (i
= 0; i
< G_N_ELEMENTS (cmdline_tests
); i
++)
213 path
= g_strdup_printf ("/shell/cmdline/%d", i
);
214 g_test_add_data_func (path
, &cmdline_tests
[i
], do_cmdline_test
);
218 for (i
= 0; i
< G_N_ELEMENTS (quote_tests
); i
++)
220 path
= g_strdup_printf ("/shell/quote/%d", i
);
221 g_test_add_data_func (path
, "e_tests
[i
], do_quote_test
);
225 for (i
= 0; i
< G_N_ELEMENTS (unquote_tests
); i
++)
227 path
= g_strdup_printf ("/shell/unquote/%d", i
);
228 g_test_add_data_func (path
, &unquote_tests
[i
], do_unquote_test
);
232 return g_test_run ();