文件

85 行
2.1 KiB
C

/*
* psh - Test cases for the shell
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
/* Test helper */
int run_test(const char *cmd, const char *expected_substring) {
char output[4096];
FILE *fp;
int passed = 0;
printf("Testing: %s\n", cmd);
/* Run command and capture output */
char full_cmd[4096];
snprintf(full_cmd, sizeof(full_cmd), "psh.exe -c \"%s\" 2>&1", cmd);
fp = _popen(full_cmd, "r");
if (fp == NULL) {
printf(" FAILED: Could not execute command\n");
return 0;
}
memset(output, 0, sizeof(output));
if (fread(output, 1, sizeof(output) - 1, fp) > 0) {
/* Remove trailing newline */
output[strcspn(output, "\n")] = '\0';
if (strstr(output, expected_substring) != NULL) {
printf(" PASSED: Got expected output containing '%s'\n", expected_substring);
passed = 1;
} else {
printf(" FAILED: Expected output containing '%s', got: '%s'\n", expected_substring, output);
}
} else {
printf(" FAILED: No output from command\n");
}
_pclose(fp);
return passed;
}
int main() {
int passed = 0;
int total = 0;
printf("=== psh Test Suite ===\n\n");
/* Test 1: echo */
total++;
if (run_test("echo hello", "hello")) passed++;
/* Test 2: pwd */
total++;
if (run_test("pwd", "F:\\Paze SH")) passed++;
/* Test 3: environment variables */
total++;
if (run_test("echo $HOME", "")) passed++; /* Just check it doesn't crash */
/* Test 4: help */
total++;
if (run_test("help", "Built-in commands")) passed++;
/* Test 5: history (empty) */
total++;
if (run_test("history", "history")) passed++;
/* Test 6: && operator */
total++;
if (run_test("echo first && echo second", "first")) passed++;
/* Test 7: || operator */
total++;
if (run_test("false || echo fallback", "fallback")) passed++;
printf("\n=== Results: %d/%d tests passed ===\n", passed, total);
return (passed == total) ? 0 : 1;
}