1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
/*
* Copyright (c) 2026 Apidae Systems
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/shell/shell.h>
#include <zephyr/shell/shell_dummy.h>
#include <zephyr/sys/atomic.h>
#include <zephyr/ztest.h>
static atomic_t hello_invocations = ATOMIC_INIT(0);
static int cmd_hello(const struct shell *sh, size_t argc, char **argv)
{
atomic_inc(&hello_invocations);
shell_print(sh, "hello from %s", argc > 1 ? argv[1] : "test");
return 0;
}
SHELL_CMD_REGISTER(hello, NULL, "test-only command — increments a counter", cmd_hello);
ZTEST_SUITE(shell, NULL, NULL, NULL, NULL, NULL);
ZTEST(shell, test_dummy_backend_is_ready)
{
const struct shell *sh = shell_backend_dummy_get_ptr();
zassert_not_null(sh, "dummy shell backend pointer is NULL");
}
ZTEST(shell, test_executes_builtin_help)
{
const struct shell *sh = shell_backend_dummy_get_ptr();
int rc = shell_execute_cmd(sh, "help");
zassert_ok(rc, "shell_execute_cmd(\"help\") failed: %d", rc);
}
ZTEST(shell, test_executes_kernel_uptime)
{
const struct shell *sh = shell_backend_dummy_get_ptr();
int rc = shell_execute_cmd(sh, "kernel uptime");
zassert_ok(rc, "shell_execute_cmd(\"kernel uptime\") failed: %d", rc);
}
ZTEST(shell, test_executes_custom_command_with_side_effect)
{
const struct shell *sh = shell_backend_dummy_get_ptr();
atomic_val_t before = atomic_get(&hello_invocations);
int rc = shell_execute_cmd(sh, "hello world");
zassert_ok(rc, "shell_execute_cmd(\"hello world\") failed: %d", rc);
atomic_val_t after = atomic_get(&hello_invocations);
zassert_equal(after, before + 1,
"custom command did not run: before=%ld after=%ld",
(long)before, (long)after);
}
ZTEST(shell, test_unknown_command_does_not_crash)
{
const struct shell *sh = shell_backend_dummy_get_ptr();
int rc = shell_execute_cmd(sh, "definitely_not_a_real_command_xyz");
zassert_not_equal(rc, 0,
"unknown command should return non-zero, got %d", rc);
}
|