minishell/tests/test_parse_cmdlists.c

149 lines
5.1 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_parse_cmdlists.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/24 17:40:48 by khais #+# #+# */
/* Updated: 2025/03/20 11:57:26 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
#include "../src/parser/cmdlist/cmdlist.h"
#include "testutil.h"
#include "unistd.h"
#include <assert.h>
#include "parse_cmdlist.h"
static void test_parse_cmdlist_empty(void)
{
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
t_cmdlist *cmd = parse_cmdlist("");
assert(cmd == NULL);
cmdlist_destroy(cmd);
}
static void test_parse_cmdlist_single_pipeline(void)
{
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
t_cmdlist *cmd = parse_cmdlist("echo this | cat -e");
assert(cmd != NULL);
assert_pipelineequal("echo this | cat -e", cmd, 0);
assert(cmd->operators[0] == OP_END);
assert(cmd->num_cmd == 1);
cmdlist_destroy(cmd);
}
static void test_parse_cmdlist_simple_and(void)
{
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
t_cmdlist *cmd = parse_cmdlist("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_cmd == 2);
cmdlist_destroy(cmd);
}
static void test_parse_cmdlist_simple_or(void)
{
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
t_cmdlist *cmd = parse_cmdlist("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_cmd == 2);
cmdlist_destroy(cmd);
}
static void test_parse_cmdlist_triple_or(void)
{
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
t_cmdlist *cmd = parse_cmdlist("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_cmd == 3);
cmdlist_destroy(cmd);
}
static void test_parse_cmdlist_triple_both_operators(void)
{
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
t_cmdlist *cmd = parse_cmdlist("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_cmd == 3);
cmdlist_destroy(cmd);
}
static void test_parse_cmdlist_quad_both_operators(void)
{
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
t_cmdlist *cmd = parse_cmdlist("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_cmdlist_invalid_pipeline(void)
{
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
t_cmdlist *cmd = parse_cmdlist("echo this | | cat -e || echo does not work");
assert(cmd == NULL);
cmdlist_destroy(cmd);
}
static void test_parse_cmdlist_simple_command(void)
{
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
t_cmdlist *cmd = parse_cmdlist("echo this");
assert(cmd != NULL);
assert_pipelineequal("echo this", cmd, 0);
assert(cmd->operators[0] == OP_END);
assert(cmd->num_cmd == 1);
cmdlist_destroy(cmd);
}
int main(void)
{
test_parse_cmdlist_empty();
do_leak_check();
test_parse_cmdlist_single_pipeline();
do_leak_check();
test_parse_cmdlist_simple_and();
do_leak_check();
test_parse_cmdlist_simple_or();
do_leak_check();
test_parse_cmdlist_triple_or();
do_leak_check();
test_parse_cmdlist_triple_both_operators();
do_leak_check();
test_parse_cmdlist_quad_both_operators();
do_leak_check();
test_parse_cmdlist_invalid_pipeline();
do_leak_check();
test_parse_cmdlist_simple_command();
do_leak_check();
return (0);
}