expansion: add some more test cases

This commit is contained in:
Khaïs COLIN 2025-03-21 14:06:33 +01:00
parent b714351d4d
commit 10b1ac6711
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
2 changed files with 62 additions and 5 deletions

View file

@ -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 \

View file

@ -5,8 +5,8 @@
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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();