42sh/tests/unit/utils/args.c
2026-01-23 17:05:56 +00:00

173 lines
4.8 KiB
C

#include "../../../src/utils/args/args.h"
#include <criterion/criterion.h>
#include <criterion/new/assert.h>
#include <criterion/redirect.h>
#include "../../../src/utils/hash_map/hash_map.h"
#include "../../../src/utils/vars/vars.h"
TestSuite(utils_args);
Test(utils_args, basic_command)
{
int argc = 3;
struct args_options options;
char *input[] = { "program", "-c", "echo Hello, World!" };
struct hash_map *vars = vars_init();
int r = args_handler(argc, input, &options, vars);
cr_expect(r == 0);
cr_expect(options.pretty_print == false);
cr_expect(options.verbose == false);
cr_expect(options.type == INPUT_CMD);
cr_expect(eq(options.input_source, "echo Hello, World!"));
hash_map_free(&vars);
}
Test(utils_args, basic_command_with_flags)
{
int argc = 5;
struct args_options options;
char *input[] = { "program", "--pretty-print", "-c", "echo Hello, World!",
"--verbose" };
struct hash_map *vars = vars_init();
int r = args_handler(argc, input, &options, vars);
cr_expect(r == 0);
cr_expect(options.pretty_print == true);
cr_expect(options.verbose == true);
cr_expect(options.type == INPUT_CMD);
cr_expect(eq(options.input_source, "echo Hello, World!"));
hash_map_free(&vars);
}
Test(utils_args, basic_file_input)
{
int argc = 2;
struct args_options options;
char *input[] = { "program", "input.txt" };
struct hash_map *vars = vars_init();
int r = args_handler(argc, input, &options, vars);
cr_expect(r == 0);
cr_expect(options.pretty_print == false);
cr_expect(options.verbose == false);
cr_expect(options.type == INPUT_FILE);
cr_expect(eq(options.input_source, "input.txt"));
hash_map_free(&vars);
}
Test(utils_args, basic_file_input_with_flags)
{
int argc = 4;
struct args_options options;
char *input[] = { "program", "--verbose", "input.txt", "--pretty-print" };
struct hash_map *vars = vars_init();
int r = args_handler(argc, input, &options, vars);
cr_expect(r == 0);
cr_expect(options.pretty_print == true);
cr_expect(options.verbose == true);
cr_expect(options.type == INPUT_FILE);
cr_expect(eq(options.input_source, "input.txt"));
hash_map_free(&vars);
}
Test(utils_args, basic_stdin_input)
{
int argc = 1;
struct args_options options;
char *input[] = { "program" };
struct hash_map *vars = vars_init();
int r = args_handler(argc, input, &options, vars);
cr_expect(r == 0);
cr_expect(options.pretty_print == false);
cr_expect(options.verbose == false);
cr_expect(options.type == INPUT_STDIN);
cr_expect(options.input_source == NULL);
hash_map_free(&vars);
}
Test(utils_args, pretty_print_and_verbose_flags)
{
int argc = 3;
struct args_options options;
char *input[] = { "program", "--pretty-print", "--verbose" };
struct hash_map *vars = vars_init();
int r = args_handler(argc, input, &options, vars);
cr_expect(r == 0);
cr_expect(options.pretty_print == true);
cr_expect(options.verbose == true);
cr_expect(options.type == INPUT_STDIN);
cr_expect(options.input_source == NULL);
hash_map_free(&vars);
}
Test(utils_args, missing_command_after_c, .init = cr_redirect_stderr)
{
int argc = 2;
struct args_options options;
char *input[] = { "program", "-c" };
struct hash_map *vars = vars_init();
int r = args_handler(argc, input, &options, vars);
cr_expect(r != 0);
cr_assert_stderr_eq_str("No command provided after -c\n");
hash_map_free(&vars);
}
Test(utils_args, unknown_option, .init = cr_redirect_stderr)
{
int argc = 2;
struct args_options options;
char *input[] = { "program", "--unknown" };
struct hash_map *vars = vars_init();
int r = args_handler(argc, input, &options, vars);
cr_expect(r != 0);
cr_assert_stderr_eq_str("Unknown option: --unknown\n");
hash_map_free(&vars);
}
Test(utils_args, multiple_command, .init = cr_redirect_stderr)
{
int argc = 4;
struct args_options options;
char *input[] = { "program", "exec.sh", "-c", "echo World" };
struct hash_map *vars = vars_init();
int r = args_handler(argc, input, &options, vars);
cr_expect(r != 0);
cr_assert_stderr_eq_str("Multiple input sources specified: echo World\n");
hash_map_free(&vars);
}
Test(utils_args, command_with_additional_arguments)
{
int argc = 5;
struct args_options options;
char *input[] = { "program", "-c", "echo Hello", "arg1", "arg2" };
struct hash_map *vars = vars_init();
int r = args_handler(argc, input, &options, vars);
cr_expect(r == 0);
cr_expect(options.type == INPUT_CMD);
cr_expect(eq(options.input_source, "echo Hello"));
cr_expect_str_eq(get_var(vars, "0"), "program");
cr_expect_str_eq(get_var(vars, "1"), "arg1");
cr_expect_str_eq(get_var(vars, "2"), "arg2");
hash_map_free(&vars);
}