diff --git a/code/espurna/terminal_commands.cpp b/code/espurna/terminal_commands.cpp index ffd33f5c..682e721a 100644 --- a/code/espurna/terminal_commands.cpp +++ b/code/espurna/terminal_commands.cpp @@ -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) { diff --git a/code/espurna/terminal_commands.h b/code/espurna/terminal_commands.h index 5a5ef417..90564cf2 100644 --- a/code/espurna/terminal_commands.h +++ b/code/espurna/terminal_commands.h @@ -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 diff --git a/code/test/unit/src/terminal/terminal.cpp b/code/test/unit/src/terminal/terminal.cpp index 8692292f..b51cdbb5 100644 --- a/code/test/unit/src/terminal/terminal.cpp +++ b/code/test/unit/src/terminal/terminal.cpp @@ -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(); }