2 * Copyright (C) 2010 Joel Rosdahl
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; either version 3 of the License, or (at your option)
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 * This file contains tests for the functions operating on struct args.
24 #include "test/framework.h"
30 struct args *args = args_init(0, NULL);
32 CHECK_INT_EQ(0, args->argc);
33 CHECK(!args->argv[0]);
37 TEST(args_init_populated)
39 char *argv[] = {"first", "second"};
40 struct args *args = args_init(2, argv);
42 CHECK_INT_EQ(2, args->argc);
43 CHECK_STR_EQ("first", args->argv[0]);
44 CHECK_STR_EQ("second", args->argv[1]);
45 CHECK(!args->argv[2]);
49 TEST(args_init_from_string)
51 struct args *args = args_init_from_string("first second\tthird\nfourth");
53 CHECK_INT_EQ(4, args->argc);
54 CHECK_STR_EQ("first", args->argv[0]);
55 CHECK_STR_EQ("second", args->argv[1]);
56 CHECK_STR_EQ("third", args->argv[2]);
57 CHECK_STR_EQ("fourth", args->argv[3]);
58 CHECK(!args->argv[4]);
64 struct args *args1 = args_init_from_string("foo");
65 struct args *args2 = args_copy(args1);
66 CHECK_ARGS_EQ_FREE12(args1, args2);
71 struct args *args = args_init_from_string("first");
72 CHECK_INT_EQ(1, args->argc);
73 args_add(args, "second");
74 CHECK_INT_EQ(2, args->argc);
75 CHECK_STR_EQ("second", args->argv[1]);
76 CHECK(!args->argv[2]);
82 struct args *args1 = args_init_from_string("first");
83 struct args *args2 = args_init_from_string("second third");
84 CHECK_INT_EQ(1, args1->argc);
85 args_extend(args1, args2);
86 CHECK_INT_EQ(3, args1->argc);
87 CHECK_STR_EQ("second", args1->argv[1]);
88 CHECK_STR_EQ("third", args1->argv[2]);
89 CHECK(!args1->argv[3]);
96 struct args *args = args_init_from_string("first second third");
98 CHECK_INT_EQ(1, args->argc);
99 CHECK_STR_EQ("first", args->argv[0]);
100 CHECK(!args->argv[1]);
106 struct args *args = args_init_from_string("first second third");
107 args_set(args, 1, "2nd");
108 CHECK_INT_EQ(3, args->argc);
109 CHECK_STR_EQ("first", args->argv[0]);
110 CHECK_STR_EQ("2nd", args->argv[1]);
111 CHECK_STR_EQ("third", args->argv[2]);
112 CHECK(!args->argv[3]);
116 TEST(args_remove_first)
118 struct args *args1 = args_init_from_string("first second third");
119 struct args *args2 = args_init_from_string("second third");
120 args_remove_first(args1);
121 CHECK_ARGS_EQ_FREE12(args1, args2);
124 TEST(args_add_prefix)
126 struct args *args1 = args_init_from_string("second third");
127 struct args *args2 = args_init_from_string("first second third");
128 args_add_prefix(args1, "first");
129 CHECK_ARGS_EQ_FREE12(args1, args2);
134 struct args *args1 = args_init_from_string("first xsecond third xfourth");
135 struct args *args2 = args_init_from_string("first third");
136 args_strip(args1, "x");
137 CHECK_ARGS_EQ_FREE12(args1, args2);
142 struct args *args = args_init_from_string("first second");
143 CHECK_STR_EQ_FREE2("first second", args_to_string(args));