summaryrefslogtreecommitdiff
path: root/utils/cmdline/base_command_test.cpp
blob: 20df8ea49512cfcd39a0b5cbf65f18ff35e37cb5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright 2010 The Kyua Authors.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
//   notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the
//   documentation and/or other materials provided with the distribution.
// * Neither the name of Google Inc. nor the names of its contributors
//   may be used to endorse or promote products derived from this software
//   without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "utils/cmdline/base_command.ipp"

#include <atf-c++.hpp>

#include "utils/cmdline/exceptions.hpp"
#include "utils/cmdline/options.hpp"
#include "utils/cmdline/parser.ipp"
#include "utils/cmdline/ui_mock.hpp"
#include "utils/defs.hpp"

namespace cmdline = utils::cmdline;


namespace {


/// Mock command to test the cmdline::base_command base class.
///
/// \param Data The type of the opaque data object passed to main().
/// \param ExpectedData The value run() will expect to find in the Data object
///     passed to main().
template< typename Data, Data ExpectedData >
class mock_cmd : public cmdline::base_command< Data > {
public:
    /// Indicates if run() has been called already and executed correctly.
    bool executed;

    /// Contains the argument of --the_string after run() is executed.
    std::string optvalue;

    /// Constructs a new mock command.
    mock_cmd(void) :
        cmdline::base_command< Data >("mock", "arg1 [arg2 [arg3]]", 1, 3,
                                      "Command for testing."),
        executed(false)
    {
        this->add_option(cmdline::string_option("the_string", "Test option",
                                                "arg"));
    }

    /// Executes the command.
    ///
    /// \param cmdline Representation of the command line to the subcommand.
    /// \param data Arbitrary data cookie passed to the command.
    ///
    /// \return A hardcoded number for testing purposes.
    int
    run(cmdline::ui* /* ui */,
        const cmdline::parsed_cmdline& cmdline, const Data& data)
    {
        if (cmdline.has_option("the_string"))
            optvalue = cmdline.get_option< cmdline::string_option >(
                "the_string");
        ATF_REQUIRE_EQ(ExpectedData, data);
        executed = true;
        return 1234;
    }
};


/// Mock command to test the cmdline::base_command_no_data base class.
class mock_cmd_no_data : public cmdline::base_command_no_data {
public:
    /// Indicates if run() has been called already and executed correctly.
    bool executed;

    /// Contains the argument of --the_string after run() is executed.
    std::string optvalue;

    /// Constructs a new mock command.
    mock_cmd_no_data(void) :
        cmdline::base_command_no_data("mock", "arg1 [arg2 [arg3]]", 1, 3,
                                      "Command for testing."),
        executed(false)
    {
        add_option(cmdline::string_option("the_string", "Test option", "arg"));
    }

    /// Executes the command.
    ///
    /// \param cmdline Representation of the command line to the subcommand.
    ///
    /// \return A hardcoded number for testing purposes.
    int
    run(cmdline::ui* /* ui */,
        const cmdline::parsed_cmdline& cmdline)
    {
        if (cmdline.has_option("the_string"))
            optvalue = cmdline.get_option< cmdline::string_option >(
                "the_string");
        executed = true;
        return 1234;
    }
};


/// Implementation of a command to get access to parse_cmdline().
class parse_cmdline_portal : public cmdline::command_proto {
public:
    /// Constructs a new mock command.
    parse_cmdline_portal(void) :
        cmdline::command_proto("portal", "arg1 [arg2 [arg3]]", 1, 3,
                               "Command for testing.")
    {
        this->add_option(cmdline::string_option("the_string", "Test option",
                                                "arg"));
    }

    /// Delegator for the internal parse_cmdline() method.
    ///
    /// \param args The input arguments to be parsed.
    ///
    /// \return The parsed command line, split in options and arguments.
    cmdline::parsed_cmdline
    operator()(const cmdline::args_vector& args) const
    {
        return parse_cmdline(args);
    }
};


}  // anonymous namespace


ATF_TEST_CASE_WITHOUT_HEAD(command_proto__parse_cmdline__ok);
ATF_TEST_CASE_BODY(command_proto__parse_cmdline__ok)
{
    cmdline::args_vector args;
    args.push_back("portal");
    args.push_back("--the_string=foo bar");
    args.push_back("one arg");
    args.push_back("another arg");
    (void)parse_cmdline_portal()(args);
}


ATF_TEST_CASE_WITHOUT_HEAD(command_proto__parse_cmdline__parse_fail);
ATF_TEST_CASE_BODY(command_proto__parse_cmdline__parse_fail)
{
    cmdline::args_vector args;
    args.push_back("portal");
    args.push_back("--foo-bar");
    ATF_REQUIRE_THROW_RE(cmdline::usage_error, "Unknown.*foo-bar",
                         (void)parse_cmdline_portal()(args));
}


ATF_TEST_CASE_WITHOUT_HEAD(command_proto__parse_cmdline__args_invalid);
ATF_TEST_CASE_BODY(command_proto__parse_cmdline__args_invalid)
{
    cmdline::args_vector args;
    args.push_back("portal");

    ATF_REQUIRE_THROW_RE(cmdline::usage_error, "Not enough arguments",
                         (void)parse_cmdline_portal()(args));

    args.push_back("1");
    args.push_back("2");
    args.push_back("3");
    args.push_back("4");
    ATF_REQUIRE_THROW_RE(cmdline::usage_error, "Too many arguments",
                         (void)parse_cmdline_portal()(args));
}


ATF_TEST_CASE_WITHOUT_HEAD(base_command__getters);
ATF_TEST_CASE_BODY(base_command__getters)
{
    mock_cmd< int, 584 > cmd;
    ATF_REQUIRE_EQ("mock", cmd.name());
    ATF_REQUIRE_EQ("arg1 [arg2 [arg3]]", cmd.arg_list());
    ATF_REQUIRE_EQ("Command for testing.", cmd.short_description());
    ATF_REQUIRE_EQ(1, cmd.options().size());
    ATF_REQUIRE_EQ("the_string", cmd.options()[0]->long_name());
}


ATF_TEST_CASE_WITHOUT_HEAD(base_command__main__ok)
ATF_TEST_CASE_BODY(base_command__main__ok)
{
    mock_cmd< int, 584 > cmd;

    cmdline::ui_mock ui;
    cmdline::args_vector args;
    args.push_back("mock");
    args.push_back("--the_string=foo bar");
    args.push_back("one arg");
    args.push_back("another arg");
    ATF_REQUIRE_EQ(1234, cmd.main(&ui, args, 584));
    ATF_REQUIRE(cmd.executed);
    ATF_REQUIRE_EQ("foo bar", cmd.optvalue);
}


ATF_TEST_CASE_WITHOUT_HEAD(base_command__main__parse_cmdline_fail)
ATF_TEST_CASE_BODY(base_command__main__parse_cmdline_fail)
{
    mock_cmd< int, 584 > cmd;

    cmdline::ui_mock ui;
    cmdline::args_vector args;
    args.push_back("mock");
    args.push_back("--foo-bar");
    ATF_REQUIRE_THROW_RE(cmdline::usage_error, "Unknown.*foo-bar",
                         cmd.main(&ui, args, 584));
    ATF_REQUIRE(!cmd.executed);
}


ATF_TEST_CASE_WITHOUT_HEAD(base_command_no_data__getters);
ATF_TEST_CASE_BODY(base_command_no_data__getters)
{
    mock_cmd_no_data cmd;
    ATF_REQUIRE_EQ("mock", cmd.name());
    ATF_REQUIRE_EQ("arg1 [arg2 [arg3]]", cmd.arg_list());
    ATF_REQUIRE_EQ("Command for testing.", cmd.short_description());
    ATF_REQUIRE_EQ(1, cmd.options().size());
    ATF_REQUIRE_EQ("the_string", cmd.options()[0]->long_name());
}


ATF_TEST_CASE_WITHOUT_HEAD(base_command_no_data__main__ok)
ATF_TEST_CASE_BODY(base_command_no_data__main__ok)
{
    mock_cmd_no_data cmd;

    cmdline::ui_mock ui;
    cmdline::args_vector args;
    args.push_back("mock");
    args.push_back("--the_string=foo bar");
    args.push_back("one arg");
    args.push_back("another arg");
    ATF_REQUIRE_EQ(1234, cmd.main(&ui, args));
    ATF_REQUIRE(cmd.executed);
    ATF_REQUIRE_EQ("foo bar", cmd.optvalue);
}


ATF_TEST_CASE_WITHOUT_HEAD(base_command_no_data__main__parse_cmdline_fail)
ATF_TEST_CASE_BODY(base_command_no_data__main__parse_cmdline_fail)
{
    mock_cmd_no_data cmd;

    cmdline::ui_mock ui;
    cmdline::args_vector args;
    args.push_back("mock");
    args.push_back("--foo-bar");
    ATF_REQUIRE_THROW_RE(cmdline::usage_error, "Unknown.*foo-bar",
                         cmd.main(&ui, args));
    ATF_REQUIRE(!cmd.executed);
}


ATF_INIT_TEST_CASES(tcs)
{
    ATF_ADD_TEST_CASE(tcs, command_proto__parse_cmdline__ok);
    ATF_ADD_TEST_CASE(tcs, command_proto__parse_cmdline__parse_fail);
    ATF_ADD_TEST_CASE(tcs, command_proto__parse_cmdline__args_invalid);

    ATF_ADD_TEST_CASE(tcs, base_command__getters);
    ATF_ADD_TEST_CASE(tcs, base_command__main__ok);
    ATF_ADD_TEST_CASE(tcs, base_command__main__parse_cmdline_fail);

    ATF_ADD_TEST_CASE(tcs, base_command_no_data__getters);
    ATF_ADD_TEST_CASE(tcs, base_command_no_data__main__ok);
    ATF_ADD_TEST_CASE(tcs, base_command_no_data__main__parse_cmdline_fail);
}