File: command_line_parser_test.c

package info (click to toggle)
aws-crt-python 0.16.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 78,328 kB
  • sloc: ansic: 330,743; python: 18,949; makefile: 6,271; sh: 3,712; asm: 754; cpp: 699; ruby: 208; java: 77; perl: 73; javascript: 46; xml: 11
file content (221 lines) | stat: -rw-r--r-- 8,551 bytes parent folder | download | duplicates (3)
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
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */
#include <aws/common/command_line_parser.h>
#include <aws/testing/aws_test_harness.h>

/* If this is tested from a dynamic library, the static state needs to be reset */
static void s_reset_static_state(void) {
    aws_cli_optind = 1;
}

static int s_test_short_argument_parse_fn(struct aws_allocator *allocator, void *ctx) {
    (void)allocator;
    (void)ctx;
    struct aws_cli_option options[] = {
        {.name = NULL, .has_arg = AWS_CLI_OPTIONS_NO_ARGUMENT, .flag = NULL, .val = 'a'},
        {.name = "beeee", .has_arg = AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, .flag = NULL, .val = 'b'},
        {.name = NULL, .has_arg = AWS_CLI_OPTIONS_OPTIONAL_ARGUMENT, .flag = NULL, .val = 'c'},
        {.name = NULL, .has_arg = 0, .flag = NULL, .val = 0},
    };

    char *const args[] = {
        "prog-name",
        "-a",
        "-b",
        "bval",
        "-c",
    };
    int argc = 5;
    int longindex = 0;
    s_reset_static_state();
    int arg = aws_cli_getopt_long(argc, args, "ab:c", options, &longindex);
    ASSERT_INT_EQUALS('a', arg);
    ASSERT_INT_EQUALS(0, longindex);
    ASSERT_INT_EQUALS(2, aws_cli_optind);
    arg = aws_cli_getopt_long(argc, args, "ab:c", options, &longindex);
    ASSERT_INT_EQUALS('b', arg);
    ASSERT_STR_EQUALS("bval", aws_cli_optarg);
    ASSERT_INT_EQUALS(1, longindex);
    ASSERT_INT_EQUALS(4, aws_cli_optind);
    arg = aws_cli_getopt_long(argc, args, "ab:c", options, &longindex);
    ASSERT_INT_EQUALS('c', arg);
    ASSERT_INT_EQUALS(2, longindex);
    ASSERT_INT_EQUALS(-1, aws_cli_getopt_long(argc, args, "ab:c", options, &longindex));

    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(short_argument_parse, s_test_short_argument_parse_fn)

static int s_test_long_argument_parse_fn(struct aws_allocator *allocator, void *ctx) {
    (void)allocator;
    (void)ctx;
    struct aws_cli_option options[] = {
        {.name = "aaee", .has_arg = AWS_CLI_OPTIONS_NO_ARGUMENT, .flag = NULL, .val = 'a'},
        {.name = "beeee", .has_arg = AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, .flag = NULL, .val = 'b'},
        {.name = "cceeee", .has_arg = AWS_CLI_OPTIONS_OPTIONAL_ARGUMENT, .flag = NULL, .val = 'c'},
        {.name = NULL, .has_arg = 0, .flag = NULL, .val = 0},
    };

    char *const args[] = {
        "prog-name",
        "--aaee",
        "--beeee",
        "bval",
        "-cceeee",
    };
    int argc = 5;
    int longindex = 0;
    s_reset_static_state();
    int arg = aws_cli_getopt_long(argc, args, "ab:c", options, &longindex);
    ASSERT_INT_EQUALS('a', arg);
    ASSERT_INT_EQUALS(0, longindex);
    ASSERT_INT_EQUALS(2, aws_cli_optind);
    arg = aws_cli_getopt_long(argc, args, "ab:c", options, &longindex);
    ASSERT_INT_EQUALS('b', arg);
    ASSERT_STR_EQUALS("bval", aws_cli_optarg);
    ASSERT_INT_EQUALS(1, longindex);
    ASSERT_INT_EQUALS(4, aws_cli_optind);
    arg = aws_cli_getopt_long(argc, args, "ab:c", options, &longindex);
    ASSERT_INT_EQUALS('c', arg);
    ASSERT_INT_EQUALS(2, longindex);

    ASSERT_INT_EQUALS(-1, aws_cli_getopt_long(argc, args, "ab:c", options, &longindex));

    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(long_argument_parse, s_test_long_argument_parse_fn)

static int s_test_unqualified_argument_parse_fn(struct aws_allocator *allocator, void *ctx) {
    (void)allocator;
    (void)ctx;
    struct aws_cli_option options[] = {
        {.name = "aaee", .has_arg = AWS_CLI_OPTIONS_NO_ARGUMENT, .flag = NULL, .val = 'a'},
        {.name = "beeee", .has_arg = AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, .flag = NULL, .val = 'b'},
        {.name = "cceeee", .has_arg = AWS_CLI_OPTIONS_OPTIONAL_ARGUMENT, .flag = NULL, .val = 'c'},
        {.name = NULL, .has_arg = 0, .flag = NULL, .val = 0},
    };

    char *const args[] = {"prog-name", "-a", "--beeee", "bval", "-c", "operand"};
    int argc = 6;
    int longindex = 0;
    s_reset_static_state();
    int arg = aws_cli_getopt_long(argc, args, "ab:c", options, &longindex);
    ASSERT_INT_EQUALS('a', arg);
    ASSERT_INT_EQUALS(0, longindex);
    ASSERT_INT_EQUALS(2, aws_cli_optind);
    arg = aws_cli_getopt_long(argc, args, "ab:c", options, &longindex);
    ASSERT_INT_EQUALS('b', arg);
    ASSERT_STR_EQUALS("bval", aws_cli_optarg);
    ASSERT_INT_EQUALS(1, longindex);
    ASSERT_INT_EQUALS(4, aws_cli_optind);
    arg = aws_cli_getopt_long(argc, args, "ab:c", options, &longindex);
    ASSERT_INT_EQUALS('c', arg);
    ASSERT_INT_EQUALS(2, longindex);

    ASSERT_INT_EQUALS(0x02, aws_cli_getopt_long(argc, args, "ab:c", options, &longindex));
    ASSERT_TRUE(aws_cli_optind == argc);
    ASSERT_STR_EQUALS("operand", aws_cli_positional_arg);

    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(unqualified_argument_parse, s_test_unqualified_argument_parse_fn)

static int s_test_unknown_argument_parse_fn(struct aws_allocator *allocator, void *ctx) {
    (void)allocator;
    (void)ctx;
    struct aws_cli_option options[] = {
        {.name = "aaee", .has_arg = AWS_CLI_OPTIONS_NO_ARGUMENT, .flag = NULL, .val = 'a'},
        {.name = "beeee", .has_arg = AWS_CLI_OPTIONS_REQUIRED_ARGUMENT, .flag = NULL, .val = 'b'},
        {.name = "cceeee", .has_arg = AWS_CLI_OPTIONS_OPTIONAL_ARGUMENT, .flag = NULL, .val = 'c'},
        {.name = NULL, .has_arg = 0, .flag = NULL, .val = 0},
    };

    char *const args[] = {"prog-name", "-BOO!", "--beeee", "bval", "-c", "operand"};
    int argc = 6;
    int longindex = 0;
    s_reset_static_state();
    int arg = aws_cli_getopt_long(argc, args, "ab:c", options, &longindex);
    ASSERT_INT_EQUALS('?', arg);
    ASSERT_INT_EQUALS(0, longindex);
    ASSERT_INT_EQUALS(2, aws_cli_optind);
    arg = aws_cli_getopt_long(argc, args, "ab:c", options, &longindex);
    ASSERT_INT_EQUALS('b', arg);
    ASSERT_STR_EQUALS("bval", aws_cli_optarg);
    ASSERT_INT_EQUALS(1, longindex);
    ASSERT_INT_EQUALS(4, aws_cli_optind);
    arg = aws_cli_getopt_long(argc, args, "ab:c", options, &longindex);
    ASSERT_INT_EQUALS('c', arg);
    ASSERT_INT_EQUALS(2, longindex);

    arg = aws_cli_getopt_long(argc, args, "ab:c", options, &longindex);
    ASSERT_TRUE(arg == 0x02);
    ASSERT_TRUE(aws_cli_optind == argc);
    ASSERT_STR_EQUALS("operand", aws_cli_positional_arg);

    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(unknown_argument_parse, s_test_unknown_argument_parse_fn)

struct subcommand_dispatch_data {
    const char *command_name;
    int argc;
    char *const *argv;
};

static int s_subcommand_callback(int argc, char *const argv[], const char *command_name, void *user_data) {
    struct subcommand_dispatch_data *dispatch_data = user_data;
    dispatch_data->command_name = command_name;
    dispatch_data->argc = argc;
    dispatch_data->argv = argv;

    return AWS_OP_SUCCESS;
}

static int s_test_command_dispatch_fn(struct aws_allocator *allocator, void *ctx) {
    (void)allocator;
    (void)ctx;

    struct subcommand_dispatch_data dispatch_data;
    AWS_ZERO_STRUCT(dispatch_data);

    struct aws_cli_subcommand_dispatch dispatch_table[] = {
        {
            .command_name = "command1",
            .subcommand_fn = s_subcommand_callback,
        },
        {
            .command_name = "command2",
            .subcommand_fn = s_subcommand_callback,
        },
    };

    char *const args_1[] = {"prog-name", "command1", "-BOO!", "--beeee", "bval", "-c", "operand"};
    ASSERT_SUCCESS(aws_cli_dispatch_on_subcommand(7, args_1, dispatch_table, 2, &dispatch_data));
    ASSERT_STR_EQUALS("command1", dispatch_data.command_name);
    ASSERT_INT_EQUALS(6, dispatch_data.argc);
    ASSERT_STR_EQUALS("command1", dispatch_data.argv[0]);

    AWS_ZERO_STRUCT(dispatch_data);
    char *const args_2[] = {"prog-name", "command2", "-BOO!", "--beeee", "bval", "-c", "operand"};
    ASSERT_SUCCESS(aws_cli_dispatch_on_subcommand(7, args_2, dispatch_table, 2, &dispatch_data));
    ASSERT_STR_EQUALS("command2", dispatch_data.command_name);
    ASSERT_INT_EQUALS(6, dispatch_data.argc);
    ASSERT_STR_EQUALS("command2", dispatch_data.argv[0]);

    char *const args_3[] = {"prog-name", "command3", "-BOO!", "--beeee", "bval", "-c", "operand"};
    ASSERT_ERROR(AWS_ERROR_UNIMPLEMENTED, aws_cli_dispatch_on_subcommand(7, args_3, dispatch_table, 2, &dispatch_data));

    char *const args_4[] = {"prog-name"};
    ASSERT_ERROR(
        AWS_ERROR_INVALID_ARGUMENT, aws_cli_dispatch_on_subcommand(1, args_4, dispatch_table, 2, &dispatch_data));

    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(test_command_dispatch, s_test_command_dispatch_fn)