Browse Source

terminal: fix linkage, printing cmd-not-found and add tests

dev
Maxim Prokhorov 6 months ago
parent
commit
5531b93269
3 changed files with 43 additions and 2 deletions
  1. +5
    -1
      code/espurna/terminal_commands.cpp
  2. +6
    -1
      code/espurna/terminal_commands.h
  3. +32
    -0
      code/test/unit/src/terminal/terminal.cpp

+ 5
- 1
code/espurna/terminal_commands.cpp View File

@ -116,10 +116,14 @@ bool find_and_call(CommandLine cmd, Print& output, Print& error_output) {
return true;
}
error(output, F("Command not found"));
error(error_output, F("Command not found"));
return false;
}
bool find_and_call(CommandLine cmd, Print& output) {
return find_and_call(cmd, output, output);
}
bool find_and_call(StringView cmd, Print& output, Print& error_output) {
auto result = parse_line(cmd);
if (result.error != parser::Error::Ok) {


+ 6
- 1
code/espurna/terminal_commands.h View File

@ -63,14 +63,19 @@ const Command* find(StringView name);
// try to parse and call command line string
bool find_and_call(StringView, Print& output);
// try to parse and call command line string
bool find_and_call(StringView, Print& output, Print& error);
// try and call an already parsed command line
bool find_and_call(CommandLine, Print& output);
// try and call an already parsed command line
bool find_and_call(CommandLine, Print& output, Print& error);
// search the given string for valid commands and call them in sequence
bool api_find_and_call(StringView, Print& output);
// search the given string for valid commands and call them in sequence
// separate outputs for command and errors
bool api_find_and_call(StringView, Print& output, Print& error);
// helper functions for most common success output


+ 32
- 0
code/test/unit/src/terminal/terminal.cpp View File

@ -412,6 +412,37 @@ void test_line_buffer_multiple() {
TEST_ASSERT(Second == second.line);
}
void test_error_output() {
PrintString out(64);
PrintString err(64);
add("test.error1", [](CommandContext&& ctx) {
ctx.error.print("foo");
});
TEST_ASSERT(find_and_call("test.error1\n", out, err));
TEST_ASSERT_EQUAL(0, out.length());
TEST_ASSERT_EQUAL_STRING("foo", err.c_str());
out.clear();
err.clear();
add("test.error2", [](CommandContext&& ctx) {
ctx.output.print("bar");
});
TEST_ASSERT(find_and_call("test.error2\n", out, err));
TEST_ASSERT_EQUAL(0, err.length());
TEST_ASSERT_EQUAL_STRING("bar", out.c_str());
out.clear();
err.clear();
TEST_ASSERT(!find_and_call("test.error3\n", out, err));
TEST_ASSERT_EQUAL(0, out.length());
TEST_ASSERT(err.length() > 0);
}
} // namespace
} // namespace test
} // namespace terminal
@ -437,6 +468,7 @@ int main(int, char**) {
RUN_TEST(test_line_buffer);
RUN_TEST(test_line_buffer_overflow);
RUN_TEST(test_line_buffer_multiple);
RUN_TEST(test_error_output);
return UNITY_END();
}

Loading…
Cancel
Save