From 77f85a4d2b46e1ccc34a4c86e8c97392fa73e280 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sat, 23 Nov 2024 19:13:18 -0800 Subject: options parser: Add a couple of tests for ambiguous and missing arguments --- src/supplemental/util/options_test.c | 46 ++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'src/supplemental/util') diff --git a/src/supplemental/util/options_test.c b/src/supplemental/util/options_test.c index f139f6fe..e365f97e 100644 --- a/src/supplemental/util/options_test.c +++ b/src/supplemental/util/options_test.c @@ -8,6 +8,7 @@ // found online at https://opensource.org/licenses/MIT. // +#include "nng/nng.h" #include #include @@ -258,6 +259,49 @@ test_mixed_long_short(void) NUTS_TRUE(opti == 6); } +void +test_ambiguous(void) +{ + int opti = 1; + char *av[2]; + int ac = 2; + int v; + char *a = NULL; + + nng_optspec spec[] = { + { "flag", 'f', 1, false }, + { "fluid", 0, 2, false }, + { NULL, 0, 0, false }, + }; + + av[0] = "program"; + av[1] = "--fl"; + NUTS_FAIL(nng_opts_parse(ac, av, spec, &v, &a, &opti), NNG_EAMBIGUOUS); +} + +void +test_missing_arg(void) +{ + int opti = 1; + char *av[2]; + int ac = 2; + int v; + char *a = NULL; + + nng_optspec spec[] = { + { "flag", 'f', 1, true }, + { NULL, 0, 0, false }, + }; + + av[0] = "program"; + av[1] = "--fl"; + NUTS_FAIL(nng_opts_parse(ac, av, spec, &v, &a, &opti), NNG_ENOARG); + av[0] = "program"; + av[1] = "-f"; + opti = 1; + NUTS_FAIL(nng_opts_parse(ac, av, spec, &v, &a, &opti), NNG_ENOARG); +} + NUTS_TESTS = { { "simple options", test_simple_options }, { "long options", test_long_options }, @@ -270,5 +314,7 @@ NUTS_TESTS = { { "bad long", test_negative_bad_long }, { "arg only", test_arg_only }, { "options mixed long short", test_mixed_long_short }, + { "ambiguous options", test_ambiguous }, + { "missing argument", test_missing_arg }, { NULL, NULL }, }; -- cgit v1.2.3-70-g09d2