xref: /minix3/external/bsd/kyua-cli/dist/cli/cmd_help_test.cpp (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc // Copyright 2010 Google Inc.
2*11be35a1SLionel Sambuc // All rights reserved.
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc // met:
7*11be35a1SLionel Sambuc //
8*11be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc //   documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc //   may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc //   without specific prior written permission.
16*11be35a1SLionel Sambuc //
17*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc 
29*11be35a1SLionel Sambuc #include "cli/cmd_help.hpp"
30*11be35a1SLionel Sambuc 
31*11be35a1SLionel Sambuc #include <algorithm>
32*11be35a1SLionel Sambuc #include <cstdlib>
33*11be35a1SLionel Sambuc #include <iterator>
34*11be35a1SLionel Sambuc 
35*11be35a1SLionel Sambuc #include <atf-c++.hpp>
36*11be35a1SLionel Sambuc 
37*11be35a1SLionel Sambuc #include "cli/common.ipp"
38*11be35a1SLionel Sambuc #include "engine/config.hpp"
39*11be35a1SLionel Sambuc #include "utils/cmdline/commands_map.ipp"
40*11be35a1SLionel Sambuc #include "utils/cmdline/exceptions.hpp"
41*11be35a1SLionel Sambuc #include "utils/cmdline/globals.hpp"
42*11be35a1SLionel Sambuc #include "utils/cmdline/options.hpp"
43*11be35a1SLionel Sambuc #include "utils/cmdline/parser.hpp"
44*11be35a1SLionel Sambuc #include "utils/cmdline/ui_mock.hpp"
45*11be35a1SLionel Sambuc #include "utils/defs.hpp"
46*11be35a1SLionel Sambuc #include "utils/sanity.hpp"
47*11be35a1SLionel Sambuc 
48*11be35a1SLionel Sambuc namespace cmdline = utils::cmdline;
49*11be35a1SLionel Sambuc namespace config = utils::config;
50*11be35a1SLionel Sambuc 
51*11be35a1SLionel Sambuc using cli::cmd_help;
52*11be35a1SLionel Sambuc 
53*11be35a1SLionel Sambuc 
54*11be35a1SLionel Sambuc namespace {
55*11be35a1SLionel Sambuc 
56*11be35a1SLionel Sambuc 
57*11be35a1SLionel Sambuc /// Mock command with a simple definition (no options, no arguments).
58*11be35a1SLionel Sambuc ///
59*11be35a1SLionel Sambuc /// Attempting to run this command will result in a crash.  It is only provided
60*11be35a1SLionel Sambuc /// to validate the generation of interactive help.
61*11be35a1SLionel Sambuc class cmd_mock_simple : public cli::cli_command {
62*11be35a1SLionel Sambuc public:
63*11be35a1SLionel Sambuc     /// Constructs a new mock command.
64*11be35a1SLionel Sambuc     ///
65*11be35a1SLionel Sambuc     /// \param name_ The name of the command to create.
cmd_mock_simple(const char * name_)66*11be35a1SLionel Sambuc     cmd_mock_simple(const char* name_) : cli::cli_command(
67*11be35a1SLionel Sambuc         name_, "", 0, 0, "Simple command")
68*11be35a1SLionel Sambuc     {
69*11be35a1SLionel Sambuc     }
70*11be35a1SLionel Sambuc 
71*11be35a1SLionel Sambuc     /// Runs the mock command.
72*11be35a1SLionel Sambuc     ///
73*11be35a1SLionel Sambuc     /// \param unused_ui Object to interact with the I/O of the program.
74*11be35a1SLionel Sambuc     /// \param unused_cmdline Representation of the command line to the
75*11be35a1SLionel Sambuc     ///     subcommand.
76*11be35a1SLionel Sambuc     /// \param unused_user_config The runtime configuration of the program.
77*11be35a1SLionel Sambuc     ///
78*11be35a1SLionel Sambuc     /// \return Nothing because this function is never called.
79*11be35a1SLionel Sambuc     int
run(cmdline::ui * UTILS_UNUSED_PARAM (ui),const cmdline::parsed_cmdline & UTILS_UNUSED_PARAM (cmdline),const config::tree & UTILS_UNUSED_PARAM (user_config))80*11be35a1SLionel Sambuc     run(cmdline::ui* UTILS_UNUSED_PARAM(ui),
81*11be35a1SLionel Sambuc         const cmdline::parsed_cmdline& UTILS_UNUSED_PARAM(cmdline),
82*11be35a1SLionel Sambuc         const config::tree& UTILS_UNUSED_PARAM(user_config))
83*11be35a1SLionel Sambuc     {
84*11be35a1SLionel Sambuc         UNREACHABLE;
85*11be35a1SLionel Sambuc     }
86*11be35a1SLionel Sambuc };
87*11be35a1SLionel Sambuc 
88*11be35a1SLionel Sambuc 
89*11be35a1SLionel Sambuc /// Mock command with a complex definition (some options, some arguments).
90*11be35a1SLionel Sambuc ///
91*11be35a1SLionel Sambuc /// Attempting to run this command will result in a crash.  It is only provided
92*11be35a1SLionel Sambuc /// to validate the generation of interactive help.
93*11be35a1SLionel Sambuc class cmd_mock_complex : public cli::cli_command {
94*11be35a1SLionel Sambuc public:
95*11be35a1SLionel Sambuc     /// Constructs a new mock command.
96*11be35a1SLionel Sambuc     ///
97*11be35a1SLionel Sambuc     /// \param name_ The name of the command to create.
cmd_mock_complex(const char * name_)98*11be35a1SLionel Sambuc     cmd_mock_complex(const char* name_) : cli::cli_command(
99*11be35a1SLionel Sambuc         name_, "[arg1 .. argN]", 0, 2, "Complex command")
100*11be35a1SLionel Sambuc     {
101*11be35a1SLionel Sambuc         add_option(cmdline::bool_option("flag_a", "Flag A"));
102*11be35a1SLionel Sambuc         add_option(cmdline::bool_option('b', "flag_b", "Flag B"));
103*11be35a1SLionel Sambuc         add_option(cmdline::string_option('c', "flag_c", "Flag C", "c_arg"));
104*11be35a1SLionel Sambuc         add_option(cmdline::string_option("flag_d", "Flag D", "d_arg", "foo"));
105*11be35a1SLionel Sambuc     }
106*11be35a1SLionel Sambuc 
107*11be35a1SLionel Sambuc     /// Runs the mock command.
108*11be35a1SLionel Sambuc     ///
109*11be35a1SLionel Sambuc     /// \param unused_ui Object to interact with the I/O of the program.
110*11be35a1SLionel Sambuc     /// \param unused_cmdline Representation of the command line to the
111*11be35a1SLionel Sambuc     ///     subcommand.
112*11be35a1SLionel Sambuc     /// \param unused_user_config The runtime configuration of the program.
113*11be35a1SLionel Sambuc     ///
114*11be35a1SLionel Sambuc     /// \return Nothing because this function is never called.
115*11be35a1SLionel Sambuc     int
run(cmdline::ui * UTILS_UNUSED_PARAM (ui),const cmdline::parsed_cmdline & UTILS_UNUSED_PARAM (cmdline),const config::tree & UTILS_UNUSED_PARAM (user_config))116*11be35a1SLionel Sambuc     run(cmdline::ui* UTILS_UNUSED_PARAM(ui),
117*11be35a1SLionel Sambuc         const cmdline::parsed_cmdline& UTILS_UNUSED_PARAM(cmdline),
118*11be35a1SLionel Sambuc         const config::tree& UTILS_UNUSED_PARAM(user_config))
119*11be35a1SLionel Sambuc     {
120*11be35a1SLionel Sambuc         UNREACHABLE;
121*11be35a1SLionel Sambuc     }
122*11be35a1SLionel Sambuc };
123*11be35a1SLionel Sambuc 
124*11be35a1SLionel Sambuc 
125*11be35a1SLionel Sambuc /// Initializes the cmdline library and generates the set of test commands.
126*11be35a1SLionel Sambuc ///
127*11be35a1SLionel Sambuc /// \param [out] commands A mapping that is updated to contain the commands to
128*11be35a1SLionel Sambuc ///     use for testing.
129*11be35a1SLionel Sambuc static void
setup(cmdline::commands_map<cli::cli_command> & commands)130*11be35a1SLionel Sambuc setup(cmdline::commands_map< cli::cli_command >& commands)
131*11be35a1SLionel Sambuc {
132*11be35a1SLionel Sambuc     cmdline::init("progname");
133*11be35a1SLionel Sambuc 
134*11be35a1SLionel Sambuc     commands.insert(new cmd_mock_simple("mock_simple"));
135*11be35a1SLionel Sambuc     commands.insert(new cmd_mock_complex("mock_complex"));
136*11be35a1SLionel Sambuc 
137*11be35a1SLionel Sambuc     commands.insert(new cmd_mock_simple("mock_simple_2"), "First");
138*11be35a1SLionel Sambuc     commands.insert(new cmd_mock_complex("mock_complex_2"), "First");
139*11be35a1SLionel Sambuc 
140*11be35a1SLionel Sambuc     commands.insert(new cmd_mock_simple("mock_simple_3"), "Second");
141*11be35a1SLionel Sambuc }
142*11be35a1SLionel Sambuc 
143*11be35a1SLionel Sambuc 
144*11be35a1SLionel Sambuc /// Performs a test on the global help (not that of a subcommand).
145*11be35a1SLionel Sambuc ///
146*11be35a1SLionel Sambuc /// \param general_options The genral options supported by the tool, if any.
147*11be35a1SLionel Sambuc /// \param expected_options Expected lines of help output documenting the
148*11be35a1SLionel Sambuc ///     options in general_options.
149*11be35a1SLionel Sambuc /// \param ui The cmdline::mock_ui object to which to write the output.
150*11be35a1SLionel Sambuc static void
global_test(const cmdline::options_vector & general_options,const std::vector<std::string> & expected_options,cmdline::ui_mock & ui)151*11be35a1SLionel Sambuc global_test(const cmdline::options_vector& general_options,
152*11be35a1SLionel Sambuc             const std::vector< std::string >& expected_options,
153*11be35a1SLionel Sambuc             cmdline::ui_mock& ui)
154*11be35a1SLionel Sambuc {
155*11be35a1SLionel Sambuc     cmdline::commands_map< cli::cli_command > mock_commands;
156*11be35a1SLionel Sambuc     setup(mock_commands);
157*11be35a1SLionel Sambuc 
158*11be35a1SLionel Sambuc     cmdline::args_vector args;
159*11be35a1SLionel Sambuc     args.push_back("help");
160*11be35a1SLionel Sambuc 
161*11be35a1SLionel Sambuc     cmd_help cmd(&general_options, &mock_commands);
162*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(EXIT_SUCCESS, cmd.main(&ui, args, engine::default_config()));
163*11be35a1SLionel Sambuc 
164*11be35a1SLionel Sambuc     std::vector< std::string > expected;
165*11be35a1SLionel Sambuc 
166*11be35a1SLionel Sambuc     expected.push_back("Usage: progname [general_options] command "
167*11be35a1SLionel Sambuc                        "[command_options] [args]");
168*11be35a1SLionel Sambuc     if (!general_options.empty()) {
169*11be35a1SLionel Sambuc         expected.push_back("");
170*11be35a1SLionel Sambuc         expected.push_back("Available general options:");
171*11be35a1SLionel Sambuc         std::copy(expected_options.begin(), expected_options.end(),
172*11be35a1SLionel Sambuc                   std::back_inserter(expected));
173*11be35a1SLionel Sambuc     }
174*11be35a1SLionel Sambuc     expected.push_back("");
175*11be35a1SLionel Sambuc     expected.push_back("Generic commands:");
176*11be35a1SLionel Sambuc     expected.push_back("  mock_complex    Complex command.");
177*11be35a1SLionel Sambuc     expected.push_back("  mock_simple     Simple command.");
178*11be35a1SLionel Sambuc     expected.push_back("");
179*11be35a1SLionel Sambuc     expected.push_back("First commands:");
180*11be35a1SLionel Sambuc     expected.push_back("  mock_complex_2  Complex command.");
181*11be35a1SLionel Sambuc     expected.push_back("  mock_simple_2   Simple command.");
182*11be35a1SLionel Sambuc     expected.push_back("");
183*11be35a1SLionel Sambuc     expected.push_back("Second commands:");
184*11be35a1SLionel Sambuc     expected.push_back("  mock_simple_3   Simple command.");
185*11be35a1SLionel Sambuc     expected.push_back("");
186*11be35a1SLionel Sambuc     expected.push_back("See kyua(1) for more details.");
187*11be35a1SLionel Sambuc 
188*11be35a1SLionel Sambuc     ATF_REQUIRE(expected == ui.out_log());
189*11be35a1SLionel Sambuc     ATF_REQUIRE(ui.err_log().empty());
190*11be35a1SLionel Sambuc }
191*11be35a1SLionel Sambuc 
192*11be35a1SLionel Sambuc 
193*11be35a1SLionel Sambuc }  // anonymous namespace
194*11be35a1SLionel Sambuc 
195*11be35a1SLionel Sambuc 
196*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(global__no_options);
ATF_TEST_CASE_BODY(global__no_options)197*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(global__no_options)
198*11be35a1SLionel Sambuc {
199*11be35a1SLionel Sambuc     cmdline::ui_mock ui;
200*11be35a1SLionel Sambuc 
201*11be35a1SLionel Sambuc     cmdline::options_vector general_options;
202*11be35a1SLionel Sambuc 
203*11be35a1SLionel Sambuc     global_test(general_options, std::vector< std::string >(), ui);
204*11be35a1SLionel Sambuc }
205*11be35a1SLionel Sambuc 
206*11be35a1SLionel Sambuc 
207*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(global__some_options);
ATF_TEST_CASE_BODY(global__some_options)208*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(global__some_options)
209*11be35a1SLionel Sambuc {
210*11be35a1SLionel Sambuc     cmdline::ui_mock ui;
211*11be35a1SLionel Sambuc 
212*11be35a1SLionel Sambuc     cmdline::options_vector general_options;
213*11be35a1SLionel Sambuc     const cmdline::bool_option flag_a("flag_a", "Flag A");
214*11be35a1SLionel Sambuc     general_options.push_back(&flag_a);
215*11be35a1SLionel Sambuc     const cmdline::string_option flag_c('c', "lc", "Flag C", "X");
216*11be35a1SLionel Sambuc     general_options.push_back(&flag_c);
217*11be35a1SLionel Sambuc 
218*11be35a1SLionel Sambuc     std::vector< std::string > expected;
219*11be35a1SLionel Sambuc     expected.push_back("  --flag_a        Flag A.");
220*11be35a1SLionel Sambuc     expected.push_back("  -c X, --lc=X    Flag C.");
221*11be35a1SLionel Sambuc 
222*11be35a1SLionel Sambuc     global_test(general_options, expected, ui);
223*11be35a1SLionel Sambuc }
224*11be35a1SLionel Sambuc 
225*11be35a1SLionel Sambuc 
226*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(subcommand__simple);
ATF_TEST_CASE_BODY(subcommand__simple)227*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(subcommand__simple)
228*11be35a1SLionel Sambuc {
229*11be35a1SLionel Sambuc     cmdline::options_vector general_options;
230*11be35a1SLionel Sambuc 
231*11be35a1SLionel Sambuc     cmdline::commands_map< cli::cli_command > mock_commands;
232*11be35a1SLionel Sambuc     setup(mock_commands);
233*11be35a1SLionel Sambuc 
234*11be35a1SLionel Sambuc     cmdline::args_vector args;
235*11be35a1SLionel Sambuc     args.push_back("help");
236*11be35a1SLionel Sambuc     args.push_back("mock_simple");
237*11be35a1SLionel Sambuc 
238*11be35a1SLionel Sambuc     cmd_help cmd(&general_options, &mock_commands);
239*11be35a1SLionel Sambuc     cmdline::ui_mock ui;
240*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(EXIT_SUCCESS, cmd.main(&ui, args, engine::default_config()));
241*11be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_collection("^Usage: progname \\[general_options\\] "
242*11be35a1SLionel Sambuc                                    "mock_simple$", ui.out_log()));
243*11be35a1SLionel Sambuc     ATF_REQUIRE(!atf::utils::grep_collection("Available.*options", ui.out_log()));
244*11be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_collection("^See kyua-mock_simple\\(1\\) for more "
245*11be35a1SLionel Sambuc                                    "details.", ui.out_log()));
246*11be35a1SLionel Sambuc     ATF_REQUIRE(ui.err_log().empty());
247*11be35a1SLionel Sambuc }
248*11be35a1SLionel Sambuc 
249*11be35a1SLionel Sambuc 
250*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(subcommand__complex);
ATF_TEST_CASE_BODY(subcommand__complex)251*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(subcommand__complex)
252*11be35a1SLionel Sambuc {
253*11be35a1SLionel Sambuc     cmdline::options_vector general_options;
254*11be35a1SLionel Sambuc     const cmdline::bool_option global_a("global_a", "Global A");
255*11be35a1SLionel Sambuc     general_options.push_back(&global_a);
256*11be35a1SLionel Sambuc     const cmdline::string_option global_c('c', "global_c", "Global C",
257*11be35a1SLionel Sambuc                                           "c_global");
258*11be35a1SLionel Sambuc     general_options.push_back(&global_c);
259*11be35a1SLionel Sambuc 
260*11be35a1SLionel Sambuc     cmdline::commands_map< cli::cli_command > mock_commands;
261*11be35a1SLionel Sambuc     setup(mock_commands);
262*11be35a1SLionel Sambuc 
263*11be35a1SLionel Sambuc     cmdline::args_vector args;
264*11be35a1SLionel Sambuc     args.push_back("help");
265*11be35a1SLionel Sambuc     args.push_back("mock_complex");
266*11be35a1SLionel Sambuc 
267*11be35a1SLionel Sambuc     cmd_help cmd(&general_options, &mock_commands);
268*11be35a1SLionel Sambuc     cmdline::ui_mock ui;
269*11be35a1SLionel Sambuc     ATF_REQUIRE_EQ(EXIT_SUCCESS, cmd.main(&ui, args, engine::default_config()));
270*11be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_collection(
271*11be35a1SLionel Sambuc         "^Usage: progname \\[general_options\\] mock_complex "
272*11be35a1SLionel Sambuc         "\\[command_options\\] \\[arg1 .. argN\\]$", ui.out_log()));
273*11be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_collection("Available general options",
274*11be35a1SLionel Sambuc                                             ui.out_log()));
275*11be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_collection("--global_a", ui.out_log()));
276*11be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_collection("--global_c=c_global",
277*11be35a1SLionel Sambuc                                             ui.out_log()));
278*11be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_collection("Available command options",
279*11be35a1SLionel Sambuc                                             ui.out_log()));
280*11be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_collection("--flag_a   *Flag A",
281*11be35a1SLionel Sambuc                                             ui.out_log()));
282*11be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_collection("-b.*--flag_b   *Flag B",
283*11be35a1SLionel Sambuc                                             ui.out_log()));
284*11be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_collection(
285*11be35a1SLionel Sambuc         "-c c_arg.*--flag_c=c_arg   *Flag C", ui.out_log()));
286*11be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_collection(
287*11be35a1SLionel Sambuc         "--flag_d=d_arg   *Flag D.*default.*foo", ui.out_log()));
288*11be35a1SLionel Sambuc     ATF_REQUIRE(atf::utils::grep_collection(
289*11be35a1SLionel Sambuc         "^See kyua-mock_complex\\(1\\) for more details.", ui.out_log()));
290*11be35a1SLionel Sambuc     ATF_REQUIRE(ui.err_log().empty());
291*11be35a1SLionel Sambuc }
292*11be35a1SLionel Sambuc 
293*11be35a1SLionel Sambuc 
294*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(subcommand__unknown);
ATF_TEST_CASE_BODY(subcommand__unknown)295*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(subcommand__unknown)
296*11be35a1SLionel Sambuc {
297*11be35a1SLionel Sambuc     cmdline::options_vector general_options;
298*11be35a1SLionel Sambuc 
299*11be35a1SLionel Sambuc     cmdline::commands_map< cli::cli_command > mock_commands;
300*11be35a1SLionel Sambuc     setup(mock_commands);
301*11be35a1SLionel Sambuc 
302*11be35a1SLionel Sambuc     cmdline::args_vector args;
303*11be35a1SLionel Sambuc     args.push_back("help");
304*11be35a1SLionel Sambuc     args.push_back("foobar");
305*11be35a1SLionel Sambuc 
306*11be35a1SLionel Sambuc     cmd_help cmd(&general_options, &mock_commands);
307*11be35a1SLionel Sambuc     cmdline::ui_mock ui;
308*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(cmdline::usage_error, "command foobar.*not exist",
309*11be35a1SLionel Sambuc                          cmd.main(&ui, args, engine::default_config()));
310*11be35a1SLionel Sambuc     ATF_REQUIRE(ui.out_log().empty());
311*11be35a1SLionel Sambuc     ATF_REQUIRE(ui.err_log().empty());
312*11be35a1SLionel Sambuc }
313*11be35a1SLionel Sambuc 
314*11be35a1SLionel Sambuc 
315*11be35a1SLionel Sambuc ATF_TEST_CASE_WITHOUT_HEAD(invalid_args);
ATF_TEST_CASE_BODY(invalid_args)316*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(invalid_args)
317*11be35a1SLionel Sambuc {
318*11be35a1SLionel Sambuc     cmdline::options_vector general_options;
319*11be35a1SLionel Sambuc 
320*11be35a1SLionel Sambuc     cmdline::commands_map< cli::cli_command > mock_commands;
321*11be35a1SLionel Sambuc     setup(mock_commands);
322*11be35a1SLionel Sambuc 
323*11be35a1SLionel Sambuc     cmdline::args_vector args;
324*11be35a1SLionel Sambuc     args.push_back("help");
325*11be35a1SLionel Sambuc     args.push_back("mock_simple");
326*11be35a1SLionel Sambuc     args.push_back("mock_complex");
327*11be35a1SLionel Sambuc 
328*11be35a1SLionel Sambuc     cmd_help cmd(&general_options, &mock_commands);
329*11be35a1SLionel Sambuc     cmdline::ui_mock ui;
330*11be35a1SLionel Sambuc     ATF_REQUIRE_THROW_RE(cmdline::usage_error, "Too many arguments",
331*11be35a1SLionel Sambuc                          cmd.main(&ui, args, engine::default_config()));
332*11be35a1SLionel Sambuc     ATF_REQUIRE(ui.out_log().empty());
333*11be35a1SLionel Sambuc     ATF_REQUIRE(ui.err_log().empty());
334*11be35a1SLionel Sambuc }
335*11be35a1SLionel Sambuc 
336*11be35a1SLionel Sambuc 
ATF_INIT_TEST_CASES(tcs)337*11be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
338*11be35a1SLionel Sambuc {
339*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, global__no_options);
340*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, global__some_options);
341*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, subcommand__simple);
342*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, subcommand__complex);
343*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, subcommand__unknown);
344*11be35a1SLionel Sambuc     ATF_ADD_TEST_CASE(tcs, invalid_args);
345*11be35a1SLionel Sambuc }
346