Add tests for the shell commander
This commit is contained in:
parent
802b70f5ee
commit
8a91a0ec6b
55
tests/shell_commander/test_shell_commander.c
Normal file
55
tests/shell_commander/test_shell_commander.c
Normal 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);
|
||||
}
|
19
tests/shell_commander/test_shell_commander.h
Normal file
19
tests/shell_commander/test_shell_commander.h
Normal 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
|
@ -1,13 +1,16 @@
|
||||
#include "test_arena.h"
|
||||
#include "test_shell_commander.h"
|
||||
#include "tester.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user