cmdline: add some more unit tests
The tests here were a little slim and missed some cases. Add some more
tests. Because tests are a good thing.
BUG=none
TEST=make run-tests
Change-Id: I4e570c9378f2cf611cb2d8a2ef85291dad859dab
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/lithium/+/2142553
Tested-by: Jack Rosenthal <[email protected]>
Reviewed-by: Paul Fagerburg <[email protected]>
Commit-Queue: Jack Rosenthal <[email protected]>
diff --git a/src/cmdline/tests.c b/src/cmdline/tests.c
index 2afde14..402658e 100644
--- a/src/cmdline/tests.c
+++ b/src/cmdline/tests.c
@@ -3,6 +3,7 @@
* found in the LICENSE file.
*/
+#include <stdbool.h>
#include <string.h>
#include "cmdline.h"
@@ -99,3 +100,92 @@
EXPECT(bigt == false);
EXPECT(!strcmp(q, "aaa"));
}
+
+DEFTEST("lithium.cmdline.additional_arguments", {})
+{
+ const char *const *argv_additional;
+ const char *const argv[] = { "foo", "bar", "baz", NULL };
+
+ struct li_cmdline spec = { 0 };
+ EXPECT(li_cmdline_parse(&spec, argv, &argv_additional)
+ == LI_CMDLINE_CONTINUE);
+ EXPECT(argv_additional == argv + 1);
+}
+
+DEFTEST("lithium.cmdline.required_positional", {})
+{
+ const char *output_string = NULL;
+
+ struct li_cmdline_argument positionals[] = {
+ {
+ .name = "SOME_POSITIONAL",
+ .help = "Foo!",
+ .action.type = LI_CMDLINE_STRING,
+ .action.dest = &output_string,
+ },
+ { 0 },
+ };
+
+ struct li_cmdline_option options[] = {
+ {
+ .shortopt = 'h',
+ .longopt = "help",
+ .action.type = LI_CMDLINE_HELP,
+ },
+ { 0 },
+ };
+
+ struct li_cmdline spec = {
+ .options = options,
+ .arguments = positionals,
+ };
+
+ const char *const argv[] = { "progname", "positional", NULL };
+ EXPECT(li_cmdline_parse(&spec, argv, NULL) == LI_CMDLINE_CONTINUE);
+ EXPECT(output_string == argv[1]);
+}
+
+static bool example_cb(const char *value, void *dest_in)
+{
+ int *dest = dest_in;
+ if (!strcmp(value, "hello")) {
+ *dest = 12;
+ return true;
+ }
+
+ return false;
+}
+
+DEFTEST("lithium.cmdline.callback", {})
+{
+ int value = -1;
+
+ struct li_cmdline_argument positionals[] = {
+ {
+ .name = "CBARG",
+ .help = "something that goes thru a callback",
+ .action.type = LI_CMDLINE_CALLBACK,
+ .action.cb = example_cb,
+ .action.dest = &value,
+ },
+ { 0 },
+ };
+
+ struct li_cmdline_option options[] = {
+ {
+ .shortopt = 'h',
+ .longopt = "help",
+ .action.type = LI_CMDLINE_HELP,
+ },
+ { 0 },
+ };
+
+ struct li_cmdline spec = {
+ .options = options,
+ .arguments = positionals,
+ };
+
+ const char *const argv[] = { "progname", "hello", NULL };
+ EXPECT(li_cmdline_parse(&spec, argv, NULL) == LI_CMDLINE_CONTINUE);
+ EXPECT(value == 12);
+}