1 /*
2  * Copyright (c) 2024 Google, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /** @file
8  *  @brief Custom header shell test suite
9  *
10  */
11 
12 #include <zephyr/kernel.h>
13 #include <zephyr/ztest.h>
14 
15 #include <zephyr/shell/shell.h>
16 #include <zephyr/shell/shell_dummy.h>
17 
shell_setup(void)18 static void *shell_setup(void)
19 {
20 	const struct shell *sh = shell_backend_dummy_get_ptr();
21 
22 	/* Wait for the initialization of the shell dummy backend. */
23 	WAIT_FOR(shell_ready(sh), 20000, k_msleep(1));
24 	zassert_true(shell_ready(sh), "timed out waiting for dummy shell backend");
25 
26 	return NULL;
27 }
28 
29 ZTEST_SUITE(sh, NULL, shell_setup, NULL, NULL, NULL);
30 
ZTEST(sh,test_shell_fprintf)31 ZTEST(sh, test_shell_fprintf)
32 {
33 	static const char expect[] = "[CUSTOM_PREFIX]testing 1 2 3";
34 	const struct shell *sh;
35 	const char *buf;
36 	size_t size;
37 
38 	sh = shell_backend_dummy_get_ptr();
39 	zassert_not_null(sh, "Failed to get shell");
40 
41 	/* Clear the output buffer */
42 	shell_backend_dummy_clear_output(sh);
43 
44 	shell_fprintf(sh, SHELL_VT100_COLOR_DEFAULT, "testing %d %s %c",
45 		      1, "2", '3');
46 	buf = shell_backend_dummy_get_output(sh, &size);
47 	zassert_true(size >= sizeof(expect), "Expected size > %u, got %d",
48 		     sizeof(expect), size);
49 
50 	/*
51 	 * There are prompts and various ANSI characters in the output, so just
52 	 * check that the string is in there somewhere.
53 	 */
54 	zassert_true(strstr(buf, expect),
55 		     "Expected string to contain '%s', got '%s'", expect, buf);
56 }
57