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

70 lines
2.5 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 = wapp_str8_lit("echo");
WpStr8 msg = wapp_str8_lit("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 = wapp_str8_lit("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 = wapp_str8_buf(64);
WpStr8 expected = wapp_str8_buf(64);
char msg[] = "hello world";
wapp_str8_copy_cstr_capped(&expected, msg);
WpStr8List cmd = wapp_dbl_list(WpStr8);
WpStr8 echo = wapp_str8_lit("echo");
WpStr8 arg = wapp_str8_lit(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 && wapp_str8_equal_to_count(&buf, &expected, strlen(msg));
return wpTesterResult(succeeded);
}
WpTestFuncResult test_commander_cmd_out_buf_failure(void) {
WpStr8 buf = wapp_str8_buf(4);
WpStr8 expected = wapp_str8_buf(64);
char msg[] = "hello world";
wapp_str8_copy_cstr_capped(&expected, msg);
WpStr8List cmd = wapp_dbl_list(WpStr8);
WpStr8 echo = wapp_str8_lit("echo");
WpStr8 arg = wapp_str8_lit(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 && !wapp_str8_equal(&buf, &expected);
return wpTesterResult(failed);
}