cmdline_test.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include "test/core/util/cmdline.h"
20 
21 #include <string.h>
22 
23 #include <grpc/support/alloc.h>
24 #include <grpc/support/log.h>
25 
28 
29 #define LOG_TEST() gpr_log(GPR_INFO, "test at %s:%d", __FILE__, __LINE__)
30 
31 static void test_simple_int(void) {
32  int x = 1;
33  gpr_cmdline* cl;
34  char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("-foo"),
35  const_cast<char*>("3")};
36 
37  LOG_TEST();
38 
39  cl = gpr_cmdline_create(nullptr);
40  gpr_cmdline_add_int(cl, "foo", nullptr, &x);
41  GPR_ASSERT(x == 1);
43  GPR_ASSERT(x == 3);
45 }
46 
47 static void test_eq_int(void) {
48  int x = 1;
49  gpr_cmdline* cl;
50  char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("-foo=3")};
51 
52  LOG_TEST();
53 
54  cl = gpr_cmdline_create(nullptr);
55  gpr_cmdline_add_int(cl, "foo", nullptr, &x);
56  GPR_ASSERT(x == 1);
58  GPR_ASSERT(x == 3);
60 }
61 
62 static void test_2dash_int(void) {
63  int x = 1;
64  gpr_cmdline* cl;
65  char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--foo"),
66  const_cast<char*>("3")};
67 
68  LOG_TEST();
69 
70  cl = gpr_cmdline_create(nullptr);
71  gpr_cmdline_add_int(cl, "foo", nullptr, &x);
72  GPR_ASSERT(x == 1);
74  GPR_ASSERT(x == 3);
76 }
77 
78 static void test_2dash_eq_int(void) {
79  int x = 1;
80  gpr_cmdline* cl;
81  char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--foo=3")};
82 
83  LOG_TEST();
84 
85  cl = gpr_cmdline_create(nullptr);
86  gpr_cmdline_add_int(cl, "foo", nullptr, &x);
87  GPR_ASSERT(x == 1);
89  GPR_ASSERT(x == 3);
91 }
92 
93 static void test_simple_string(void) {
94  const char* x = nullptr;
95  gpr_cmdline* cl;
96  char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("-foo"),
97  const_cast<char*>("3")};
98 
99  LOG_TEST();
100 
101  cl = gpr_cmdline_create(nullptr);
102  gpr_cmdline_add_string(cl, "foo", nullptr, &x);
103  GPR_ASSERT(x == nullptr);
105  GPR_ASSERT(0 == strcmp(x, "3"));
107 }
108 
109 static void test_eq_string(void) {
110  const char* x = nullptr;
111  gpr_cmdline* cl;
112  char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("-foo=3")};
113 
114  LOG_TEST();
115 
116  cl = gpr_cmdline_create(nullptr);
117  gpr_cmdline_add_string(cl, "foo", nullptr, &x);
118  GPR_ASSERT(x == nullptr);
120  GPR_ASSERT(0 == strcmp(x, "3"));
122 }
123 
124 static void test_2dash_string(void) {
125  const char* x = nullptr;
126  gpr_cmdline* cl;
127  char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--foo"),
128  const_cast<char*>("3")};
129 
130  LOG_TEST();
131 
132  cl = gpr_cmdline_create(nullptr);
133  gpr_cmdline_add_string(cl, "foo", nullptr, &x);
134  GPR_ASSERT(x == nullptr);
136  GPR_ASSERT(0 == strcmp(x, "3"));
138 }
139 
140 static void test_2dash_eq_string(void) {
141  const char* x = nullptr;
142  gpr_cmdline* cl;
143  char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--foo=3")};
144 
145  LOG_TEST();
146 
147  cl = gpr_cmdline_create(nullptr);
148  gpr_cmdline_add_string(cl, "foo", nullptr, &x);
149  GPR_ASSERT(x == nullptr);
151  GPR_ASSERT(0 == strcmp(x, "3"));
153 }
154 
155 static void test_flag_on(void) {
156  int x = 2;
157  gpr_cmdline* cl;
158  char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--foo")};
159 
160  LOG_TEST();
161 
162  cl = gpr_cmdline_create(nullptr);
163  gpr_cmdline_add_flag(cl, "foo", nullptr, &x);
164  GPR_ASSERT(x == 2);
166  GPR_ASSERT(x == 1);
168 }
169 
170 static void test_flag_no(void) {
171  int x = 2;
172  gpr_cmdline* cl;
173  char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--no-foo")};
174 
175  LOG_TEST();
176 
177  cl = gpr_cmdline_create(nullptr);
178  gpr_cmdline_add_flag(cl, "foo", nullptr, &x);
179  GPR_ASSERT(x == 2);
181  GPR_ASSERT(x == 0);
183 }
184 
185 static void test_flag_val_1(void) {
186  int x = 2;
187  gpr_cmdline* cl;
188  char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--foo=1")};
189 
190  LOG_TEST();
191 
192  cl = gpr_cmdline_create(nullptr);
193  gpr_cmdline_add_flag(cl, "foo", nullptr, &x);
194  GPR_ASSERT(x == 2);
196  GPR_ASSERT(x == 1);
198 }
199 
200 static void test_flag_val_0(void) {
201  int x = 2;
202  gpr_cmdline* cl;
203  char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--foo=0")};
204 
205  LOG_TEST();
206 
207  cl = gpr_cmdline_create(nullptr);
208  gpr_cmdline_add_flag(cl, "foo", nullptr, &x);
209  GPR_ASSERT(x == 2);
211  GPR_ASSERT(x == 0);
213 }
214 
215 static void test_flag_val_true(void) {
216  int x = 2;
217  gpr_cmdline* cl;
218  char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--foo=true")};
219 
220  LOG_TEST();
221 
222  cl = gpr_cmdline_create(nullptr);
223  gpr_cmdline_add_flag(cl, "foo", nullptr, &x);
224  GPR_ASSERT(x == 2);
226  GPR_ASSERT(x == 1);
228 }
229 
230 static void test_flag_val_false(void) {
231  int x = 2;
232  gpr_cmdline* cl;
233  char* args[] = {const_cast<char*>(__FILE__),
234  const_cast<char*>("--foo=false")};
235 
236  LOG_TEST();
237 
238  cl = gpr_cmdline_create(nullptr);
239  gpr_cmdline_add_flag(cl, "foo", nullptr, &x);
240  GPR_ASSERT(x == 2);
242  GPR_ASSERT(x == 0);
244 }
245 
246 static void test_many(void) {
247  const char* str = nullptr;
248  int x = 0;
249  int flag = 2;
250  gpr_cmdline* cl;
251 
252  char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--str"),
253  const_cast<char*>("hello"), const_cast<char*>("-x=4"),
254  const_cast<char*>("-no-flag")};
255 
256  LOG_TEST();
257 
258  cl = gpr_cmdline_create(nullptr);
259  gpr_cmdline_add_string(cl, "str", nullptr, &str);
260  gpr_cmdline_add_int(cl, "x", nullptr, &x);
261  gpr_cmdline_add_flag(cl, "flag", nullptr, &flag);
263  GPR_ASSERT(x == 4);
264  GPR_ASSERT(0 == strcmp(str, "hello"));
265  GPR_ASSERT(flag == 0);
267 }
268 
269 static void extra_arg_cb(void* user_data, const char* arg) {
270  int* count = static_cast<int*>(user_data);
271  GPR_ASSERT(arg != nullptr);
272  GPR_ASSERT(strlen(arg) == 1);
273  GPR_ASSERT(arg[0] == 'a' + *count);
274  ++*count;
275 }
276 
277 static void test_extra(void) {
278  gpr_cmdline* cl;
279  int count = 0;
280  char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("a"),
281  const_cast<char*>("b"), const_cast<char*>("c")};
282 
283  LOG_TEST();
284 
285  cl = gpr_cmdline_create(nullptr);
286  gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
287  &count);
289  GPR_ASSERT(count == 3);
291 }
292 
293 static void test_extra_dashdash(void) {
294  gpr_cmdline* cl;
295  int count = 0;
296  char* args[] = {const_cast<char*>(__FILE__), const_cast<char*>("--"),
297  const_cast<char*>("a"), const_cast<char*>("b"),
298  const_cast<char*>("c")};
299 
300  LOG_TEST();
301 
302  cl = gpr_cmdline_create(nullptr);
303  gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
304  &count);
306  GPR_ASSERT(count == 3);
308 }
309 
310 static void test_usage(void) {
311  gpr_cmdline* cl;
312 
313  const char* str = nullptr;
314  int x = 0;
315  int flag = 2;
316 
317  LOG_TEST();
318 
319  cl = gpr_cmdline_create(nullptr);
320  gpr_cmdline_add_string(cl, "str", nullptr, &str);
321  gpr_cmdline_add_int(cl, "x", nullptr, &x);
322  gpr_cmdline_add_flag(cl, "flag", nullptr, &flag);
323  gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
324  nullptr);
325 
327  GPR_ASSERT(usage ==
328  "Usage: test [--str=string] [--x=int] "
329  "[--flag|--no-flag] [file...]\n");
330 
331  usage = gpr_cmdline_usage_string(cl, "/foo/test");
332  GPR_ASSERT(usage ==
333  "Usage: test [--str=string] [--x=int] "
334  "[--flag|--no-flag] [file...]\n");
335 
337 }
338 
339 static void test_help(void) {
340  gpr_cmdline* cl;
341 
342  const char* str = nullptr;
343  int x = 0;
344  int flag = 2;
345 
346  char* help[] = {const_cast<char*>(__FILE__), const_cast<char*>("-h")};
347 
348  LOG_TEST();
349 
350  cl = gpr_cmdline_create(nullptr);
352  gpr_cmdline_add_string(cl, "str", nullptr, &str);
353  gpr_cmdline_add_int(cl, "x", nullptr, &x);
354  gpr_cmdline_add_flag(cl, "flag", nullptr, &flag);
355  gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
356  nullptr);
357 
359 
361 }
362 
363 static void test_badargs1(void) {
364  gpr_cmdline* cl;
365 
366  const char* str = nullptr;
367  int x = 0;
368  int flag = 2;
369 
370  char* bad_arg_name[] = {const_cast<char*>(__FILE__),
371  const_cast<char*>("--y")};
372 
373  LOG_TEST();
374 
375  cl = gpr_cmdline_create(nullptr);
377  gpr_cmdline_add_string(cl, "str", nullptr, &str);
378  gpr_cmdline_add_int(cl, "x", nullptr, &x);
379  gpr_cmdline_add_flag(cl, "flag", nullptr, &flag);
380  gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
381  nullptr);
382 
383  GPR_ASSERT(0 ==
384  gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(bad_arg_name), bad_arg_name));
385 
387 }
388 
389 static void test_badargs2(void) {
390  gpr_cmdline* cl;
391 
392  const char* str = nullptr;
393  int x = 0;
394  int flag = 2;
395 
396  char* bad_int_value[] = {const_cast<char*>(__FILE__),
397  const_cast<char*>("--x"),
398  const_cast<char*>("henry")};
399 
400  LOG_TEST();
401 
402  cl = gpr_cmdline_create(nullptr);
404  gpr_cmdline_add_string(cl, "str", nullptr, &str);
405  gpr_cmdline_add_int(cl, "x", nullptr, &x);
406  gpr_cmdline_add_flag(cl, "flag", nullptr, &flag);
407  gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
408  nullptr);
409 
410  GPR_ASSERT(
411  0 == gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(bad_int_value), bad_int_value));
412 
414 }
415 
416 static void test_badargs3(void) {
417  gpr_cmdline* cl;
418 
419  const char* str = nullptr;
420  int x = 0;
421  int flag = 2;
422 
423  char* bad_bool_value[] = {const_cast<char*>(__FILE__),
424  const_cast<char*>("--flag=henry")};
425 
426  LOG_TEST();
427 
428  cl = gpr_cmdline_create(nullptr);
430  gpr_cmdline_add_string(cl, "str", nullptr, &str);
431  gpr_cmdline_add_int(cl, "x", nullptr, &x);
432  gpr_cmdline_add_flag(cl, "flag", nullptr, &flag);
433  gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
434  nullptr);
435 
436  GPR_ASSERT(0 == gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(bad_bool_value),
437  bad_bool_value));
438 
440 }
441 
442 static void test_badargs4(void) {
443  gpr_cmdline* cl;
444 
445  const char* str = nullptr;
446  int x = 0;
447  int flag = 2;
448 
449  char* bad_bool_value[] = {const_cast<char*>(__FILE__),
450  const_cast<char*>("--no-str")};
451 
452  LOG_TEST();
453 
454  cl = gpr_cmdline_create(nullptr);
456  gpr_cmdline_add_string(cl, "str", nullptr, &str);
457  gpr_cmdline_add_int(cl, "x", nullptr, &x);
458  gpr_cmdline_add_flag(cl, "flag", nullptr, &flag);
459  gpr_cmdline_on_extra_arg(cl, "file", "filenames to process", extra_arg_cb,
460  nullptr);
461 
462  GPR_ASSERT(0 == gpr_cmdline_parse(cl, GPR_ARRAY_SIZE(bad_bool_value),
463  bad_bool_value));
464 
466 }
467 
468 int main(int argc, char** argv) {
469  grpc::testing::TestEnvironment env(&argc, argv);
470  test_simple_int();
471  test_eq_int();
472  test_2dash_int();
475  test_eq_string();
478  test_flag_on();
479  test_flag_no();
480  test_flag_val_1();
481  test_flag_val_0();
484  test_many();
485  test_extra();
487  test_usage();
488  test_help();
489  test_badargs1();
490  test_badargs2();
491  test_badargs3();
492  test_badargs4();
493  return 0;
494 }
gpr_cmdline_set_survive_failure
void gpr_cmdline_set_survive_failure(gpr_cmdline *cl)
Definition: cmdline.cc:75
main
int main(int argc, char **argv)
Definition: cmdline_test.cc:468
xds_interop_client.str
str
Definition: xds_interop_client.py:487
test_2dash_string
static void test_2dash_string(void)
Definition: cmdline_test.cc:124
flag
uint32_t flag
Definition: ssl_versions.cc:162
test_eq_int
static void test_eq_int(void)
Definition: cmdline_test.cc:47
log.h
generate.env
env
Definition: generate.py:37
gpr_cmdline_usage_string
std::string gpr_cmdline_usage_string(gpr_cmdline *cl, const char *argv0)
Definition: cmdline.cc:151
test_2dash_eq_int
static void test_2dash_eq_int(void)
Definition: cmdline_test.cc:78
test_many
static void test_many(void)
Definition: cmdline_test.cc:246
test_flag_val_1
static void test_flag_val_1(void)
Definition: cmdline_test.cc:185
test_simple_string
static void test_simple_string(void)
Definition: cmdline_test.cc:93
string.h
useful.h
gpr_cmdline_destroy
void gpr_cmdline_destroy(gpr_cmdline *cl)
Definition: cmdline.cc:79
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
test_extra_dashdash
static void test_extra_dashdash(void)
Definition: cmdline_test.cc:293
gpr_cmdline_add_flag
void gpr_cmdline_add_flag(gpr_cmdline *cl, const char *name, const char *help, int *value)
Definition: cmdline.cc:110
test_usage
static void test_usage(void)
Definition: cmdline_test.cc:310
test_flag_val_false
static void test_flag_val_false(void)
Definition: cmdline_test.cc:230
test_flag_on
static void test_flag_on(void)
Definition: cmdline_test.cc:155
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
test_2dash_eq_string
static void test_2dash_eq_string(void)
Definition: cmdline_test.cc:140
gpr_cmdline_add_string
void gpr_cmdline_add_string(gpr_cmdline *cl, const char *name, const char *help, const char **value)
Definition: cmdline.cc:115
arg
Definition: cmdline.cc:40
asyncio_get_stats.help
help
Definition: asyncio_get_stats.py:39
extra_arg_cb
static void extra_arg_cb(void *user_data, const char *arg)
Definition: cmdline_test.cc:269
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
test_flag_val_0
static void test_flag_val_0(void)
Definition: cmdline_test.cc:200
test_flag_val_true
static void test_flag_val_true(void)
Definition: cmdline_test.cc:215
test_help
static void test_help(void)
Definition: cmdline_test.cc:339
test_config.h
test_eq_string
static void test_eq_string(void)
Definition: cmdline_test.cc:109
test_badargs1
static void test_badargs1(void)
Definition: cmdline_test.cc:363
test_badargs4
static void test_badargs4(void)
Definition: cmdline_test.cc:442
GPR_ARRAY_SIZE
#define GPR_ARRAY_SIZE(array)
Definition: useful.h:129
gpr_cmdline_add_int
void gpr_cmdline_add_int(gpr_cmdline *cl, const char *name, const char *help, int *value)
Definition: cmdline.cc:105
bloaty::usage
const char usage[]
Definition: bloaty.cc:1843
test_badargs3
static void test_badargs3(void)
Definition: cmdline_test.cc:416
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
test_simple_int
static void test_simple_int(void)
Definition: cmdline_test.cc:31
alloc.h
cmdline.h
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
test_badargs2
static void test_badargs2(void)
Definition: cmdline_test.cc:389
gpr_cmdline_create
gpr_cmdline * gpr_cmdline_create(const char *description)
Definition: cmdline.cc:66
gpr_cmdline
Definition: cmdline.cc:48
test_extra
static void test_extra(void)
Definition: cmdline_test.cc:277
test_2dash_int
static void test_2dash_int(void)
Definition: cmdline_test.cc:62
LOG_TEST
#define LOG_TEST()
Definition: cmdline_test.cc:29
gpr_cmdline_on_extra_arg
void gpr_cmdline_on_extra_arg(gpr_cmdline *cl, const char *name, const char *help, void(*on_extra_arg)(void *user_data, const char *arg), void *user_data)
Definition: cmdline.cc:120
gpr_cmdline_parse
int gpr_cmdline_parse(gpr_cmdline *cl, int argc, char **argv)
Definition: cmdline.cc:309
test_flag_no
static void test_flag_no(void)
Definition: cmdline_test.cc:170


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:48