2025-02-28 18:48:30 +01:00
|
|
|
/* ************************************************************************** */
|
|
|
|
|
/* */
|
|
|
|
|
/* ::: :::::::: */
|
|
|
|
|
/* expansion.c :+: :+: :+: */
|
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
|
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
|
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
|
/* Created: 2025/02/23 15:00:18 by jguelen #+# #+# */
|
2025-03-01 17:03:15 +01:00
|
|
|
/* Updated: 2025/03/01 11:07:17 by jguelen ### ########.fr */
|
2025-02-28 18:48:30 +01:00
|
|
|
/* */
|
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
|
|
#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);
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-01 17:03:15 +01:00
|
|
|
//Test to be remade since structures changed in the function calls and returns.
|
2025-02-28 18:48:30 +01:00
|
|
|
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);
|
|
|
|
|
}
|