tests: put usefull test functions in own files

This commit is contained in:
Khaïs COLIN 2025-03-11 15:59:59 +01:00
parent f5ae3a5d8d
commit 9707316085
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
12 changed files with 205 additions and 132 deletions

View file

@ -21,7 +21,11 @@ endif
tests = $(addprefix test_,$(rawtests)) tests = $(addprefix test_,$(rawtests))
run_tests = $(addprefix run_test_,$(rawtests)) run_tests = $(addprefix run_test_,$(rawtests))
test_objs = $(addsuffix .o,$(tests)) 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) all_objs = $(objs) $(test_objs)
deps = $(all_objs:.o=.d) deps = $(all_objs:.o=.d)
LDLIBS = \ LDLIBS = \

View file

@ -5,8 +5,8 @@
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/21 14:06/21 by khais #+# #+# */ /* Created: 2025/03/27 14:05/00 by khais #+# #+# */
/* Updated: 2025/03/21 14:06:21 by khais ### ########.fr */ /* Updated: 2025/03/27 14:05:00 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,15 +22,6 @@
#include "../src/parser/wordsplit/wordsplit.h" #include "../src/parser/wordsplit/wordsplit.h"
#include "../src/parser/wordlist/wordlist.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. ** Test file for the different expansion/substitution types of minishell.
*/ */

View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse_command_list.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <assert.h>
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);
}

View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse_command_list.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

23
tests/parse_pipeline.c Normal file
View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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);
}

20
tests/parse_pipeline.h Normal file
View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/11 15:07:23 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/cmdgroup/cmdgroup.h"
#include "../src/parser/wordsplit/wordsplit.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) static void test_cmdgroup_parsing_empty(void)
{ {
t_cmdgroup *cmd; t_cmdgroup *cmd;

View file

@ -1,84 +1,22 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* parse_command_lists.c :+: :+: :+: */ /* test_parse_command_lists.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/24 17:40:48 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 "ft_printf.h"
#include "../src/parser/command_list/command_list.h" #include "../src/parser/command_list/command_list.h"
#include "../src/parser/wordsplit/wordsplit.h"
#include "testutil.h" #include "testutil.h"
#include "unistd.h" #include "unistd.h"
#include <assert.h> #include <assert.h>
#include "parse_command_list.h"
static t_cmdlist *parse_command_list(char *input)
{
ft_dprintf(STDERR_FILENO, "Now checking command list with input [%s]\n", input);
t_wordlist *words = minishell_wordsplit(input);
t_cmdlist *cmd = cmdlist_from_wordlist(words);
wordlist_destroy(words);
return (cmd);
}
static t_pipeline *parse_pipeline(char *input)
{
t_wordlist *words = minishell_wordsplit(input);
t_pipeline *pipeline = pipeline_from_wordlist(words);
return (pipeline);
}
static void assert_simple_commandequal(t_simple_cmd *expected, t_simple_cmd *got, int idx)
{
ft_dprintf(STDERR_FILENO, "Checking cmd idx %d\n", idx);
int i = 0;
t_wordlist *expected_word = expected->words;
t_wordlist *got_word = got->words;
while (expected_word != NULL)
{
ft_dprintf(STDERR_FILENO, "Checking word %d: ", i);
assert_strequal(expected_word->word->word, got_word->word->word);
ft_dprintf(STDERR_FILENO, "Checking word %d: expected flags=%d got flags=%d\n", i, expected_word->word->flags, got_word->word->flags);
assert(expected_word->word->flags == got_word->word->flags);
expected_word = expected_word->next;
got_word = got_word->next;
i++;
}
assert(expected_word == NULL);
assert(got_word == NULL);
}
static void assert_pipelineequal(char *expected, t_cmdlist *cmd, int idx)
{
ft_printf("Expected command list %p to have at least %d pipelines, and got %d\n", cmd, idx + 1, cmd->num_pipelines);
assert(cmd->num_pipelines >= idx + 1);
t_pipeline *got = cmd->pipelines[idx];
ft_dprintf(STDERR_FILENO, "Expected pipeline %p to equal [%s]\n", got, expected);
t_pipeline *expected_pipeline = parse_pipeline(expected);
assert(expected_pipeline == got || expected_pipeline != NULL);
int j = 0;
while (j < got->num_cmd)
{
ft_dprintf(STDERR_FILENO, "Got pipeline cmd %d: ", j);
wordlist_debug(got->cmds[j]->words);
j++;
}
ft_dprintf(STDERR_FILENO, "Expected pipeline to have %d commands, got pipeline with %d\n", expected_pipeline->num_cmd, got->num_cmd);
assert(expected_pipeline->num_cmd == got->num_cmd);
int i = 0;
while (i < expected_pipeline->num_cmd)
{
assert_simple_commandequal(expected_pipeline->cmds[i], got->cmds[i], i);
i++;
}
pipeline_destroy(expected_pipeline);
}
static void test_parse_command_list_empty(void) static void test_parse_command_list_empty(void)
{ {

View file

@ -1,45 +1,22 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* parse_pipelines.c :+: :+: :+: */ /* test_parse_pipelines.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/21 13:13:58 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/pipeline/pipeline.h"
#include "../src/parser/wordsplit/wordsplit.h"
#include "../src/ft_errno.h" #include "../src/ft_errno.h"
#include <assert.h> #include <assert.h>
#include "ft_printf.h" #include "testutil.h"
#include "libft.h"
#include "unistd.h"
#include <limits.h> #include <limits.h>
#include "parse_pipeline.h"
static t_pipeline *parse_pipeline(char *input) #include <stdlib.h>
{
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);
}
static void test_parse_empty_pipeline(void) static void test_parse_empty_pipeline(void)
{ {

View file

@ -1,29 +1,19 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* quote_removal.c :+: :+: :+: */ /* test_quote_removal.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/28 13:46:56 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 "../src/parser/remove_quotes/remove_quotes.h"
#include "testutil.h" #include "testutil.h"
#include <assert.h> #include <assert.h>
#include <stdlib.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);
}
static void test_quote_removal_no_quotes_single_word(void) static void test_quote_removal_no_quotes_single_word(void)
{ {

View file

@ -6,12 +6,18 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/13 15:21:09 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 "libft.h"
#include <assert.h> #include <assert.h>
#include "testutil.h"
#include "unistd.h"
#include <assert.h>
#include "parse_pipeline.h"
#include "../src/parser/wordlist/wordlist.h"
#include "../src/parser/wordsplit/wordsplit.h"
void assert_strequal(char *str1, char *str2) void assert_strequal(char *str1, char *str2)
{ {
@ -33,3 +39,81 @@ void do_leak_check(void)
if (__lsan_do_leak_check != NULL) if (__lsan_do_leak_check != NULL)
__lsan_do_leak_check(); __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);
}

View file

@ -6,14 +6,23 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/13 15:57:21 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 #ifndef TESTUTIL_H
# define TESTUTIL_H # define TESTUTIL_H
# 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 assert_strequal(char *str1, char *str2);
void do_leak_check(void); 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 #endif