aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/programoptions/programoptions_test.cpp
blob: bbb5e2ffc20d520c1ce5552ccdbe89b63c0438f4 (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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "programoptions_testutils.h"
#include <vespa/vespalib/util/programoptions.h>
#include <vespa/vespalib/testkit/testapp.h>
#include <iostream>

namespace vespalib {

class Test : public vespalib::TestApp
{
public:
    void testSyntaxPage();
    void testNormalUsage();
    void testFailures();
    void testVectorArgument();
    void testAllHiddenOption();
    void testOptionsAfterArguments();
    int Main() override;
};

int
Test::Main()
{
    TEST_INIT("programoptions_test");
    srandom(1);
    testSyntaxPage();
    testNormalUsage();
    testFailures();
    testVectorArgument();
    testAllHiddenOption();
    // Currently not supported
    // testOptionsAfterArguments();
    TEST_DONE();
}

struct MyOptions : public ProgramOptions {
    bool boolOpt;
    bool boolWithDefOpt;
    int intOpt;
    uint32_t uintOpt;
    float floatOpt;
    std::string stringOpt;
    std::string argString;
    int argInt;
    std::string argOptionalString;
    std::map<std::string, std::string> properties;
    int anotherOptionalArg;

    MyOptions(int argc, const char *const *argv);
    ~MyOptions();
};

MyOptions::MyOptions(int argc, const char* const* argv)
    : ProgramOptions(argc, argv)
{
        // Required options
    addOption("uintopt u", uintOpt, "Sets an unsigned int");
        // Optional options
    addOption("b bool", boolOpt, "Enables a flag");
    addOption("boolwithdef", boolWithDefOpt, true, "If set turns to false");

    addOption("intopt i", intOpt, 5, "Sets a signed int");
    addOption("floatopt", floatOpt, 4.0f, "Sets a float\nMultiline baby");
    addOption("string s", stringOpt, std::string("ballalaika"),
              "Sets a string value. This is a very long description that "
              "should be broken down into multiple lines in some sensible "
              "way.");
    addOptionHeader("Advanced options");
    addOption("p properties", properties, "Property map");
    addHiddenIdentifiers("prop");
    setArgumentTypeName("key");
    setArgumentTypeName("value", 1);

    addArgument("argString", argString, "Required string argument.");
    addArgument("argInt", argInt, "Required int argument.");
    addArgument("argOptionalString", argOptionalString, std::string("foo"),
                "Optional string argument with a long description so we "
                "can see that it will be broken correctly.");
    addArgument("argSecondOptional", anotherOptionalArg, 3,
                "Yet another optional argument");

    setSyntaxMessage("A test program to see if this utility works.");
    setSyntaxPageMaxLeftColumnSize(25);
}

MyOptions::~MyOptions() { }

void Test::testSyntaxPage() {
    AppOptions opts("myapp");
    MyOptions options(opts.getArgCount(), opts.getArguments());
    std::ostringstream actual;
    options.writeSyntaxPage(actual);

    std::string expected(
"\nA test program to see if this utility works.\n\n"
"Usage: myapp [options] <argString> <argInt> [argOptionalString] [argSecondOptional]\n\n"
"Arguments:\n"
" argString (string)      : Required string argument.\n"
" argInt (int)            : Required int argument.\n"
" argOptionalString (string)\n"
"                         : Optional string argument with a long description so\n"
"                           we can see that it will be broken correctly.\n"
"                           (optional)\n"
" argSecondOptional (int) : Yet another optional argument (optional)\n\n"
"Options:\n"
" --uintopt -u <uint>  : Sets an unsigned int (required)\n"
" -b --bool            : Enables a flag\n"
" --boolwithdef        : If set turns to false\n"
" --intopt -i <int>    : Sets a signed int (default 5)\n"
" --floatopt <float>   : Sets a float\n"
"                        Multiline baby (default 4)\n"
" --string -s <string> : Sets a string value. This is a very long description\n"
"                        that should be broken down into multiple lines in some\n"
"                        sensible way. (default \"ballalaika\")\n\n"
"Advanced options:\n"
" -p --properties <key> <value> : Property map (default empty)\n"
    );
    EXPECT_EQUAL(expected, actual.str());
}

void Test::testNormalUsage() {
    {
        AppOptions opts("myapp -b --uintopt 4 -s foo tit 1 tei 6");
        MyOptions options(opts.getArgCount(), opts.getArguments());
        options.parse();
        EXPECT_EQUAL(true, options.boolOpt);
        EXPECT_EQUAL(true, options.boolWithDefOpt);
        EXPECT_EQUAL(5, options.intOpt);
        EXPECT_EQUAL(4u, options.uintOpt);
        EXPECT_APPROX(4, options.floatOpt, 0.00001);
        EXPECT_EQUAL("foo", options.stringOpt);
        EXPECT_EQUAL("tit", options.argString);
        EXPECT_EQUAL(1, options.argInt);
        EXPECT_EQUAL("tei", options.argOptionalString);
        EXPECT_EQUAL(0u, options.properties.size());
        EXPECT_EQUAL(6, options.anotherOptionalArg);
    }
    {
        AppOptions opts("myapp --uintopt 6 tit 1");
        MyOptions options(opts.getArgCount(), opts.getArguments());
        options.parse();
        EXPECT_EQUAL(false, options.boolOpt);
        EXPECT_EQUAL(true, options.boolWithDefOpt);
        EXPECT_EQUAL(5, options.intOpt);
        EXPECT_EQUAL(6u, options.uintOpt);
        EXPECT_APPROX(4, options.floatOpt, 0.00001);
        EXPECT_EQUAL("ballalaika", options.stringOpt);
        EXPECT_EQUAL("tit", options.argString);
        EXPECT_EQUAL(1, options.argInt);
        EXPECT_EQUAL("foo", options.argOptionalString);
        EXPECT_EQUAL(0u, options.properties.size());
        EXPECT_EQUAL(3, options.anotherOptionalArg);
    }
        // Arguments coming after options.
        // (Required for nesting of short options)
    {
        AppOptions opts("myapp --uintopt --intopt 6 -8 tit 1 tei");
        MyOptions options(opts.getArgCount(), opts.getArguments());
        options.parse();
        EXPECT_EQUAL(false, options.boolOpt);
        EXPECT_EQUAL(true, options.boolWithDefOpt);
        EXPECT_EQUAL(-8, options.intOpt);
        EXPECT_EQUAL(6u, options.uintOpt);
        EXPECT_APPROX(4, options.floatOpt, 0.00001);
        EXPECT_EQUAL("ballalaika", options.stringOpt);
        EXPECT_EQUAL("tit", options.argString);
        EXPECT_EQUAL(1, options.argInt);
        EXPECT_EQUAL("tei", options.argOptionalString);
        EXPECT_EQUAL(0u, options.properties.size());
    }
    {
        AppOptions opts( "myapp -uib 6 -8 --boolwithdef tit 1 tei");
        MyOptions options(opts.getArgCount(), opts.getArguments());
        options.parse();
        EXPECT_EQUAL(true, options.boolOpt);
        EXPECT_EQUAL(false, options.boolWithDefOpt);
        EXPECT_EQUAL(-8, options.intOpt);
        EXPECT_EQUAL(6u, options.uintOpt);
        EXPECT_APPROX(4, options.floatOpt, 0.00001);
        EXPECT_EQUAL("ballalaika", options.stringOpt);
        EXPECT_EQUAL("tit", options.argString);
        EXPECT_EQUAL(1, options.argInt);
        EXPECT_EQUAL("tei", options.argOptionalString);
        EXPECT_EQUAL(0u, options.properties.size());
    }
        // Properties
    {
        AppOptions opts("myapp -u 6 -p foo bar --prop hmm brr tit 1 tei");
        MyOptions options(opts.getArgCount(), opts.getArguments());
        options.parse();
        EXPECT_EQUAL(false, options.boolOpt);
        EXPECT_EQUAL(true, options.boolWithDefOpt);
        EXPECT_EQUAL(5, options.intOpt);
        EXPECT_EQUAL(6u, options.uintOpt);
        EXPECT_APPROX(4, options.floatOpt, 0.00001);
        EXPECT_EQUAL("ballalaika", options.stringOpt);
        EXPECT_EQUAL("tit", options.argString);
        EXPECT_EQUAL(1, options.argInt);
        EXPECT_EQUAL("tei", options.argOptionalString);
        EXPECT_EQUAL(2u, options.properties.size());
        EXPECT_EQUAL("bar", options.properties["foo"]);
        EXPECT_EQUAL("brr", options.properties["hmm"]);
    }
}

void Test::testFailures() {
        // Non-existing long option
    {
        AppOptions opts("myapp -b --uintopt 4 -s foo --none");
        MyOptions options(opts.getArgCount(), opts.getArguments());
        try{
            options.parse();
            TEST_FATAL("Expected exception");
        } catch (InvalidCommandLineArgumentsException& e) {
            EXPECT_EQUAL("Invalid option 'none'.", e.getMessage());
        }
    }
        // Non-existing short option
    {
        AppOptions opts("myapp -b --uintopt 4 -s foo -q");
        MyOptions options(opts.getArgCount(), opts.getArguments());
        try{
            options.parse();
            TEST_FATAL("Expected exception");
        } catch (InvalidCommandLineArgumentsException& e) {
            EXPECT_EQUAL("Invalid option 'q'.", e.getMessage());
        }
    }
        // Lacking option argument
    {
        AppOptions opts("myapp -b --uintopt 4 -s");
        MyOptions options(opts.getArgCount(), opts.getArguments());
        try{
            options.parse();
            TEST_FATAL("Expected exception");
        } catch (InvalidCommandLineArgumentsException& e) {
            EXPECT_EQUAL("Option 's' needs 1 arguments. Only 0 available.",
                       e.getMessage());
        }
    }
        // Out of signed ranged
    {
        AppOptions opts("myapp -b --uintopt 4 -intopt 3000000000");
        MyOptions options(opts.getArgCount(), opts.getArguments());
        try{
            options.parse();
            TEST_FATAL("Expected exception");
        } catch (InvalidCommandLineArgumentsException& e) {
            EXPECT_EQUAL("The argument '3000000000' can not be interpreted as a "
                       "number of type int.", e.getMessage());
        }
    }
        // Negative value to unsigned var (Currently doesnt fail)
/*
    {
        AppOptions opts("myapp -b --uintopt -1 foo 0");
        MyOptions options(opts.getArgCount(), opts.getArguments());
        try{
            options.parse();
            TEST_FATAL("Expected exception");
        } catch (InvalidCommandLineArgumentsException& e) {
            EXPECT_EQUAL("The argument '-1' can not be interpreted as a "
                         "number of type uint.", e.getMessage());
        }
    }
    */
        // Lacking required option
    {
        AppOptions opts("myapp -b");
        MyOptions options(opts.getArgCount(), opts.getArguments());
        try{
            options.parse();
            TEST_FATAL("Expected exception");
        } catch (InvalidCommandLineArgumentsException& e) {
            EXPECT_EQUAL("Option 'uintopt' has no default and must be set.",
                       e.getMessage());
        }
    }
        // Lacking required argument
    {
        AppOptions opts("myapp --uintopt 1 tit");
        MyOptions options(opts.getArgCount(), opts.getArguments());
        try{
            options.parse();
            TEST_FATAL("Expected exception");
        } catch (InvalidCommandLineArgumentsException& e) {
            EXPECT_EQUAL("Insufficient data is given to set required argument "
                       "'argInt'.",
                       e.getMessage());
        }
    }
        // Argument of wrong type
    {
        AppOptions opts("myapp --uintopt 1 tit en");
        MyOptions options(opts.getArgCount(), opts.getArguments());
        try{
            options.parse();
            TEST_FATAL("Expected exception");
        } catch (InvalidCommandLineArgumentsException& e) {
            EXPECT_EQUAL("The argument 'en' can not be interpreted as a number "
                       "of type int.",
                       e.getMessage());
        }
    }
}

void Test::testVectorArgument()
{
    AppOptions opts("myapp foo bar baz");
    std::vector<std::string> args;
    ProgramOptions options(opts.getArgCount(), opts.getArguments());
    options.addListArgument("ids", args, "Vector element");
    std::ostringstream actual;
    options.writeSyntaxPage(actual);
    std::string expected(
"\nUsage: myapp [ids...]\n\n"
"Arguments:\n"
" ids (string[]) : Vector element\n"
    );
    EXPECT_EQUAL(expected, actual.str());

    options.parse();
    EXPECT_EQUAL(3u, args.size());
    EXPECT_EQUAL("foo", args[0]);
    EXPECT_EQUAL("bar", args[1]);
    EXPECT_EQUAL("baz", args[2]);
}

void Test::testAllHiddenOption()
{
    AppOptions opts("myapp --foo bar");
    std::string option;
    ProgramOptions options(opts.getArgCount(), opts.getArguments());
    options.addOption("", option, "Description");
    options.addHiddenIdentifiers("foo");
    std::ostringstream actual;
    options.writeSyntaxPage(actual);
    std::string expected("\nUsage: myapp\n");
    EXPECT_EQUAL(expected, actual.str());

    options.parse();
    EXPECT_EQUAL("bar", option);
}

void Test::testOptionsAfterArguments()
{
    AppOptions opts("myapp bar --foo baz");
    std::string option;
    std::string argument;
    ProgramOptions options(opts.getArgCount(), opts.getArguments());
    options.addOption("foo", option, "Description");
    options.addArgument("arg", argument, "Description");
    options.parse();
    EXPECT_EQUAL("baz", option);
    EXPECT_EQUAL("bar", argument);
}

} // vespalib

TEST_APPHOOK(vespalib::Test)