Skip to content

Commit

Permalink
tests: Add shell tests
Browse files Browse the repository at this point in the history
This adds the following tests:

help tests:
shell_exec(help): 0
shell_exec(help dummy): 0
shell_exec(help invalid): -22

select tests:
shell_exec(select): 0
shell_exec(select dummy): 0
shell_exec(select invalid): -22

module tests:
shell_exec(dummy cmd1): 0
shell_exec(dummy cmd1 arg1): -22
shell_exec(dummy cmd2 arg1): 0
shell_exec(dummy cmd2 arg1 arg2): -22
shell_exec(dummy cmd3 arg1 arg2): 0
shell_exec(dummy cmd3 arg1 arg2 arg3): -22
shell_exec(dummy cmd4 arg1 arg2 arg3): -22
shell_exec(cmd1): 0
shell_exec(cmd1 arg1): -22
shell_exec(cmd2 arg1): 0
shell_exec(cmd2 arg1 arg2): -22
shell_exec(cmd3 arg1 arg2): 0
shell_exec(cmd3 arg1 arg2 arg3): -22
shell_exec(cmd4 arg1 arg2 arg3): -22

app handler tests:
shell_exec(cmd4 arg1 arg2 arg3): 0

Signed-off-by: Luiz Augusto von Dentz <[email protected]>
  • Loading branch information
Vudentz authored and Anas Nashif committed Jun 9, 2017
1 parent a04d22c commit aac4f48
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tests/shell/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
BOARD ?= qemu_x86
CONF_FILE = prj.conf

include ${ZEPHYR_BASE}/Makefile.test
2 changes: 2 additions & 0 deletions tests/shell/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CONFIG_CONSOLE_SHELL=y
CONFIG_ZTEST=y
3 changes: 3 additions & 0 deletions tests/shell/src/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include $(ZEPHYR_BASE)/tests/Makefile.test

obj-y = main.o
123 changes: 123 additions & 0 deletions tests/shell/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright (c) 2017 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/

/** @file
* @brief Interactive shell test suite
*
*/

#include <zephyr.h>
#include <ztest.h>

#include <shell/shell.h>

static void test_shell_exec(const char *line, int result)
{
char cmd[80];
int ret;

strncpy(cmd, line, sizeof(cmd));
ret = shell_exec(cmd);

TC_PRINT("shell_exec(%s): %d\n", line, ret);

zassert_true(ret == result, line);
}

static void test_help(void)
{
test_shell_exec("help", 0);
test_shell_exec("help dummy", 0);
test_shell_exec("help invalid", -EINVAL);
}

static void test_select(void)
{
test_shell_exec("select", 0);
test_shell_exec("select dummy", 0);
test_shell_exec("select invalid", -EINVAL);
}

static void test_exit(void)
{
test_shell_exec("exit", 0);
}

static void test_module(void)
{
test_shell_exec("dummy cmd1", 0);
test_shell_exec("dummy cmd1 arg1", -EINVAL);

test_shell_exec("dummy cmd2 arg1", 0);
test_shell_exec("dummy cmd2 arg1 arg2", -EINVAL);

test_shell_exec("dummy cmd3 arg1 arg2", 0);
test_shell_exec("dummy cmd3 arg1 arg2 arg3", -EINVAL);

test_shell_exec("dummy cmd4 arg1 arg2 arg3", -EINVAL);

shell_register_default_module("dummy");

test_shell_exec("cmd1", 0);
test_shell_exec("cmd1 arg1", -EINVAL);

test_shell_exec("cmd2 arg1", 0);
test_shell_exec("cmd2 arg1 arg2", -EINVAL);

test_shell_exec("cmd3 arg1 arg2", 0);
test_shell_exec("cmd3 arg1 arg2 arg3", -EINVAL);

test_shell_exec("cmd4 arg1 arg2 arg3", -EINVAL);
}

static int app_cmd_handler(int argc, char *argv[])
{
return 0;
}

static void test_app_handler(void)
{
shell_register_app_cmd_handler(app_cmd_handler);

test_shell_exec("cmd4 arg1 arg2 arg3", 0);
}

static int dummy_cmd(int argc, char *argv[])
{
if (!strcmp(argv[0], "cmd1")) {
return argc == 1 ? 0 : -EINVAL;
}

if (!strcmp(argv[0], "cmd2")) {
return argc == 2 ? 0 : -EINVAL;
}

if (!strcmp(argv[0], "cmd3")) {
return argc == 3 ? 0 : -EINVAL;
}

return -EINVAL;
}

static const struct shell_cmd dummy_cmds[] = {
{ "cmd1", dummy_cmd, "" },
{ "cmd2", dummy_cmd, "<arg1>" },
{ "cmd3", dummy_cmd, "<arg1> <arg2>" },
{ NULL, NULL }
};

void test_main(void)
{
SHELL_REGISTER("dummy", dummy_cmds);

ztest_test_suite(shell_test_suite,
ztest_unit_test(test_help),
ztest_unit_test(test_select),
ztest_unit_test(test_exit),
ztest_unit_test(test_module),
ztest_unit_test(test_app_handler));
ztest_run_test_suite(shell_test_suite);
}
3 changes: 3 additions & 0 deletions tests/shell/testcase.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[test]
tags = shell
platform_whitelist = qemu_x86

0 comments on commit aac4f48

Please sign in to comment.