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

101 lines
2 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 "lexer/lexer.h"
#include "io_backend/io_backend.h"
TestSuite(peek_token);
Test(peek_token, basic_empty)
{
// simulates input
char command[] = "";
struct iob_context context =
{
IOB_MODE_CMD,
command
};
iob_init(&context);
// test
struct token *tok = peek_token();
// expected
enum token_type type_expected = TOKEN_EOF;
char *string_expected = NULL;
cr_assert(eq(str, string_expected, tok->data));
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);
// test
struct token *tok = peek_token();
// 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);
}
Test(peek_token, basic_words)
{
// simulates input
char command[] = "echo hello there";
struct iob_context context =
{
IOB_MODE_CMD,
command
};
iob_init(&context);
// ======= echo
struct token *tok = peek_token();
enum token_type type_expected = TOKEN_WORD;
char string_expected[] = "echo";
cr_assert(eq(str, string_expected, tok->data));
cr_assert(type_expected == tok->type);
free_token(&tok);
// ======= hello
tok = peek_token();
char string_expected2[] = "hello";
cr_assert(eq(str, string_expected2, tok->data));
cr_assert(type_expected == tok->type);
free_token(&tok);
// ======= there
tok = peek_token();
char string_expected3[] = "there";
cr_assert(eq(str, string_expected3, tok->data));
cr_assert(type_expected == tok->type);
free_token(&tok);
}