minishell/tests/word_splitting.c

285 lines
9.6 KiB
C
Raw Normal View History

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* word_splitting.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/13 15:17:56 by khais #+# #+# */
/* Updated: 2025/03/06 15:05:58 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include <assert.h>
#include "testutil.h"
#include "../src/parser/wordsplit/wordsplit.h"
#include <stdlib.h>
/*
** https://bash-hackers.gabe565.com/syntax/words/
*/
2025-02-17 13:51:08 +01:00
static void test_wordsplit_singleword(void)
{
t_wordlist *words;
words = minishell_wordsplit("echo");
assert_strequal("echo", wordlist_get(words, 0)->word);
assert_strequal(" ", wordlist_get(words, 0)->marker);
assert(NULL == wordlist_get(words, 1));
wordlist_destroy(words);
}
2025-02-17 13:51:08 +01:00
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_strequal(" ", wordlist_get(words, 0)->marker);
assert(NULL == wordlist_get(words, 1));
wordlist_destroy(words);
}
2025-02-17 13:51:08 +01:00
static void test_wordsplit_multiword(void)
{
t_wordlist *words;
2025-02-14 15:44:10 +01:00
words = minishell_wordsplit("\t echo\tThe file is named $MYFILE \t");
assert_strequal("echo", wordlist_get(words, 0)->word);
assert_strequal(" ", wordlist_get(words, 0)->marker);
assert_strequal("The", wordlist_get(words, 1)->word);
assert_strequal(" ", wordlist_get(words, 1)->marker);
assert_strequal("file", wordlist_get(words, 2)->word);
assert_strequal(" ", wordlist_get(words, 2)->marker);
assert_strequal("is", wordlist_get(words, 3)->word);
assert_strequal(" ", wordlist_get(words, 3)->marker);
assert_strequal("named", wordlist_get(words, 4)->word);
assert_strequal(" ", wordlist_get(words, 4)->marker);
assert_strequal("$MYFILE", wordlist_get(words, 5)->word);
assert_strequal(" ", wordlist_get(words, 5)->marker);
assert(NULL == wordlist_get(words, 6));
wordlist_destroy(words);
}
// kco work marker
2025-02-17 13:51:08 +01:00
static void test_wordsplit_multiword_with_single_quotes(void)
2025-02-14 17:50:56 +01:00
{
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);
}
2025-02-17 13:51:08 +01:00
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);
}
2025-02-17 14:00:01 +01:00
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);
}
2025-02-17 16:14:14 +01:00
static void test_wordsplit_operator_word(void)
{
t_wordlist *words;
words = minishell_wordsplit(">test");
assert_strequal(">", wordlist_get(words, 0)->word);
assert_strequal("test", wordlist_get(words, 1)->word);
assert(NULL == wordlist_get(words, 2));
wordlist_destroy(words);
}
static void test_wordsplit_all_operators(void)
{
t_wordlist *words;
words = minishell_wordsplit("|&&||()<>><<>");
assert_strequal("|", wordlist_get(words, 0)->word);
assert_strequal("&&", wordlist_get(words, 1)->word);
assert_strequal("||", wordlist_get(words, 2)->word);
assert_strequal("(", wordlist_get(words, 3)->word);
assert_strequal(")", wordlist_get(words, 4)->word);
assert_strequal("<", wordlist_get(words, 5)->word);
assert_strequal(">>", wordlist_get(words, 6)->word);
assert_strequal("<<", wordlist_get(words, 7)->word);
assert_strequal(">", wordlist_get(words, 8)->word);
assert(NULL == wordlist_get(words, 9));
wordlist_destroy(words);
}
static void test_wordsplit_operator_combining(void)
{
t_wordlist *words;
words = minishell_wordsplit("|||>>><<<&&&");
assert_strequal("||", wordlist_get(words, 0)->word);
assert_strequal("|", wordlist_get(words, 1)->word);
assert_strequal(">>", wordlist_get(words, 2)->word);
assert_strequal(">", wordlist_get(words, 3)->word);
assert_strequal("<<", wordlist_get(words, 4)->word);
assert_strequal("<", wordlist_get(words, 5)->word);
assert_strequal("&&", wordlist_get(words, 6)->word);
assert_strequal("&", wordlist_get(words, 7)->word);
assert(NULL == wordlist_get(words, 8));
wordlist_destroy(words);
}
static void test_wordsplit_var_substitution(void)
{
t_wordlist *words;
words = minishell_wordsplit("echo VAR=$VAR here");
assert_strequal("echo", wordlist_get(words, 0)->word);
assert(0 == wordlist_get(words, 0)->flags);
assert_strequal("VAR=$VAR", wordlist_get(words, 1)->word);
assert(W_HASDOLLAR == wordlist_get(words, 1)->flags);
assert_strequal("here", wordlist_get(words, 2)->word);
assert(0 == wordlist_get(words, 2)->flags);
wordlist_destroy(words);
2025-02-20 14:22:38 +01:00
}
static void test_wordsplit_var_substitution_quotes(void)
{
t_wordlist *words;
words = minishell_wordsplit("VAR=\"$VAR\"");
assert(W_HASDOLLAR & wordlist_get(words, 0)->flags);
wordlist_destroy(words);
words = minishell_wordsplit("VAR='$VAR'");
assert((W_HASDOLLAR & wordlist_get(words, 0)->flags) == 0);
wordlist_destroy(words);
}
2025-02-20 14:22:38 +01:00
static void test_wordsplit_quote_detection_nonnested(void)
{
t_wordlist *words;
words = minishell_wordsplit("echo 'single quotes' here \"double quotes\" here");
assert_strequal("echo", wordlist_get(words, 0)->word);
assert(0 == wordlist_get(words, 0)->flags);
assert_strequal("'single quotes'", wordlist_get(words, 1)->word);
assert(W_QUOTED == wordlist_get(words, 1)->flags);
assert_strequal("here", wordlist_get(words, 2)->word);
assert(0 == wordlist_get(words, 2)->flags);
assert_strequal("\"double quotes\"", wordlist_get(words, 3)->word);
assert((W_QUOTED | W_DQUOTE) == wordlist_get(words, 3)->flags);
assert_strequal("here", wordlist_get(words, 4)->word);
assert(0 == wordlist_get(words, 4)->flags);
wordlist_destroy(words);
}
2025-02-20 14:22:38 +01:00
static void test_wordsplit_quote_detection_nested_double_in_simple(void)
{
t_wordlist *words;
char *str;
str = "'these are single quotes \"with double\" inside'";
words = minishell_wordsplit(str);
assert_strequal(str, wordlist_get(words, 0)->word);
assert(W_QUOTED == wordlist_get(words, 0)->flags);
wordlist_destroy(words);
str = "'\"these are single quotes with double inside\"'";
words = minishell_wordsplit(str);
assert_strequal(str, wordlist_get(words, 0)->word);
assert(W_QUOTED == wordlist_get(words, 0)->flags);
wordlist_destroy(words);
}
static void test_wordsplit_quote_detection_nested_single_in_double(void)
{
t_wordlist *words;
char *str;
str = "\"these are double quotes 'with single' inside\"";
words = minishell_wordsplit(str);
assert_strequal(str, wordlist_get(words, 0)->word);
assert((W_QUOTED | W_DQUOTE) == wordlist_get(words, 0)->flags);
wordlist_destroy(words);
str = "\"'these are double quotes with single inside'\"";
words = minishell_wordsplit(str);
assert_strequal(str, wordlist_get(words, 0)->word);
assert((W_QUOTED | W_DQUOTE) == wordlist_get(words, 0)->flags);
wordlist_destroy(words);
}
int main(void) {
test_wordsplit_singleword();
test_wordsplit_singleword_with_blanks();
2025-02-14 15:44:10 +01:00
test_wordsplit_multiword();
2025-02-14 17:50:56 +01:00
test_wordsplit_multiword_with_single_quotes();
2025-02-17 13:51:08 +01:00
test_wordsplit_multiword_with_double_quotes();
2025-02-17 14:00:01 +01:00
test_wordsplit_mixed_single_in_double();
test_wordsplit_mixed_double_in_single();
test_wordsplit_mixed_broken();
test_wordsplit_unclosed_single();
test_wordsplit_unclosed_double();
2025-02-17 16:14:14 +01:00
test_wordsplit_operator_word();
test_wordsplit_all_operators();
test_wordsplit_operator_combining();
test_wordsplit_var_substitution();
test_wordsplit_var_substitution_quotes();
2025-02-20 14:22:38 +01:00
test_wordsplit_quote_detection_nonnested();
test_wordsplit_quote_detection_nested_double_in_simple();
test_wordsplit_quote_detection_nested_single_in_double();
return (0);
}