Commit aac4f488 authored by Luiz Augusto von Dentz's avatar Luiz Augusto von Dentz Committed by Anas Nashif
Browse files

tests: Add shell tests



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: default avatarLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
parent a04d22c6
Loading
Loading
Loading
Loading

tests/shell/Makefile

0 → 100644
+4 −0
Original line number Diff line number Diff line
BOARD ?= qemu_x86
CONF_FILE = prj.conf

include ${ZEPHYR_BASE}/Makefile.test

tests/shell/prj.conf

0 → 100644
+2 −0
Original line number Diff line number Diff line
CONFIG_CONSOLE_SHELL=y
CONFIG_ZTEST=y
+3 −0
Original line number Diff line number Diff line
include $(ZEPHYR_BASE)/tests/Makefile.test

obj-y = main.o

tests/shell/src/main.c

0 → 100644
+123 −0
Original line number Diff line number Diff line
/*
 * 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 −0
Original line number Diff line number Diff line
[test]
tags = shell
platform_whitelist = qemu_x86