/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* test_here_doc.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/07 11:43:32 by khais #+# #+# */ /* Updated: 2025/03/11 13:45:45 by khais ### ########.fr */ /* */ /* ************************************************************************** */ #include #include "testutil.h" #include "libft.h" #include #include #include "../src/executing/here_doc/here_doc.h" static void test_here_doc_filename_generation(void) { char *filename1; char *filename2; ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__); filename1 = here_doc_random_filename(); filename2 = here_doc_random_filename(); assert(filename1 != NULL); assert(filename2 != NULL); ft_dprintf(STDERR_FILENO, "Got filename: [%s]\n", filename1); ft_dprintf(STDERR_FILENO, "Got filename: [%s]\n", filename2); assert(ft_strcmp(filename1, filename2) != 0); free(filename1); free(filename2); do_leak_check(); } static void test_here_doc_invalid_args(void) { t_worddesc *marker; ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__); assert(-1 == here_doc(NULL, 0)); marker = worddesc_create(NULL, 0, NULL); assert(-1 == here_doc(marker, 0)); worddesc_destroy(marker); marker = worddesc_create(ft_strdup("EOF"), 0, NULL); assert(-1 == here_doc(marker, -1)); worddesc_destroy(marker); do_leak_check(); } static void test_here_doc_only_end_marker(void) { t_worddesc *marker; int infile; int result; ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__); marker = worddesc_create(ft_strdup("EOF"), 0, NULL); infile = open("./here_doc_only_eof.input", O_RDONLY); result = here_doc(marker, infile); close(infile); assert(result != -1); assert(NULL == get_next_line(result)); close(result); worddesc_destroy(marker); do_leak_check(); } static void test_here_doc_input_plus_end_marker(void) { t_worddesc *marker; int infile; int result; ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__); marker = worddesc_create(ft_strdup("EOF"), 0, NULL); infile = open("./here_doc_input_plus_eof.input", O_RDONLY); result = here_doc(marker, infile); close(infile); worddesc_destroy(marker); assert(result != -1); assert_strequal("input\n", get_next_line(result)); close(result); do_leak_check(); } static void test_here_doc_input_no_end_marker(void) { t_worddesc *marker; int infile; int result; ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__); marker = worddesc_create(ft_strdup("EOF"), 0, NULL); infile = open("./here_doc_input_no_eof.input", O_RDONLY); result = here_doc(marker, infile); close(infile); worddesc_destroy(marker); assert(result != -1); assert_strequal("input\n", get_next_line(result)); close(result); do_leak_check(); } int main(void) { test_here_doc_filename_generation(); test_here_doc_invalid_args(); test_here_doc_only_end_marker(); test_here_doc_input_plus_end_marker(); test_here_doc_input_no_end_marker(); // TODO: expansion handling return (0); }