cmdgroup parsing: handle parsing a single cmdlist (badly)

This commit is contained in:
Khaïs COLIN 2025-03-11 16:41:38 +01:00
parent 9707316085
commit d8dd1613c8
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
8 changed files with 157 additions and 29 deletions

View file

@ -6,18 +6,20 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/13 15:21:09 by khais #+# #+# */
/* Updated: 2025/03/11 16:33:46 by khais ### ########.fr */
/* Updated: 2025/03/11 18:12:05 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <assert.h>
#include "testutil.h"
#include "parse_command_list.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/cmdgroup/cmdgroup_item.h"
void assert_strequal(char *str1, char *str2)
{
@ -60,14 +62,9 @@ void assert_simple_commandequal(t_simple_cmd *expected, t_simple_cmd *got, int i
assert(got_word == NULL);
}
void assert_pipelineequal(char *expected, t_cmdlist *cmd, int idx)
void assert_pipelineequal_raw(t_pipeline *expected, t_pipeline *got)
{
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);
assert(expected == got || expected != NULL);
int j = 0;
while (j < got->num_cmd)
{
@ -75,14 +72,24 @@ void assert_pipelineequal(char *expected, t_cmdlist *cmd, int idx)
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);
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_pipeline->num_cmd)
while (i < expected->num_cmd)
{
assert_simple_commandequal(expected_pipeline->cmds[i], got->cmds[i], i);
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_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_pipelineequal_raw(expected_pipeline, got);
pipeline_destroy(expected_pipeline);
}
@ -117,3 +124,28 @@ t_cmdgroup *parse_cmdgroup(char *input)
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);
ft_dprintf(STDERR_FILENO, "expecteing to have at least %d items, and got %d\n", item_number + 1, got_cmd->item_num);
assert(got_cmd->item_num > item_number);
ft_dprintf(STDERR_FILENO, "expecteing item %p to be of type list got %d\n", got_cmd->items[item_number], got_cmd->items[item_number].type);
t_cmdlist *expected = parse_command_list(expected_str);
t_cmdlist *got = got_cmd->items[item_number].inner.cmdlist;
if (expected == NULL)
{
ft_dprintf(STDERR_FILENO, "expecting the list to be null\n");
assert(expected == got);
return ;
}
ft_dprintf(STDERR_FILENO, "checking if the list matches...\n");
assert(expected->num_pipelines == got->num_pipelines);
int i = 0;
while (i < expected->num_pipelines)
{
assert_pipelineequal_raw(expected->pipelines[i], got->pipelines[i]);
assert(expected->operators[i] == got->operators[i]);
i++;
}
}