feat(vars): unit tests
This commit is contained in:
parent
c448776268
commit
e65c55f5c9
1 changed files with 85 additions and 0 deletions
85
tests/unit/utils/vars.c
Normal file
85
tests/unit/utils/vars.c
Normal file
|
|
@ -0,0 +1,85 @@
|
||||||
|
#define _POSIX_C_SOURCE 200809L
|
||||||
|
#include "../../../src/utils/vars/vars.h"
|
||||||
|
|
||||||
|
#include <criterion/criterion.h>
|
||||||
|
#include <criterion/new/assert.h>
|
||||||
|
#include <criterion/redirect.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "../../../src/utils/hash_map/hash_map.h"
|
||||||
|
#include "../../../src/utils/string_utils/string_utils.h"
|
||||||
|
|
||||||
|
TestSuite(utils_vars);
|
||||||
|
|
||||||
|
Test(utils_vars, init_free)
|
||||||
|
{
|
||||||
|
struct hash_map *map = vars_init();
|
||||||
|
cr_expect_not_null(map);
|
||||||
|
hash_map_free(&map);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(utils_vars, get_defaults)
|
||||||
|
{
|
||||||
|
struct hash_map *map = vars_init();
|
||||||
|
cr_expect_not_null(map);
|
||||||
|
cr_assert_str_eq(get_var(map, "?"), "0");
|
||||||
|
char int_str[11];
|
||||||
|
int_to_str((int)getpid(), int_str);
|
||||||
|
cr_assert_str_eq(get_var(map, "$"), int_str);
|
||||||
|
int_to_str((int)getuid(), int_str);
|
||||||
|
cr_assert_str_eq(get_var(map, "UID"), int_str);
|
||||||
|
|
||||||
|
hash_map_free(&map);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(utils_vars, set_vars)
|
||||||
|
{
|
||||||
|
struct hash_map *map = vars_init();
|
||||||
|
cr_expect_not_null(map);
|
||||||
|
set_var_copy(map, "key1", "value1");
|
||||||
|
cr_assert_str_eq(get_var(map, "key1"), "value1");
|
||||||
|
set_var_copy(map, "key2", "value2");
|
||||||
|
cr_assert_str_eq(get_var(map, "key2"), "value2");
|
||||||
|
|
||||||
|
hash_map_free(&map);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(utils_vars, get_env_vars)
|
||||||
|
{
|
||||||
|
struct hash_map *map = vars_init();
|
||||||
|
cr_expect_not_null(map);
|
||||||
|
cr_assert_eq(get_var_or_env(map, "ENV_TEST"), NULL);
|
||||||
|
setenv("ENV_TEST", "value1", 0);
|
||||||
|
cr_assert_str_eq(get_var_or_env(map, "ENV_TEST"), "value1");
|
||||||
|
setenv("ENV_TEST", "value2", 1);
|
||||||
|
cr_assert_str_eq(get_var_or_env(map, "ENV_TEST"), "value2");
|
||||||
|
|
||||||
|
hash_map_free(&map);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(utils_vars, set_vars_update)
|
||||||
|
{
|
||||||
|
struct hash_map *map = vars_init();
|
||||||
|
cr_expect_not_null(map);
|
||||||
|
set_var_copy(map, "key", "value1");
|
||||||
|
cr_assert_str_eq(get_var(map, "key"), "value1");
|
||||||
|
set_var_copy(map, "key", "value2");
|
||||||
|
cr_assert_str_eq(get_var(map, "key"), "value2");
|
||||||
|
|
||||||
|
hash_map_free(&map);
|
||||||
|
}
|
||||||
|
|
||||||
|
Test(utils_vars, set_vars_int)
|
||||||
|
{
|
||||||
|
struct hash_map *map = vars_init();
|
||||||
|
cr_expect_not_null(map);
|
||||||
|
set_var_int(map, "key1", 100);
|
||||||
|
cr_assert_str_eq(get_var(map, "key1"), "100");
|
||||||
|
set_var_int(map, "key1", 200);
|
||||||
|
cr_assert_str_eq(get_var(map, "key1"), "200");
|
||||||
|
set_var_int(map, "key2", 10);
|
||||||
|
cr_assert_str_eq(get_var(map, "key2"), "10");
|
||||||
|
|
||||||
|
hash_map_free(&map);
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue