Files
c2/v0/test_token.c
T
seeseemelk ed12c0a38e Refactor token tests to use in-memory strings instead of files
- Replaced all write_test_file() calls with buffer_open_string()
- Removed file I/O overhead - tests now run entirely in memory
- Removed unnecessary #include <stdio.h>, <stdlib.h>, <string.h> from test_token.c
- Simplified test_tokenstream_open_fail() to directly test NULL buffer handling
- Tests are faster and cleaner without temporary file creation
- No test output changes - all 12 tests still pass

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-04-24 11:03:14 +02:00

106 lines
4.0 KiB
C

#include "test.h"
#include "token.h"
static void test_tokenstream_open_fail(void) {
TokenStream* ts = tokenstream_open(NULL);
if (ts != NULL) fail("expected NULL for NULL buffer");
}
static void test_tokenstream_simple_keyword(void) {
Buffer* buf = buffer_open_string("module");
if (buf == NULL) fail("could not create buffer");
TokenStream* ts = tokenstream_open(buf);
if (ts == NULL) fail("could not create tokenstream");
Token t = tokenstream_next(ts);
if (t != TOKEN_MODULE) fail("expected TOKEN_MODULE");
Token eof = tokenstream_next(ts);
if (eof != -1) fail("expected EOF");
tokenstream_close(ts);
}
static void test_tokenstream_keywords_and_symbols(void) {
Buffer* buf = buffer_open_string("module main; import stdio;");
if (buf == NULL) fail("could not create buffer");
TokenStream* ts = tokenstream_open(buf);
if (ts == NULL) fail("could not create tokenstream");
if (tokenstream_next(ts) != TOKEN_MODULE) fail("expected TOKEN_MODULE");
if (tokenstream_next(ts) != TOKEN_IDENTIFIER) fail("expected TOKEN_IDENTIFIER (main)");
if (tokenstream_next(ts) != TOKEN_SEMICOLON) fail("expected TOKEN_SEMICOLON");
if (tokenstream_next(ts) != TOKEN_IMPORT) fail("expected TOKEN_IMPORT");
if (tokenstream_next(ts) != TOKEN_IDENTIFIER) fail("expected TOKEN_IDENTIFIER (stdio)");
if (tokenstream_next(ts) != TOKEN_SEMICOLON) fail("expected TOKEN_SEMICOLON");
if (tokenstream_next(ts) != -1) fail("expected EOF");
tokenstream_close(ts);
}
static void test_tokenstream_parentheses_and_brackets(void) {
Buffer* buf = buffer_open_string("()[]");
if (buf == NULL) fail("could not create buffer");
TokenStream* ts = tokenstream_open(buf);
if (ts == NULL) fail("could not create tokenstream");
if (tokenstream_next(ts) != TOKEN_PARENT_OPEN) fail("expected TOKEN_PARENT_OPEN");
if (tokenstream_next(ts) != TOKEN_PARENT_CLOSE) fail("expected TOKEN_PARENT_CLOSE");
if (tokenstream_next(ts) != TOKEN_BRACKET_OPEN) fail("expected TOKEN_BRACKET_OPEN");
if (tokenstream_next(ts) != TOKEN_BRACKET_CLOSE) fail("expected TOKEN_BRACKET_CLOSE");
if (tokenstream_next(ts) != -1) fail("expected EOF");
tokenstream_close(ts);
}
static void test_tokenstream_comma(void) {
Buffer* buf = buffer_open_string("a,b,c");
if (buf == NULL) fail("could not create buffer");
TokenStream* ts = tokenstream_open(buf);
if (ts == NULL) fail("could not create tokenstream");
if (tokenstream_next(ts) != TOKEN_IDENTIFIER) fail("expected a");
if (tokenstream_next(ts) != TOKEN_COMMA) fail("expected comma");
if (tokenstream_next(ts) != TOKEN_IDENTIFIER) fail("expected b");
if (tokenstream_next(ts) != TOKEN_COMMA) fail("expected comma");
if (tokenstream_next(ts) != TOKEN_IDENTIFIER) fail("expected c");
if (tokenstream_next(ts) != -1) fail("expected EOF");
tokenstream_close(ts);
}
static void test_tokenstream_whitespace_ignored(void) {
Buffer* buf = buffer_open_string(" module \n\t import ; ");
if (buf == NULL) fail("could not create buffer");
TokenStream* ts = tokenstream_open(buf);
if (ts == NULL) fail("could not create tokenstream");
if (tokenstream_next(ts) != TOKEN_MODULE) fail("expected TOKEN_MODULE");
if (tokenstream_next(ts) != TOKEN_IMPORT) fail("expected TOKEN_IMPORT");
if (tokenstream_next(ts) != TOKEN_SEMICOLON) fail("expected TOKEN_SEMICOLON");
if (tokenstream_next(ts) != -1) fail("expected EOF");
tokenstream_close(ts);
}
static void test_tokenstream_void_function_signature(void) {
Buffer* buf = buffer_open_string("void main()");
if (buf == NULL) fail("could not create buffer");
TokenStream* ts = tokenstream_open(buf);
if (ts == NULL) fail("could not create tokenstream");
if (tokenstream_next(ts) != TOKEN_VOID) fail("expected TOKEN_VOID");
if (tokenstream_next(ts) != TOKEN_IDENTIFIER) fail("expected TOKEN_IDENTIFIER");
if (tokenstream_next(ts) != TOKEN_PARENT_OPEN) fail("expected TOKEN_PARENT_OPEN");
if (tokenstream_next(ts) != TOKEN_PARENT_CLOSE) fail("expected TOKEN_PARENT_CLOSE");
if (tokenstream_next(ts) != -1) fail("expected EOF");
tokenstream_close(ts);
}