1 // Copyright 2010 Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "utils/cmdline/base_command.ipp"
31 #include <atf-c++.hpp>
33 #include "utils/cmdline/exceptions.hpp"
34 #include "utils/cmdline/options.hpp"
35 #include "utils/cmdline/parser.ipp"
36 #include "utils/cmdline/ui_mock.hpp"
37 #include "utils/defs.hpp"
39 namespace cmdline
= utils::cmdline
;
45 /// Mock command to test the cmdline::base_command base class.
47 /// \param Data The type of the opaque data object passed to main().
48 /// \param ExpectedData The value run() will expect to find in the Data object
50 template< typename Data
, Data ExpectedData
>
51 class mock_cmd
: public cmdline::base_command
< Data
> {
53 /// Indicates if run() has been called already and executed correctly.
56 /// Contains the argument of --the_string after run() is executed.
59 /// Constructs a new mock command.
61 cmdline::base_command
< Data
>("mock", "arg1 [arg2 [arg3]]", 1, 3,
62 "Command for testing."),
65 this->add_option(cmdline::string_option("the_string", "Test option",
69 /// Executes the command.
71 /// \param unused_ui Object to interact with the I/O of the program.
72 /// \param cmdline Representation of the command line to the subcommand.
73 /// \param data Arbitrary data cookie passed to the command.
75 /// \return A hardcoded number for testing purposes.
77 run(cmdline::ui
* UTILS_UNUSED_PARAM(ui
),
78 const cmdline::parsed_cmdline
& cmdline
, const Data
& data
)
80 if (cmdline
.has_option("the_string"))
81 optvalue
= cmdline
.get_option
< cmdline::string_option
>(
83 ATF_REQUIRE_EQ(ExpectedData
, data
);
90 /// Mock command to test the cmdline::base_command_no_data base class.
91 class mock_cmd_no_data
: public cmdline::base_command_no_data
{
93 /// Indicates if run() has been called already and executed correctly.
96 /// Contains the argument of --the_string after run() is executed.
99 /// Constructs a new mock command.
100 mock_cmd_no_data(void) :
101 cmdline::base_command_no_data("mock", "arg1 [arg2 [arg3]]", 1, 3,
102 "Command for testing."),
105 add_option(cmdline::string_option("the_string", "Test option", "arg"));
108 /// Executes the command.
110 /// \param unused_ui Object to interact with the I/O of the program.
111 /// \param cmdline Representation of the command line to the subcommand.
113 /// \return A hardcoded number for testing purposes.
115 run(cmdline::ui
* UTILS_UNUSED_PARAM(ui
),
116 const cmdline::parsed_cmdline
& cmdline
)
118 if (cmdline
.has_option("the_string"))
119 optvalue
= cmdline
.get_option
< cmdline::string_option
>(
127 /// Implementation of a command to get access to parse_cmdline().
128 class parse_cmdline_portal
: public cmdline::command_proto
{
130 /// Constructs a new mock command.
131 parse_cmdline_portal(void) :
132 cmdline::command_proto("portal", "arg1 [arg2 [arg3]]", 1, 3,
133 "Command for testing.")
135 this->add_option(cmdline::string_option("the_string", "Test option",
139 /// Delegator for the internal parse_cmdline() method.
141 /// \param args The input arguments to be parsed.
143 /// \return The parsed command line, split in options and arguments.
144 cmdline::parsed_cmdline
145 operator()(const cmdline::args_vector
& args
) const
147 return parse_cmdline(args
);
152 } // anonymous namespace
155 ATF_TEST_CASE_WITHOUT_HEAD(command_proto__parse_cmdline__ok
);
156 ATF_TEST_CASE_BODY(command_proto__parse_cmdline__ok
)
158 cmdline::args_vector args
;
159 args
.push_back("portal");
160 args
.push_back("--the_string=foo bar");
161 args
.push_back("one arg");
162 args
.push_back("another arg");
163 (void)parse_cmdline_portal()(args
);
167 ATF_TEST_CASE_WITHOUT_HEAD(command_proto__parse_cmdline__parse_fail
);
168 ATF_TEST_CASE_BODY(command_proto__parse_cmdline__parse_fail
)
170 cmdline::args_vector args
;
171 args
.push_back("portal");
172 args
.push_back("--foo-bar");
173 ATF_REQUIRE_THROW_RE(cmdline::usage_error
, "Unknown.*foo-bar",
174 (void)parse_cmdline_portal()(args
));
178 ATF_TEST_CASE_WITHOUT_HEAD(command_proto__parse_cmdline__args_invalid
);
179 ATF_TEST_CASE_BODY(command_proto__parse_cmdline__args_invalid
)
181 cmdline::args_vector args
;
182 args
.push_back("portal");
184 ATF_REQUIRE_THROW_RE(cmdline::usage_error
, "Not enough arguments",
185 (void)parse_cmdline_portal()(args
));
191 ATF_REQUIRE_THROW_RE(cmdline::usage_error
, "Too many arguments",
192 (void)parse_cmdline_portal()(args
));
196 ATF_TEST_CASE_WITHOUT_HEAD(base_command__getters
);
197 ATF_TEST_CASE_BODY(base_command__getters
)
199 mock_cmd
< int, 584 > cmd
;
200 ATF_REQUIRE_EQ("mock", cmd
.name());
201 ATF_REQUIRE_EQ("arg1 [arg2 [arg3]]", cmd
.arg_list());
202 ATF_REQUIRE_EQ("Command for testing.", cmd
.short_description());
203 ATF_REQUIRE_EQ(1, cmd
.options().size());
204 ATF_REQUIRE_EQ("the_string", cmd
.options()[0]->long_name());
208 ATF_TEST_CASE_WITHOUT_HEAD(base_command__main__ok
)
209 ATF_TEST_CASE_BODY(base_command__main__ok
)
211 mock_cmd
< int, 584 > cmd
;
214 cmdline::args_vector args
;
215 args
.push_back("mock");
216 args
.push_back("--the_string=foo bar");
217 args
.push_back("one arg");
218 args
.push_back("another arg");
219 ATF_REQUIRE_EQ(1234, cmd
.main(&ui
, args
, 584));
220 ATF_REQUIRE(cmd
.executed
);
221 ATF_REQUIRE_EQ("foo bar", cmd
.optvalue
);
225 ATF_TEST_CASE_WITHOUT_HEAD(base_command__main__parse_cmdline_fail
)
226 ATF_TEST_CASE_BODY(base_command__main__parse_cmdline_fail
)
228 mock_cmd
< int, 584 > cmd
;
231 cmdline::args_vector args
;
232 args
.push_back("mock");
233 args
.push_back("--foo-bar");
234 ATF_REQUIRE_THROW_RE(cmdline::usage_error
, "Unknown.*foo-bar",
235 cmd
.main(&ui
, args
, 584));
236 ATF_REQUIRE(!cmd
.executed
);
240 ATF_TEST_CASE_WITHOUT_HEAD(base_command_no_data__getters
);
241 ATF_TEST_CASE_BODY(base_command_no_data__getters
)
243 mock_cmd_no_data cmd
;
244 ATF_REQUIRE_EQ("mock", cmd
.name());
245 ATF_REQUIRE_EQ("arg1 [arg2 [arg3]]", cmd
.arg_list());
246 ATF_REQUIRE_EQ("Command for testing.", cmd
.short_description());
247 ATF_REQUIRE_EQ(1, cmd
.options().size());
248 ATF_REQUIRE_EQ("the_string", cmd
.options()[0]->long_name());
252 ATF_TEST_CASE_WITHOUT_HEAD(base_command_no_data__main__ok
)
253 ATF_TEST_CASE_BODY(base_command_no_data__main__ok
)
255 mock_cmd_no_data cmd
;
258 cmdline::args_vector args
;
259 args
.push_back("mock");
260 args
.push_back("--the_string=foo bar");
261 args
.push_back("one arg");
262 args
.push_back("another arg");
263 ATF_REQUIRE_EQ(1234, cmd
.main(&ui
, args
));
264 ATF_REQUIRE(cmd
.executed
);
265 ATF_REQUIRE_EQ("foo bar", cmd
.optvalue
);
269 ATF_TEST_CASE_WITHOUT_HEAD(base_command_no_data__main__parse_cmdline_fail
)
270 ATF_TEST_CASE_BODY(base_command_no_data__main__parse_cmdline_fail
)
272 mock_cmd_no_data cmd
;
275 cmdline::args_vector args
;
276 args
.push_back("mock");
277 args
.push_back("--foo-bar");
278 ATF_REQUIRE_THROW_RE(cmdline::usage_error
, "Unknown.*foo-bar",
279 cmd
.main(&ui
, args
));
280 ATF_REQUIRE(!cmd
.executed
);
284 ATF_INIT_TEST_CASES(tcs
)
286 ATF_ADD_TEST_CASE(tcs
, command_proto__parse_cmdline__ok
);
287 ATF_ADD_TEST_CASE(tcs
, command_proto__parse_cmdline__parse_fail
);
288 ATF_ADD_TEST_CASE(tcs
, command_proto__parse_cmdline__args_invalid
);
290 ATF_ADD_TEST_CASE(tcs
, base_command__getters
);
291 ATF_ADD_TEST_CASE(tcs
, base_command__main__ok
);
292 ATF_ADD_TEST_CASE(tcs
, base_command__main__parse_cmdline_fail
);
294 ATF_ADD_TEST_CASE(tcs
, base_command_no_data__getters
);
295 ATF_ADD_TEST_CASE(tcs
, base_command_no_data__main__ok
);
296 ATF_ADD_TEST_CASE(tcs
, base_command_no_data__main__parse_cmdline_fail
);