diff options
author | Alfred E. Heggestad <aeh@db.org> | 2016-08-07 10:42:47 +0200 |
---|---|---|
committer | Alfred E. Heggestad <aeh@db.org> | 2016-08-07 10:42:47 +0200 |
commit | e3ecbddba06f91c3ef5b3cd8305a8f2fdb6c9cde (patch) | |
tree | c12474268e452b242c688689bd8eba3d92f4a151 /test | |
parent | 7f35380ee3a39faad1f10fcbb6b115b8ddc5f2a7 (diff) |
cmd: add support for long commands
- Long commands are commands that is 2 or more characters,
and MUST be completed by pressing enter.
- A given command can have a long-command, a short-command
or both.
- All long commands are using '/' (slash) prefix
- a simple TAB-completion is supported, while punching in
your long command you can press TAB to see a list of
commands that matches the input string.
Feedback is welcome, specially on the long-command strings
itself. If you want to suggest changes, please make a small
and non-intrusive patch and send it as a Pull Request (PR).
Diffstat (limited to 'test')
-rw-r--r-- | test/cmd.c | 70 | ||||
-rw-r--r-- | test/main.c | 1 | ||||
-rw-r--r-- | test/test.h | 1 |
3 files changed, 71 insertions, 1 deletions
@@ -32,7 +32,7 @@ static int cmd_test(struct re_printf *pf, void *arg) static const struct cmd cmdv[] = { - {'@', 0, "Test command", cmd_test}, + {NULL, '@', 0, "Test command", cmd_test}, }; @@ -77,3 +77,71 @@ int test_cmd(void) out: return err; } + + +static int long_handler(struct re_printf *pf, void *arg) +{ + struct cmd_arg *carg = arg; + struct test *test = carg->data; + int err = 0; + (void)pf; + + ASSERT_STREQ("123", carg->prm); + + ++test->cmd_called; + + out: + return err; +} + + +static const struct cmd longcmdv[] = { + { "test", 0, 0, "Test Command", long_handler}, +}; + + +int test_cmd_long(void) +{ + struct test test; + const struct cmd *cmd; + static const char *input_str = "/test 123\n"; + struct cmd_ctx *ctx = NULL; + size_t i; + int err; + + memset(&test, 0, sizeof(test)); + + /* Verify that the command does not exist */ + cmd = cmd_find_long("test"); + ASSERT_TRUE(cmd == NULL); + + /* Register and verify command */ + err = cmd_register(longcmdv, ARRAY_SIZE(longcmdv)); + ASSERT_EQ(0, err); + + cmd = cmd_find_long("test"); + ASSERT_TRUE(cmd != NULL); + + /* Feed it some input data .. */ + + for (i=0; i<strlen(input_str); i++) { + + err = cmd_process(&ctx, input_str[i], &pf_null, &test); + ASSERT_EQ(0, err); + } + + err = cmd_process_long("test 123", 8, &pf_null, &test); + ASSERT_EQ(0, err); + + ASSERT_EQ(2, test.cmd_called); + + /* Cleanup .. */ + + cmd_unregister(longcmdv); + + cmd = cmd_find_long("test"); + ASSERT_TRUE(cmd == NULL); + + out: + return err; +} diff --git a/test/main.c b/test/main.c index 6991671..3a5dd77 100644 --- a/test/main.c +++ b/test/main.c @@ -29,6 +29,7 @@ static const struct test tests[] = { TEST(test_call_max), TEST(test_call_dtmf), TEST(test_cmd), + TEST(test_cmd_long), TEST(test_contact), TEST(test_cplusplus), TEST(test_mos), diff --git a/test/test.h b/test/test.h index 4c6c11b..bd41c82 100644 --- a/test/test.h +++ b/test/test.h @@ -95,6 +95,7 @@ int mock_ausrc_register(struct ausrc **ausrcp); /* test cases */ int test_cmd(void); +int test_cmd_long(void); int test_contact(void); int test_ua_alloc(void); int test_uag_find_param(void); |