mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
envp_get_val: add tests + make it more consistent with envp_get_key
In particular, this is what is now changed: - if line is null or empty, return null - if line contains no =, return an empty string This way, if non-null is returned, that means that line was valid. If null is returned, there is either an error with line, or malloc failed.
This commit is contained in:
parent
af90bec318
commit
ab6a4d6368
2 changed files with 35 additions and 20 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/18 15:11:14 by khais #+# #+# */
|
||||
/* Updated: 2025/02/18 15:33:36 by khais ### ########.fr */
|
||||
/* Updated: 2025/02/18 16:12:23 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -14,19 +14,23 @@
|
|||
#include "libft.h"
|
||||
#include <assert.h>
|
||||
|
||||
static void test_envp_get_key(char *line, char *expected_key)
|
||||
static void test_envp_parsing(char *line, char *expected_key, char *expected_value)
|
||||
{
|
||||
char *value = envp_get_key(line);
|
||||
ft_dprintf(STDERR_FILENO, "for envp value '%s', expecting key to eq '%s', and got '%s'\n", line, expected_key, value);
|
||||
assert(expected_key == value || ft_strcmp(expected_key, value) == 0);
|
||||
free(value);
|
||||
char *got_key = envp_get_key(line);
|
||||
char *got_value = envp_get_val(line);
|
||||
ft_dprintf(STDERR_FILENO, "for envp value '%s', expecting key to eq '%s', and got '%s'\n", line, expected_key, got_key);
|
||||
assert(expected_key == got_key || ft_strcmp(expected_key, got_key) == 0);
|
||||
ft_dprintf(STDERR_FILENO, "for envp value '%s', expecting value to eq '%s', and got '%s'\n", line, expected_value, got_value);
|
||||
assert(expected_value == got_value || ft_strcmp(expected_value, got_value) == 0);
|
||||
free(got_key);
|
||||
free(got_value);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
test_envp_get_key("SHELL=/bin/fish", "SHELL");
|
||||
test_envp_get_key("=/bin/fish", "");
|
||||
test_envp_get_key(NULL, NULL);
|
||||
test_envp_get_key("", NULL);
|
||||
test_envp_get_key("VARNAME", "VARNAME");
|
||||
test_envp_parsing("SHELL=/bin/fish", "SHELL", "/bin/fish");
|
||||
test_envp_parsing("=/bin/fish", "", "/bin/fish");
|
||||
test_envp_parsing(NULL, NULL, NULL);
|
||||
test_envp_parsing("", NULL, NULL);
|
||||
test_envp_parsing("VARNAME", "VARNAME", "");
|
||||
return (0);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue