From 10b1ac6711eda1cb8d89c1893e40bc91b3d8ec30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Fri, 21 Mar 2025 14:06:33 +0100 Subject: [PATCH] expansion: add some more test cases --- tests/Makefile | 2 +- tests/expansion.c | 65 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/tests/Makefile b/tests/Makefile index 54dc2cb..5c35dbd 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,12 +1,12 @@ # make gets confused if a file with the same name exists in the sources, so some # file are prefixed with test_ rawtests = \ + expansion \ test_here_doc \ test_wordlist_idx \ test_redirection_parsing \ quote_removal \ cmdlist_use_after_free \ - expansion \ metacharacters \ parse_command_lists \ parse_pipelines \ diff --git a/tests/expansion.c b/tests/expansion.c index d3e446a..c4933ae 100644 --- a/tests/expansion.c +++ b/tests/expansion.c @@ -5,8 +5,8 @@ /* +:+ +:+ +:+ */ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2025/03/20 14:51/50 by khais #+# #+# */ -/* Updated: 2025/03/20 16:22:15 by jguelen ### ########.fr */ +/* Created: 2025/03/21 14:06/21 by khais #+# #+# */ +/* Updated: 2025/03/21 14:06:21 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,6 +35,16 @@ static t_worddesc *create_single_word(char *str) ** Test file for the different expansion/substitution types of minishell. */ +static void test_replace_in_str_insert(void) +{ + char *line; + + // insertion is not supported, we must replace at least one char + line = replace_in_str("le canari qui fait cuicui", 3, 3, "petit "); + assert_strequal("le petit anari qui fait cuicui", line); + free(line); +} + static void test_insert_instr(void) { char *line; @@ -54,6 +64,10 @@ static void test_insert_instr(void) line = replace_in_str("le canari qui fait cuicui", 2, 2, " petit "); assert_strequal("le petit canari qui fait cuicui", line); free(line); + // premier charactere debut du remplacement + line = replace_in_str("le petit canari qui fait cuicui", 0, 1, "Le"); + assert_strequal("Le petit canari qui fait cuicui", line); + free(line); do_leak_check(); } @@ -62,7 +76,7 @@ static void test_insert_instr(void) ** this behavior at any point if you would rather we ignored them and therefore ** did not replace those. */ -void test_env_variable_expansion(void) +static void test_env_variable_expansion(void) { t_wordlist *list; t_minishell *app; @@ -89,6 +103,46 @@ void test_env_variable_expansion(void) do_leak_check(); } +static void test_env_var_expansion_with_invalid_ident(void) +{ + t_wordlist *list; + t_minishell *app; + char *value; + char *key; + + value = ft_strdup("value"); + key = ft_strdup("VAR"); + app = ft_calloc(1, sizeof(t_minishell)); + app->env = env_set_entry(&(app->env), key, value); + list = minishell_wordsplit("$''VAR"); + list = wordlist_var_expansion(list, app); + assert_strequal("''VAR", list->word->word); + wordlist_destroy(list); + env_destroy(app->env); + free(app); + do_leak_check(); +} + +static void test_env_var_expansion_with_trailing_dollar(void) +{ + t_wordlist *list; + t_minishell *app; + char *value; + char *key; + + value = ft_strdup("value"); + key = ft_strdup("VAR"); + app = ft_calloc(1, sizeof(t_minishell)); + app->env = env_set_entry(&(app->env), key, value); + list = minishell_wordsplit("$VAR$"); + list = wordlist_var_expansion(list, app); + assert_strequal("value$", list->word->word); + wordlist_destroy(list); + env_destroy(app->env); + free(app); + do_leak_check(); +} + static void test_cmd_path_expansion(void) { t_minishell *app; @@ -185,7 +239,7 @@ static void test_filename_star_expansion(void) do_leak_check(); } -void simple_sub_test(void) +static void simple_sub_test(void) { t_wordlist *list; t_minishell *app; @@ -207,6 +261,9 @@ void simple_sub_test(void) int main(void) { + test_env_var_expansion_with_trailing_dollar(); + test_env_var_expansion_with_invalid_ident(); + test_replace_in_str_insert(); if (chdir("./expand_test") == -1) assert("chdir failure" && false); simple_sub_test();