diff --git a/tests/unit/expansion/expand.c b/tests/unit/expansion/expand.c index cb84c80..fd4295d 100644 --- a/tests/unit/expansion/expand.c +++ b/tests/unit/expansion/expand.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "../../../src/expansion/expansion.h" #include "../../../src/utils/ast/ast.h" @@ -234,3 +235,43 @@ Test(expand, random) "RANDOM variable should expand to a value between 0 and 32767"); ast_free(&ast); } + +Test(expand, pid) +{ + char str[] = "$$"; + 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(); + vars_default(vars); + + struct ast_command *command2 = expand(ast_command, vars); + cr_assert_not_null(command2, "Expansion returned NULL"); + int pid = atoi((char *)command2->command->data); + cr_assert(pid == getpid(), + "$$ variable should expand to the pid of the process"); + ast_free(&ast); + hash_map_free(&vars); +} + +Test(expand, default_last_exit_code) +{ + char str[] = "$?"; + 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(); + vars_default(vars); + + struct ast_command *command2 = expand(ast_command, vars); + cr_assert_not_null(command2, "Expansion returned NULL"); + int code = atoi((char *)command2->command->data); + cr_assert(code == 0, + "$? variable should expand to the last exit code (default 0)"); + ast_free(&ast); + hash_map_free(&vars); +}