diff options
author | Amitay Isaacs <amitay@gmail.com> | 2019-11-11 18:32:49 +1100 |
---|---|---|
committer | Martin Schwenke <martins@samba.org> | 2019-11-14 12:03:46 +0000 |
commit | 963a639101f4c55467e33667a698fffb350a931f (patch) | |
tree | 5249b9d5bd664e1194bb087da482bcbbc4c5b59f | |
parent | e469d6c1191ab9fa78e1ced6d7aa7b37dce7c5a9 (diff) | |
download | samba-963a639101f4c55467e33667a698fffb350a931f.tar.gz |
ctdb-tests: Add tests for cmdline_add() api
Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
Autobuild-User(master): Martin Schwenke <martins@samba.org>
Autobuild-Date(master): Thu Nov 14 12:03:46 UTC 2019 on sn-devel-184
-rwxr-xr-x | ctdb/tests/UNIT/cunit/cmdline_test_001.sh | 24 | ||||
-rw-r--r-- | ctdb/tests/src/cmdline_test.c | 89 |
2 files changed, 113 insertions, 0 deletions
diff --git a/ctdb/tests/UNIT/cunit/cmdline_test_001.sh b/ctdb/tests/UNIT/cunit/cmdline_test_001.sh index 2835c8e574e..e95900087ec 100755 --- a/ctdb/tests/UNIT/cunit/cmdline_test_001.sh +++ b/ctdb/tests/UNIT/cunit/cmdline_test_001.sh @@ -72,3 +72,27 @@ ok <<EOF arg1 EOF unit_test cmdline_test 6 + +ok <<EOF +Usage: test7 [<options>] <command> [<args>] + +Help Options: + -h, --help Show this help message + +Basic Commands: + cmd1 command one help + cmd2 command two help + +Advanced Commands: + cmd3 command three help + cmd4 command four help + +Ultimate Commands: + cmd5 command five help + cmd6 command six help + +one +three +six +EOF +unit_test cmdline_test 7 diff --git a/ctdb/tests/src/cmdline_test.c b/ctdb/tests/src/cmdline_test.c index 72c34acd654..916d820553b 100644 --- a/ctdb/tests/src/cmdline_test.c +++ b/ctdb/tests/src/cmdline_test.c @@ -350,6 +350,91 @@ static void test6(void) talloc_free(mem_ctx); } +static int test7_func(TALLOC_CTX *mem_ctx, + int argc, + const char **argv, + void *private_data) +{ + assert(argc == 1); + + printf("%s\n", argv[0]); + + return 0; +} + +static struct cmdline_command test7_basic_commands[] = { + { "cmd1", test7_func, "command one help", NULL }, + { "cmd2", test7_func, "command two help", NULL }, + CMDLINE_TABLEEND +}; + +static struct cmdline_command test7_advanced_commands[] = { + { "cmd3", test7_func, "command three help", NULL }, + { "cmd4", test7_func, "command four help", NULL }, + CMDLINE_TABLEEND +}; + +static struct cmdline_command test7_ultimate_commands[] = { + { "cmd5", test7_func, "command five help", NULL }, + { "cmd6", test7_func, "command six help", NULL }, + CMDLINE_TABLEEND +}; + +static void test7(void) +{ + TALLOC_CTX *mem_ctx; + struct cmdline_context *cmdline; + const char *argv1[] = { "cmd1", "one" }; + const char *argv2[] = { "cmd3", "three" }; + const char *argv3[] = { "cmd6", "six" }; + int ret, result; + + mem_ctx = talloc_new(NULL); + assert(mem_ctx != NULL); + + ret = cmdline_init(mem_ctx, + "test7", + NULL, + "Basic", + test7_basic_commands, + &cmdline); + assert(ret == 0); + + ret = cmdline_add(cmdline, "Advanced", test7_advanced_commands); + assert(ret == 0); + + ret = cmdline_add(cmdline, "Ultimate", test7_ultimate_commands); + assert(ret == 0); + + cmdline_usage(cmdline, NULL); + + printf("\n"); + + ret = cmdline_parse(cmdline, 2, argv1, false); + assert(ret == 0); + + ret = cmdline_run(cmdline, NULL, &result); + assert(ret == 0); + assert(result == 0); + + ret = cmdline_parse(cmdline, 2, argv2, false); + assert(ret == 0); + + ret = cmdline_run(cmdline, NULL, &result); + assert(ret == 0); + assert(result == 0); + + ret = cmdline_parse(cmdline, 2, argv3, false); + assert(ret == 0); + + ret = cmdline_run(cmdline, NULL, &result); + assert(ret == 0); + assert(result == 0); + + talloc_free(mem_ctx); +} + + int main(int argc, const char **argv) { int num; @@ -385,6 +470,10 @@ int main(int argc, const char **argv) case 6: test6(); break; + + case 7: + test7(); + break; } return 0; |