diff --git a/tests/shell_commander/test_shell_commander.c b/tests/shell_commander/test_shell_commander.c new file mode 100644 index 0000000..a0e1432 --- /dev/null +++ b/tests/shell_commander/test_shell_commander.c @@ -0,0 +1,55 @@ +#include "test_shell_commander.h" +#include "commander.h" +#include "tester.h" +#include +#include +#include +#include + +TestFuncResult test_commander_cmd_success(void) { + CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_DISCARD, NULL, 0, + "echo", "hello world"); + + bool succeeded = result.exited && result.exit_code == EXIT_SUCCESS && + result.error == SHELL_ERR_NO_ERROR; + + return wapp_tester_result(succeeded); +} + +TestFuncResult test_commander_cmd_failure(void) { + CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_DISCARD, NULL, 0, + "grep", "2>&1"); + + bool failed = result.exited && result.exit_code != EXIT_SUCCESS && + result.error == SHELL_ERR_NO_ERROR; + + return wapp_tester_result(failed); +} + +TestFuncResult test_commander_cmd_out_buf_success(void) { + char buf[64] = {0}; + char expected_output[64] = {0}; + const char *msg = "hello world"; + sprintf(expected_output, "%s\n", msg); + + CMDResult result = + wapp_shell_commander_execute(SHELL_OUTPUT_CAPTURE, buf, 64, "echo", msg); + + bool succeeded = result.exited && result.exit_code == EXIT_SUCCESS && + result.error == SHELL_ERR_NO_ERROR && + strcmp(buf, expected_output) == 0; + + return wapp_tester_result(succeeded); +} + +TestFuncResult test_commander_cmd_out_buf_failure(void) { + char buf[4] = {0}; + const char *msg = "hello world"; + CMDResult result = + wapp_shell_commander_execute(SHELL_OUTPUT_CAPTURE, buf, 4, "echo", msg); + + bool failed = !result.exited && result.exit_code != EXIT_SUCCESS && + result.error == SHELL_ERR_OUT_BUF_FULL && strcmp(buf, msg) != 0; + + return wapp_tester_result(failed); +} diff --git a/tests/shell_commander/test_shell_commander.h b/tests/shell_commander/test_shell_commander.h new file mode 100644 index 0000000..4cd6fc4 --- /dev/null +++ b/tests/shell_commander/test_shell_commander.h @@ -0,0 +1,19 @@ +#ifndef TEST_SHELL_COMMANDER_H +#define TEST_SHELL_COMMANDER_H + +#include "tester.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +TestFuncResult test_commander_cmd_success(void); +TestFuncResult test_commander_cmd_failure(void); +TestFuncResult test_commander_cmd_out_buf_success(void); +TestFuncResult test_commander_cmd_out_buf_failure(void); + +#ifdef __cplusplus +} +#endif // __cplusplus + +#endif // !TEST_SHELL_COMMANDER_H diff --git a/tests/wapptest.c b/tests/wapptest.c index a7156c3..279e1b3 100644 --- a/tests/wapptest.c +++ b/tests/wapptest.c @@ -1,13 +1,16 @@ #include "test_arena.h" +#include "test_shell_commander.h" #include "tester.h" #include int main(void) { - wapp_tester_run_tests(test_arena_init, - test_arena_init_succeeds_when_reserving_very_large_size, - test_arena_alloc_succeeds_when_within_capacity, - test_arena_alloc_fails_when_over_capacity, - test_arena_clear, test_arena_destroy); + wapp_tester_run_tests( + test_arena_init, test_arena_init_succeeds_when_reserving_very_large_size, + test_arena_alloc_succeeds_when_within_capacity, + test_arena_alloc_fails_when_over_capacity, test_arena_clear, + test_arena_destroy, test_commander_cmd_success, + test_commander_cmd_failure, test_commander_cmd_out_buf_success, + test_commander_cmd_out_buf_failure); return EXIT_SUCCESS; }