]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
ctdb-tests: Add tests for cmdline_add() api
authorAmitay Isaacs <amitay@gmail.com>
Mon, 11 Nov 2019 07:32:49 +0000 (18:32 +1100)
committerMartin Schwenke <martins@samba.org>
Thu, 14 Nov 2019 12:03:46 +0000 (12:03 +0000)
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

ctdb/tests/UNIT/cunit/cmdline_test_001.sh
ctdb/tests/src/cmdline_test.c

index 2835c8e574e0edee6ab1e2e664b28d0dda21659d..e95900087ec79de6c0126555f6f3697a094beccf 100755 (executable)
@@ -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
index 72c34acd6542a758526021e320056c484c8b3af3..916d820553b7ec16be0f002e11eb5e266b42bf72 100644 (file)
@@ -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;