/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* test_parse_command_lists.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/24 17:40:48 by khais #+# #+# */ /* Updated: 2025/03/18 13:09:51 by khais ### ########.fr */ /* */ /* ************************************************************************** */ #include "ft_printf.h" #include "../src/parser/command_list/command_list.h" #include "testutil.h" #include "unistd.h" #include #include "parse_command_list.h" static void test_parse_command_list_empty(void) { ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__); t_cmdlist *cmd = parse_command_list(""); assert(cmd == NULL); cmdlist_destroy(cmd); } static void test_parse_command_list_single_pipeline(void) { ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__); t_cmdlist *cmd = parse_command_list("echo this | cat -e"); assert(cmd != NULL); assert_pipelineequal("echo this | cat -e", cmd, 0); assert(cmd->operators[0] == OP_END); assert(cmd->num_cmds == 1); cmdlist_destroy(cmd); } static void test_parse_command_list_simple_and(void) { ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__); t_cmdlist *cmd = parse_command_list("echo this | cat -e && echo works | wc -c"); assert(cmd != NULL); assert_pipelineequal("echo this | cat -e", cmd, 0); assert(cmd->operators[0] == OP_AND); assert_pipelineequal("echo works | wc -c", cmd, 1); assert(cmd->num_cmds == 2); cmdlist_destroy(cmd); } static void test_parse_command_list_simple_or(void) { ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__); t_cmdlist *cmd = parse_command_list("echo this | cat -e || echo works | wc -c"); assert(cmd != NULL); assert_pipelineequal("echo this | cat -e", cmd, 0); assert(cmd->operators[0] == OP_OR); assert_pipelineequal("echo works | wc -c", cmd, 1); assert(cmd->num_cmds == 2); cmdlist_destroy(cmd); } static void test_parse_command_list_triple_or(void) { ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__); t_cmdlist *cmd = parse_command_list("echo this | cat -e || echo works | wc -c || echo as well | cut -d' ' -f1"); assert(cmd != NULL); assert_pipelineequal("echo this | cat -e", cmd, 0); assert(cmd->operators[0] == OP_OR); assert_pipelineequal("echo works | wc -c", cmd, 1); assert(cmd->operators[1] == OP_OR); assert_pipelineequal("echo as well | cut -d' ' -f1", cmd, 2); assert(cmd->num_cmds == 3); cmdlist_destroy(cmd); } static void test_parse_command_list_triple_both_operators(void) { ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__); t_cmdlist *cmd = parse_command_list("echo this | cat -e || echo works | wc -c && echo as well | cut -d' ' -f1"); assert(cmd != NULL); assert_pipelineequal("echo this | cat -e", cmd, 0); assert(cmd->operators[0] == OP_OR); assert_pipelineequal("echo works | wc -c", cmd, 1); assert(cmd->operators[1] == OP_AND); assert_pipelineequal("echo as well | cut -d' ' -f1", cmd, 2); assert(cmd->operators[2] == OP_END); assert(cmd->num_cmds == 3); cmdlist_destroy(cmd); } static void test_parse_command_list_quad_both_operators(void) { ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__); t_cmdlist *cmd = parse_command_list("echo this | cat -e || echo works | wc -c && echo as well | cut -d' ' -f1 || echo final"); assert(cmd != NULL); assert_pipelineequal("echo this | cat -e", cmd, 0); assert(cmd->operators[0] == OP_OR); assert_pipelineequal("echo works | wc -c", cmd, 1); assert(cmd->operators[1] == OP_AND); assert_pipelineequal("echo as well | cut -d' ' -f1", cmd, 2); assert(cmd->operators[2] == OP_OR); assert_pipelineequal("echo final", cmd, 3); assert(cmd->operators[3] == OP_END); cmdlist_destroy(cmd); } static void test_parse_command_list_invalid_pipeline(void) { ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__); t_cmdlist *cmd = parse_command_list("echo this | | cat -e || echo does not work"); assert(cmd == NULL); cmdlist_destroy(cmd); } static void test_parse_command_list_simple_command(void) { ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__); t_cmdlist *cmd = parse_command_list("echo this"); assert(cmd != NULL); assert_pipelineequal("echo this", cmd, 0); assert(cmd->operators[0] == OP_END); assert(cmd->num_cmds == 1); cmdlist_destroy(cmd); } int main(void) { test_parse_command_list_empty(); do_leak_check(); test_parse_command_list_single_pipeline(); do_leak_check(); test_parse_command_list_simple_and(); do_leak_check(); test_parse_command_list_simple_or(); do_leak_check(); test_parse_command_list_triple_or(); do_leak_check(); test_parse_command_list_triple_both_operators(); do_leak_check(); test_parse_command_list_quad_both_operators(); do_leak_check(); test_parse_command_list_invalid_pipeline(); do_leak_check(); test_parse_command_list_simple_command(); do_leak_check(); return (0); }