wizapp-stdlib/tests/shell_commander/test_shell_commander.cc
Abdelrahman d3fccd61b5 Reintroduce C++ support and add usage tests for C++ (#4)
Reviewed-on: #4
Co-authored-by: Abdelrahman <said.abdelrahman89@gmail.com>
Co-committed-by: Abdelrahman <said.abdelrahman89@gmail.com>
2025-08-10 22:33:40 +00:00

71 lines
2.5 KiB
C++

#include "test_shell_commander.h"
#include "wapp.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
TestFuncResult test_commander_cmd_success(void) {
Str8List cmd = {};
Str8Node echo = wapp_str8_node_from_cstr("echo");
Str8Node msg = wapp_str8_node_from_cstr("hello world");
wapp_str8_list_push_back(&cmd, &echo);
wapp_str8_list_push_back(&cmd, &msg);
CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_DISCARD, nullptr, &cmd);
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) {
Str8List cmd = {};
Str8Node grep = wapp_str8_node_from_cstr("grep");
wapp_str8_list_push_back(&cmd, &grep);
CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_DISCARD, nullptr, &cmd);
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) {
Str8 buf = wapp_str8_buf(64);
Str8 expected = wapp_str8_buf(64);
char msg[] = "hello world";
wapp_str8_copy_cstr_capped(&expected, msg);
Str8List cmd = {};
Str8Node echo = wapp_str8_node_from_cstr("echo");
Str8Node arg = wapp_str8_node_from_cstr(msg);
wapp_str8_list_push_back(&cmd, &echo);
wapp_str8_list_push_back(&cmd, &arg);
CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_CAPTURE, &buf, &cmd);
bool succeeded = result.exited && result.exit_code == EXIT_SUCCESS &&
result.error == SHELL_ERR_NO_ERROR && wapp_str8_equal_to_count(&buf, &expected, strlen(msg));
return wapp_tester_result(succeeded);
}
TestFuncResult test_commander_cmd_out_buf_failure(void) {
Str8 buf = wapp_str8_buf(4);
Str8 expected = wapp_str8_buf(64);
char msg[] = "hello world";
wapp_str8_copy_cstr_capped(&expected, msg);
Str8List cmd = {};
Str8Node echo = wapp_str8_node_from_cstr("echo");
Str8Node arg = wapp_str8_node_from_cstr(msg);
wapp_str8_list_push_back(&cmd, &echo);
wapp_str8_list_push_back(&cmd, &arg);
CMDResult result = wapp_shell_commander_execute(SHELL_OUTPUT_CAPTURE, &buf, &cmd);
bool failed = !result.exited && result.exit_code != EXIT_SUCCESS &&
result.error == SHELL_ERR_OUT_BUF_FULL && !wapp_str8_equal(&buf, &expected);
return wapp_tester_result(failed);
}