From 9707316085c08e5ff2df4a09b82a620b15086d7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Tue, 11 Mar 2025 15:59:59 +0100 Subject: [PATCH] tests: put usefull test functions in own files --- tests/Makefile | 6 ++- tests/expansion.c | 13 +---- tests/parse_command_list.c | 26 ++++++++++ tests/parse_command_list.h | 20 ++++++++ tests/parse_pipeline.c | 23 +++++++++ tests/parse_pipeline.h | 20 ++++++++ tests/test_cmdgroup_parsing.c | 11 +--- tests/test_parse_command_lists.c | 68 ++----------------------- tests/test_parse_pipelines.c | 33 ++---------- tests/test_quote_removal.c | 16 ++---- tests/testutil.c | 86 +++++++++++++++++++++++++++++++- tests/testutil.h | 15 ++++-- 12 files changed, 205 insertions(+), 132 deletions(-) create mode 100644 tests/parse_command_list.c create mode 100644 tests/parse_command_list.h create mode 100644 tests/parse_pipeline.c create mode 100644 tests/parse_pipeline.h diff --git a/tests/Makefile b/tests/Makefile index a00e213..259c7fe 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -21,7 +21,11 @@ endif tests = $(addprefix test_,$(rawtests)) run_tests = $(addprefix run_test_,$(rawtests)) test_objs = $(addsuffix .o,$(tests)) -objs := $(addprefix ../,$(objs)) testutil.o +objs := $(addprefix ../,$(objs)) \ + testutil.o \ + parse_command_list.o \ + parse_pipeline.o \ + all_objs = $(objs) $(test_objs) deps = $(all_objs:.o=.d) LDLIBS = \ diff --git a/tests/expansion.c b/tests/expansion.c index c4933ae..88db8e7 100644 --- a/tests/expansion.c +++ b/tests/expansion.c @@ -5,8 +5,8 @@ /* +:+ +:+ +:+ */ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2025/03/21 14:06/21 by khais #+# #+# */ -/* Updated: 2025/03/21 14:06:21 by khais ### ########.fr */ +/* Created: 2025/03/27 14:05/00 by khais #+# #+# */ +/* Updated: 2025/03/27 14:05:00 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,15 +22,6 @@ #include "../src/parser/wordsplit/wordsplit.h" #include "../src/parser/wordlist/wordlist.h" -static 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); -} - /* ** Test file for the different expansion/substitution types of minishell. */ diff --git a/tests/parse_command_list.c b/tests/parse_command_list.c new file mode 100644 index 0000000..cb318a7 --- /dev/null +++ b/tests/parse_command_list.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_command_list.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: khais +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/11 15:54:43 by khais #+# #+# */ +/* Updated: 2025/03/11 15:55:01 by khais ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../src/parser/command_list/command_list.h" +#include "../src/parser/wordsplit/wordsplit.h" +#include "libft.h" +#include "unistd.h" +#include + +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); +} diff --git a/tests/parse_command_list.h b/tests/parse_command_list.h new file mode 100644 index 0000000..ae0b193 --- /dev/null +++ b/tests/parse_command_list.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_command_list.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: khais +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/11 15:55:10 by khais #+# #+# */ +/* Updated: 2025/03/11 15:55:25 by khais ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef PARSE_COMMAND_LIST_H +# define PARSE_COMMAND_LIST_H + +# include "../src/parser/command_list/command_list.h" + +t_cmdlist *parse_command_list(char *input); + +#endif // PARSE_COMMAND_LIST_H diff --git a/tests/parse_pipeline.c b/tests/parse_pipeline.c new file mode 100644 index 0000000..3fafe90 --- /dev/null +++ b/tests/parse_pipeline.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_pipeline.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: khais +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 + +t_pipeline *parse_pipeline(char *input) +{ + t_wordlist *words = minishell_wordsplit(input); + t_pipeline *pipeline = pipeline_from_wordlist(words); + wordlist_destroy(words); + return (pipeline); +} diff --git a/tests/parse_pipeline.h b/tests/parse_pipeline.h new file mode 100644 index 0000000..9ffd6f8 --- /dev/null +++ b/tests/parse_pipeline.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_pipeline.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: khais +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 diff --git a/tests/test_cmdgroup_parsing.c b/tests/test_cmdgroup_parsing.c index a5acd9e..1b53a82 100644 --- a/tests/test_cmdgroup_parsing.c +++ b/tests/test_cmdgroup_parsing.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/11 15:07:23 by khais #+# #+# */ -/* Updated: 2025/03/11 15:20:17 by khais ### ########.fr */ +/* Updated: 2025/03/11 16:33:42 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,15 +16,6 @@ #include "../src/parser/cmdgroup/cmdgroup.h" #include "../src/parser/wordsplit/wordsplit.h" -static 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); -} - static void test_cmdgroup_parsing_empty(void) { t_cmdgroup *cmd; diff --git a/tests/test_parse_command_lists.c b/tests/test_parse_command_lists.c index c6554ee..0465128 100644 --- a/tests/test_parse_command_lists.c +++ b/tests/test_parse_command_lists.c @@ -1,84 +1,22 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* parse_command_lists.c :+: :+: :+: */ +/* test_parse_command_lists.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/24 17:40:48 by khais #+# #+# */ -/* Updated: 2025/03/04 15:16:00 by khais ### ########.fr */ +/* Updated: 2025/03/11 16:30:05 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 - -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); -} +#include "parse_command_list.h" static void test_parse_command_list_empty(void) { diff --git a/tests/test_parse_pipelines.c b/tests/test_parse_pipelines.c index 5f7ca16..260a91d 100644 --- a/tests/test_parse_pipelines.c +++ b/tests/test_parse_pipelines.c @@ -1,45 +1,22 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* parse_pipelines.c :+: :+: :+: */ +/* test_parse_pipelines.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/21 13:13:58 by khais #+# #+# */ -/* Updated: 2025/03/04 15:08:43 by khais ### ########.fr */ +/* Updated: 2025/03/11 16:33:11 by khais ### ########.fr */ /* */ /* ************************************************************************** */ #include "../src/parser/pipeline/pipeline.h" -#include "../src/parser/wordsplit/wordsplit.h" #include "../src/ft_errno.h" #include -#include "ft_printf.h" -#include "libft.h" -#include "unistd.h" +#include "testutil.h" #include - -static t_pipeline *parse_pipeline(char *input) -{ - t_wordlist *words = minishell_wordsplit(input); - t_pipeline *pipeline = pipeline_from_wordlist(words); - wordlist_destroy(words); - return (pipeline); -} - -static 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); -} +#include "parse_pipeline.h" +#include static void test_parse_empty_pipeline(void) { diff --git a/tests/test_quote_removal.c b/tests/test_quote_removal.c index de3ca57..415f8c3 100644 --- a/tests/test_quote_removal.c +++ b/tests/test_quote_removal.c @@ -1,29 +1,19 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* quote_removal.c :+: :+: :+: */ +/* test_quote_removal.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/28 13:46:56 by khais #+# #+# */ -/* Updated: 2025/03/07 11:24:30 by khais ### ########.fr */ +/* Updated: 2025/03/11 16:31:50 by khais ### ########.fr */ /* */ /* ************************************************************************** */ -#include "../src/parser/wordlist/wordlist.h" -#include "../src/parser/wordsplit/wordsplit.h" #include "../src/parser/remove_quotes/remove_quotes.h" #include "testutil.h" #include - -static 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); -} +#include static void test_quote_removal_no_quotes_single_word(void) { diff --git a/tests/testutil.c b/tests/testutil.c index e390926..7e4aa79 100644 --- a/tests/testutil.c +++ b/tests/testutil.c @@ -6,12 +6,18 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/13 15:21:09 by khais #+# #+# */ -/* Updated: 2025/03/11 11:46:04 by khais ### ########.fr */ +/* Updated: 2025/03/11 16:33:46 by khais ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" #include +#include "testutil.h" +#include "unistd.h" +#include +#include "parse_pipeline.h" +#include "../src/parser/wordlist/wordlist.h" +#include "../src/parser/wordsplit/wordsplit.h" void assert_strequal(char *str1, char *str2) { @@ -33,3 +39,81 @@ 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(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); +} + +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); +} diff --git a/tests/testutil.h b/tests/testutil.h index 1742535..cedba3e 100644 --- a/tests/testutil.h +++ b/tests/testutil.h @@ -6,14 +6,23 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/13 15:57:21 by khais #+# #+# */ -/* Updated: 2025/03/09 12:36:44 by khais ### ########.fr */ +/* Updated: 2025/03/11 16:34:10 by khais ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef TESTUTIL_H # define TESTUTIL_H -void assert_strequal(char *str1, char *str2); -void do_leak_check(void); +# include "../src/parser/simple_cmd/simple_cmd.h" +# include "../src/parser/command_list/command_list.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); #endif