removed glib.def from EXTRA_DIST ... ... and added it here.
[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   printf ("\nCommand line was: '%s'\n", cmdline);
99
100   printf ("Expected result (%d args):\n", result->argc);
101   
102   i = 0;
103   while (result->argv[i])
104     {
105       printf (" %3d '%s'\n", i, result->argv[i]);
106
107       ++i;
108     }  
109
110   printf ("Actual result (%d args):\n", argc);
111   
112   i = 0;
113   while (argv[i])
114     {
115       printf (" %3d '%s'\n", i, argv[i]);
116
117       ++i;
118     }
119 }
120
121 static void
122 do_argv_test (const gchar *cmdline, const TestResult *result)
123 {
124   gint argc;
125   gchar **argv;
126   GError *err;
127   gint i;
128
129   err = NULL;
130   if (!g_shell_parse_argv (cmdline, &argc, &argv, &err))
131     {
132       fprintf (stderr, "Error parsing command line that should work fine: %s\n",
133                err->message);
134       
135       exit (1);
136     }
137   
138   if (argc != result->argc)
139     {
140       fprintf (stderr, "Expected and actual argc don't match\n");
141       print_test (cmdline, argc, argv, result);
142       exit (1);
143     }
144
145   i = 0;
146   while (argv[i])
147     {
148       if (strcmp (argv[i], result->argv[i]) != 0)
149         {
150           fprintf (stderr, "Expected and actual arg %d do not match\n", i);
151           print_test (cmdline, argc, argv, result);
152           exit (1);
153         }
154       
155       ++i;
156     }
157
158   if (argv[i] != NULL)
159     {
160       fprintf (stderr, "argv didn't get NULL-terminated\n");
161       exit (1);
162     }
163 }
164
165 static void
166 run_tests (void)
167 {
168   gint i;
169   
170   i = 0;
171   while (test_command_lines[i])
172     {
173       printf ("g_shell_parse_argv() test %d - ", i);
174       do_argv_test (test_command_lines[i], &correct_results[i]);
175       printf ("ok (%s)\n", test_command_lines[i]);
176       
177       ++i;
178     }
179 }
180
181 int
182 main (int   argc,
183       char *argv[])
184 {
185   run_tests ();
186   
187   return 0;
188 }
189
190