feat(expansion): expand_var and expand
This commit is contained in:
parent
a16712e802
commit
34c741d86e
4 changed files with 276 additions and 14 deletions
220
tests/unit/expansion/expand.c
Normal file
220
tests/unit/expansion/expand.c
Normal file
|
|
@ -0,0 +1,220 @@
|
|||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <criterion/criterion.h>
|
||||
#include <criterion/new/assert.h>
|
||||
#include <criterion/redirect.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "../../../src/expansion/expansion.h"
|
||||
#include "../../../src/utils/ast/ast.h"
|
||||
#include "../../../src/utils/hash_map/hash_map.h"
|
||||
#include "../../../src/utils/vars/vars.h"
|
||||
|
||||
TestSuite(expand);
|
||||
|
||||
Test(expand, no_expansion)
|
||||
{
|
||||
char str[] = "echo something";
|
||||
char *str_heap = strdup(str);
|
||||
struct list *list = list_append(NULL, str_heap);
|
||||
struct ast *ast = ast_create_command(list);
|
||||
struct ast_command *ast_command = ast_get_command(ast);
|
||||
|
||||
struct ast_command *command2 = expand(ast_command, NULL);
|
||||
cr_assert_not_null(command2, "Expansion returned NULL");
|
||||
cr_assert_str_eq((char *)command2->command->data, "echo something",
|
||||
"String without variables should remain unchanged");
|
||||
ast_free(&ast);
|
||||
}
|
||||
|
||||
Test(expand, single_quotes_no_expansion)
|
||||
{
|
||||
char str[] = "echo '$VAR'";
|
||||
char *str_heap = strdup(str);
|
||||
struct list *list = list_append(NULL, str_heap);
|
||||
struct ast *ast = ast_create_command(list);
|
||||
struct ast_command *ast_command = ast_get_command(ast);
|
||||
|
||||
struct hash_map *vars = vars_init();
|
||||
set_var_copy(vars, "VAR", "expanded");
|
||||
|
||||
struct ast_command *command2 = expand(ast_command, vars);
|
||||
cr_assert_not_null(command2, "Expansion returned NULL");
|
||||
cr_assert_str_eq((char *)command2->command->data, "echo $VAR",
|
||||
"Variable should not expand inside single quotes");
|
||||
ast_free(&ast);
|
||||
hash_map_free(vars);
|
||||
}
|
||||
|
||||
Test(expand, single_dollar)
|
||||
{
|
||||
char str[] = "echo $ sign";
|
||||
char *str_heap = strdup(str);
|
||||
struct list *list = list_append(NULL, str_heap);
|
||||
struct ast *ast = ast_create_command(list);
|
||||
struct ast_command *ast_command = ast_get_command(ast);
|
||||
|
||||
struct hash_map *vars = vars_init();
|
||||
set_var_copy(vars, "VAR", "expanded");
|
||||
|
||||
struct ast_command *command2 = expand(ast_command, vars);
|
||||
cr_assert_not_null(command2, "Expansion returned NULL");
|
||||
cr_assert_str_eq((char *)command2->command->data, "echo $ sign",
|
||||
"Variable should not expand inside single quotes");
|
||||
ast_free(&ast);
|
||||
hash_map_free(vars);
|
||||
}
|
||||
|
||||
Test(expand, empty_braces_no_expansion)
|
||||
{
|
||||
char str[] = "echo ${}";
|
||||
char *str_heap = strdup(str);
|
||||
struct list *list = list_append(NULL, str_heap);
|
||||
struct ast *ast = ast_create_command(list);
|
||||
struct ast_command *ast_command = ast_get_command(ast);
|
||||
|
||||
struct hash_map *vars = vars_init();
|
||||
set_var_copy(vars, "VAR", "expanded");
|
||||
|
||||
struct ast_command *command2 = expand(ast_command, vars);
|
||||
cr_assert_null(command2, "Expansion should fail on empty braces");
|
||||
ast_free(&ast);
|
||||
hash_map_free(vars);
|
||||
}
|
||||
|
||||
Test(expand, basic_expansion)
|
||||
{
|
||||
char str[] = "echo $VAR";
|
||||
char *str_heap = strdup(str);
|
||||
struct list *list = list_append(NULL, str_heap);
|
||||
struct ast *ast = ast_create_command(list);
|
||||
struct ast_command *ast_command = ast_get_command(ast);
|
||||
|
||||
struct hash_map *vars = vars_init();
|
||||
set_var_copy(vars, "VAR", "expanded");
|
||||
|
||||
struct ast_command *command2 = expand(ast_command, vars);
|
||||
cr_assert_not_null(command2, "Expansion returned NULL");
|
||||
cr_assert_str_eq((char *)command2->command->data, "echo expanded",
|
||||
"Variable should expand correctly");
|
||||
ast_free(&ast);
|
||||
hash_map_free(vars);
|
||||
}
|
||||
|
||||
Test(expand, multiple_expansion)
|
||||
{
|
||||
char str[] = "echo $VAR1 $VAR2 ${VAR3}";
|
||||
char *str_heap = strdup(str);
|
||||
struct list *list = list_append(NULL, str_heap);
|
||||
struct ast *ast = ast_create_command(list);
|
||||
struct ast_command *ast_command = ast_get_command(ast);
|
||||
|
||||
struct hash_map *vars = vars_init();
|
||||
set_var_copy(vars, "VAR1", "expanded");
|
||||
set_var_copy(vars, "VAR2", "values");
|
||||
set_var_copy(vars, "VAR3", "here");
|
||||
|
||||
struct ast_command *command2 = expand(ast_command, vars);
|
||||
cr_assert_not_null(command2, "Expansion returned NULL");
|
||||
cr_assert_str_eq((char *)command2->command->data,
|
||||
"echo expanded values here",
|
||||
"Multiple variables should expand correctly");
|
||||
ast_free(&ast);
|
||||
hash_map_free(vars);
|
||||
}
|
||||
|
||||
Test(expand, env_variable)
|
||||
{
|
||||
char str[] = "echo $MY_ENV_VAR";
|
||||
char *str_heap = strdup(str);
|
||||
struct list *list = list_append(NULL, str_heap);
|
||||
struct ast *ast = ast_create_command(list);
|
||||
struct ast_command *ast_command = ast_get_command(ast);
|
||||
|
||||
setenv("MY_ENV_VAR", "environment", 0);
|
||||
|
||||
struct ast_command *command2 = expand(ast_command, NULL);
|
||||
cr_assert_not_null(command2, "Expansion returned NULL");
|
||||
cr_assert_str_eq((char *)command2->command->data, "echo environment",
|
||||
"Environment variable should expand correctly");
|
||||
ast_free(&ast);
|
||||
}
|
||||
|
||||
Test(expand, undefined_variable)
|
||||
{
|
||||
char str[] = "echo $UNDEFINED";
|
||||
char *str_heap = strdup(str);
|
||||
struct list *list = list_append(NULL, str_heap);
|
||||
struct ast *ast = ast_create_command(list);
|
||||
struct ast_command *ast_command = ast_get_command(ast);
|
||||
|
||||
struct hash_map *vars = vars_init();
|
||||
|
||||
struct ast_command *command2 = expand(ast_command, vars);
|
||||
cr_assert_not_null(command2, "Expansion returned NULL");
|
||||
cr_assert_str_eq((char *)command2->command->data, "echo ",
|
||||
"Undefined variable should expand to empty string");
|
||||
ast_free(&ast);
|
||||
hash_map_free(vars);
|
||||
}
|
||||
|
||||
Test(expand, nested_expansion)
|
||||
{
|
||||
char str[] = "echo $B";
|
||||
char *str_heap = strdup(str);
|
||||
struct list *list = list_append(NULL, str_heap);
|
||||
struct ast *ast = ast_create_command(list);
|
||||
struct ast_command *ast_command = ast_get_command(ast);
|
||||
|
||||
struct hash_map *vars = vars_init();
|
||||
set_var_copy(vars, "A", "expanded");
|
||||
set_var_copy(vars, "B", "$A");
|
||||
|
||||
struct ast_command *command2 = expand(ast_command, vars);
|
||||
cr_assert_not_null(command2, "Expansion returned NULL");
|
||||
cr_assert_str_eq((char *)command2->command->data, "echo expanded",
|
||||
"Nested variable should expand correctly");
|
||||
ast_free(&ast);
|
||||
hash_map_free(vars);
|
||||
}
|
||||
|
||||
Test(expand, mixed_quotes_expansion)
|
||||
{
|
||||
char str[] = "echo \"$VAR1 and '$VAR2'\"";
|
||||
char *str_heap = strdup(str);
|
||||
struct list *list = list_append(NULL, str_heap);
|
||||
struct ast *ast = ast_create_command(list);
|
||||
struct ast_command *ast_command = ast_get_command(ast);
|
||||
|
||||
struct hash_map *vars = vars_init();
|
||||
set_var_copy(vars, "VAR1", "expanded");
|
||||
set_var_copy(vars, "VAR2", "not_expanded");
|
||||
|
||||
struct ast_command *command2 = expand(ast_command, vars);
|
||||
cr_assert_not_null(command2, "Expansion returned NULL");
|
||||
cr_assert_str_eq((char *)command2->command->data,
|
||||
"echo \"expanded and $VAR2\"",
|
||||
"Variable in double quotes should expand, while variable "
|
||||
"in single quotes should not");
|
||||
ast_free(&ast);
|
||||
hash_map_free(vars);
|
||||
}
|
||||
|
||||
Test(expand, adjacent_variables)
|
||||
{
|
||||
char str[] = "echo $VAR1$VAR2";
|
||||
char *str_heap = strdup(str);
|
||||
struct list *list = list_append(NULL, str_heap);
|
||||
struct ast *ast = ast_create_command(list);
|
||||
struct ast_command *ast_command = ast_get_command(ast);
|
||||
|
||||
struct hash_map *vars = vars_init();
|
||||
set_var_copy(vars, "VAR1", "hello");
|
||||
set_var_copy(vars, "VAR2", "world");
|
||||
|
||||
struct ast_command *command2 = expand(ast_command, vars);
|
||||
cr_assert_not_null(command2, "Expansion returned NULL");
|
||||
cr_assert_str_eq((char *)command2->command->data, "echo helloworld",
|
||||
"Adjacent variables should expand correctly");
|
||||
ast_free(&ast);
|
||||
hash_map_free(vars);
|
||||
}
|
||||
|
|
@ -6,11 +6,6 @@
|
|||
|
||||
TestSuite(parse_var_name);
|
||||
|
||||
// char *input = "$MY$VAR";
|
||||
// char *input = "$MY$VAR$";
|
||||
// char *input = "$MY$VAR${}";
|
||||
// char *input = "$MY$VAR${1}";
|
||||
|
||||
Test(parse_var_name, basic_variable)
|
||||
{
|
||||
char *input = "$MY_VAR";
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue