2026-01-08 15:58:24 +01:00
|
|
|
#include <criterion/criterion.h>
|
|
|
|
|
#include <criterion/new/assert.h>
|
2026-01-08 17:03:52 +01:00
|
|
|
#include <stddef.h>
|
2026-01-08 15:58:24 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
2026-01-08 17:03:52 +01:00
|
|
|
#include <sys/types.h>
|
2026-01-08 15:58:24 +01:00
|
|
|
|
|
|
|
|
#include "lexer/lexer.h"
|
2026-01-17 17:53:37 +01:00
|
|
|
#include "io_backend/io_backend.h"
|
2026-01-08 15:58:24 +01:00
|
|
|
|
2026-01-17 17:53:37 +01:00
|
|
|
TestSuite(peek_token);
|
2026-01-08 15:58:24 +01:00
|
|
|
|
2026-01-17 17:53:37 +01:00
|
|
|
Test(peek_token, basic_empty)
|
2026-01-08 15:58:24 +01:00
|
|
|
{
|
2026-01-17 17:53:37 +01:00
|
|
|
// simulates input
|
|
|
|
|
char command[] = "";
|
|
|
|
|
struct iob_context context =
|
|
|
|
|
{
|
|
|
|
|
IOB_MODE_CMD,
|
|
|
|
|
command
|
|
|
|
|
};
|
|
|
|
|
iob_init(&context);
|
2026-01-08 15:58:24 +01:00
|
|
|
|
2026-01-17 17:53:37 +01:00
|
|
|
// test
|
|
|
|
|
struct token *tok = peek_token();
|
2026-01-08 15:58:24 +01:00
|
|
|
|
2026-01-17 17:53:37 +01:00
|
|
|
// 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);
|
2026-01-08 15:58:24 +01:00
|
|
|
|
2026-01-17 17:53:37 +01:00
|
|
|
free_token(&tok);
|
2026-01-08 15:58:24 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-17 17:53:37 +01:00
|
|
|
Test(peek_token, basic_word)
|
2026-01-08 15:58:24 +01:00
|
|
|
{
|
2026-01-17 17:53:37 +01:00
|
|
|
// simulates input
|
|
|
|
|
char command[] = "hello";
|
|
|
|
|
struct iob_context context =
|
|
|
|
|
{
|
|
|
|
|
IOB_MODE_CMD,
|
|
|
|
|
command
|
|
|
|
|
};
|
|
|
|
|
iob_init(&context);
|
2026-01-08 15:58:24 +01:00
|
|
|
|
2026-01-17 17:53:37 +01:00
|
|
|
// test
|
|
|
|
|
struct token *tok = peek_token();
|
2026-01-08 15:58:24 +01:00
|
|
|
|
2026-01-17 17:53:37 +01:00
|
|
|
// expected
|
|
|
|
|
enum token_type type_expected = TOKEN_WORD;
|
|
|
|
|
char string_expected[] = "hello";
|
2026-01-08 15:58:24 +01:00
|
|
|
|
2026-01-17 17:53:37 +01:00
|
|
|
cr_assert(eq(str, string_expected, tok->data));
|
|
|
|
|
cr_assert(type_expected == tok->type);
|
|
|
|
|
free_token(&tok);
|
2026-01-08 15:58:24 +01:00
|
|
|
}
|
2026-01-17 17:53:37 +01:00
|
|
|
Test(peek_token, basic_words)
|
2026-01-08 15:58:24 +01:00
|
|
|
{
|
2026-01-17 17:53:37 +01:00
|
|
|
// 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);
|
2026-01-08 15:58:24 +01:00
|
|
|
}
|