diff options
Diffstat (limited to '42sh/tests/unit/tests_parser.c')
| -rw-r--r-- | 42sh/tests/unit/tests_parser.c | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/42sh/tests/unit/tests_parser.c b/42sh/tests/unit/tests_parser.c new file mode 100644 index 0000000..8d0f84c --- /dev/null +++ b/42sh/tests/unit/tests_parser.c @@ -0,0 +1,147 @@ +#include <criterion/criterion.h> //TODO Include the 2 libs of criterion +#include <stddef.h> +#include <stdio.h> +#include <stdlib.h> + +#include "ast/ast.h" +#include "lexer/expansion.h" +#include "lexer/lexer.h" +#include "lexer/token.h" +#include "utils/utils.h" + +TestSuite(ParserBasic); + +Test(ParserBasic, onlyone) +{ + struct string *input = string_create("echo"); + struct lexer *test = lexer_new(input); + + struct ast *root = parse(lexer); + + cr_expect(root->type == AST_LIST); + + ast_free(root); + lexer_free(test); +} + +/* +Test(ParserBasic, easytricky) +{ + struct string *input = string_create("\'echo\' H\'el\'lo World \n test"); + struct lexer *l = lexer_new(input); + + struct token t = lexer_pop(l); + + cr_expect_eq(t.type, TOKEN_WORD); + cr_expect_str_eq(t.value->data, "echo"); + + t = lexer_pop(l); + + cr_expect_eq(t.type, TOKEN_WORD); + cr_expect_str_eq(t.value->data, "Hello"); + + t = lexer_pop(l); + + cr_expect_eq(t.type, TOKEN_WORD); + cr_expect_str_eq(t.value->data, "World"); + + t = lexer_pop(l); + + cr_expect_eq(t.type, TOKEN_NEWLINE); + + t = lexer_pop(l); + + cr_expect_eq(t.type, TOKEN_WORD); + cr_expect_str_eq(t.value->data, "test"); + + lexer_free(l); + string_free(input); +} + +Test(ParserBasic, harderdaddy) +{ + struct string *input = string_create("echo\'\' H\\'ello World\\' \n test"); + struct lexer *l = lexer_new(input); + + struct token t = lexer_pop(l); + + cr_expect_eq(t.type, TOKEN_WORD); + cr_expect_str_eq(t.value->data, "echo"); + + t = lexer_pop(l); + + cr_expect_eq(t.type, TOKEN_WORD); + cr_expect_str_eq(t.value->data, "H\'ello World\'"); + + t = lexer_pop(l); + + cr_expect_eq(t.type, TOKEN_NEWLINE); + + t = lexer_pop(l); + + cr_expect_eq(t.type, TOKEN_WORD); + cr_expect_str_eq(t.value->data, "test"); + + lexer_free(l); + string_free(input); +} + +Test(ParserBasic, routine) +{ + struct string *input = string_create("i'f' 't''r''u''e'; 'then'\necho ok;\nelse echo \\'ko\\';"); + struct lexer *l = lexer_new(input); + + struct token t = lexer_pop(l); + + cr_expect_eq(t.type, TOKEN_IF); + + t = lexer_pop(l); + + cr_expect_eq(t.type, TOKEN_WORD); + cr_expect_str_eq(t.value->data, "true"); + + t = lexer_pop(l); + cr_expect_eq(t.type, TOKEN_SEMICOLON); + + t = lexer_pop(l); + cr_expect_eq(t.type, TOKEN_THEN); + + t = lexer_pop(l); + cr_expect_eq(t.type, TOKEN_NEWLINE); + + t = lexer_pop(l); + + cr_expect_eq(t.type, TOKEN_WORD); + cr_expect_str_eq(t.value->data, "echo"); + + t = lexer_pop(l); + + cr_expect_eq(t.type, TOKEN_WORD); + cr_expect_str_eq(t.value->data, "ok"); + + t = lexer_pop(l); + cr_expect_eq(t.type, TOKEN_SEMICOLON); + + t = lexer_pop(l); + cr_expect_eq(t.type, TOKEN_NEWLINE); + + t = lexer_pop(l); + + cr_expect_eq(t.type, TOKEN_ELSE); + + t = lexer_pop(l); + + cr_expect_eq(t.type, TOKEN_WORD); + cr_expect_str_eq(t.value->data, "echo"); + + t = lexer_pop(l); + + cr_expect_eq(t.type, TOKEN_WORD); + cr_expect_str_eq(t.value->data, "\'ko\'"); + + t = lexer_pop(l); + cr_expect_eq(t.type, TOKEN_SEMICOLON); + + lexer_free(l); + string_free(input); +}*\ |
