/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* test_parse_pipelines.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/21 13:13:58 by khais #+# #+# */ /* Updated: 2025/03/11 16:33:11 by khais ### ########.fr */ /* */ /* ************************************************************************** */ #include "../src/parser/pipeline/pipeline.h" #include "../src/ft_errno.h" #include #include "testutil.h" #include #include "parse_pipeline.h" #include static void test_parse_empty_pipeline(void) { t_pipeline *pipeline = parse_pipeline(""); assert(pipeline == NULL); pipeline_destroy(pipeline); } static void test_parse_pipeline_single_cmd(void) { t_pipeline *pipeline = parse_pipeline("echo hello world"); assert(pipeline->num_cmd == 1); assert_pipeline_cmd_word(pipeline, "echo", 0, 0); assert_pipeline_cmd_word(pipeline, "hello", 0, 1); assert_pipeline_cmd_word(pipeline, "world", 0, 2); 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); } static void test_parse_pipeline_three_cmd(void) { t_pipeline *pipeline = parse_pipeline("echo hello world | tee output.txt | cat -e"); 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); assert_pipeline_cmd_word(pipeline, "cat", 2, 0); assert_pipeline_cmd_word(pipeline, "-e", 2, 1); pipeline_destroy(pipeline); } static void test_parse_pipeline_four_cmd(void) { t_pipeline *pipeline = parse_pipeline("echo hello world | tee output.txt | cat -e | hexdump -C"); 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); assert_pipeline_cmd_word(pipeline, "cat", 2, 0); assert_pipeline_cmd_word(pipeline, "-e", 2, 1); assert_pipeline_cmd_word(pipeline, "hexdump", 3, 0); assert_pipeline_cmd_word(pipeline, "-C", 3, 1); pipeline_destroy(pipeline); } static void test_parse_pipeline_double_pipe_rejected(void) { ft_errno(FT_ESUCCESS); assert(parse_pipeline("echo hello | | tee output.txt") == NULL); assert(ft_errno_get() == FT_EUNEXPECTED_PIPE); } static void test_parse_pipeline_triple_pipe_rejected(void) { ft_errno(FT_ESUCCESS); assert(parse_pipeline("echo hello | | | tee output.txt") == NULL); assert(ft_errno_get() == FT_EUNEXPECTED_PIPE); } static void test_parse_pipeline_pipe_at_start_rejected(void) { ft_errno(FT_ESUCCESS); assert(parse_pipeline("| echo hello | tee output.txt") == NULL); assert(ft_errno_get() == FT_EUNEXPECTED_PIPE); } static void test_parse_pipeline_pipe_at_end_rejected(void) { ft_errno(FT_ESUCCESS); assert(parse_pipeline("echo hello | tee output.txt |") == NULL); assert(ft_errno_get() == FT_EUNEXPECTED_PIPE); } int main(void) { test_parse_empty_pipeline(); test_parse_pipeline_single_cmd(); test_parse_pipeline_two_cmd(); test_parse_pipeline_three_cmd(); test_parse_pipeline_four_cmd(); test_parse_pipeline_double_pipe_rejected(); test_parse_pipeline_triple_pipe_rejected(); test_parse_pipeline_pipe_at_start_rejected(); test_parse_pipeline_pipe_at_end_rejected(); return (0); }