3 * Copyright (c) 2020 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include "TestShell.h"
20 #include <shell/shell.h>
21 #include <support/CodeUtils.h>
22 #include <support/UnitTestRegistration.h>
31 using namespace chip::Shell;
32 using namespace chip::Logging;
34 // =================================
36 // =================================
38 struct test_shell_vector
44 static const struct test_shell_vector test_vector_shell_tokenizer[] = {
45 { .line = "hello how are you?", .argv = (const char *[]){ "hello", "how", "are", "you?" } },
46 { .line = "hey yo yo", .argv = (const char *[]){ "hey", "yo", "yo" } },
47 { .line = "This has double spaces", .argv = (const char *[]){ "This", "has", "double", "spaces" } },
48 { .line = " leading space", .argv = (const char *[]){ "leading", "space" } },
49 { .line = "trailing space ", .argv = (const char *[]){ "trailing", "space", "" } },
50 { .line = "no_space", .argv = (const char *[]){ "no_space" } },
51 { .line = "escaped\\ space", .argv = (const char *[]){ "escaped space" } },
52 { .line = "escape\\\\", .argv = (const char *[]){ "escape\\" } },
53 { .line = "extended\\ escaped\\ space and\\ more", .argv = (const char *[]){ "extended escaped space", "and more" } },
54 { .line = " ", .argv = (const char *[]){ "" } },
55 { .line = "", .argv = (const char *[]){} },
58 // =================================
60 // =================================
62 #define TEST_SHELL_MAX_TOKENS 5
64 static void TestShell_Tokenizer(nlTestSuite * inSuite, void * inContext)
66 int numOfTestVectors = ArraySize(test_vector_shell_tokenizer);
67 int numOfTestsRan = 0;
68 const struct test_shell_vector * test_params;
69 char * argv[TEST_SHELL_MAX_TOKENS];
70 int argc = TEST_SHELL_MAX_TOKENS;
75 for (int vectorIndex = 0; vectorIndex < numOfTestVectors; vectorIndex++)
77 test_params = &test_vector_shell_tokenizer[vectorIndex];
78 strcpy(line, test_params->line);
80 count = shell_line_tokenize(line, argv, argc);
82 for (int i = 0; i < count; i++)
84 NL_TEST_ASSERT(inSuite, strcmp(argv[i], test_params->argv[i]) == 0);
88 NL_TEST_ASSERT(inSuite, numOfTestsRan > 0);
92 * Test Suite. It lists all the test functions.
94 static const nlTest sTests[] = {
96 NL_TEST_DEF("Test Shell: TestShell_Tokenizer", TestShell_Tokenizer),
103 nlTestSuite theSuite = { "CHIP Shell tests", &sTests[0], nullptr, nullptr };
105 // Run test suit againt one context.
106 nlTestRunner(&theSuite, nullptr);
107 return nlTestRunnerStats(&theSuite);
110 CHIP_REGISTER_TEST_SUITE(TestShell)