pipeline: parse basic pipeline commands with two commands

This commit is contained in:
Khaïs COLIN 2025-02-21 14:13:51 +01:00
parent 2b8bb859d1
commit 5f1485d1d5
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
4 changed files with 97 additions and 9 deletions

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/21 13:13:58 by khais #+# #+# */
/* Updated: 2025/02/21 13:44:16 by khais ### ########.fr */
/* Updated: 2025/02/21 14:07:52 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -29,11 +29,13 @@ static void assert_pipeline_cmd_word(t_pipeline *pipeline, char *expected_word,
{
if (pipeline->num_cmd <= cmd_num)
{
ft_dprintf(STDERR_FILENO, "expected pipeline %p to have at least %d words, but got %d\n", pipeline, cmd_num + 1, pipeline->num_cmd);
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, "for pipeline %p cmd %d word %d, expected '%s', and got '%s'\n", pipeline, cmd_num, word_num, expected_word, got_word);
ft_dprintf(STDERR_FILENO, "'%s'\n", got_word);
assert(expected_word == got_word || ft_strncmp(expected_word, got_word, INT_MAX) == 0);
}
@ -54,9 +56,21 @@ static void test_parse_pipeline_single_cmd(void)
pipeline_destroy(pipeline);
}
static void test_parse_pipeline_two_cmd(void)
{
t_pipeline *pipeline = parse_pipeline("echo hello world | tee output.txt");
assert_pipeline_cmd_word(pipeline, "echo", 0, 0);
assert_pipeline_cmd_word(pipeline, "hello", 0, 1);
assert_pipeline_cmd_word(pipeline, "world", 0, 2);
assert_pipeline_cmd_word(pipeline, "tee", 1, 0);
assert_pipeline_cmd_word(pipeline, "output.txt", 1, 1);
pipeline_destroy(pipeline);
}
int main(void)
{
test_parse_empty_pipeline();
test_parse_pipeline_single_cmd();
test_parse_pipeline_two_cmd();
return (0);
}