/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* word_splitting.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/13 15:17:56 by khais #+# #+# */ /* Updated: 2025/02/17 14:55:31 by khais ### ########.fr */ /* */ /* ************************************************************************** */ #include #include "testutil.h" #include "libft.h" #include "../src/parser/wordsplit/wordsplit.h" #include "unistd.h" #include /* ** https://bash-hackers.gabe565.com/syntax/words/ */ static void test_wordsplit_singleword(void) { t_wordlist *words; words = minishell_wordsplit("echo"); assert_strequal("echo", wordlist_get(words, 0)->word); assert(NULL == wordlist_get(words, 1)); wordlist_destroy(words); } static void test_wordsplit_singleword_with_blanks(void) { t_wordlist *words; words = minishell_wordsplit("\t \t echo \t\t "); assert_strequal("echo", wordlist_get(words, 0)->word); assert(NULL == wordlist_get(words, 1)); wordlist_destroy(words); } static void test_wordsplit_multiword(void) { t_wordlist *words; words = minishell_wordsplit("\t echo\tThe file is named $MYFILE \t"); assert_strequal("echo", wordlist_get(words, 0)->word); assert_strequal("The", wordlist_get(words, 1)->word); assert_strequal("file", wordlist_get(words, 2)->word); assert_strequal("is", wordlist_get(words, 3)->word); assert_strequal("named", wordlist_get(words, 4)->word); assert_strequal("$MYFILE", wordlist_get(words, 5)->word); assert(NULL == wordlist_get(words, 6)); wordlist_destroy(words); } static void test_wordsplit_multiword_with_single_quotes(void) { t_wordlist *words; words = minishell_wordsplit("\t echo\t' \t The file is named $MYFILE ' \t"); assert_strequal("echo", wordlist_get(words, 0)->word); assert_strequal("' \t The file is named $MYFILE '", wordlist_get(words, 1)->word); assert(NULL == wordlist_get(words, 2)); wordlist_destroy(words); } static void test_wordsplit_multiword_with_double_quotes(void) { t_wordlist *words; words = minishell_wordsplit("\t echo\t\" \t The file is named $MYFILE \" \t"); assert_strequal("echo", wordlist_get(words, 0)->word); assert_strequal("\" \t The file is named $MYFILE \"", wordlist_get(words, 1)->word); assert(NULL == wordlist_get(words, 2)); wordlist_destroy(words); } static void test_wordsplit_mixed_single_in_double(void) { t_wordlist *words; words = minishell_wordsplit("hello \"mixed ' \tquotes \t'\" there"); assert_strequal("hello", wordlist_get(words, 0)->word); assert_strequal("\"mixed ' \tquotes \t'\"", wordlist_get(words, 1)->word); assert_strequal("there", wordlist_get(words, 2)->word); assert(NULL == wordlist_get(words, 3)); wordlist_destroy(words); } static void test_wordsplit_mixed_double_in_single(void) { t_wordlist *words; words = minishell_wordsplit("hello 'mixed \" quotes \"' there"); assert_strequal("hello", wordlist_get(words, 0)->word); assert_strequal("'mixed \" quotes \"'", wordlist_get(words, 1)->word); assert_strequal("there", wordlist_get(words, 2)->word); assert(NULL == wordlist_get(words, 3)); wordlist_destroy(words); } static void test_wordsplit_mixed_broken(void) { t_wordlist *words; words = minishell_wordsplit("hello '\"mixed 'quotes'\"' there"); assert_strequal("hello", wordlist_get(words, 0)->word); assert_strequal("'\"mixed \'quotes'\"'", wordlist_get(words, 1)->word); assert_strequal("there", wordlist_get(words, 2)->word); assert(NULL == wordlist_get(words, 3)); wordlist_destroy(words); } static void test_wordsplit_unclosed_single(void) { t_wordlist *words; words = minishell_wordsplit("'hello"); assert(words == NULL); } static void test_wordsplit_unclosed_double(void) { t_wordlist *words; words = minishell_wordsplit("\"hello"); assert(words == NULL); } int main(void) { test_wordsplit_singleword(); test_wordsplit_singleword_with_blanks(); test_wordsplit_multiword(); test_wordsplit_multiword_with_single_quotes(); test_wordsplit_multiword_with_double_quotes(); test_wordsplit_mixed_single_in_double(); test_wordsplit_mixed_double_in_single(); test_wordsplit_mixed_broken(); test_wordsplit_unclosed_single(); test_wordsplit_unclosed_double(); return (0); }