42sh/tests/unit/io_backend/io_backend.c
2026-01-27 19:56:33 +01:00

96 lines
2.7 KiB
C

#include <criterion/criterion.h>
#include <criterion/new/assert.h>
#include <io_backend/io_backend.h>
#include <stdio.h>
TestSuite(IO_Backend);
// IOB Init
Test(IO_Backend, init_null)
{
struct iob_context ctx = { .mode = IOB_MODE_NULL, .args = NULL };
int actual = iob_init(&ctx);
int expected = IOB_ERROR_BAD_ARG;
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
}
Test(IO_Backend, init_stdin)
{
struct iob_context ctx = { .mode = IOB_MODE_STDIN, .args = NULL };
int actual = iob_init(&ctx);
int expected = 0;
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
iob_close();
}
// WARNING: this one could fail because of iob_close in the previous test
// Same applies for other tests
Test(IO_Backend, init_script)
{
char *script_name = "script.tmp";
struct iob_context ctx = { .mode = IOB_MODE_SCRIPT, .args = script_name };
// Create file
FILE *f = fopen(script_name, "w");
fclose(f);
int actual = iob_init(&ctx);
int expected = 0;
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
iob_close();
remove(script_name);
}
Test(IO_Backend, init_script_not_a_file)
{
char *script_name = "not_a_file.tmp";
struct iob_context ctx = { .mode = IOB_MODE_SCRIPT, .args = script_name };
int actual = iob_init(&ctx);
int expected = IOB_ERROR_CANNOT_OPEN_FILE;
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
}
Test(IO_Backend, init_script_null)
{
struct iob_context ctx = { .mode = IOB_MODE_SCRIPT, .args = NULL };
int actual = iob_init(&ctx);
int expected = IOB_ERROR_CANNOT_OPEN_FILE;
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
}
Test(IO_Backend, init_cmd)
{
char *cmd = "iamacommand --yesido";
struct iob_context ctx = { .mode = IOB_MODE_CMD, .args = cmd };
int actual = iob_init(&ctx);
int expected = 0;
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
iob_close();
}
Test(IO_Backend, init_cmd_null)
{
struct iob_context ctx = { .mode = IOB_MODE_CMD, .args = NULL };
int actual = iob_init(&ctx);
int expected = IOB_ERROR_BAD_ARG;
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
}
Test(IO_Backend, init_already_init)
{
char *cmd = "iamacommand --yesido";
struct iob_context ctx = { .mode = IOB_MODE_CMD, .args = cmd };
iob_init(&ctx);
int actual = iob_init(&ctx);
int expected = IOB_ERROR_MODULE_ALREADY_INITIALIZED;
cr_expect(actual == expected, "Expected: %d. Got: %d", expected, actual);
iob_close();
}
Test(IO_Backend, close_not_init)
{
iob_close(); // Shouldn't do anything
}
// IOB Stream
// TODO