Add tests for the shell commander

This commit is contained in:
Abdelrahman Said 2024-06-17 00:15:10 +01:00
parent 802b70f5ee
commit 8a91a0ec6b
3 changed files with 82 additions and 5 deletions

View File

@ -0,0 +1,55 @@
#include "test_shell_commander.h"
#include "commander.h"
#include "tester.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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);
}

View File

@ -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

View File

@ -1,13 +1,16 @@
#include "test_arena.h" #include "test_arena.h"
#include "test_shell_commander.h"
#include "tester.h" #include "tester.h"
#include <stdlib.h> #include <stdlib.h>
int main(void) { int main(void) {
wapp_tester_run_tests(test_arena_init, wapp_tester_run_tests(
test_arena_init_succeeds_when_reserving_very_large_size, test_arena_init, test_arena_init_succeeds_when_reserving_very_large_size,
test_arena_alloc_succeeds_when_within_capacity, test_arena_alloc_succeeds_when_within_capacity,
test_arena_alloc_fails_when_over_capacity, test_arena_alloc_fails_when_over_capacity, test_arena_clear,
test_arena_clear, test_arena_destroy); 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; return EXIT_SUCCESS;
} }