Skip to content

Commit

Permalink
tests: shell: Add test for raw argument option
Browse files Browse the repository at this point in the history
Added test for commands with SHELL_OPT_ARG_RAW flag set.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
  • Loading branch information
nordic-krch committed Apr 27, 2020
1 parent 7fea6ea commit 1cf3158
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions tests/shell/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ static void test_shell_execute_cmd(const char *cmd, int result)

TC_PRINT("shell_execute_cmd(%s): %d\n", cmd, ret);

zassert_true(ret == result, cmd);
zassert_true(ret == result, "cmd: %s, got:%d, expected:%d",
cmd, ret, result);
}

static void test_cmd_help(void)
Expand Down Expand Up @@ -206,7 +207,6 @@ SHELL_CMD_ARG_REGISTER(test_shell_cmd, NULL, "help", cmd_test_module, 1, 0);
static int cmd_wildcard(const struct shell *shell, size_t argc, char **argv)
{
int valid_arguments = 0;

for (size_t i = 1; i < argc; i++) {
if (!strcmp("argument_1", argv[i])) {
valid_arguments++;
Expand Down Expand Up @@ -306,7 +306,7 @@ static void test_set_root_cmd(void)
err = shell_set_root_cmd("shell");
zassert_equal(err, 0, "Unexpected error %d", err);

test_shell_execute_cmd("shell colors", -ENOEXEC);
test_shell_execute_cmd("shell colors", 1);
test_shell_execute_cmd("colors on", 0);

err = shell_set_root_cmd(NULL);
Expand All @@ -316,6 +316,35 @@ static void test_set_root_cmd(void)
test_shell_execute_cmd("shell colors on", 0);
}

#define RAW_ARG "aaa \"\" bbb"
#define CMD_NAME test_cmd_raw_arg

static int cmd_raw_arg(const struct shell *shell, size_t argc, char **argv)
{
if (argc == 2) {
if (strcmp(argv[0], STRINGIFY(CMD_NAME))) {
return -1;
}
if (strcmp(argv[1], RAW_ARG)) {
return -1;
}
} else if (argc > 2) {
return -1;
}

return 0;
}

SHELL_CMD_ARG_REGISTER(CMD_NAME, NULL, NULL, cmd_raw_arg, 1, SHELL_OPT_ARG_RAW);

static void test_raw_arg(void)
{
test_shell_execute_cmd("test_cmd_raw_arg aaa \"\" bbb", 0);
test_shell_execute_cmd("test_cmd_raw_arg", 0);
test_shell_execute_cmd("select test_cmd_raw_arg", 0);
test_shell_execute_cmd("aaa \"\" bbb", 0);
}

void test_main(void)
{
ztest_test_suite(shell_test_suite,
Expand All @@ -324,11 +353,13 @@ void test_main(void)
ztest_unit_test(test_cmd_shell),
ztest_unit_test(test_cmd_history),
ztest_unit_test(test_cmd_select),
ztest_unit_test(test_set_root_cmd),
ztest_unit_test(test_cmd_resize),
ztest_unit_test(test_shell_module),
ztest_unit_test(test_shell_wildcards_static),
ztest_unit_test(test_shell_wildcards_dynamic));
ztest_unit_test(test_shell_wildcards_dynamic),
ztest_unit_test(test_set_root_cmd),
ztest_unit_test(test_raw_arg)
);

ztest_run_test_suite(shell_test_suite);
}

0 comments on commit 1cf3158

Please sign in to comment.