mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
Parsing-refactor: Determining structures.
This commit is contained in:
parent
befe219436
commit
5ff990ef50
17 changed files with 103 additions and 702 deletions
|
|
@ -5,7 +5,6 @@ rawtests = \
|
|||
test_cmdlist_use_after_free \
|
||||
test_here_doc \
|
||||
test_wordlist_idx \
|
||||
test_redirection_parsing \
|
||||
test_quote_removal \
|
||||
test_metacharacters \
|
||||
test_parse_cmdlists \
|
||||
|
|
|
|||
|
|
@ -1,168 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* test_redirection_parsing.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/10 16:50/33 by khais #+# #+# */
|
||||
/* Updated: 2025/03/10 16:50:33 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <assert.h>
|
||||
#include "../src/parser/simple_cmd/simple_cmd.h"
|
||||
#include "../src/parser/wordsplit/wordsplit.h"
|
||||
#include "../src/postprocess/redirections/redirection_parsing.h"
|
||||
#include "../src/postprocess/redirections/redirection_parsing.h"
|
||||
#include "../src/postprocess/redirections/redirection_list.h"
|
||||
#include "../src/ft_errno.h"
|
||||
#include "ft_printf.h"
|
||||
#include "stdio.h"
|
||||
#include "testutil.h"
|
||||
|
||||
static t_simple_cmd *parse_simple_cmd(char *input)
|
||||
{
|
||||
t_wordlist *words = minishell_wordsplit(input);
|
||||
t_simple_cmd *cmd = simple_cmd_from_wordlist(words);
|
||||
return (cmd);
|
||||
}
|
||||
|
||||
static void test_redirection_parsing_no_redirections(void)
|
||||
{
|
||||
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
||||
t_simple_cmd *cmd = parse_simple_cmd("echo hello world");
|
||||
assert(parse_redirections(cmd) != NULL);
|
||||
assert(cmd->redirections == NULL);
|
||||
simple_cmd_destroy(cmd);
|
||||
do_leak_check();
|
||||
}
|
||||
|
||||
static void test_redirection_parsing_output_at_start(void)
|
||||
{
|
||||
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
||||
t_simple_cmd *cmd = parse_simple_cmd(">outfile echo hello world");
|
||||
assert(parse_redirections(cmd) != NULL);
|
||||
assert_strequal("outfile", cmd->redirections->redirection->marker->word);
|
||||
assert(cmd->redirections->redirection->type == REDIR_OUTPUT);
|
||||
assert(cmd->redirections->next == NULL);
|
||||
assert_strequal("echo", cmd->words->word->word);
|
||||
assert_strequal("hello", cmd->words->next->word->word);
|
||||
assert_strequal("world", cmd->words->next->next->word->word);
|
||||
assert(NULL == cmd->words->next->next->next);
|
||||
simple_cmd_destroy(cmd);
|
||||
do_leak_check();
|
||||
}
|
||||
|
||||
static void test_redirection_parsing_output_at_end(void)
|
||||
{
|
||||
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
||||
t_simple_cmd *cmd = parse_simple_cmd("echo hello world > outfile");
|
||||
assert(parse_redirections(cmd) != NULL);
|
||||
assert_strequal("echo", cmd->words->word->word);
|
||||
assert_strequal("hello", cmd->words->next->word->word);
|
||||
assert_strequal("world", cmd->words->next->next->word->word);
|
||||
assert(NULL == cmd->words->next->next->next);
|
||||
assert_strequal("outfile", cmd->redirections->redirection->marker->word);
|
||||
assert(cmd->redirections->redirection->type == REDIR_OUTPUT);
|
||||
assert(cmd->redirections->next == NULL);
|
||||
simple_cmd_destroy(cmd);
|
||||
do_leak_check();
|
||||
}
|
||||
|
||||
static void test_redirection_parsing_output_in_middle(void)
|
||||
{
|
||||
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
||||
t_simple_cmd *cmd = parse_simple_cmd("echo hello >outfile world");
|
||||
assert(parse_redirections(cmd) != NULL);
|
||||
assert_strequal("echo", cmd->words->word->word);
|
||||
assert_strequal("hello", cmd->words->next->word->word);
|
||||
assert_strequal("world", cmd->words->next->next->word->word);
|
||||
assert(NULL == cmd->words->next->next->next);
|
||||
assert_strequal("outfile", cmd->redirections->redirection->marker->word);
|
||||
assert(cmd->redirections->redirection->type == REDIR_OUTPUT);
|
||||
assert(cmd->redirections->next == NULL);
|
||||
simple_cmd_destroy(cmd);
|
||||
do_leak_check();
|
||||
}
|
||||
|
||||
static void test_redirection_parsing_output_no_filename_at_end(void)
|
||||
{
|
||||
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
||||
ft_errno(FT_ESUCCESS);
|
||||
t_simple_cmd *cmd = parse_simple_cmd("echo hello world >");
|
||||
assert(parse_redirections(cmd) == NULL);
|
||||
assert(FT_EMALFORMED_REDIRECTION == ft_errno_get());
|
||||
simple_cmd_destroy(cmd);
|
||||
do_leak_check();
|
||||
}
|
||||
|
||||
static void test_redirection_parsing_null(void)
|
||||
{
|
||||
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
||||
assert(parse_redirections(NULL) == NULL);
|
||||
do_leak_check();
|
||||
}
|
||||
|
||||
static void test_redirection_parsing_input_in_middle(void)
|
||||
{
|
||||
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
||||
t_simple_cmd *cmd = parse_simple_cmd("echo hello <outfile world");
|
||||
assert(parse_redirections(cmd) != NULL);
|
||||
assert_strequal("echo", cmd->words->word->word);
|
||||
assert_strequal("hello", cmd->words->next->word->word);
|
||||
assert_strequal("world", cmd->words->next->next->word->word);
|
||||
assert(NULL == cmd->words->next->next->next);
|
||||
assert_strequal("outfile", cmd->redirections->redirection->marker->word);
|
||||
assert(cmd->redirections->redirection->type == REDIR_INPUT);
|
||||
assert(cmd->redirections->next == NULL);
|
||||
simple_cmd_destroy(cmd);
|
||||
do_leak_check();
|
||||
}
|
||||
|
||||
static void test_redirection_parsing_here_doc_in_middle(void)
|
||||
{
|
||||
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
||||
t_simple_cmd *cmd = parse_simple_cmd("echo hello <<outfile world");
|
||||
assert(parse_redirections(cmd) != NULL);
|
||||
assert_strequal("echo", cmd->words->word->word);
|
||||
assert_strequal("hello", cmd->words->next->word->word);
|
||||
assert_strequal("world", cmd->words->next->next->word->word);
|
||||
assert(NULL == cmd->words->next->next->next);
|
||||
assert_strequal("outfile", cmd->redirections->redirection->marker->word);
|
||||
assert(cmd->redirections->redirection->type == REDIR_HERE_DOC);
|
||||
assert(cmd->redirections->next == NULL);
|
||||
simple_cmd_destroy(cmd);
|
||||
do_leak_check();
|
||||
}
|
||||
|
||||
|
||||
static void test_redirection_parsing_append_in_middle(void)
|
||||
{
|
||||
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
||||
t_simple_cmd *cmd = parse_simple_cmd("echo hello >> outfile world");
|
||||
assert(parse_redirections(cmd) != NULL);
|
||||
assert_strequal("echo", cmd->words->word->word);
|
||||
assert_strequal("hello", cmd->words->next->word->word);
|
||||
assert_strequal("world", cmd->words->next->next->word->word);
|
||||
assert(NULL == cmd->words->next->next->next);
|
||||
assert_strequal("outfile", cmd->redirections->redirection->marker->word);
|
||||
assert(cmd->redirections->redirection->type == REDIR_APPEND);
|
||||
assert(cmd->redirections->next == NULL);
|
||||
simple_cmd_destroy(cmd);
|
||||
do_leak_check();
|
||||
}
|
||||
|
||||
|
||||
int main(void) {
|
||||
test_redirection_parsing_no_redirections();
|
||||
test_redirection_parsing_output_at_start();
|
||||
test_redirection_parsing_output_at_end();
|
||||
test_redirection_parsing_output_in_middle();
|
||||
test_redirection_parsing_output_no_filename_at_end();
|
||||
test_redirection_parsing_null();
|
||||
test_redirection_parsing_input_in_middle();
|
||||
test_redirection_parsing_here_doc_in_middle();
|
||||
test_redirection_parsing_append_in_middle();
|
||||
return (0);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue