From 2b5a8a428cbbdda8ef0efa7b2c77c100b8dbfa0c Mon Sep 17 00:00:00 2001 From: hachem Date: Sun, 30 Aug 2026 04:30:33 +0200 Subject: test: add lexer testing --- tests/lexer_test.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 tests/lexer_test.c (limited to 'tests/lexer_test.c') diff --git a/tests/lexer_test.c b/tests/lexer_test.c new file mode 100644 index 0000000..c5c9f14 --- /dev/null +++ b/tests/lexer_test.c @@ -0,0 +1,63 @@ +#include +#include + +#include "lexer/lexer.h" +#include "tests.h" + +static bool token_matches(struct Token token, enum TokenType type, const char* text) +{ + size_t length = strlen(text); + return token.type == type + && token.length == length + && memcmp(token.start, text, length) == 0; +} + +static void test_identifiers_and_integers(struct TestContext* context) +{ + struct Lexer lexer = create_lexer("rax 42"); + check(context, token_matches(scan_token(&lexer), TOKEN_IDENTIFIER, "rax")); + check(context, token_matches(scan_token(&lexer), TOKEN_INTEGER, "42")); + check(context, scan_token(&lexer).type == TOKEN_EOF); +} + +static void test_operators(struct TestContext* context) +{ + struct Lexer lexer = create_lexer("= == += != /"); + check(context, scan_token(&lexer).type == TOKEN_EQUAL); + check(context, scan_token(&lexer).type == TOKEN_EQUAL_EQUAL); + check(context, scan_token(&lexer).type == TOKEN_PLUS_EQUAL); + check(context, scan_token(&lexer).type == TOKEN_BANG_EQUAL); + check(context, scan_token(&lexer).type == TOKEN_SLASH); +} + +static void test_literals(struct TestContext* context) +{ + struct Lexer lexer = create_lexer("\"hi\\n\" '0'"); + check(context, token_matches(scan_token(&lexer), TOKEN_STRING, "\"hi\\n\"")); + check(context, token_matches(scan_token(&lexer), TOKEN_CHAR, "'0'")); +} + +static void test_keywords(struct TestContext* context) +{ + struct Lexer lexer = create_lexer("const proc mainly"); + check(context, scan_token(&lexer).type == TOKEN_CONST); + check(context, scan_token(&lexer).type == TOKEN_PROC); + check(context, scan_token(&lexer).type == TOKEN_IDENTIFIER); +} + +static void test_line_counting(struct TestContext* context) +{ + struct Lexer lexer = create_lexer("a\nb\nc"); + check(context, scan_token(&lexer).line == 1); + check(context, scan_token(&lexer).line == 2); + check(context, scan_token(&lexer).line == 3); +} + +void run_lexer_tests(struct TestContext* context) +{ + test_identifiers_and_integers(context); + test_operators(context); + test_literals(context); + test_keywords(context); + test_line_counting(context); +} -- cgit v1.3