mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
144 lines
5 KiB
C
144 lines
5 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* testutil.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2025/02/13 15:21:09 by khais #+# #+# */
|
|
/* Updated: 2025/03/20 11:57:29 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);
|
|
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->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");
|
|
}
|