42sh/tests/unit/lexer/lexer_tests.c

62 lines
1.6 KiB
C
Raw Normal View History

#include <criterion/criterion.h>
#include <criterion/new/assert.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include "io_backend/io_backend.h"
#include "lexer/lexer.h"
TestSuite(peek_token);
Test(peek_token, basic_empty)
{
// simulates input
char command[] = "";
struct iob_context context = { IOB_MODE_CMD, command };
iob_init(&context);
struct lexer_context ctx = { .end_previous_token = NULL,
.remaining_chars = 0,
.only_digits = false,
.previous_token = NULL,
.current_token = NULL };
// test
struct token *tok = peek_token(&ctx);
// expected
enum token_type type_expected = TOKEN_EOF;
char *string_expected = NULL;
cr_assert(tok->data == string_expected);
cr_assert(type_expected == tok->type);
free_token(&tok);
}
Test(peek_token, basic_word)
{
// simulates input
char command[] = "hello";
struct iob_context context = { IOB_MODE_CMD, command };
iob_init(&context);
struct lexer_context ctx = { .end_previous_token = NULL,
.remaining_chars = 0,
.only_digits = false,
.previous_token = NULL,
.current_token = NULL };
// test
struct token *tok = peek_token(&ctx);
// expected
enum token_type type_expected = TOKEN_WORD;
char string_expected[] = "hello";
cr_assert(eq(str, string_expected, tok->data));
cr_assert(type_expected == tok->type);
free_token(&tok);
}