minishell/tests/expansion.c
Jérôme Guélen e348040ea4
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.
2025-03-21 10:07:32 +01:00

109 lines
3.4 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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);
}