#include //TODO Include the 2 libs of criterion #include #include #include #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); }