mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
211 lines
7.4 KiB
C
211 lines
7.4 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* parse_command_lists.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2025/02/24 17:40:48 by khais #+# #+# */
|
|
/* Updated: 2025/03/04 15:16:00 by khais ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "ft_printf.h"
|
|
|
|
#include "../src/parser/command_list/command_list.h"
|
|
#include "../src/parser/wordsplit/wordsplit.h"
|
|
#include "testutil.h"
|
|
#include "unistd.h"
|
|
#include <assert.h>
|
|
|
|
static t_cmdlist *parse_command_list(char *input)
|
|
{
|
|
ft_dprintf(STDERR_FILENO, "Now checking command list with input [%s]\n", input);
|
|
t_wordlist *words = minishell_wordsplit(input);
|
|
t_cmdlist *cmd = cmdlist_from_wordlist(words);
|
|
wordlist_destroy(words);
|
|
return (cmd);
|
|
}
|
|
|
|
static t_pipeline *parse_pipeline(char *input)
|
|
{
|
|
t_wordlist *words = minishell_wordsplit(input);
|
|
t_pipeline *pipeline = pipeline_from_wordlist(words);
|
|
return (pipeline);
|
|
}
|
|
|
|
static void assert_simple_commandequal(t_simple_cmd *expected, t_simple_cmd *got, int idx)
|
|
{
|
|
ft_dprintf(STDERR_FILENO, "Checking cmd idx %d\n", idx);
|
|
int i = 0;
|
|
t_wordlist *expected_word = expected->words;
|
|
t_wordlist *got_word = got->words;
|
|
while (expected_word != NULL)
|
|
{
|
|
ft_dprintf(STDERR_FILENO, "Checking word %d: ", i);
|
|
assert_strequal(expected_word->word->word, got_word->word->word);
|
|
ft_dprintf(STDERR_FILENO, "Checking word %d: expected flags=%d got flags=%d\n", i, expected_word->word->flags, got_word->word->flags);
|
|
assert(expected_word->word->flags == got_word->word->flags);
|
|
expected_word = expected_word->next;
|
|
got_word = got_word->next;
|
|
i++;
|
|
}
|
|
assert(expected_word == NULL);
|
|
assert(got_word == NULL);
|
|
}
|
|
|
|
static void assert_pipelineequal(char *expected, t_cmdlist *cmd, int idx)
|
|
{
|
|
ft_printf("Expected command list %p to have at least %d pipelines, and got %d\n", cmd, idx + 1, cmd->num_pipelines);
|
|
assert(cmd->num_pipelines >= idx + 1);
|
|
t_pipeline *got = cmd->pipelines[idx];
|
|
ft_dprintf(STDERR_FILENO, "Expected pipeline %p to equal [%s]\n", got, expected);
|
|
t_pipeline *expected_pipeline = parse_pipeline(expected);
|
|
assert(expected_pipeline == got || expected_pipeline != NULL);
|
|
int j = 0;
|
|
while (j < got->num_cmd)
|
|
{
|
|
ft_dprintf(STDERR_FILENO, "Got pipeline cmd %d: ", j);
|
|
wordlist_debug(got->cmds[j]->words);
|
|
j++;
|
|
}
|
|
ft_dprintf(STDERR_FILENO, "Expected pipeline to have %d commands, got pipeline with %d\n", expected_pipeline->num_cmd, got->num_cmd);
|
|
assert(expected_pipeline->num_cmd == got->num_cmd);
|
|
int i = 0;
|
|
while (i < expected_pipeline->num_cmd)
|
|
{
|
|
assert_simple_commandequal(expected_pipeline->cmds[i], got->cmds[i], i);
|
|
i++;
|
|
}
|
|
pipeline_destroy(expected_pipeline);
|
|
}
|
|
|
|
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_pipelines == 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_pipelines == 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_pipelines == 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_pipelines == 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_pipelines == 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_pipelines == 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);
|
|
}
|