2025-02-24 17:52:05 +01:00
|
|
|
/* ************************************************************************** */
|
|
|
|
|
/* */
|
|
|
|
|
/* ::: :::::::: */
|
2025-03-18 14:55:58 +01:00
|
|
|
/* test_parse_cmdlists.c :+: :+: :+: */
|
2025-02-24 17:52:05 +01:00
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
|
/* Created: 2025/02/24 17:40:48 by khais #+# #+# */
|
2025-03-19 16:26:45 +01:00
|
|
|
/* Updated: 2025/03/20 11:57:26 by khais ### ########.fr */
|
2025-02-24 17:52:05 +01:00
|
|
|
/* */
|
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
2025-02-26 14:07:55 +01:00
|
|
|
#include "ft_printf.h"
|
|
|
|
|
|
2025-03-18 14:55:58 +01:00
|
|
|
#include "../src/parser/cmdlist/cmdlist.h"
|
2025-02-24 18:18:15 +01:00
|
|
|
#include "testutil.h"
|
|
|
|
|
#include "unistd.h"
|
2025-02-24 17:52:05 +01:00
|
|
|
#include <assert.h>
|
2025-03-18 14:55:58 +01:00
|
|
|
#include "parse_cmdlist.h"
|
2025-02-24 17:52:05 +01:00
|
|
|
|
2025-03-18 14:55:58 +01:00
|
|
|
static void test_parse_cmdlist_empty(void)
|
2025-02-24 17:52:05 +01:00
|
|
|
{
|
2025-03-04 13:31:22 +01:00
|
|
|
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
2025-03-18 14:55:58 +01:00
|
|
|
t_cmdlist *cmd = parse_cmdlist("");
|
2025-02-24 17:52:05 +01:00
|
|
|
assert(cmd == NULL);
|
2025-02-25 15:15:23 +01:00
|
|
|
cmdlist_destroy(cmd);
|
2025-02-24 17:52:05 +01:00
|
|
|
}
|
|
|
|
|
|
2025-03-18 14:55:58 +01:00
|
|
|
static void test_parse_cmdlist_single_pipeline(void)
|
2025-02-25 12:53:12 +01:00
|
|
|
{
|
2025-03-04 13:31:22 +01:00
|
|
|
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
2025-03-18 14:55:58 +01:00
|
|
|
t_cmdlist *cmd = parse_cmdlist("echo this | cat -e");
|
2025-02-25 12:53:12 +01:00
|
|
|
assert(cmd != NULL);
|
|
|
|
|
assert_pipelineequal("echo this | cat -e", cmd, 0);
|
2025-02-25 15:02:23 +01:00
|
|
|
assert(cmd->operators[0] == OP_END);
|
2025-03-19 16:26:45 +01:00
|
|
|
assert(cmd->num_cmd == 1);
|
2025-02-25 15:15:23 +01:00
|
|
|
cmdlist_destroy(cmd);
|
2025-02-25 12:53:12 +01:00
|
|
|
}
|
|
|
|
|
|
2025-03-18 14:55:58 +01:00
|
|
|
static void test_parse_cmdlist_simple_and(void)
|
2025-02-24 18:18:15 +01:00
|
|
|
{
|
2025-03-04 13:31:22 +01:00
|
|
|
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
2025-03-18 14:55:58 +01:00
|
|
|
t_cmdlist *cmd = parse_cmdlist("echo this | cat -e && echo works | wc -c");
|
2025-02-24 18:18:15 +01:00
|
|
|
assert(cmd != NULL);
|
2025-02-25 12:53:12 +01:00
|
|
|
assert_pipelineequal("echo this | cat -e", cmd, 0);
|
2025-02-25 15:02:23 +01:00
|
|
|
assert(cmd->operators[0] == OP_AND);
|
2025-02-25 12:53:12 +01:00
|
|
|
assert_pipelineequal("echo works | wc -c", cmd, 1);
|
2025-03-19 16:26:45 +01:00
|
|
|
assert(cmd->num_cmd == 2);
|
2025-02-25 15:15:23 +01:00
|
|
|
cmdlist_destroy(cmd);
|
2025-02-24 18:18:15 +01:00
|
|
|
}
|
|
|
|
|
|
2025-03-18 14:55:58 +01:00
|
|
|
static void test_parse_cmdlist_simple_or(void)
|
2025-02-25 12:39:19 +01:00
|
|
|
{
|
2025-03-04 13:31:22 +01:00
|
|
|
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
2025-03-18 14:55:58 +01:00
|
|
|
t_cmdlist *cmd = parse_cmdlist("echo this | cat -e || echo works | wc -c");
|
2025-02-25 12:39:19 +01:00
|
|
|
assert(cmd != NULL);
|
|
|
|
|
assert_pipelineequal("echo this | cat -e", cmd, 0);
|
2025-02-25 15:02:23 +01:00
|
|
|
assert(cmd->operators[0] == OP_OR);
|
2025-02-25 12:39:19 +01:00
|
|
|
assert_pipelineequal("echo works | wc -c", cmd, 1);
|
2025-03-19 16:26:45 +01:00
|
|
|
assert(cmd->num_cmd == 2);
|
2025-02-25 15:15:23 +01:00
|
|
|
cmdlist_destroy(cmd);
|
2025-02-25 12:39:19 +01:00
|
|
|
}
|
|
|
|
|
|
2025-03-18 14:55:58 +01:00
|
|
|
static void test_parse_cmdlist_triple_or(void)
|
2025-02-25 13:46:11 +01:00
|
|
|
{
|
2025-03-04 13:31:22 +01:00
|
|
|
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
2025-03-18 14:55:58 +01:00
|
|
|
t_cmdlist *cmd = parse_cmdlist("echo this | cat -e || echo works | wc -c || echo as well | cut -d' ' -f1");
|
2025-02-25 13:46:11 +01:00
|
|
|
assert(cmd != NULL);
|
|
|
|
|
assert_pipelineequal("echo this | cat -e", cmd, 0);
|
2025-02-25 15:02:23 +01:00
|
|
|
assert(cmd->operators[0] == OP_OR);
|
2025-02-25 13:46:11 +01:00
|
|
|
assert_pipelineequal("echo works | wc -c", cmd, 1);
|
2025-02-25 15:02:23 +01:00
|
|
|
assert(cmd->operators[1] == OP_OR);
|
2025-02-25 13:46:11 +01:00
|
|
|
assert_pipelineequal("echo as well | cut -d' ' -f1", cmd, 2);
|
2025-03-19 16:26:45 +01:00
|
|
|
assert(cmd->num_cmd == 3);
|
2025-02-25 15:15:23 +01:00
|
|
|
cmdlist_destroy(cmd);
|
2025-02-25 13:46:11 +01:00
|
|
|
}
|
|
|
|
|
|
2025-03-18 14:55:58 +01:00
|
|
|
static void test_parse_cmdlist_triple_both_operators(void)
|
2025-02-25 15:02:23 +01:00
|
|
|
{
|
2025-03-04 13:31:22 +01:00
|
|
|
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
2025-03-18 14:55:58 +01:00
|
|
|
t_cmdlist *cmd = parse_cmdlist("echo this | cat -e || echo works | wc -c && echo as well | cut -d' ' -f1");
|
2025-02-25 15:02:23 +01:00
|
|
|
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);
|
2025-03-19 16:26:45 +01:00
|
|
|
assert(cmd->num_cmd == 3);
|
2025-02-25 15:15:23 +01:00
|
|
|
cmdlist_destroy(cmd);
|
2025-02-25 15:02:23 +01:00
|
|
|
}
|
|
|
|
|
|
2025-03-18 14:55:58 +01:00
|
|
|
static void test_parse_cmdlist_quad_both_operators(void)
|
2025-02-25 15:02:23 +01:00
|
|
|
{
|
2025-03-04 13:31:22 +01:00
|
|
|
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
2025-03-18 14:55:58 +01:00
|
|
|
t_cmdlist *cmd = parse_cmdlist("echo this | cat -e || echo works | wc -c && echo as well | cut -d' ' -f1 || echo final");
|
2025-02-25 15:02:23 +01:00
|
|
|
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);
|
2025-02-25 15:15:23 +01:00
|
|
|
cmdlist_destroy(cmd);
|
2025-02-25 15:02:23 +01:00
|
|
|
}
|
|
|
|
|
|
2025-03-18 14:55:58 +01:00
|
|
|
static void test_parse_cmdlist_invalid_pipeline(void)
|
2025-02-26 14:07:55 +01:00
|
|
|
{
|
2025-03-04 13:31:22 +01:00
|
|
|
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
2025-03-18 14:55:58 +01:00
|
|
|
t_cmdlist *cmd = parse_cmdlist("echo this | | cat -e || echo does not work");
|
2025-02-26 14:07:55 +01:00
|
|
|
assert(cmd == NULL);
|
2025-03-04 13:31:22 +01:00
|
|
|
cmdlist_destroy(cmd);
|
2025-02-26 14:07:55 +01:00
|
|
|
}
|
|
|
|
|
|
2025-03-18 14:55:58 +01:00
|
|
|
static void test_parse_cmdlist_simple_command(void)
|
2025-03-04 15:17:04 +01:00
|
|
|
{
|
|
|
|
|
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
2025-03-18 14:55:58 +01:00
|
|
|
t_cmdlist *cmd = parse_cmdlist("echo this");
|
2025-03-04 15:17:04 +01:00
|
|
|
assert(cmd != NULL);
|
|
|
|
|
assert_pipelineequal("echo this", cmd, 0);
|
|
|
|
|
assert(cmd->operators[0] == OP_END);
|
2025-03-19 16:26:45 +01:00
|
|
|
assert(cmd->num_cmd == 1);
|
2025-03-04 15:17:04 +01:00
|
|
|
cmdlist_destroy(cmd);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-24 17:52:05 +01:00
|
|
|
int main(void)
|
|
|
|
|
{
|
2025-03-18 14:55:58 +01:00
|
|
|
test_parse_cmdlist_empty();
|
2025-03-04 15:12:35 +01:00
|
|
|
do_leak_check();
|
2025-03-18 14:55:58 +01:00
|
|
|
test_parse_cmdlist_single_pipeline();
|
2025-03-04 15:12:35 +01:00
|
|
|
do_leak_check();
|
2025-03-18 14:55:58 +01:00
|
|
|
test_parse_cmdlist_simple_and();
|
2025-03-04 15:12:35 +01:00
|
|
|
do_leak_check();
|
2025-03-18 14:55:58 +01:00
|
|
|
test_parse_cmdlist_simple_or();
|
2025-03-04 15:12:35 +01:00
|
|
|
do_leak_check();
|
2025-03-18 14:55:58 +01:00
|
|
|
test_parse_cmdlist_triple_or();
|
2025-03-04 15:12:35 +01:00
|
|
|
do_leak_check();
|
2025-03-18 14:55:58 +01:00
|
|
|
test_parse_cmdlist_triple_both_operators();
|
2025-03-04 15:12:35 +01:00
|
|
|
do_leak_check();
|
2025-03-18 14:55:58 +01:00
|
|
|
test_parse_cmdlist_quad_both_operators();
|
2025-03-04 15:12:35 +01:00
|
|
|
do_leak_check();
|
2025-03-18 14:55:58 +01:00
|
|
|
test_parse_cmdlist_invalid_pipeline();
|
2025-03-04 15:14:55 +01:00
|
|
|
do_leak_check();
|
2025-03-18 14:55:58 +01:00
|
|
|
test_parse_cmdlist_simple_command();
|
2025-03-04 15:17:04 +01:00
|
|
|
do_leak_check();
|
2025-02-24 17:52:05 +01:00
|
|
|
return (0);
|
|
|
|
|
}
|