maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
env_test.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* env_test.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/06/10 16:46:20 by dmdemirk #+# #+# */
9/* Updated: 2024/06/10 17:22:59 by dmdemirk ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include <assert.h>
14#include "libft.h"
15#include "env.h"
16#include "shell.h"
17#include <stdio.h>
18
19void env_tests(t_ms_data *data, char **envp);
20void test_init_env(t_ms_data *data, char **envp);
21void test_get_env(t_ms_data *data, char **envp);
22void test_set_env(t_ms_data *data, char **envp);
23void test_unset_env(t_ms_data *data);
24
25void env_tests(t_ms_data *data, char **envp)
26{
27 ft_printf("\nENV TESTS\n");
28 test_init_env(data, envp);
29 test_get_env(data, envp);
30 test_set_env(data, envp);
31 test_unset_env(data);
32 free_env(data->envp);
33}
34
35void test_init_env(t_ms_data *data, char **envp)
36{
37 char **test_envp;
38 int envp_count;
39 int test_envp_count;
40
41 data->envp = NULL;
42 init_env(&data->envp, envp);
43 test_envp = env_to_array(data->envp);
44 envp_count = 0;
45 while (envp[envp_count])
46 envp_count++;
47 test_envp_count = 0;
48 while (test_envp[test_envp_count])
49 test_envp_count++;
50 assert(envp_count == test_envp_count);
51 ft_printf("\033[0m");
52 ft_printf("\033[0;32m");
53 ft_printf("init_env -> OK\n");
54 ft_printf("\033[0m");
55}
56
57void test_get_env(t_ms_data *data, char **envp)
58{
59 char *value;
60
61 data->envp = NULL;
62 init_env(&data->envp, envp);
63 value = get_env(data->envp, "LOGNAME");
64 assert(value != NULL);
65 assert(ft_strcmp(value, getenv("LOGNAME")) == 0);
66 ft_printf("\033[0m");
67 ft_printf("\033[0;32m");
68 ft_printf("get_env -> OK\n");
69 ft_printf("\033[0m");
70}
71
72void test_set_env(t_ms_data *data, char **envp)
73{
74 char *value;
75
76 data->envp = NULL;
77 init_env(&data->envp, envp);
78 set_env(&data->envp, "TEST", "test");
79 value = get_env(data->envp, "TEST");
80 assert(value != NULL);
81 assert(ft_strcmp(value, "test") == 0);
82 ft_printf("\033[0m");
83 ft_printf("\033[0;32m");
84 ft_printf("set_env -> OK\n");
85 ft_printf("\033[0m");
86}
87
89{
90 char *value;
91
92 set_env(&data->envp, "TEST", "test");
93 value = get_env(data->envp, "TEST");
94 assert(value != NULL);
95 assert(ft_strcmp(value, "test") == 0);
96 unset_env(&data->envp, "TEST");
97 value = get_env(data->envp, "TEST");
98 assert(value == NULL);
99 ft_printf("\033[0m");
100 ft_printf("\033[0;32m");
101 ft_printf("unset_env -> OK\n");
102 ft_printf("\033[0m");
103}
int unset_env(t_env **env, const char *name)
Definition env.c:96
void set_env(t_env **env, const char *key, const char *value)
Definition env.c:73
char ** env_to_array(t_env *envp)
Definition utils.c:23
void init_env(t_env **data_envp, char **envp)
Definition env.c:24
void free_env(t_env *envp)
Definition utils.c:73
char * get_env(t_env *envp, const char *key)
Definition env.c:59
void test_set_env(t_ms_data *data, char **envp)
Definition env_test.c:72
void test_get_env(t_ms_data *data, char **envp)
Definition env_test.c:57
void env_tests(t_ms_data *data, char **envp)
Definition env_test.c:25
void test_unset_env(t_ms_data *data)
Definition env_test.c:88
void test_init_env(t_ms_data *data, char **envp)
Definition env_test.c:35
int ft_printf(const char *format,...)
Definition ft_printf.c:37
int ft_strcmp(const char *s1, const char *s2)
Definition ft_strcmp.c:24
t_env * envp
Definition shell.h:24