tests: shell: add a test for shell_device_filter

Add a test for shell_device_filter and shell_device_lookup.

Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com>
This commit is contained in:
Fabio Baltieri 2024-04-28 22:31:56 +01:00 committed by David Leach
commit 3574050ed2
4 changed files with 85 additions and 0 deletions

View file

@ -0,0 +1,9 @@
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(shell_device_filter)
FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})

View file

@ -0,0 +1,7 @@
# SPDX-License-Identifier: Apache-2.0
CONFIG_ZTEST=y
CONFIG_SHELL=y
CONFIG_SHELL_BACKEND_SERIAL=n
CONFIG_SHELL_BACKEND_DUMMY=n
CONFIG_PM=y

View file

@ -0,0 +1,59 @@
/*
* Copyright 2024 Google LLC
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <string.h>
#include <zephyr/device.h>
#include <zephyr/kernel.h>
#include <zephyr/shell/shell.h>
#include <zephyr/ztest.h>
DEVICE_DEFINE(device_0, "device@0", NULL, NULL,
NULL, NULL,
POST_KERNEL, 0, NULL);
DEVICE_DEFINE(device_1, "device@1", NULL, NULL,
NULL, NULL,
POST_KERNEL, 1, NULL);
DEVICE_DEFINE(device_2, "xx_device@2", NULL, NULL,
NULL, NULL,
POST_KERNEL, 2, NULL);
static const struct device *d0 = &DEVICE_NAME_GET(device_0);
static const struct device *d1 = &DEVICE_NAME_GET(device_1);
static const struct device *d2 = &DEVICE_NAME_GET(device_2);
ZTEST(shell_device_filter, test_unfiltered)
{
zassert_equal_ptr(d0, shell_device_filter(0, NULL));
zassert_equal_ptr(d1, shell_device_filter(1, NULL));
zassert_equal_ptr(d2, shell_device_filter(2, NULL));
zassert_equal_ptr(NULL, shell_device_filter(3, NULL));
zassert_equal_ptr(d0, shell_device_lookup(0, NULL));
zassert_equal_ptr(d1, shell_device_lookup(1, NULL));
zassert_equal_ptr(d2, shell_device_lookup(2, NULL));
zassert_equal_ptr(NULL, shell_device_lookup(3, NULL));
}
ZTEST(shell_device_filter, test_prefix)
{
zassert_equal_ptr(d2, shell_device_lookup(0, "xx_"));
zassert_equal_ptr(NULL, shell_device_lookup(1, "xx_"));
}
static bool pm_device_test_filter(const struct device *dev)
{
return strstr(dev->name, "@1") != NULL;
}
ZTEST(shell_device_filter, test_filter)
{
zassert_equal_ptr(d1, shell_device_filter(0, pm_device_test_filter));
zassert_equal_ptr(NULL, shell_device_filter(1, pm_device_test_filter));
}
ZTEST_SUITE(shell_device_filter, NULL, NULL, NULL, NULL, NULL);

View file

@ -0,0 +1,10 @@
# SPDX-License-Identifier: Apache-2.0
tests:
shell.device_filter:
platform_allow:
- native_sim
tags:
- shell
integration_platforms:
- native_sim