summaryrefslogtreecommitdiff
path: root/42sh/tests/unit/tests_lexer.c
diff options
context:
space:
mode:
authorMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:07:58 +0200
committerMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:07:58 +0200
commit967be9e750221ab2ab783f95df79bb26d290a45e (patch)
tree6802900a5e975f9f68b169f0f503f040056d6952 /42sh/tests/unit/tests_lexer.c
add: added projectsHEADmain
Diffstat (limited to '42sh/tests/unit/tests_lexer.c')
-rw-r--r--42sh/tests/unit/tests_lexer.c288
1 files changed, 288 insertions, 0 deletions
diff --git a/42sh/tests/unit/tests_lexer.c b/42sh/tests/unit/tests_lexer.c
new file mode 100644
index 0000000..b7617aa
--- /dev/null
+++ b/42sh/tests/unit/tests_lexer.c
@@ -0,0 +1,288 @@
+#include <criterion/criterion.h> //TODO Include the 2 libs of criterion
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "lexer/expansion.h"
+#include "lexer/lexer.h"
+#include "lexer/token.h"
+#include "utils/libstring.h"
+
+TestSuite(Trying);
+
+Test(Trying, Hello_World)
+{
+ printf("Hello World!\n");
+ cr_expect_eq(1, 0);
+}
+
+TestSuite(LexerBasic);
+
+Test(LexerBasic, New)
+{
+ struct string *input = string_create("echo Hello World !");
+ struct lexer *test = lexer_new(input);
+ struct string *txt = test->input;
+
+ cr_expect_str_eq("echo Hello World !", txt->data);
+ cr_expect(strlen("echo Hello World !") == txt->length,
+ "Expected 17, got %zu\n", txt->length);
+
+ lexer_free(test);
+ string_free(input);
+}
+
+Test(LexerBasic, SimplePeek)
+{
+ struct string *input = string_create("echo Hello World !");
+ struct lexer *test = lexer_new(input);
+
+ struct token head = lexer_peek(test);
+
+ cr_expect_eq(TOKEN_WORD, head.type);
+ cr_expect_str_eq(head.value->data, "echo");
+
+ lexer_pop(test);
+
+ head = lexer_pop(test);
+
+ cr_expect_eq(TOKEN_WORD, head.type);
+ cr_expect_str_eq(head.value->data, "Hello");
+
+ head = lexer_pop(test);
+
+ cr_expect_eq(TOKEN_WORD, head.type);
+ cr_expect_str_eq(head.value->data, "World");
+
+ head = lexer_pop(test);
+
+ cr_expect_eq(head.type, TOKEN_NEG);
+
+ head = lexer_peek(test);
+ cr_expect_eq(TOKEN_EOF, head.type);
+
+ lexer_free(test);
+}
+
+Test(LexerBasic, PeekComment)
+{
+ struct string *input =
+ string_create("echo; Hello # zoubi zoubi le commentaire \n World !");
+ struct lexer *test = lexer_new(input);
+
+ struct token head = lexer_peek(test);
+
+ cr_expect_eq(TOKEN_WORD, head.type);
+ cr_expect_str_eq(head.value->data, "echo");
+
+ lexer_pop(test);
+
+ head = lexer_pop(test);
+ cr_expect_eq(head.type, TOKEN_SEMICOLON);
+
+ head = lexer_pop(test);
+ cr_expect_eq(TOKEN_WORD, head.type);
+ cr_expect_str_eq(head.value->data, "Hello");
+
+ head = lexer_pop(test);
+ cr_expect_eq(head.type, TOKEN_NEWLINE);
+
+ head = lexer_pop(test);
+
+ cr_expect_eq(TOKEN_WORD, head.type);
+ cr_expect_str_eq(head.value->data, "World");
+
+ head = lexer_pop(test);
+
+ cr_expect_eq(head.type, TOKEN_NEG);
+
+ head = lexer_peek(test);
+ cr_expect_eq(TOKEN_EOF, head.type);
+
+ lexer_free(test);
+}
+
+Test(LexerBasic, HardPeek)
+{
+ struct string *input = string_create("echo; Hello\n World !");
+ struct lexer *test = lexer_new(input);
+
+ struct token head = lexer_peek(test);
+
+ cr_expect_eq(TOKEN_WORD, head.type);
+ cr_expect_str_eq(head.value->data, "echo");
+
+ lexer_pop(test);
+
+ head = lexer_pop(test);
+ cr_expect_eq(head.type, TOKEN_SEMICOLON);
+
+ head = lexer_pop(test);
+ cr_expect_eq(TOKEN_WORD, head.type);
+ cr_expect_str_eq(head.value->data, "Hello");
+
+ head = lexer_pop(test);
+ cr_expect_eq(head.type, TOKEN_NEWLINE);
+
+ head = lexer_pop(test);
+
+ cr_expect_eq(TOKEN_WORD, head.type);
+ cr_expect_str_eq(head.value->data, "World");
+
+ head = lexer_pop(test);
+
+ cr_expect_eq(head.type, TOKEN_NEG);
+
+ head = lexer_peek(test);
+ cr_expect_eq(TOKEN_EOF, head.type);
+
+ lexer_free(test);
+}
+
+Test(LexerBasic, 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(LexerBasic, 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(LexerBasic, 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);
+}
+
+Test(LexerBasic, redir)
+{
+ struct string *input = string_create("echo Hello >> test.txt");
+ struct lexer *test = lexer_new(input);
+
+ struct token t = lexer_pop(test);
+ cr_expect_eq(t.type, TOKEN_WORD);
+ cr_expect_str_eq(t.value->data, "echo");
+
+ t = lexer_pop(test);
+
+ cr_expect_eq(t.type, TOKEN_WORD);
+ cr_expect_str_eq(t.value->data, "Hello");
+
+ t = lexer_pop(test);
+ cr_expect_eq(t.type, TOKEN_REDIR);
+ cr_expect_str_eq(t.value->data, ">>");
+
+ t = lexer_pop(test);
+ cr_expect_eq(t.type, TOKEN_WORD);
+ cr_expect_str_eq(t.value->data, "test.txt");
+
+ lexer_free(test);
+ string_free(input);
+}