diff --git a/Makefile b/Makefile index c304aa6..c6ea97d 100644 --- a/Makefile +++ b/Makefile @@ -56,6 +56,7 @@ TESTS = tests/tenv.c \ tests/texecute.c \ tests/tcd.c \ tests/tsource.c \ + tests/techo.c \ COVERAGE = -lcriterion --coverage diff --git a/src/builtin/builtin_echo.c b/src/builtin/builtin_echo.c index e57e73a..5dab8c8 100644 --- a/src/builtin/builtin_echo.c +++ b/src/builtin/builtin_echo.c @@ -14,7 +14,11 @@ int builtin_echo(char **argv, env_t *env) { for (int i = 1; argv[i]; i++) { - printf("%s", argv[i]); + for (int j = 0; argv[i][j]; j++) { + if (argv[i][j] == '\\') + j++; + printf("%c", argv[i][j]); + } if (argv[i + 1]) printf(" "); } diff --git a/tests/techo.c b/tests/techo.c new file mode 100644 index 0000000..602867b --- /dev/null +++ b/tests/techo.c @@ -0,0 +1,42 @@ +/* +** EPITECH PROJECT, 2020 +** test_echo.c +** File description: +** techo +*/ + +#include +#include +#include "builtin.h" + +Test(echo, normal_message, .init = cr_redirect_stdout) +{ + env_t env = {}; + char *av[] = {"echo", "hello", NULL}; + builtin_echo(av, &env); + cr_assert_stdout_eq_str("hello\n"); +} + +Test(echo, with_simple_backslash, .init = cr_redirect_stdout) +{ + env_t env = {}; + char *av[] = {"echo", "\\n", NULL}; + builtin_echo(av, &env); + cr_assert_stdout_eq_str("n\n"); +} + +Test(echo, with_double_backslash, .init = cr_redirect_stdout) +{ + env_t env = {}; + char *av[] = {"echo", "\\\\n", NULL}; + builtin_echo(av, &env); + cr_assert_stdout_eq_str("\\n\n"); +} + +Test(echo, multiple_args, .init = cr_redirect_stdout) +{ + env_t env = {}; + char *av[] = {"echo", "Hello", "I\'m", "Marvin", NULL}; + builtin_echo(av, &env); + cr_assert_stdout_eq_str("Hello I\'m Marvin\n"); +} \ No newline at end of file