First public contribution.
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 * Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
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 #include "mrt2_glib2_test.h"
41 typedef struct _TestResult TestResult;
53 test_command_lines[] =
57 /* 2 */ "foo \"bar\"",
58 /* 3 */ "foo '' 'bar'",
59 /* 4 */ "foo \"bar\"'baz'blah'foo'\\''blah'\"boo\"",
60 /* 5 */ "foo \t \tblah\tfoo\t\tbar baz",
61 /* 6 */ "foo ' spaces more spaces lots of spaces in this ' \t",
62 /* 7 */ "foo \\\nbar",
64 /* 9 */ "foo \\\" la la la",
65 /* 10 */ "foo \\ foo woo woo\\ ",
66 /* 11 */ "foo \"yada yada \\$\\\"\"",
67 /* 12 */ "foo \"c:\\\\\"",
71 static const gchar *result0[] = { "foo", "bar", NULL };
72 static const gchar *result1[] = { "foo", "bar", NULL };
73 static const gchar *result2[] = { "foo", "bar", NULL };
74 static const gchar *result3[] = { "foo", "", "bar", NULL };
75 static const gchar *result4[] = { "foo", "barbazblahfoo'blahboo", NULL };
76 static const gchar *result5[] = { "foo", "blah", "foo", "bar", "baz", NULL };
77 static const gchar *result6[] = { "foo", " spaces more spaces lots of spaces in this ", NULL };
78 static const gchar *result7[] = { "foo", "bar", NULL };
79 static const gchar *result8[] = { "foo", "", "", NULL };
80 static const gchar *result9[] = { "foo", "\"", "la", "la", "la", NULL };
81 static const gchar *result10[] = { "foo", " foo", "woo", "woo ", NULL };
82 static const gchar *result11[] = { "foo", "yada yada $\"", NULL };
83 static const gchar *result12[] = { "foo", "c:\\", NULL };
85 static const TestResult
88 { G_N_ELEMENTS (result0) - 1, result0 },
89 { G_N_ELEMENTS (result1) - 1, result1 },
90 { G_N_ELEMENTS (result2) - 1, result2 },
91 { G_N_ELEMENTS (result3) - 1, result3 },
92 { G_N_ELEMENTS (result4) - 1, result4 },
93 { G_N_ELEMENTS (result5) - 1, result5 },
94 { G_N_ELEMENTS (result6) - 1, result6 },
95 { G_N_ELEMENTS (result7) - 1, result7 },
96 { G_N_ELEMENTS (result8) - 1, result8 },
97 { G_N_ELEMENTS (result9) - 1, result9 },
98 { G_N_ELEMENTS (result10) - 1, result10 },
99 { G_N_ELEMENTS (result11) - 1, result11 },
100 { G_N_ELEMENTS (result12) - 1, result12 }
104 print_test (const gchar *cmdline, gint argc, gchar **argv,
105 const TestResult *result)
109 g_print("Command line was: '%s'\n", cmdline);
111 g_print("Expected result (%d args):\n", result->argc);
114 while (result->argv[i])
116 g_print(" %3d '%s'\n", i, result->argv[i]);
120 g_print("Actual result (%d args):\n", argc);
125 g_print(" %3d '%s'\n", i, argv[i]);
131 do_argv_test (const gchar *cmdline, const TestResult *result)
139 if (!g_shell_parse_argv (cmdline, &argc, &argv, &err))
141 g_print("Error parsing command line that should work fine: %s\n",
143 g_assert(FALSE && "shell-test");
147 if (argc != result->argc)
149 g_print("Expected and actual argc don't match\n");
150 print_test (cmdline, argc, argv, result);
157 if (strcmp (argv[i], result->argv[i]) != 0)
159 g_print("Expected and actual arg %d do not match\n", i);
160 print_test (cmdline, argc, argv, result);
169 g_print("argv didn't get NULL-terminated\n");
180 while (test_command_lines[i])
182 do_argv_test (test_command_lines[i], &correct_results[i]);
187 static gboolean any_test_failed = FALSE;
189 #define CHECK_STRING_RESULT(expression, expected_value) \
190 check_string_result (#expression, __FILE__, __LINE__, expression, expected_value)
193 check_string_result (const char *expression,
194 const char *file_name,
197 const char *expected)
201 if (expected == NULL)
202 match = result == NULL;
204 match = result != NULL && strcmp (result, expected) == 0;
208 if (!any_test_failed)
211 g_print("FAIL: check failed in %s, line %d\n", file_name, line_number);
212 g_print("evaluated: %s\n", expression);
213 g_print("expected: %s\n", expected == NULL ? "NULL" : expected);
214 g_print("got: %s\n", result == NULL ? "NULL" : result);
215 g_assert(FALSE && "shell-test");
216 any_test_failed = TRUE;
223 test_shell_unquote (const char *str)
229 result = g_shell_unquote (str, &error);
233 /* Leaks the error, which is no big deal and easy to fix if we
237 if (error->domain != G_SHELL_ERROR)
238 return g_strdup ("error in domain other than G_SHELL_ERROR");
240 /* It would be nice to check the error message too, but that's
241 * localized, so it's too much of a pain.
245 case G_SHELL_ERROR_BAD_QUOTING:
246 return g_strdup ("G_SHELL_ERROR_BAD_QUOTING");
247 case G_SHELL_ERROR_EMPTY_STRING:
248 return g_strdup ("G_SHELL_ERROR_EMPTY_STRING");
249 case G_SHELL_ERROR_FAILED:
250 return g_strdup ("G_SHELL_ERROR_FAILED");
252 return g_strdup ("bad error code in G_SHELL_ERROR domain");
261 g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
262 g_set_print_handler(mrtPrintHandler);
268 CHECK_STRING_RESULT (g_shell_quote (""), "''");
269 CHECK_STRING_RESULT (g_shell_quote ("a"), "'a'");
270 CHECK_STRING_RESULT (g_shell_quote ("("), "'('");
271 CHECK_STRING_RESULT (g_shell_quote ("'"), "''\\'''");
272 CHECK_STRING_RESULT (g_shell_quote ("'a"), "''\\''a'");
273 CHECK_STRING_RESULT (g_shell_quote ("a'"), "'a'\\'''");
274 CHECK_STRING_RESULT (g_shell_quote ("a'a"), "'a'\\''a'");
276 CHECK_STRING_RESULT (test_shell_unquote (""), "");
277 CHECK_STRING_RESULT (test_shell_unquote ("a"), "a");
278 CHECK_STRING_RESULT (test_shell_unquote ("'a'"), "a");
279 CHECK_STRING_RESULT (test_shell_unquote ("'('"), "(");
280 CHECK_STRING_RESULT (test_shell_unquote ("''\\'''"), "'");
281 CHECK_STRING_RESULT (test_shell_unquote ("''\\''a'"), "'a");
282 CHECK_STRING_RESULT (test_shell_unquote ("'a'\\'''"), "a'");
283 CHECK_STRING_RESULT (test_shell_unquote ("'a'\\''a'"), "a'a");
285 CHECK_STRING_RESULT (test_shell_unquote ("\\\\"), "\\");
286 CHECK_STRING_RESULT (test_shell_unquote ("\\\n"), "");
288 CHECK_STRING_RESULT (test_shell_unquote ("'\\''"), "G_SHELL_ERROR_BAD_QUOTING");
290 #if defined (_MSC_VER) && (_MSC_VER <= 1200)
291 /* using \x22 instead of \" to work around a msvc 5.0, 6.0 compiler bug */
292 CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\x22\""), "\"");
294 CHECK_STRING_RESULT (test_shell_unquote ("\"\\\"\""), "\"");
297 CHECK_STRING_RESULT (test_shell_unquote ("\""), "G_SHELL_ERROR_BAD_QUOTING");
298 CHECK_STRING_RESULT (test_shell_unquote ("'"), "G_SHELL_ERROR_BAD_QUOTING");
300 CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\\\""), "\\");
301 CHECK_STRING_RESULT (test_shell_unquote ("\x22\\`\""), "`");
302 CHECK_STRING_RESULT (test_shell_unquote ("\x22\\$\""), "$");
303 CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\n\""), "\n");
305 CHECK_STRING_RESULT (test_shell_unquote ("\"\\'\""), "\\'");
306 CHECK_STRING_RESULT (test_shell_unquote ("\x22\\\r\""), "\\\r");
307 CHECK_STRING_RESULT (test_shell_unquote ("\x22\\n\""), "\\n");
310 testResultXml("shell-test");
311 #endif /* EMULATOR */
312 return any_test_failed ? 1 : 0;