Files
wizapp-stdlib/tests/shell_commander/test_shell_commander.cc
T
2026-06-26 16:18:46 +01:00

70 lines
2.4 KiB
C++

#include "test_shell_commander.h"
#include "wapp.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
WpTestFuncResult test_commander_cmd_success(void) {
WpStr8List cmd = wapp_dbl_list(WpStr8);
WpStr8 echo = wpStr8Lit("echo");
WpStr8 msg = wpStr8Lit("hello world");
wapp_dbl_list_push_back(WpStr8, &cmd, &echo);
wapp_dbl_list_push_back(WpStr8, &cmd, &msg);
CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_DISCARD, nullptr, &cmd);
b8 succeeded = result.exited && result.exit_code == EXIT_SUCCESS &&
result.error == SHELL_ERR_NO_ERROR;
return wpTesterResult(succeeded);
}
WpTestFuncResult test_commander_cmd_failure(void) {
WpStr8List cmd = wapp_dbl_list(WpStr8);
WpStr8 grep = wpStr8Lit("grep");
wapp_dbl_list_push_back(WpStr8, &cmd, &grep);
CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_DISCARD, nullptr, &cmd);
b8 failed = result.exited && result.exit_code != EXIT_SUCCESS &&
result.error == SHELL_ERR_NO_ERROR;
return wpTesterResult(failed);
}
WpTestFuncResult test_commander_cmd_out_buf_success(void) {
WpStr8 buf = wpStr8Buf(64);
WpStr8 expected = wpStr8Buf(64);
char msg[] = "hello world";
wpStr8CopyCstrCapped(&expected, msg);
WpStr8List cmd = wapp_dbl_list(WpStr8);
WpStr8 echo = wpStr8Lit("echo");
WpStr8 arg = wpStr8Lit(msg);
wapp_dbl_list_push_back(WpStr8, &cmd, &echo);
wapp_dbl_list_push_back(WpStr8, &cmd, &arg);
CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_CAPTURE, &buf, &cmd);
b8 succeeded = result.exited && result.exit_code == EXIT_SUCCESS &&
result.error == SHELL_ERR_NO_ERROR && wpStr8EqualToCount(&buf, &expected, strlen(msg));
return wpTesterResult(succeeded);
}
WpTestFuncResult test_commander_cmd_out_buf_failure(void) {
WpStr8 buf = wpStr8Buf(4);
WpStr8 expected = wpStr8Buf(64);
char msg[] = "hello world";
wpStr8CopyCstrCapped(&expected, msg);
WpStr8List cmd = wapp_dbl_list(WpStr8);
WpStr8 echo = wpStr8Lit("echo");
WpStr8 arg = wpStr8Lit(msg);
wapp_dbl_list_push_back(WpStr8, &cmd, &echo);
wapp_dbl_list_push_back(WpStr8, &cmd, &arg);
CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_CAPTURE, &buf, &cmd);
b8 failed = !result.exited && result.exit_code != EXIT_SUCCESS &&
result.error == SHELL_ERR_OUT_BUF_FULL && !wpStr8Equal(&buf, &expected);
return wpTesterResult(failed);
}