mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
fix(norm): remove tests
This commit is contained in:
parent
313bef63fd
commit
5e8edb9a99
32 changed files with 0 additions and 1837 deletions
|
|
@ -1,60 +0,0 @@
|
||||||
# make gets confused if a file with the same name exists in the sources, so some
|
|
||||||
# file are prefixed with test_
|
|
||||||
rawtests = \
|
|
||||||
test_here_doc \
|
|
||||||
expansion \
|
|
||||||
test_cmdlist_use_after_free \
|
|
||||||
test_wordlist_idx \
|
|
||||||
test_quote_removal \
|
|
||||||
test_metacharacters \
|
|
||||||
test_parse_cmdlists \
|
|
||||||
test_parse_pipelines \
|
|
||||||
test_parse_simple_cmds \
|
|
||||||
test_env_manip \
|
|
||||||
test_word_splitting \
|
|
||||||
|
|
||||||
ifeq ($(CFLAGS),)
|
|
||||||
CFLAGS = -Wall -Wextra -Werror -g
|
|
||||||
endif
|
|
||||||
tests = $(addprefix test_,$(rawtests))
|
|
||||||
run_tests = $(addprefix run_test_,$(rawtests))
|
|
||||||
test_objs = $(addsuffix .o,$(tests))
|
|
||||||
objs := $(addprefix ../,$(objs)) \
|
|
||||||
testutil.o \
|
|
||||||
parse_cmdlist.o \
|
|
||||||
parse_pipeline.o \
|
|
||||||
|
|
||||||
all_objs = $(objs) $(test_objs)
|
|
||||||
deps = $(all_objs:.o=.d)
|
|
||||||
LDLIBS = \
|
|
||||||
-lreadline \
|
|
||||||
-lft
|
|
||||||
LIBFTDIR = ../libft/
|
|
||||||
LIBFT = $(LIBFTDIR)libft.a
|
|
||||||
IFLAGS = -I$(LIBFTDIR)
|
|
||||||
LINCLUDE = -L$(LIBFTDIR)
|
|
||||||
|
|
||||||
.PHONY: run fclean run_test_%
|
|
||||||
|
|
||||||
.NOTPARALLEL: run
|
|
||||||
run: $(run_tests)
|
|
||||||
@echo "Finished running C tests"
|
|
||||||
|
|
||||||
-include $(deps)
|
|
||||||
|
|
||||||
%.o: %.c
|
|
||||||
$(CC) -c $(CFLAGS) $(IFLAGS) -o $*.o $*.c
|
|
||||||
$(CC) -MM $(CFLAGS) $(IFLAGS) -MT $*.o $*.c > $*.d
|
|
||||||
|
|
||||||
test_%: %.o $(objs)
|
|
||||||
$(CC) $(CFLAGS) -rdynamic -o $@ $*.o $(objs) $(LINCLUDE) $(LDLIBS)
|
|
||||||
|
|
||||||
run_test_%: test_%
|
|
||||||
@echo
|
|
||||||
@echo "====== Now running test: $* ======"
|
|
||||||
@echo
|
|
||||||
./test_$*
|
|
||||||
@echo "====== End of test: $* ======"
|
|
||||||
|
|
||||||
fclean:
|
|
||||||
rm -f $(tests)
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
echo hello exp
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
echo hello
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
echo hello
|
|
||||||
|
|
@ -1,265 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* expansion.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2025/04/09 15:50/06 by khais #+# #+# */
|
|
||||||
/* Updated: 2025/04/09 15:50:06 by khais ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include <assert.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include "testutil.h"
|
|
||||||
#include "../src/subst/replace_substr.h"
|
|
||||||
#include "../src/subst/subst.h"
|
|
||||||
#include "../src/env/env.h"
|
|
||||||
#include "../src/env/env_manip.h"
|
|
||||||
#include "../src/parser/wordsplit/wordsplit.h"
|
|
||||||
#include "../src/parser/wordlist/wordlist.h"
|
|
||||||
|
|
||||||
/*
|
|
||||||
** Test file for the different expansion/substitution types of minishell.
|
|
||||||
*/
|
|
||||||
|
|
||||||
static void test_replace_in_str_insert(void)
|
|
||||||
{
|
|
||||||
char *line;
|
|
||||||
|
|
||||||
// insertion is not supported, we must replace at least one char
|
|
||||||
line = replace_in_str("le canari qui fait cuicui", 3, 3, "petit ");
|
|
||||||
assert_strequal("le petit anari qui fait cuicui", line);
|
|
||||||
free(line);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_insert_instr(void)
|
|
||||||
{
|
|
||||||
char *line;
|
|
||||||
|
|
||||||
line = replace_in_str("abcdefghijk", 1, 4, "souris");
|
|
||||||
assert_strequal("asourisfghijk", line);
|
|
||||||
free(line);
|
|
||||||
line = replace_in_str("abcdefgh" , 2, 2, "non ce n'est pas ma faute");
|
|
||||||
assert_strequal("abnon ce n'est pas ma fautedefgh", line);
|
|
||||||
free(line);
|
|
||||||
line = replace_in_str("le petit canari qui fait cuicui", 3, 8, "");
|
|
||||||
assert_strequal("le canari qui fait cuicui", line);
|
|
||||||
free(line);
|
|
||||||
line = replace_in_str("le petit canari qui fait cuicui", 3, 8, NULL);
|
|
||||||
assert_strequal("le canari qui fait cuicui", line);
|
|
||||||
free(line);
|
|
||||||
line = replace_in_str("le canari qui fait cuicui", 2, 2, " petit ");
|
|
||||||
assert_strequal("le petit canari qui fait cuicui", line);
|
|
||||||
free(line);
|
|
||||||
// premier charactere debut du remplacement
|
|
||||||
line = replace_in_str("le petit canari qui fait cuicui", 0, 1, "Le");
|
|
||||||
assert_strequal("Le petit canari qui fait cuicui", line);
|
|
||||||
free(line);
|
|
||||||
do_leak_check();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
** NOTE/REMINDER: I currently replace $0 to $9 with nothing but I can change
|
|
||||||
** this behavior at any point if you would rather we ignored them and therefore
|
|
||||||
** did not replace those.
|
|
||||||
*/
|
|
||||||
static void test_env_variable_expansion(void)
|
|
||||||
{
|
|
||||||
t_wordlist *list;
|
|
||||||
t_minishell *app;
|
|
||||||
char *value;
|
|
||||||
char *key;
|
|
||||||
|
|
||||||
value = ft_strdup("/home/jguelen/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin");
|
|
||||||
key = ft_strdup("PATH");
|
|
||||||
app = ft_calloc(1, sizeof(t_minishell));
|
|
||||||
app->env = env_set_entry(&(app->env), key, value);
|
|
||||||
key = ft_strdup("USER");
|
|
||||||
value = ft_strdup("jguelen");
|
|
||||||
app->env = env_set_entry(&(app->env), key, value);
|
|
||||||
list = minishell_wordsplit("$USER$USER $PATH \"'$USER'$USER\" a$test'b' '$USER'");
|
|
||||||
list = wordlist_var_expansion(list, app);
|
|
||||||
assert_strequal("jguelenjguelen", list->word->word);
|
|
||||||
assert_strequal("/home/jguelen/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin", list->next->word->word);
|
|
||||||
assert_strequal("\"'jguelen'jguelen\"", list->next->next->word->word);
|
|
||||||
assert_strequal("a'b'", list->next->next->next->word->word);
|
|
||||||
assert_strequal("'$USER'", list->next->next->next->next->word->word);
|
|
||||||
wordlist_destroy(list);
|
|
||||||
env_destroy(app->env);
|
|
||||||
free(app);
|
|
||||||
do_leak_check();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_env_var_expansion_with_invalid_ident(void)
|
|
||||||
{
|
|
||||||
t_wordlist *list;
|
|
||||||
t_minishell *app;
|
|
||||||
char *value;
|
|
||||||
char *key;
|
|
||||||
|
|
||||||
value = ft_strdup("value");
|
|
||||||
key = ft_strdup("VAR");
|
|
||||||
app = ft_calloc(1, sizeof(t_minishell));
|
|
||||||
app->env = env_set_entry(&(app->env), key, value);
|
|
||||||
list = minishell_wordsplit("$''VAR");
|
|
||||||
list = wordlist_var_expansion(list, app);
|
|
||||||
assert_strequal("''VAR", list->word->word);
|
|
||||||
wordlist_destroy(list);
|
|
||||||
env_destroy(app->env);
|
|
||||||
free(app);
|
|
||||||
do_leak_check();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_env_var_expansion_with_trailing_dollar(void)
|
|
||||||
{
|
|
||||||
t_wordlist *list;
|
|
||||||
t_minishell *app;
|
|
||||||
char *value;
|
|
||||||
char *key;
|
|
||||||
|
|
||||||
value = ft_strdup("value");
|
|
||||||
key = ft_strdup("VAR");
|
|
||||||
app = ft_calloc(1, sizeof(t_minishell));
|
|
||||||
app->env = env_set_entry(&(app->env), key, value);
|
|
||||||
list = minishell_wordsplit("$VAR$");
|
|
||||||
list = wordlist_var_expansion(list, app);
|
|
||||||
assert_strequal("value$", list->word->word);
|
|
||||||
wordlist_destroy(list);
|
|
||||||
env_destroy(app->env);
|
|
||||||
free(app);
|
|
||||||
do_leak_check();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_cmd_path_expansion(void)
|
|
||||||
{
|
|
||||||
t_minishell *app;
|
|
||||||
char *key;
|
|
||||||
char *value;
|
|
||||||
char *cmdpath;
|
|
||||||
|
|
||||||
key = ft_strdup("PATH");
|
|
||||||
value = ft_strdup("./port:/usr/bin");
|
|
||||||
app = ft_calloc(1, sizeof(t_minishell));
|
|
||||||
app->env = env_set_entry(&(app->env), key, value);
|
|
||||||
cmdpath = get_cmdpath("ls", app);
|
|
||||||
assert_strequal("./port/ls", cmdpath);
|
|
||||||
free(cmdpath);
|
|
||||||
value = ft_strdup(":/usr/bin");
|
|
||||||
key = ft_strdup("PATH");
|
|
||||||
app->env = env_set_entry(&(app->env), key, value);
|
|
||||||
cmdpath = get_cmdpath("ls", app);
|
|
||||||
assert_strequal("./ls", cmdpath);
|
|
||||||
free(cmdpath);
|
|
||||||
cmdpath = get_cmdpath("peekaboo", app);
|
|
||||||
assert(cmdpath == NULL);
|
|
||||||
free(cmdpath);
|
|
||||||
env_destroy(app->env);
|
|
||||||
free(app);
|
|
||||||
do_leak_check();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_filename_star_expansion(void)
|
|
||||||
{
|
|
||||||
t_worddesc *filepattern;
|
|
||||||
t_wordlist *expanded;
|
|
||||||
t_wordlist *tmp;
|
|
||||||
|
|
||||||
//test1 Everything except . and ..
|
|
||||||
ft_printf("test_filename_star_expansion\n");
|
|
||||||
filepattern = create_single_word("*");
|
|
||||||
expanded = expand_star(filepattern);
|
|
||||||
tmp = expanded;
|
|
||||||
assert_strequal("aba", tmp->word->word);
|
|
||||||
tmp = tmp->next;
|
|
||||||
assert(tmp);
|
|
||||||
assert_strequal("abcda", tmp->word->word);
|
|
||||||
tmp = tmp->next;
|
|
||||||
assert(tmp);
|
|
||||||
assert_strequal("aiia", tmp->word->word);
|
|
||||||
tmp = tmp->next;
|
|
||||||
assert(tmp);
|
|
||||||
assert_strequal("axr", tmp->word->word);
|
|
||||||
tmp = tmp->next;
|
|
||||||
assert(tmp);
|
|
||||||
assert_strequal("directory", tmp->word->word);
|
|
||||||
tmp = tmp->next;
|
|
||||||
assert(tmp);
|
|
||||||
assert_strequal("exp", tmp->word->word);
|
|
||||||
tmp = tmp->next;
|
|
||||||
assert(tmp);
|
|
||||||
assert_strequal("ls", tmp->word->word);
|
|
||||||
tmp = tmp->next;
|
|
||||||
assert(tmp);
|
|
||||||
assert_strequal("port", tmp->word->word);
|
|
||||||
tmp = tmp->next;
|
|
||||||
assert(tmp);
|
|
||||||
assert_strequal("yuhbqa", tmp->word->word);
|
|
||||||
tmp = tmp->next;
|
|
||||||
assert(!tmp);
|
|
||||||
wordlist_destroy(expanded);
|
|
||||||
worddesc_destroy(filepattern);
|
|
||||||
//test2
|
|
||||||
filepattern = create_single_word("**a*'b'*a*");
|
|
||||||
expanded = expand_star(filepattern);
|
|
||||||
assert(wordlist_size(expanded) == 2);
|
|
||||||
assert_strequal("aba", expanded->word->word);
|
|
||||||
assert_strequal("abcda", expanded->next->word->word);
|
|
||||||
worddesc_destroy(filepattern);
|
|
||||||
wordlist_destroy(expanded);
|
|
||||||
//test ., .. and .plop
|
|
||||||
filepattern = create_single_word(".*");
|
|
||||||
expanded = expand_star(filepattern);
|
|
||||||
assert(wordlist_size(expanded) == 3);
|
|
||||||
assert_strequal(".", expanded->word->word);
|
|
||||||
assert_strequal("..", expanded->next->word->word);
|
|
||||||
assert_strequal(".plop", expanded->next->next->word->word);
|
|
||||||
worddesc_destroy(filepattern);
|
|
||||||
wordlist_destroy(expanded);
|
|
||||||
//test zero result
|
|
||||||
filepattern = create_single_word("e*x***p*b");
|
|
||||||
expanded = expand_star(filepattern);
|
|
||||||
assert(expanded);
|
|
||||||
assert_strequal(filepattern->word, expanded->word->word);
|
|
||||||
worddesc_destroy(filepattern);
|
|
||||||
wordlist_destroy(expanded);
|
|
||||||
do_leak_check();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void simple_sub_test(void)
|
|
||||||
{
|
|
||||||
t_wordlist *list;
|
|
||||||
t_minishell *app;
|
|
||||||
char *value;
|
|
||||||
char *key;
|
|
||||||
|
|
||||||
value = ft_strdup("val");
|
|
||||||
key = ft_strdup("KEY");
|
|
||||||
app = ft_calloc(1, sizeof(t_minishell));
|
|
||||||
app->env = env_set_entry(&(app->env), key, value);
|
|
||||||
list = minishell_wordsplit("v$KEY");
|
|
||||||
list = wordlist_var_expansion(list, app);
|
|
||||||
assert_strequal("vval", list->word->word);
|
|
||||||
wordlist_destroy(list);
|
|
||||||
env_destroy(app->env);
|
|
||||||
free(app);
|
|
||||||
do_leak_check();
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
test_env_var_expansion_with_trailing_dollar();
|
|
||||||
test_env_var_expansion_with_invalid_ident();
|
|
||||||
test_replace_in_str_insert();
|
|
||||||
if (chdir("./expand_test") == -1)
|
|
||||||
assert("chdir failure" && false);
|
|
||||||
simple_sub_test();
|
|
||||||
test_insert_instr();
|
|
||||||
test_env_variable_expansion();
|
|
||||||
test_cmd_path_expansion();
|
|
||||||
test_filename_star_expansion();
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
input
|
|
||||||
|
|
@ -1,2 +0,0 @@
|
||||||
input
|
|
||||||
EOF
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
EOF
|
|
||||||
|
|
@ -1,3 +0,0 @@
|
||||||
hello
|
|
||||||
$USER
|
|
||||||
end
|
|
||||||
|
|
@ -1,26 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* parse_cmdlist.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2025/03/11 15:54:43 by khais #+# #+# */
|
|
||||||
/* Updated: 2025/03/18 15:05:39 by khais ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "../src/parser/cmdlist/cmdlist.h"
|
|
||||||
#include "../src/parser/wordsplit/wordsplit.h"
|
|
||||||
#include "libft.h"
|
|
||||||
#include "unistd.h"
|
|
||||||
#include <assert.h>
|
|
||||||
|
|
||||||
t_cmdlist *parse_cmdlist(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);
|
|
||||||
}
|
|
||||||
|
|
@ -1,20 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* parse_cmdlist.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2025/03/11 15:55:10 by khais #+# #+# */
|
|
||||||
/* Updated: 2025/03/18 15:05:25 by khais ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef PARSE_CMDLIST_H
|
|
||||||
# define PARSE_CMDLIST_H
|
|
||||||
|
|
||||||
# include "../src/parser/cmdlist/cmdlist.h"
|
|
||||||
|
|
||||||
t_cmdlist *parse_cmdlist(char *input);
|
|
||||||
|
|
||||||
#endif // PARSE_CMDLIST_H
|
|
||||||
|
|
@ -1,23 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* parse_pipeline.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2025/03/11 16:01:48 by khais #+# #+# */
|
|
||||||
/* Updated: 2025/03/11 16:24:41 by khais ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "../src/parser/wordsplit/wordsplit.h"
|
|
||||||
#include "../src/parser/pipeline/pipeline.h"
|
|
||||||
#include <assert.h>
|
|
||||||
|
|
||||||
t_pipeline *parse_pipeline(char *input)
|
|
||||||
{
|
|
||||||
t_wordlist *words = minishell_wordsplit(input);
|
|
||||||
t_pipeline *pipeline = pipeline_from_wordlist(words);
|
|
||||||
wordlist_destroy(words);
|
|
||||||
return (pipeline);
|
|
||||||
}
|
|
||||||
|
|
@ -1,20 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* parse_pipeline.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2025/03/11 16:01:00 by khais #+# #+# */
|
|
||||||
/* Updated: 2025/03/11 16:02:15 by khais ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef PARSE_PIPELINE_H
|
|
||||||
# define PARSE_PIPELINE_H
|
|
||||||
|
|
||||||
# include "../src/parser/pipeline/pipeline.h"
|
|
||||||
|
|
||||||
t_pipeline *parse_pipeline(char *input);
|
|
||||||
|
|
||||||
#endif // PARSE_PIPELINE_H
|
|
||||||
|
|
@ -1,28 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* test_cmdlist_use_after_free.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2025/03/03 11:40:37 by khais #+# #+# */
|
|
||||||
/* Updated: 2025/03/18 15:05:18 by khais ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "../src/parser/wordsplit/wordsplit.h"
|
|
||||||
#include "../src/parser/cmdlist/cmdlist.h"
|
|
||||||
#include "libft.h"
|
|
||||||
#include <assert.h>
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
t_wordlist *words;
|
|
||||||
t_cmdlist *cmd;
|
|
||||||
cmd = NULL;
|
|
||||||
words = minishell_wordsplit("|");
|
|
||||||
cmd = cmdlist_from_wordlist(words);
|
|
||||||
wordlist_destroy(words);
|
|
||||||
assert(cmd == NULL);
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
@ -1,155 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* test_env_manip.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2025/02/18 15:11:14 by khais #+# #+# */
|
|
||||||
/* Updated: 2025/02/19 16:41:53 by khais ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "../src/env/env.h"
|
|
||||||
#include "../src/env/env_convert.h"
|
|
||||||
#include "../src/env/env_manip.h"
|
|
||||||
#include "../src/env/envp.h"
|
|
||||||
#include "../src/ft_errno.h"
|
|
||||||
#include "libft.h"
|
|
||||||
#include "unistd.h"
|
|
||||||
#include <assert.h>
|
|
||||||
|
|
||||||
static void test_envp_parsing(char *line, char *expected_key, char *expected_value)
|
|
||||||
{
|
|
||||||
char *got_key = envp_get_key(line);
|
|
||||||
char *got_value = envp_get_val(line);
|
|
||||||
ft_dprintf(STDERR_FILENO, "for envp value '%s', expecting key to eq '%s', and got '%s'\n", line, expected_key, got_key);
|
|
||||||
assert(expected_key == got_key || ft_strcmp(expected_key, got_key) == 0);
|
|
||||||
ft_dprintf(STDERR_FILENO, "for envp value '%s', expecting value to eq '%s', and got '%s'\n", line, expected_value, got_value);
|
|
||||||
assert(expected_value == got_value || ft_strcmp(expected_value, got_value) == 0);
|
|
||||||
free(got_key);
|
|
||||||
free(got_value);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void assert_env_value(t_env *env, char *key, char *value)
|
|
||||||
{
|
|
||||||
char *got_value = env_get_val(env, key);
|
|
||||||
ft_dprintf(STDERR_FILENO, "expecting %s=%s to exist, and got %s=%s\n", key, value, key, got_value);
|
|
||||||
assert(ft_strcmp(value, got_value) == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_env_set_entry_empty_env(void)
|
|
||||||
{
|
|
||||||
t_env *env;
|
|
||||||
|
|
||||||
env = NULL;
|
|
||||||
assert(env_set_entry(&env, ft_strdup("VAR"), ft_strdup("hello")) != NULL);
|
|
||||||
assert_env_value(env, "VAR", "hello");
|
|
||||||
assert(1 == env_get_size(env));
|
|
||||||
env_destroy(env);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_env_set_entry_existing_value(void)
|
|
||||||
{
|
|
||||||
t_env *env;
|
|
||||||
|
|
||||||
env = NULL;
|
|
||||||
assert(env_set_entry(&env, ft_strdup("VAR"), ft_strdup("hello")) != NULL);
|
|
||||||
assert(env_set_entry(&env, ft_strdup("VAR"), ft_strdup("there")) != NULL);
|
|
||||||
assert_env_value(env, "VAR", "there");
|
|
||||||
assert(1 == env_get_size(env));
|
|
||||||
env_destroy(env);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_env_set_entry_multiple(void)
|
|
||||||
{
|
|
||||||
t_env *env;
|
|
||||||
|
|
||||||
env = NULL;
|
|
||||||
assert(env_set_entry(&env, ft_strdup("VAR"), ft_strdup("hello")) != NULL);
|
|
||||||
assert(env_set_entry(&env, ft_strdup("SHELL"), ft_strdup("/bin/bash")) != NULL);
|
|
||||||
assert(env_set_entry(&env, ft_strdup("TERM"), ft_strdup("xterm-kitty")) != NULL);
|
|
||||||
assert_env_value(env, "VAR", "hello");
|
|
||||||
assert_env_value(env, "SHELL", "/bin/bash");
|
|
||||||
assert_env_value(env, "TERM", "xterm-kitty");
|
|
||||||
assert(3 == env_get_size(env));
|
|
||||||
env_destroy(env);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_env_set_entry_nullargs(void)
|
|
||||||
{
|
|
||||||
t_env *env;
|
|
||||||
|
|
||||||
env = NULL;
|
|
||||||
assert(env_set_entry(&env, ft_strdup("VAR"), ft_strdup("hello")) != NULL);
|
|
||||||
ft_errno(FT_ESUCCESS);
|
|
||||||
assert(env_set_entry(&env, NULL, ft_strdup("hello")) == NULL);
|
|
||||||
assert(ft_errno_get() == FT_EINVAL);
|
|
||||||
assert(env_set_entry(&env, ft_strdup("VAR"), NULL) == NULL);
|
|
||||||
ft_errno(FT_ESUCCESS);
|
|
||||||
assert(env_set_entry(&env, ft_strdup(""), ft_strdup("value")) == NULL);
|
|
||||||
assert(ft_errno_get() == FT_EBADID);
|
|
||||||
assert_env_value(env, "VAR", "hello");
|
|
||||||
assert(1 == env_get_size(env));
|
|
||||||
env_destroy(env);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_env_from_envp(void)
|
|
||||||
{
|
|
||||||
|
|
||||||
t_env *env;
|
|
||||||
char *envp[] = {"VAR=hello", "SHELL=/bin/bash", "TERM=xterm-kitty", NULL};
|
|
||||||
|
|
||||||
env = env_from_envp(envp);
|
|
||||||
assert(env != NULL);
|
|
||||||
assert_env_value(env, "VAR", "hello");
|
|
||||||
assert_env_value(env, "SHELL", "/bin/bash");
|
|
||||||
assert_env_value(env, "TERM", "xterm-kitty");
|
|
||||||
assert(3 == env_get_size(env));
|
|
||||||
env_destroy(env);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_env_from_envp_invalid(void)
|
|
||||||
{
|
|
||||||
|
|
||||||
t_env *env;
|
|
||||||
char *envp[] = {"VAR=hello", "", "TERM=xterm-kitty", NULL};
|
|
||||||
|
|
||||||
ft_dprintf(STDERR_FILENO, "test_env_from_envp\n");
|
|
||||||
env = env_from_envp(envp);
|
|
||||||
assert(env == NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_envp_from_env(void)
|
|
||||||
{
|
|
||||||
t_env *env;
|
|
||||||
char *envp[] = {"VAR=hello", "SHELL=/bin/bash", "TERM=xterm-kitty", NULL};
|
|
||||||
|
|
||||||
env = env_from_envp(envp);
|
|
||||||
char **got_envp = envp_from_env(env);
|
|
||||||
t_env *got_env = env_from_envp(got_envp);
|
|
||||||
assert(got_env != NULL);
|
|
||||||
assert_env_value(got_env, "VAR", "hello");
|
|
||||||
assert_env_value(got_env, "SHELL", "/bin/bash");
|
|
||||||
assert_env_value(got_env, "TERM", "xterm-kitty");
|
|
||||||
assert(3 == env_get_size(got_env));
|
|
||||||
env_destroy(env);
|
|
||||||
env_destroy(got_env);
|
|
||||||
envp_destroy(got_envp);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void) {
|
|
||||||
test_envp_parsing("SHELL=/bin/fish", "SHELL", "/bin/fish");
|
|
||||||
test_envp_parsing("=/bin/fish", "", "/bin/fish");
|
|
||||||
test_envp_parsing(NULL, NULL, NULL);
|
|
||||||
test_envp_parsing("", NULL, NULL);
|
|
||||||
test_envp_parsing("VARNAME", "VARNAME", "");
|
|
||||||
test_env_set_entry_empty_env();
|
|
||||||
test_env_set_entry_existing_value();
|
|
||||||
test_env_set_entry_multiple();
|
|
||||||
test_env_set_entry_nullargs();
|
|
||||||
test_env_from_envp();
|
|
||||||
test_env_from_envp_invalid();
|
|
||||||
test_envp_from_env();
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
@ -1,169 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* test_here_doc.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2025/03/07 11:43:32 by khais #+# #+# */
|
|
||||||
/* Updated: 2025/04/08 16:34:50 by khais ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include <assert.h>
|
|
||||||
#include "testutil.h"
|
|
||||||
#include "libft.h"
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include "../src/executing/here_doc/here_doc.h"
|
|
||||||
#include "../src/env/env_manip.h"
|
|
||||||
#include "../src/ft_errno.h"
|
|
||||||
|
|
||||||
static void test_here_doc_filename_generation(void)
|
|
||||||
{
|
|
||||||
char *filename1;
|
|
||||||
char *filename2;
|
|
||||||
|
|
||||||
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
|
||||||
ft_errno(FT_ESUCCESS);
|
|
||||||
filename1 = here_doc_random_filename();
|
|
||||||
filename2 = here_doc_random_filename();
|
|
||||||
assert(filename1 != NULL);
|
|
||||||
assert(filename2 != NULL);
|
|
||||||
ft_dprintf(STDERR_FILENO, "Got filename: [%s]\n", filename1);
|
|
||||||
ft_dprintf(STDERR_FILENO, "Got filename: [%s]\n", filename2);
|
|
||||||
assert(ft_strcmp(filename1, filename2) != 0);
|
|
||||||
free(filename1);
|
|
||||||
free(filename2);
|
|
||||||
do_leak_check();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_here_doc_invalid_args(void)
|
|
||||||
{
|
|
||||||
t_worddesc *marker;
|
|
||||||
|
|
||||||
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
|
||||||
ft_errno(FT_ESUCCESS);
|
|
||||||
assert(-1 == here_doc(NULL, 0, NULL));
|
|
||||||
marker = worddesc_create(NULL, 0, NULL, WORD_TOKEN);
|
|
||||||
assert(-1 == here_doc(marker, 0, NULL));
|
|
||||||
worddesc_destroy(marker);
|
|
||||||
marker = worddesc_create(ft_strdup("EOF"), 0, NULL, WORD_TOKEN);
|
|
||||||
assert(-1 == here_doc(marker, -1, NULL));
|
|
||||||
worddesc_destroy(marker);
|
|
||||||
do_leak_check();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_here_doc_only_end_marker(void)
|
|
||||||
{
|
|
||||||
t_worddesc *marker;
|
|
||||||
int infile;
|
|
||||||
int result;
|
|
||||||
|
|
||||||
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
|
||||||
ft_errno(FT_ESUCCESS);
|
|
||||||
marker = worddesc_create(ft_strdup("EOF"), 0, NULL, WORD_TOKEN);
|
|
||||||
infile = open("./here_doc_only_eof.input", O_RDONLY);
|
|
||||||
result = here_doc(marker, infile, NULL);
|
|
||||||
close(infile);
|
|
||||||
assert(result != -1);
|
|
||||||
assert(NULL == get_next_line(result));
|
|
||||||
close(result);
|
|
||||||
worddesc_destroy(marker);
|
|
||||||
do_leak_check();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_here_doc_input_plus_end_marker(void)
|
|
||||||
{
|
|
||||||
t_worddesc *marker;
|
|
||||||
int infile;
|
|
||||||
int result;
|
|
||||||
|
|
||||||
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
|
||||||
ft_errno(FT_ESUCCESS);
|
|
||||||
marker = worddesc_create(ft_strdup("EOF"), 0, NULL, WORD_TOKEN);
|
|
||||||
infile = open("./here_doc_input_plus_eof.input", O_RDONLY);
|
|
||||||
result = here_doc(marker, infile, NULL);
|
|
||||||
close(infile);
|
|
||||||
worddesc_destroy(marker);
|
|
||||||
assert(result != -1);
|
|
||||||
assert_strequal("input\n", get_next_line(result));
|
|
||||||
close(result);
|
|
||||||
do_leak_check();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_here_doc_input_no_end_marker(void)
|
|
||||||
{
|
|
||||||
t_worddesc *marker;
|
|
||||||
int infile;
|
|
||||||
int result;
|
|
||||||
|
|
||||||
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
|
||||||
marker = worddesc_create(ft_strdup("EOF"), 0, NULL, WORD_TOKEN);
|
|
||||||
infile = open("./here_doc_input_no_eof.input", O_RDONLY);
|
|
||||||
result = here_doc(marker, infile, NULL);
|
|
||||||
close(infile);
|
|
||||||
worddesc_destroy(marker);
|
|
||||||
assert(result != -1);
|
|
||||||
assert_strequal("input\n", get_next_line(result));
|
|
||||||
close(result);
|
|
||||||
do_leak_check();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_here_doc_with_expansion(void)
|
|
||||||
{
|
|
||||||
t_worddesc *marker;
|
|
||||||
int infile;
|
|
||||||
int result;
|
|
||||||
t_minishell app;
|
|
||||||
|
|
||||||
ft_bzero(&app, sizeof(t_minishell));
|
|
||||||
app.env = env_set_entry(&app.env, ft_strdup("USER"), ft_strdup("kcolin"));
|
|
||||||
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
|
||||||
ft_errno(FT_ESUCCESS);
|
|
||||||
marker = worddesc_create(ft_strdup("EOF"), 0, NULL, WORD_TOKEN);
|
|
||||||
infile = open("./here_doc_with_expansion.input", O_RDONLY);
|
|
||||||
result = here_doc(marker, infile, &app);
|
|
||||||
close(infile);
|
|
||||||
worddesc_destroy(marker);
|
|
||||||
assert(result != -1);
|
|
||||||
assert_strequal("hello\n", get_next_line(result));
|
|
||||||
assert_strequal("kcolin\n", get_next_line(result));
|
|
||||||
assert_strequal("end\n", get_next_line(result));
|
|
||||||
close(result);
|
|
||||||
do_leak_check();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_here_doc_with_no_expansion(void)
|
|
||||||
{
|
|
||||||
t_worddesc *marker;
|
|
||||||
int infile;
|
|
||||||
int result;
|
|
||||||
t_minishell app;
|
|
||||||
|
|
||||||
ft_bzero(&app, sizeof(t_minishell));
|
|
||||||
app.env = env_set_entry(&app.env, ft_strdup("USER"), ft_strdup("kcolin"));
|
|
||||||
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
|
||||||
marker = worddesc_create(ft_strdup("EOF"), W_QUOTED, NULL, WORD_TOKEN);
|
|
||||||
infile = open("./here_doc_with_expansion.input", O_RDONLY);
|
|
||||||
result = here_doc(marker, infile, &app);
|
|
||||||
close(infile);
|
|
||||||
worddesc_destroy(marker);
|
|
||||||
assert(result != -1);
|
|
||||||
assert_strequal("hello\n", get_next_line(result));
|
|
||||||
assert_strequal("$USER\n", get_next_line(result));
|
|
||||||
assert_strequal("end\n", get_next_line(result));
|
|
||||||
close(result);
|
|
||||||
do_leak_check();
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void) {
|
|
||||||
test_here_doc_filename_generation();
|
|
||||||
test_here_doc_invalid_args();
|
|
||||||
test_here_doc_only_end_marker();
|
|
||||||
test_here_doc_input_plus_end_marker();
|
|
||||||
test_here_doc_input_no_end_marker();
|
|
||||||
test_here_doc_with_expansion();
|
|
||||||
test_here_doc_with_no_expansion();
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
@ -1,67 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* metacharacters.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2025/02/06 15:21:00 by kcolin #+# #+# */
|
|
||||||
/* Updated: 2025/02/11 18:32:01 by khais ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "../src/parser/matchers/metacharacter.h"
|
|
||||||
#include <assert.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
void test_metacharacters(void)
|
|
||||||
{
|
|
||||||
dup2(STDERR_FILENO, STDIN_FILENO);
|
|
||||||
char c = 'a';
|
|
||||||
printf("not metachar:");
|
|
||||||
while (c != 'z')
|
|
||||||
{
|
|
||||||
printf("%c", c);
|
|
||||||
assert(!is_metacharacter(c));
|
|
||||||
c++;
|
|
||||||
}
|
|
||||||
c = 'A';
|
|
||||||
while (c != 'Z')
|
|
||||||
{
|
|
||||||
printf("%c", c);
|
|
||||||
assert(!is_metacharacter(c));
|
|
||||||
c++;
|
|
||||||
}
|
|
||||||
c = '0';
|
|
||||||
while (c != '9')
|
|
||||||
{
|
|
||||||
printf("%c", c);
|
|
||||||
assert(!is_metacharacter(c));
|
|
||||||
c++;
|
|
||||||
}
|
|
||||||
char *not_metachars = ";[]{}*+=_-";
|
|
||||||
int i = 0;
|
|
||||||
while (not_metachars[i] != '\0')
|
|
||||||
{
|
|
||||||
dprintf(STDERR_FILENO, "%c", not_metachars[i]);
|
|
||||||
assert(!is_metacharacter(not_metachars[i]));
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
char *metachars = " \t\n|&()<>";
|
|
||||||
i = 0;
|
|
||||||
printf("\nmetachar:");
|
|
||||||
while (metachars[i] != '\0')
|
|
||||||
{
|
|
||||||
dprintf(STDERR_FILENO, "%c (%d) ", metachars[i], metachars[i]);
|
|
||||||
assert(is_metacharacter(metachars[i]));
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
test_metacharacters();
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
@ -1,149 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* 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);
|
|
||||||
}
|
|
||||||
|
|
@ -1,117 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* test_parse_pipelines.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2025/02/21 13:13:58 by khais #+# #+# */
|
|
||||||
/* Updated: 2025/03/11 16:33:11 by khais ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "../src/parser/pipeline/pipeline.h"
|
|
||||||
#include "../src/ft_errno.h"
|
|
||||||
#include <assert.h>
|
|
||||||
#include "testutil.h"
|
|
||||||
#include <limits.h>
|
|
||||||
#include "parse_pipeline.h"
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
static void test_parse_empty_pipeline(void)
|
|
||||||
{
|
|
||||||
t_pipeline *pipeline = parse_pipeline("");
|
|
||||||
assert(pipeline == NULL);
|
|
||||||
pipeline_destroy(pipeline);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_parse_pipeline_single_cmd(void)
|
|
||||||
{
|
|
||||||
t_pipeline *pipeline = parse_pipeline("echo hello world");
|
|
||||||
assert(pipeline->num_cmd == 1);
|
|
||||||
assert_pipeline_cmd_word(pipeline, "echo", 0, 0);
|
|
||||||
assert_pipeline_cmd_word(pipeline, "hello", 0, 1);
|
|
||||||
assert_pipeline_cmd_word(pipeline, "world", 0, 2);
|
|
||||||
pipeline_destroy(pipeline);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_parse_pipeline_two_cmd(void)
|
|
||||||
{
|
|
||||||
t_pipeline *pipeline = parse_pipeline("echo hello world | tee output.txt");
|
|
||||||
assert_pipeline_cmd_word(pipeline, "echo", 0, 0);
|
|
||||||
assert_pipeline_cmd_word(pipeline, "hello", 0, 1);
|
|
||||||
assert_pipeline_cmd_word(pipeline, "world", 0, 2);
|
|
||||||
assert_pipeline_cmd_word(pipeline, "tee", 1, 0);
|
|
||||||
assert_pipeline_cmd_word(pipeline, "output.txt", 1, 1);
|
|
||||||
pipeline_destroy(pipeline);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_parse_pipeline_three_cmd(void)
|
|
||||||
{
|
|
||||||
t_pipeline *pipeline = parse_pipeline("echo hello world | tee output.txt | cat -e");
|
|
||||||
assert_pipeline_cmd_word(pipeline, "echo", 0, 0);
|
|
||||||
assert_pipeline_cmd_word(pipeline, "hello", 0, 1);
|
|
||||||
assert_pipeline_cmd_word(pipeline, "world", 0, 2);
|
|
||||||
assert_pipeline_cmd_word(pipeline, "tee", 1, 0);
|
|
||||||
assert_pipeline_cmd_word(pipeline, "output.txt", 1, 1);
|
|
||||||
assert_pipeline_cmd_word(pipeline, "cat", 2, 0);
|
|
||||||
assert_pipeline_cmd_word(pipeline, "-e", 2, 1);
|
|
||||||
pipeline_destroy(pipeline);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_parse_pipeline_four_cmd(void)
|
|
||||||
{
|
|
||||||
t_pipeline *pipeline = parse_pipeline("echo hello world | tee output.txt | cat -e | hexdump -C");
|
|
||||||
assert_pipeline_cmd_word(pipeline, "echo", 0, 0);
|
|
||||||
assert_pipeline_cmd_word(pipeline, "hello", 0, 1);
|
|
||||||
assert_pipeline_cmd_word(pipeline, "world", 0, 2);
|
|
||||||
assert_pipeline_cmd_word(pipeline, "tee", 1, 0);
|
|
||||||
assert_pipeline_cmd_word(pipeline, "output.txt", 1, 1);
|
|
||||||
assert_pipeline_cmd_word(pipeline, "cat", 2, 0);
|
|
||||||
assert_pipeline_cmd_word(pipeline, "-e", 2, 1);
|
|
||||||
assert_pipeline_cmd_word(pipeline, "hexdump", 3, 0);
|
|
||||||
assert_pipeline_cmd_word(pipeline, "-C", 3, 1);
|
|
||||||
pipeline_destroy(pipeline);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_parse_pipeline_double_pipe_rejected(void)
|
|
||||||
{
|
|
||||||
ft_errno(FT_ESUCCESS);
|
|
||||||
assert(parse_pipeline("echo hello | | tee output.txt") == NULL);
|
|
||||||
assert(ft_errno_get() == FT_EUNEXPECTED_PIPE);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_parse_pipeline_triple_pipe_rejected(void)
|
|
||||||
{
|
|
||||||
ft_errno(FT_ESUCCESS);
|
|
||||||
assert(parse_pipeline("echo hello | | | tee output.txt") == NULL);
|
|
||||||
assert(ft_errno_get() == FT_EUNEXPECTED_PIPE);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_parse_pipeline_pipe_at_start_rejected(void)
|
|
||||||
{
|
|
||||||
ft_errno(FT_ESUCCESS);
|
|
||||||
assert(parse_pipeline("| echo hello | tee output.txt") == NULL);
|
|
||||||
assert(ft_errno_get() == FT_EUNEXPECTED_PIPE);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_parse_pipeline_pipe_at_end_rejected(void)
|
|
||||||
{
|
|
||||||
ft_errno(FT_ESUCCESS);
|
|
||||||
assert(parse_pipeline("echo hello | tee output.txt |") == NULL);
|
|
||||||
assert(ft_errno_get() == FT_EUNEXPECTED_PIPE);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
test_parse_empty_pipeline();
|
|
||||||
test_parse_pipeline_single_cmd();
|
|
||||||
test_parse_pipeline_two_cmd();
|
|
||||||
test_parse_pipeline_three_cmd();
|
|
||||||
test_parse_pipeline_four_cmd();
|
|
||||||
test_parse_pipeline_double_pipe_rejected();
|
|
||||||
test_parse_pipeline_triple_pipe_rejected();
|
|
||||||
test_parse_pipeline_pipe_at_start_rejected();
|
|
||||||
test_parse_pipeline_pipe_at_end_rejected();
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
@ -1,48 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* parse_simple_cmds.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2025/02/21 12:20:20 by khais #+# #+# */
|
|
||||||
/* Updated: 2025/03/07 12:32:04 by khais ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include <assert.h>
|
|
||||||
#include "../src/parser/simple_cmd/simple_cmd.h"
|
|
||||||
#include "../src/parser/wordsplit/wordsplit.h"
|
|
||||||
#include "../src/parser/wordlist/wordlist.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_parse_empty_command(void)
|
|
||||||
{
|
|
||||||
t_simple_cmd *cmd = parse_simple_cmd("");
|
|
||||||
assert(cmd != NULL);
|
|
||||||
simple_cmd_destroy(cmd);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_parse_nonempty_command(void)
|
|
||||||
{
|
|
||||||
t_simple_cmd *cmd = parse_simple_cmd("echo Hello World!");
|
|
||||||
assert(cmd != NULL);
|
|
||||||
assert_strequal("echo", wordlist_get(cmd->words, 0)->word);
|
|
||||||
assert_strequal("Hello", wordlist_get(cmd->words, 1)->word);
|
|
||||||
assert_strequal("World!", wordlist_get(cmd->words, 2)->word);
|
|
||||||
simple_cmd_destroy(cmd);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
test_parse_empty_command();
|
|
||||||
test_parse_nonempty_command();
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
@ -1,106 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* test_quote_removal.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2025/02/28 13:46:56 by khais #+# #+# */
|
|
||||||
/* Updated: 2025/03/11 16:31:50 by khais ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "../src/parser/remove_quotes/remove_quotes.h"
|
|
||||||
#include "testutil.h"
|
|
||||||
#include <assert.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
static void test_quote_removal_no_quotes_single_word(void)
|
|
||||||
{
|
|
||||||
t_worddesc *word = create_single_word("word");
|
|
||||||
t_worddesc *got_word = remove_quotes(word);
|
|
||||||
assert_strequal("word", got_word->word);
|
|
||||||
assert(got_word->marker == NULL);
|
|
||||||
worddesc_destroy(word);
|
|
||||||
worddesc_destroy(got_word);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_quote_removal_null(void)
|
|
||||||
{
|
|
||||||
t_worddesc *word = NULL;
|
|
||||||
t_worddesc *got_word = remove_quotes(word);
|
|
||||||
assert(got_word == NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_quote_removal_single_quotes(void)
|
|
||||||
{
|
|
||||||
t_worddesc *word = create_single_word("'word'");
|
|
||||||
t_worddesc *got_word = remove_quotes(word);
|
|
||||||
assert_strequal("word", got_word->word);
|
|
||||||
assert(got_word->marker == NULL);
|
|
||||||
worddesc_destroy(word);
|
|
||||||
worddesc_destroy(got_word);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_quote_removal_double_quotes(void)
|
|
||||||
{
|
|
||||||
t_worddesc *word = create_single_word("\"word\"");
|
|
||||||
t_worddesc *got_word = remove_quotes(word);
|
|
||||||
assert_strequal("word", got_word->word);
|
|
||||||
assert(got_word->marker == NULL);
|
|
||||||
worddesc_destroy(word);
|
|
||||||
worddesc_destroy(got_word);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_quote_removal_mixed_single_in_double(void)
|
|
||||||
{
|
|
||||||
t_worddesc *word = create_single_word("\"'word'\"");
|
|
||||||
t_worddesc *got_word = remove_quotes(word);
|
|
||||||
assert_strequal("'word'", got_word->word);
|
|
||||||
assert(got_word->marker == NULL);
|
|
||||||
worddesc_destroy(word);
|
|
||||||
worddesc_destroy(got_word);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_quote_removal_mixed_double_in_single(void)
|
|
||||||
{
|
|
||||||
t_worddesc *word = create_single_word("'\"word\"'");
|
|
||||||
t_worddesc *got_word = remove_quotes(word);
|
|
||||||
assert_strequal("\"word\"", got_word->word);
|
|
||||||
assert(got_word->marker == NULL);
|
|
||||||
worddesc_destroy(word);
|
|
||||||
worddesc_destroy(got_word);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_quote_removal_middle_of_word(void)
|
|
||||||
{
|
|
||||||
t_worddesc *word = create_single_word("var='VALUE'here");
|
|
||||||
t_worddesc *got_word = remove_quotes(word);
|
|
||||||
assert_strequal("var=VALUEhere", got_word->word);
|
|
||||||
assert(got_word->marker == NULL);
|
|
||||||
worddesc_destroy(word);
|
|
||||||
worddesc_destroy(got_word);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_quote_removal_nested_middle_of_word(void)
|
|
||||||
{
|
|
||||||
t_worddesc *word = create_single_word("var=\"'VALUE'here\"");
|
|
||||||
t_worddesc *got_word = remove_quotes(word);
|
|
||||||
assert_strequal("var='VALUE'here", got_word->word);
|
|
||||||
assert(got_word->marker == NULL);
|
|
||||||
worddesc_destroy(word);
|
|
||||||
worddesc_destroy(got_word);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int main(void) {
|
|
||||||
test_quote_removal_no_quotes_single_word();
|
|
||||||
test_quote_removal_null();
|
|
||||||
test_quote_removal_single_quotes();
|
|
||||||
test_quote_removal_double_quotes();
|
|
||||||
test_quote_removal_mixed_single_in_double();
|
|
||||||
test_quote_removal_mixed_double_in_single();
|
|
||||||
test_quote_removal_middle_of_word();
|
|
||||||
test_quote_removal_nested_middle_of_word();
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
@ -1,338 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* word_splitting.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2025/02/13 15:17:56 by khais #+# #+# */
|
|
||||||
/* Updated: 2025/03/06 16:50:56 by khais ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include <assert.h>
|
|
||||||
#include "testutil.h"
|
|
||||||
#include "../src/parser/wordsplit/wordsplit.h"
|
|
||||||
#include <unistd.h>
|
|
||||||
#include "libft.h"
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
/*
|
|
||||||
** https://bash-hackers.gabe565.com/syntax/words/
|
|
||||||
*/
|
|
||||||
static void test_wordsplit_singleword(void)
|
|
||||||
{
|
|
||||||
t_wordlist *words;
|
|
||||||
|
|
||||||
words = minishell_wordsplit("echo");
|
|
||||||
assert_strequal("echo", wordlist_get(words, 0)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 0)->marker);
|
|
||||||
assert(NULL == wordlist_get(words, 1));
|
|
||||||
wordlist_destroy(words);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_wordsplit_singleword_with_blanks(void)
|
|
||||||
{
|
|
||||||
t_wordlist *words;
|
|
||||||
|
|
||||||
words = minishell_wordsplit("\t \t echo \t\t ");
|
|
||||||
assert_strequal("echo", wordlist_get(words, 0)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 0)->marker);
|
|
||||||
assert(NULL == wordlist_get(words, 1));
|
|
||||||
wordlist_destroy(words);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_wordsplit_multiword(void)
|
|
||||||
{
|
|
||||||
t_wordlist *words;
|
|
||||||
|
|
||||||
words = minishell_wordsplit("\t echo\tThe file is named $MYFILE \t");
|
|
||||||
assert_strequal("echo", wordlist_get(words, 0)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 0)->marker);
|
|
||||||
assert_strequal("The", wordlist_get(words, 1)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 1)->marker);
|
|
||||||
assert_strequal("file", wordlist_get(words, 2)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 2)->marker);
|
|
||||||
assert_strequal("is", wordlist_get(words, 3)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 3)->marker);
|
|
||||||
assert_strequal("named", wordlist_get(words, 4)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 4)->marker);
|
|
||||||
assert_strequal("$MYFILE", wordlist_get(words, 5)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 5)->marker);
|
|
||||||
assert(NULL == wordlist_get(words, 6));
|
|
||||||
wordlist_destroy(words);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_wordsplit_multiword_with_single_quotes(void)
|
|
||||||
{
|
|
||||||
t_wordlist *words;
|
|
||||||
|
|
||||||
words = minishell_wordsplit("\t echo\t' \t The file is named $MYFILE ' \t");
|
|
||||||
assert_strequal("echo", wordlist_get(words, 0)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 0)->marker);
|
|
||||||
assert_strequal("' \t The file is named $MYFILE '", wordlist_get(words, 1)->word);
|
|
||||||
// This marker is one char shorter because the tab character above is
|
|
||||||
// represented here with two chars
|
|
||||||
ft_dprintf(STDERR_FILENO, "the marker below is shorter than the string above because the string above contains a tab, it is normal\n");
|
|
||||||
assert_strequal(" '''''''''''''''''''''''''''''''''' ", wordlist_get(words, 1)->marker);
|
|
||||||
assert(NULL == wordlist_get(words, 2));
|
|
||||||
wordlist_destroy(words);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_wordsplit_multiword_with_double_quotes(void)
|
|
||||||
{
|
|
||||||
t_wordlist *words;
|
|
||||||
|
|
||||||
words = minishell_wordsplit("\t echo\t\" \t The file is named $MYFILE \" \t");
|
|
||||||
assert_strequal("echo", wordlist_get(words, 0)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 0)->marker);
|
|
||||||
assert_strequal("\" \t The file is named $MYFILE \"", wordlist_get(words, 1)->word);
|
|
||||||
// This marker is one char shorter because the tab character above is
|
|
||||||
// represented here with two chars
|
|
||||||
ft_dprintf(STDERR_FILENO, "the marker below is shorter than the string above because the string above contains a tab, it is normal\n");
|
|
||||||
assert_strequal(" \"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\" ", wordlist_get(words, 1)->marker);
|
|
||||||
assert(NULL == wordlist_get(words, 2));
|
|
||||||
wordlist_destroy(words);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_wordsplit_mixed_single_in_double(void)
|
|
||||||
{
|
|
||||||
t_wordlist *words;
|
|
||||||
|
|
||||||
words = minishell_wordsplit("hello \"mixed ' \tquotes \t'\" there");
|
|
||||||
assert_strequal("hello", wordlist_get(words, 0)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 0)->marker);
|
|
||||||
assert_strequal("\"mixed ' \tquotes \t'\"", wordlist_get(words, 1)->word);
|
|
||||||
assert_strequal(" \"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\" ", wordlist_get(words, 1)->marker);
|
|
||||||
assert_strequal("there", wordlist_get(words, 2)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 2)->marker);
|
|
||||||
assert(NULL == wordlist_get(words, 3));
|
|
||||||
wordlist_destroy(words);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_wordsplit_mixed_double_in_single(void)
|
|
||||||
{
|
|
||||||
t_wordlist *words;
|
|
||||||
|
|
||||||
words = minishell_wordsplit("hello 'mixed \" quotes \"' there");
|
|
||||||
assert_strequal("hello", wordlist_get(words, 0)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 0)->marker);
|
|
||||||
assert_strequal("'mixed \" quotes \"'", wordlist_get(words, 1)->word);
|
|
||||||
assert_strequal(" '''''''''''''''' ", wordlist_get(words, 1)->marker);
|
|
||||||
assert_strequal("there", wordlist_get(words, 2)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 2)->marker);
|
|
||||||
assert(NULL == wordlist_get(words, 3));
|
|
||||||
wordlist_destroy(words);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_wordsplit_mixed_broken(void)
|
|
||||||
{
|
|
||||||
t_wordlist *words;
|
|
||||||
|
|
||||||
words = minishell_wordsplit("hello '\"mixed 'quotes'\"' there");
|
|
||||||
assert_strequal("hello", wordlist_get(words, 0)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 0)->marker);
|
|
||||||
assert_strequal("'\"mixed 'quotes'\"'", wordlist_get(words, 1)->word);
|
|
||||||
assert_strequal(" ''''''' ' ", wordlist_get(words, 1)->marker);
|
|
||||||
assert_strequal("there", wordlist_get(words, 2)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 2)->marker);
|
|
||||||
assert(NULL == wordlist_get(words, 3));
|
|
||||||
wordlist_destroy(words);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_wordsplit_unclosed_single(void)
|
|
||||||
{
|
|
||||||
t_wordlist *words;
|
|
||||||
|
|
||||||
words = minishell_wordsplit("'hello");
|
|
||||||
assert(words == NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_wordsplit_unclosed_double(void)
|
|
||||||
{
|
|
||||||
t_wordlist *words;
|
|
||||||
|
|
||||||
words = minishell_wordsplit("\"hello");
|
|
||||||
assert(words == NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_wordsplit_operator_word(void)
|
|
||||||
{
|
|
||||||
t_wordlist *words;
|
|
||||||
|
|
||||||
words = minishell_wordsplit(">test");
|
|
||||||
assert_strequal(">", wordlist_get(words, 0)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 0)->marker);
|
|
||||||
assert_strequal("test", wordlist_get(words, 1)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 1)->marker);
|
|
||||||
assert(NULL == wordlist_get(words, 2));
|
|
||||||
wordlist_destroy(words);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_wordsplit_all_operators(void)
|
|
||||||
{
|
|
||||||
t_wordlist *words;
|
|
||||||
|
|
||||||
words = minishell_wordsplit("|&&||()<>><<>");
|
|
||||||
assert_strequal("|", wordlist_get(words, 0)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 0)->marker);
|
|
||||||
assert_strequal("&&", wordlist_get(words, 1)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 1)->marker);
|
|
||||||
assert_strequal("||", wordlist_get(words, 2)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 2)->marker);
|
|
||||||
assert_strequal("(", wordlist_get(words, 3)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 3)->marker);
|
|
||||||
assert_strequal(")", wordlist_get(words, 4)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 4)->marker);
|
|
||||||
assert_strequal("<", wordlist_get(words, 5)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 5)->marker);
|
|
||||||
assert_strequal(">>", wordlist_get(words, 6)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 6)->marker);
|
|
||||||
assert_strequal("<<", wordlist_get(words, 7)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 7)->marker);
|
|
||||||
assert_strequal(">", wordlist_get(words, 8)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 8)->marker);
|
|
||||||
assert(NULL == wordlist_get(words, 9));
|
|
||||||
wordlist_destroy(words);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_wordsplit_operator_combining(void)
|
|
||||||
{
|
|
||||||
t_wordlist *words;
|
|
||||||
|
|
||||||
words = minishell_wordsplit("|||>>><<<&&&");
|
|
||||||
assert_strequal("||", wordlist_get(words, 0)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 0)->marker);
|
|
||||||
assert_strequal("|", wordlist_get(words, 1)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 1)->marker);
|
|
||||||
assert_strequal(">>", wordlist_get(words, 2)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 2)->marker);
|
|
||||||
assert_strequal(">", wordlist_get(words, 3)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 3)->marker);
|
|
||||||
assert_strequal("<<", wordlist_get(words, 4)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 4)->marker);
|
|
||||||
assert_strequal("<", wordlist_get(words, 5)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 5)->marker);
|
|
||||||
assert_strequal("&&", wordlist_get(words, 6)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 6)->marker);
|
|
||||||
assert_strequal("&", wordlist_get(words, 7)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 7)->marker);
|
|
||||||
assert(NULL == wordlist_get(words, 8));
|
|
||||||
wordlist_destroy(words);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_wordsplit_var_substitution(void)
|
|
||||||
{
|
|
||||||
t_wordlist *words;
|
|
||||||
|
|
||||||
words = minishell_wordsplit("echo VAR=$VAR here");
|
|
||||||
assert_strequal("echo", wordlist_get(words, 0)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 0)->marker);
|
|
||||||
assert(0 == wordlist_get(words, 0)->flags);
|
|
||||||
assert_strequal("VAR=$VAR", wordlist_get(words, 1)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 1)->marker);
|
|
||||||
assert(W_HASDOLLAR == wordlist_get(words, 1)->flags);
|
|
||||||
assert_strequal("here", wordlist_get(words, 2)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 2)->marker);
|
|
||||||
assert(0 == wordlist_get(words, 2)->flags);
|
|
||||||
wordlist_destroy(words);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_wordsplit_var_substitution_quotes(void)
|
|
||||||
{
|
|
||||||
t_wordlist *words;
|
|
||||||
|
|
||||||
words = minishell_wordsplit("VAR=\"$VAR\"");
|
|
||||||
assert_strequal("VAR=\"$VAR\"", wordlist_get(words, 0)->word);
|
|
||||||
assert_strequal(" \"\"\"\" ", wordlist_get(words, 0)->marker);
|
|
||||||
assert(W_HASDOLLAR & wordlist_get(words, 0)->flags);
|
|
||||||
wordlist_destroy(words);
|
|
||||||
words = minishell_wordsplit("VAR='$VAR'");
|
|
||||||
assert_strequal("VAR='$VAR'", wordlist_get(words, 0)->word);
|
|
||||||
assert((W_HASDOLLAR & wordlist_get(words, 0)->flags) == 0);
|
|
||||||
assert_strequal(" '''' ", wordlist_get(words, 0)->marker);
|
|
||||||
wordlist_destroy(words);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_wordsplit_quote_detection_nonnested(void)
|
|
||||||
{
|
|
||||||
t_wordlist *words;
|
|
||||||
|
|
||||||
words = minishell_wordsplit("echo 'single quotes' here \"double quotes\" here");
|
|
||||||
assert_strequal("echo", wordlist_get(words, 0)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 0)->marker);
|
|
||||||
assert(0 == wordlist_get(words, 0)->flags);
|
|
||||||
assert_strequal("'single quotes'", wordlist_get(words, 1)->word);
|
|
||||||
assert_strequal(" ''''''''''''' ", wordlist_get(words, 1)->marker);
|
|
||||||
assert(W_QUOTED == wordlist_get(words, 1)->flags);
|
|
||||||
assert_strequal("here", wordlist_get(words, 2)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 2)->marker);
|
|
||||||
assert(0 == wordlist_get(words, 2)->flags);
|
|
||||||
assert_strequal("\"double quotes\"", wordlist_get(words, 3)->word);
|
|
||||||
assert_strequal(" \"\"\"\"\"\"\"\"\"\"\"\"\" ", wordlist_get(words, 3)->marker);
|
|
||||||
assert((W_QUOTED | W_DQUOTE) == wordlist_get(words, 3)->flags);
|
|
||||||
assert_strequal("here", wordlist_get(words, 4)->word);
|
|
||||||
assert_strequal(" ", wordlist_get(words, 4)->marker);
|
|
||||||
assert(0 == wordlist_get(words, 4)->flags);
|
|
||||||
wordlist_destroy(words);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_wordsplit_quote_detection_nested_double_in_simple(void)
|
|
||||||
{
|
|
||||||
t_wordlist *words;
|
|
||||||
char *str;
|
|
||||||
|
|
||||||
str = "'these are single quotes \"with double\" inside'";
|
|
||||||
words = minishell_wordsplit(str);
|
|
||||||
assert_strequal(str, wordlist_get(words, 0)->word);
|
|
||||||
assert_strequal(" '''''''''''''''''''''''''''''''''''''''''''' ", wordlist_get(words, 0)->marker);
|
|
||||||
assert(W_QUOTED == wordlist_get(words, 0)->flags);
|
|
||||||
wordlist_destroy(words);
|
|
||||||
str = "'\"these are single quotes with double inside\"'";
|
|
||||||
words = minishell_wordsplit(str);
|
|
||||||
assert_strequal(str, wordlist_get(words, 0)->word);
|
|
||||||
assert_strequal(" '''''''''''''''''''''''''''''''''''''''''''' ", wordlist_get(words, 0)->marker);
|
|
||||||
assert(W_QUOTED == wordlist_get(words, 0)->flags);
|
|
||||||
wordlist_destroy(words);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_wordsplit_quote_detection_nested_single_in_double(void)
|
|
||||||
{
|
|
||||||
t_wordlist *words;
|
|
||||||
char *str;
|
|
||||||
|
|
||||||
str = "\"these are double quotes 'with single' inside\"";
|
|
||||||
words = minishell_wordsplit(str);
|
|
||||||
assert_strequal(str, wordlist_get(words, 0)->word);
|
|
||||||
assert((W_QUOTED | W_DQUOTE) == wordlist_get(words, 0)->flags);
|
|
||||||
assert_strequal(" \"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\" ", wordlist_get(words, 0)->marker);
|
|
||||||
wordlist_destroy(words);
|
|
||||||
str = "\"'these are double quotes with single inside'\"";
|
|
||||||
words = minishell_wordsplit(str);
|
|
||||||
assert_strequal(str, wordlist_get(words, 0)->word);
|
|
||||||
assert_strequal(" \"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\" ", wordlist_get(words, 0)->marker);
|
|
||||||
assert((W_QUOTED | W_DQUOTE) == wordlist_get(words, 0)->flags);
|
|
||||||
wordlist_destroy(words);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void) {
|
|
||||||
test_wordsplit_singleword();
|
|
||||||
test_wordsplit_singleword_with_blanks();
|
|
||||||
test_wordsplit_multiword();
|
|
||||||
test_wordsplit_multiword_with_single_quotes();
|
|
||||||
test_wordsplit_multiword_with_double_quotes();
|
|
||||||
test_wordsplit_mixed_single_in_double();
|
|
||||||
test_wordsplit_mixed_double_in_single();
|
|
||||||
test_wordsplit_mixed_broken();
|
|
||||||
test_wordsplit_unclosed_single();
|
|
||||||
test_wordsplit_unclosed_double();
|
|
||||||
test_wordsplit_operator_word();
|
|
||||||
test_wordsplit_all_operators();
|
|
||||||
test_wordsplit_operator_combining();
|
|
||||||
test_wordsplit_var_substitution();
|
|
||||||
test_wordsplit_var_substitution_quotes();
|
|
||||||
test_wordsplit_quote_detection_nonnested();
|
|
||||||
test_wordsplit_quote_detection_nested_double_in_simple();
|
|
||||||
test_wordsplit_quote_detection_nested_single_in_double();
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
@ -1,70 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* test_wordlist_idx.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2025/03/10 14:59:19 by khais #+# #+# */
|
|
||||||
/* Updated: 2025/04/09 15:50:20 by khais ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "testutil.h"
|
|
||||||
#include "libft.h"
|
|
||||||
#include "../src/parser/wordlist/wordlist.h"
|
|
||||||
#include <assert.h>
|
|
||||||
|
|
||||||
static void test_wordlist_idx_pop_null(void)
|
|
||||||
{
|
|
||||||
t_wordlist *list;
|
|
||||||
|
|
||||||
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
|
||||||
assert(NULL == wordlist_pop_idx(NULL, 10));
|
|
||||||
list = NULL;
|
|
||||||
assert(NULL == wordlist_pop_idx(&list, 10));
|
|
||||||
do_leak_check();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_wordlist_idx_pop_single_elem(void)
|
|
||||||
{
|
|
||||||
t_wordlist *list;
|
|
||||||
t_worddesc *got;
|
|
||||||
|
|
||||||
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
|
||||||
list = wordlist_create(worddesc_create(ft_strdup("hello"), 0, ft_strdup(""), WORD_TOKEN));
|
|
||||||
assert(NULL == wordlist_pop_idx(&list, 1));
|
|
||||||
got = wordlist_pop_idx(&list, 0);
|
|
||||||
assert(NULL != got);
|
|
||||||
assert(NULL == list);
|
|
||||||
assert_strequal("hello", got->word);
|
|
||||||
assert(NULL == wordlist_pop_idx(&list, 0));
|
|
||||||
do_leak_check();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_wordlist_idx_pop_second_then_first(void)
|
|
||||||
{
|
|
||||||
t_wordlist *list;
|
|
||||||
t_worddesc *got;
|
|
||||||
|
|
||||||
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
|
||||||
list = wordlist_create(worddesc_create(ft_strdup("hello"), 0, NULL, WORD_TOKEN));
|
|
||||||
list = wordlist_push(list, worddesc_create(ft_strdup("world"), 0, NULL, WORD_TOKEN));
|
|
||||||
got = wordlist_pop_idx(&list, 1);
|
|
||||||
assert(NULL != got);
|
|
||||||
assert(NULL != list);
|
|
||||||
assert_strequal("world", got->word);
|
|
||||||
got = wordlist_pop_idx(&list, 0);
|
|
||||||
assert(NULL != got);
|
|
||||||
assert(NULL == list);
|
|
||||||
assert_strequal("hello", got->word);
|
|
||||||
do_leak_check();
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
test_wordlist_idx_pop_null();
|
|
||||||
test_wordlist_idx_pop_single_elem();
|
|
||||||
test_wordlist_idx_pop_second_then_first();
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
137
tests/testutil.c
137
tests/testutil.c
|
|
@ -1,137 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* testutil.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2025/02/13 15:21:09 by khais #+# #+# */
|
|
||||||
/* Updated: 2025/04/09 15:49:47 by khais ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "libft.h"
|
|
||||||
#include <assert.h>
|
|
||||||
#include "testutil.h"
|
|
||||||
#include "parse_cmdlist.h"
|
|
||||||
#include "unistd.h"
|
|
||||||
#include <assert.h>
|
|
||||||
#include "parse_pipeline.h"
|
|
||||||
#include "../src/parser/wordlist/wordlist.h"
|
|
||||||
#include "../src/parser/wordsplit/wordsplit.h"
|
|
||||||
#include "../src/parser/cmdlist/cmdlist_item.h"
|
|
||||||
|
|
||||||
void assert_strequal(char *str1, char *str2)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
ft_dprintf(STDERR_FILENO, "Expected\t[%s]\n", str1);
|
|
||||||
ft_dprintf(STDERR_FILENO, "to eq \t[%s]\n", str2);
|
|
||||||
if (str1 == str2)
|
|
||||||
return ;
|
|
||||||
ret = ft_strcmp(str1, str2);
|
|
||||||
assert(ret == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
extern void __lsan_do_leak_check(void)
|
|
||||||
__attribute__((weak));
|
|
||||||
|
|
||||||
void do_leak_check(void)
|
|
||||||
{
|
|
||||||
if (__lsan_do_leak_check != NULL)
|
|
||||||
__lsan_do_leak_check();
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
void assert_pipelineequal_raw(t_pipeline *expected, t_pipeline *got)
|
|
||||||
{
|
|
||||||
assert(expected == got || expected != NULL);
|
|
||||||
ft_dprintf(STDERR_FILENO, "Expected pipeline to have %d commands, got pipeline with %d\n", expected->num_cmd, got->num_cmd);
|
|
||||||
assert(expected->num_cmd == got->num_cmd);
|
|
||||||
int i = 0;
|
|
||||||
while (i < expected->num_cmd)
|
|
||||||
{
|
|
||||||
assert_simple_commandequal(expected->cmds[i], got->cmds[i], i);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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_cmd);
|
|
||||||
assert(cmd->num_cmd >= idx + 1);
|
|
||||||
t_pipeline *got = cmd->cmds[idx]->inner.pipeline;
|
|
||||||
ft_dprintf(STDERR_FILENO, "Expected pipeline %p to equal [%s]\n", got, expected);
|
|
||||||
t_pipeline *expected_pipeline = parse_pipeline(expected);
|
|
||||||
assert_pipelineequal_raw(expected_pipeline, got);
|
|
||||||
pipeline_destroy(expected_pipeline);
|
|
||||||
}
|
|
||||||
|
|
||||||
t_worddesc *create_single_word(char *str)
|
|
||||||
{
|
|
||||||
t_wordlist *words = minishell_wordsplit(str);
|
|
||||||
assert(wordlist_get(words, 0) != NULL);
|
|
||||||
assert(wordlist_get(words, 1) == NULL);
|
|
||||||
t_worddesc *word = wordlist_pop(&words);
|
|
||||||
return (word);
|
|
||||||
}
|
|
||||||
|
|
||||||
void assert_pipeline_cmd_word(t_pipeline *pipeline, char *expected_word, int cmd_num, int word_num)
|
|
||||||
{
|
|
||||||
if (pipeline->num_cmd <= cmd_num)
|
|
||||||
{
|
|
||||||
ft_dprintf(STDERR_FILENO, "expected pipeline %p to have at least %d cmds, but got %d\n", pipeline, cmd_num + 1, pipeline->num_cmd);
|
|
||||||
assert(false);
|
|
||||||
}
|
|
||||||
ft_dprintf(STDERR_FILENO, "for pipeline %p cmd %d word %d, expected '%s', and got ", pipeline, cmd_num, word_num, expected_word);
|
|
||||||
assert(pipeline->cmds[cmd_num] != NULL && "null cmd at that location");
|
|
||||||
char *got_word = wordlist_get(pipeline->cmds[cmd_num]->words, word_num)->word;
|
|
||||||
ft_dprintf(STDERR_FILENO, "'%s'\n", got_word);
|
|
||||||
assert(expected_word == got_word || ft_strncmp(expected_word, got_word, INT_MAX) == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
t_cmdgroup *parse_cmdgroup(char *input)
|
|
||||||
{
|
|
||||||
ft_dprintf(STDERR_FILENO, "Now checking command group with input [%s]\n", input);
|
|
||||||
t_wordlist *words = minishell_wordsplit(input);
|
|
||||||
t_cmdgroup *cmd = cmdgroup_from_wordlist(words);
|
|
||||||
wordlist_destroy(words);
|
|
||||||
return (cmd);
|
|
||||||
}
|
|
||||||
|
|
||||||
void assert_cmdgroup_itemlistequal(char *expected_str, t_cmdgroup *got_cmd, int item_number)
|
|
||||||
{
|
|
||||||
ft_dprintf(STDERR_FILENO, "checking that item %d of cmdlist %p matches [%s]\n", item_number, got_cmd, expected_str);
|
|
||||||
t_cmdlist *expected = parse_cmdlist(expected_str);
|
|
||||||
if (expected == NULL)
|
|
||||||
{
|
|
||||||
ft_dprintf(STDERR_FILENO, "expecting the list to be null\n");
|
|
||||||
return ;
|
|
||||||
}
|
|
||||||
ft_dprintf(STDERR_FILENO, "checking if the list matches...\n");
|
|
||||||
int i = 0;
|
|
||||||
while (i < expected->num_cmd)
|
|
||||||
{
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
assert(false && "not yet implemented");
|
|
||||||
}
|
|
||||||
|
|
@ -1,29 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* testutil.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2025/02/13 15:57:21 by khais #+# #+# */
|
|
||||||
/* Updated: 2025/03/18 15:05:34 by khais ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef TESTUTIL_H
|
|
||||||
# define TESTUTIL_H
|
|
||||||
|
|
||||||
# include "../src/parser/simple_cmd/simple_cmd.h"
|
|
||||||
# include "../src/parser/cmdlist/cmdlist.h"
|
|
||||||
# include "../src/parser/cmdgroup/cmdgroup.h"
|
|
||||||
|
|
||||||
void assert_strequal(char *str1, char *str2);
|
|
||||||
void do_leak_check(void);
|
|
||||||
void assert_simple_commandequal(t_simple_cmd *expected, t_simple_cmd *got, int idx);
|
|
||||||
void assert_pipelineequal(char *expected, t_cmdlist *cmd, int idx);
|
|
||||||
t_worddesc *create_single_word(char *str);
|
|
||||||
void assert_pipeline_cmd_word(t_pipeline *pipeline, char *expected_word, int cmd_num, int word_num);
|
|
||||||
t_cmdgroup *parse_cmdgroup(char *input);
|
|
||||||
void assert_cmdgroup_itemlistequal(char *expected_str, t_cmdgroup *got_cmd, int item_number);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue