Subst: A norm NON-compliant and incomplete version

The code does not update flags yet and is to be refactored to conform to the
norm.
Tests required but needing a new version of the wordsplitting code.
This commit is contained in:
Jérôme Guélen 2025-02-28 18:48:30 +01:00
parent 2dd54e2827
commit e348040ea4
No known key found for this signature in database
10 changed files with 328 additions and 91 deletions

View file

@ -6,6 +6,7 @@ rawtests = \
test_redirection_parsing \
quote_removal \
cmdlist_use_after_free \
expansion \
metacharacters \
parse_command_lists \
parse_pipelines \

109
tests/expansion.c Normal file
View file

@ -0,0 +1,109 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* expansion.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/23 15:00:18 by jguelen #+# #+# */
/* Updated: 2025/02/28 14:23:30 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include <assert.h>
#include <stdbool.h>
#include "testutil.h"
#include "../src/subst/subst.h"
#include "../src/subst/replace_substr.h"
#include "../../env/env.h"
#include "../../env/env_manip.h"
/*
** Test file for the different expansion/substitution types of minishell.
*/
static void test_insert_instr(void)
{
char *line;
line = replace_in_str("abcdefghijk", 1, 4, "souris");
assert_strequal("asourisfghijk", line);
free(line);
line = replace_in_str("abcdefgh" , 2, 2, "non ce n'est pas ma faute");
assert_strequal("abnon ce n'est pas ma fautedefgh", line);
free(line);
line = replace_in_str("le petit canari qui fait cuicui", 3, 8, "");
assert_strequal("le canari qui fait cuicui", line);
free(line);
line = replace_in_str("le petit canari qui fait cuicui", 3, 8, NULL);
assert_strequal("le canari qui fait cuicui", line);
free(line);
line = replace_in_str("le canari qui fait cuicui", 2, 2, " petit ");
assert_strequal("le petit canari qui fait cuicui", line);
free(line);
}
static void test_env_variable_expansion(void)
{
char *token;
char *tk;
t_env *env;
token = ft_strdup("$USER");
if (!token)
assert("ft_strdup failed" && false);
env = NULL;
env = env_set_entry(&env, "USER", "jguelen");
if (env == NULL)
assert("malloc failed" && false);
env = env_set_entry(&env, "_canard", "coing coing");
if (!env)
assert("malloc failed: slipped on a duck" && false);
tk = word_var_expansion(&token, env);
if (!tk)
assert("internal word_var_expansion failure" && false);
assert_strequal("jguelen", tk);
free(token);
token = ft_strdup("\"$_caneton\"");
if (!token)
assert("ft_strdup failed" && false);
tk = word_var_expansion(&token, env);
if (!tk)
assert("internal word_var_expansion failure" && false);
assert_strequal("\"\"", tk);
free(token);
token = ft_strdup("$_canard$USER$''$USER\"\"\"$_canard\"$");
if (!token)
assert("ft_strdup failed" && false);
tk = word_var_expansion(token, env);
if (!tk)
assert("internal word_var_expansion failure" && false);
assert_strequal("coing coingjguelen''jguelencoing coing$", tk);
free(token);
token = ft_strdup("$_can'a'rd");
if (!token)
assert("ft_strdup failed" && false);
tk = word_var_expansion(token, env);
if (!tk)
assert("internal word_var_expansion failure" && false);
assert_strequal("'a'rd", tk);
free(token);
env_destroy(env);
}
static void test_filename_path_expansion(void)
{
}
static void test_filename_star_expansion(void)
{
}
int main(void)
{
test_insert_instr();
test_env_variable_expansion();
test_filename_path_expansion();
test_filename_star_expansion();
return (0);
}