aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/getopt/ut/posix_getopt_ut.cpp
blob: 549a5ae3a2b142e2d0ac326f265f56434fb3fc4b (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
#include <library/cpp/getopt/posix_getopt.h>
 
#include <library/cpp/testing/unittest/registar.h>

using namespace NLastGetopt;

Y_UNIT_TEST_SUITE(TPosixGetoptTest) {
    Y_UNIT_TEST(TestSimple) {
        int argc = 6;
        const char* argv0[] = {"program", "-b", "-f1", "-f", "2", "zzzz"}; 
        char** const argv = (char**)argv0; 

        NLastGetopt::optreset = 1;
        UNIT_ASSERT_VALUES_EQUAL('b', NLastGetopt::getopt(argc, argv, "bf:"));
        UNIT_ASSERT_VALUES_EQUAL('f', NLastGetopt::getopt(argc, argv, "bf:"));
        UNIT_ASSERT_VALUES_EQUAL(NLastGetopt::optarg, TString("1"));
        UNIT_ASSERT_VALUES_EQUAL('f', NLastGetopt::getopt(argc, argv, "bf:"));
        UNIT_ASSERT_VALUES_EQUAL(NLastGetopt::optarg, TString("2"));
        UNIT_ASSERT_VALUES_EQUAL(-1, NLastGetopt::getopt(argc, argv, "bf:"));

        UNIT_ASSERT_VALUES_EQUAL(5, NLastGetopt::optind);
    }

    Y_UNIT_TEST(TestLong) {
        int daggerset = 0;
        /* options descriptor */
        const NLastGetopt::option longopts[] = {
            {"buffy", no_argument, nullptr, 'b'}, 
            {"fluoride", required_argument, nullptr, 'f'}, 
            {"daggerset", no_argument, &daggerset, 1}, 
            {nullptr, 0, nullptr, 0}}; 

        int argc = 7;
        const char* argv0[] = {"program", "-b", "--buffy", "-f1", "--fluoride=2", "--daggerset", "zzzz"}; 
        char** const argv = (char**)argv0; 

        int longIndex;

        NLastGetopt::optreset = 1;
        UNIT_ASSERT_VALUES_EQUAL('b', NLastGetopt::getopt_long(argc, argv, "bf:", longopts, &longIndex));
        UNIT_ASSERT_VALUES_EQUAL(0, longIndex);
        UNIT_ASSERT_VALUES_EQUAL('b', NLastGetopt::getopt_long(argc, argv, "bf:", longopts, nullptr));
        UNIT_ASSERT_VALUES_EQUAL('f', NLastGetopt::getopt_long(argc, argv, "bf:", longopts, &longIndex));
        UNIT_ASSERT_VALUES_EQUAL(1, longIndex);
        UNIT_ASSERT_VALUES_EQUAL('f', NLastGetopt::getopt_long(argc, argv, "bf:", longopts, nullptr));
        UNIT_ASSERT_VALUES_EQUAL(0, NLastGetopt::getopt_long(argc, argv, "bf:", longopts, nullptr));
        UNIT_ASSERT_VALUES_EQUAL(-1, NLastGetopt::getopt_long(argc, argv, "bf:", longopts, nullptr));

        UNIT_ASSERT_VALUES_EQUAL(6, NLastGetopt::optind);
    }

    Y_UNIT_TEST(TestLongPermutation) {
        int daggerset = 0;
        /* options descriptor */
        const NLastGetopt::option longopts[] = {
            {"buffy", no_argument, nullptr, 'b'}, 
            {"fluoride", required_argument, nullptr, 'f'}, 
            {"daggerset", no_argument, &daggerset, 1}, 
            {nullptr, 0, nullptr, 0}}; 

        int argc = 7;
        const char* argv0[] = {"program", "aa", "-b", "bb", "cc", "--buffy", "dd"}; 
        char** const argv = (char**)argv0; 

        NLastGetopt::optreset = 1;
        UNIT_ASSERT_VALUES_EQUAL('b', NLastGetopt::getopt_long(argc, argv, "bf:", longopts, nullptr));
        UNIT_ASSERT_VALUES_EQUAL('b', NLastGetopt::getopt_long(argc, argv, "bf:", longopts, nullptr));
        UNIT_ASSERT_VALUES_EQUAL(-1, NLastGetopt::getopt_long(argc, argv, "bf:", longopts, nullptr));

        UNIT_ASSERT_VALUES_EQUAL(3, NLastGetopt::optind);
    }

    Y_UNIT_TEST(TestNoOptionsOptionsWithDoubleDash) {
        const NLastGetopt::option longopts[] = {
            {"buffy", no_argument, nullptr, 'b'}, 
            {"fluoride", no_argument, nullptr, 'f'}, 
            {nullptr, 0, nullptr, 0}}; 

        int argc = 2;
        const char* argv0[] = {"program", "--bf"}; 
        char** const argv = (char**)argv0; 

        NLastGetopt::optreset = 1;
        UNIT_ASSERT_VALUES_EQUAL('?', NLastGetopt::getopt_long(argc, argv, "bf", longopts, nullptr));
    }

    Y_UNIT_TEST(TestLongOnly) {
        const NLastGetopt::option longopts[] = {
            {"foo", no_argument, nullptr, 'F'}, 
            {"fluoride", no_argument, nullptr, 'f'}, 
            {"ogogo", no_argument, nullptr, 'o'}, 
            {nullptr, 0, nullptr, 0}}; 

        int argc = 4;
        const char* argv0[] = {"program", "--foo", "-foo", "-fo"}; 
        char** const argv = (char**)argv0; 

        NLastGetopt::optreset = 1;
        UNIT_ASSERT_VALUES_EQUAL('F', NLastGetopt::getopt_long_only(argc, argv, "fo", longopts, nullptr));
        UNIT_ASSERT_VALUES_EQUAL('F', NLastGetopt::getopt_long_only(argc, argv, "fo", longopts, nullptr));
        UNIT_ASSERT_VALUES_EQUAL('f', NLastGetopt::getopt_long_only(argc, argv, "fo", longopts, nullptr));
        UNIT_ASSERT_VALUES_EQUAL('o', NLastGetopt::getopt_long_only(argc, argv, "fo", longopts, nullptr));
        UNIT_ASSERT_VALUES_EQUAL(-1, NLastGetopt::getopt_long_only(argc, argv, "fo", longopts, nullptr)); 
    }

    Y_UNIT_TEST(TestLongWithoutOnlySingleDashNowAllowed) {
        const NLastGetopt::option longopts[] = {
            {"foo", no_argument, nullptr, 'F'}, 
            {"zoo", no_argument, nullptr, 'z'}, 
            {nullptr, 0, nullptr, 0}}; 

        int argc = 2;
        const char* argv0[] = {"program", "-foo"}; 
        char** const argv = (char**)argv0; 

        NLastGetopt::optreset = 1;
        UNIT_ASSERT_VALUES_EQUAL('?', NLastGetopt::getopt_long(argc, argv, "z", longopts, nullptr));
    }
}