mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
cmdgroup parsing: handle parsing a single cmdlist (badly)
This commit is contained in:
parent
9707316085
commit
d8dd1613c8
8 changed files with 157 additions and 29 deletions
|
|
@ -5,8 +5,8 @@
|
|||
/* +:+ +:+ +:+ */
|
||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/11 15:07:23 by khais #+# #+# */
|
||||
/* Updated: 2025/03/11 16:33:42 by khais ### ########.fr */
|
||||
/* Created: 2025/03/11 17:30/41 by khais #+# #+# */
|
||||
/* Updated: 2025/03/11 17:30:41 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -14,7 +14,7 @@
|
|||
#include <assert.h>
|
||||
#include "libft.h"
|
||||
#include "../src/parser/cmdgroup/cmdgroup.h"
|
||||
#include "../src/parser/wordsplit/wordsplit.h"
|
||||
#include "../src/parser/cmdgroup/cmdgroup_item.h"
|
||||
|
||||
static void test_cmdgroup_parsing_empty(void)
|
||||
{
|
||||
|
|
@ -30,8 +30,27 @@ static void test_cmdgroup_parsing_empty(void)
|
|||
do_leak_check();
|
||||
}
|
||||
|
||||
static void test_cmdgroup_parsing_single_cmdlist(void)
|
||||
{
|
||||
t_cmdgroup *cmd;
|
||||
|
||||
// arange
|
||||
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
||||
cmd = parse_cmdgroup("echo this | cat -e && echo works | wc -c");
|
||||
// assert
|
||||
assert(NULL != cmd);
|
||||
assert(1 == cmd->item_num);
|
||||
assert(TYPE_LIST == cmd->items[0].type);
|
||||
assert_cmdgroup_itemlistequal("echo this | cat -e && echo works | wc -c", cmd, 0);
|
||||
// cleanup
|
||||
cmdgroup_destroy(cmd);
|
||||
do_leak_check();
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test_cmdgroup_parsing_empty();
|
||||
test_cmdgroup_parsing_single_cmdlist();
|
||||
// redirections
|
||||
return (0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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++;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/13 15:57:21 by khais #+# #+# */
|
||||
/* Updated: 2025/03/11 16:34:10 by khais ### ########.fr */
|
||||
/* Updated: 2025/03/11 17:26:49 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -24,5 +24,6 @@ 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