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>
This commit is contained in:
2026-04-24 11:03:14 +02:00
parent 49b9db5b75
commit ed12c0a38e
+13 -34
View File
@@ -1,30 +1,14 @@
#include "test.h" #include "test.h"
#include "token.h" #include "token.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Helper to create a test file with content */
static void write_test_file(const char* filename, const char* content) {
FILE* f = fopen(filename, "w");
if (f) {
fputs(content, f);
fclose(f);
}
}
static void test_tokenstream_open_fail(void) { static void test_tokenstream_open_fail(void) {
Buffer* buf = buffer_open_file("v0/does_not_exist.c2"); TokenStream* ts = tokenstream_open(NULL);
if (buf != NULL) fail("expected NULL for non-existent file");
TokenStream* ts = tokenstream_open(buf);
if (ts != NULL) fail("expected NULL for NULL buffer"); if (ts != NULL) fail("expected NULL for NULL buffer");
} }
static void test_tokenstream_simple_keyword(void) { static void test_tokenstream_simple_keyword(void) {
write_test_file("v0/test_token_tmp.c2", "module"); Buffer* buf = buffer_open_string("module");
Buffer* buf = buffer_open_file("v0/test_token_tmp.c2"); if (buf == NULL) fail("could not create buffer");
if (buf == NULL) fail("could not open file");
TokenStream* ts = tokenstream_open(buf); TokenStream* ts = tokenstream_open(buf);
if (ts == NULL) fail("could not create tokenstream"); if (ts == NULL) fail("could not create tokenstream");
@@ -39,9 +23,8 @@ static void test_tokenstream_simple_keyword(void) {
} }
static void test_tokenstream_keywords_and_symbols(void) { static void test_tokenstream_keywords_and_symbols(void) {
write_test_file("v0/test_token_tmp.c2", "module main; import stdio;"); Buffer* buf = buffer_open_string("module main; import stdio;");
Buffer* buf = buffer_open_file("v0/test_token_tmp.c2"); if (buf == NULL) fail("could not create buffer");
if (buf == NULL) fail("could not open file");
TokenStream* ts = tokenstream_open(buf); TokenStream* ts = tokenstream_open(buf);
if (ts == NULL) fail("could not create tokenstream"); if (ts == NULL) fail("could not create tokenstream");
@@ -58,9 +41,8 @@ static void test_tokenstream_keywords_and_symbols(void) {
} }
static void test_tokenstream_parentheses_and_brackets(void) { static void test_tokenstream_parentheses_and_brackets(void) {
write_test_file("v0/test_token_tmp.c2", "()[]"); Buffer* buf = buffer_open_string("()[]");
Buffer* buf = buffer_open_file("v0/test_token_tmp.c2"); if (buf == NULL) fail("could not create buffer");
if (buf == NULL) fail("could not open file");
TokenStream* ts = tokenstream_open(buf); TokenStream* ts = tokenstream_open(buf);
if (ts == NULL) fail("could not create tokenstream"); if (ts == NULL) fail("could not create tokenstream");
@@ -75,9 +57,8 @@ static void test_tokenstream_parentheses_and_brackets(void) {
} }
static void test_tokenstream_comma(void) { static void test_tokenstream_comma(void) {
write_test_file("v0/test_token_tmp.c2", "a,b,c"); Buffer* buf = buffer_open_string("a,b,c");
Buffer* buf = buffer_open_file("v0/test_token_tmp.c2"); if (buf == NULL) fail("could not create buffer");
if (buf == NULL) fail("could not open file");
TokenStream* ts = tokenstream_open(buf); TokenStream* ts = tokenstream_open(buf);
if (ts == NULL) fail("could not create tokenstream"); if (ts == NULL) fail("could not create tokenstream");
@@ -93,9 +74,8 @@ static void test_tokenstream_comma(void) {
} }
static void test_tokenstream_whitespace_ignored(void) { static void test_tokenstream_whitespace_ignored(void) {
write_test_file("v0/test_token_tmp.c2", " module \n\t import ; "); Buffer* buf = buffer_open_string(" module \n\t import ; ");
Buffer* buf = buffer_open_file("v0/test_token_tmp.c2"); if (buf == NULL) fail("could not create buffer");
if (buf == NULL) fail("could not open file");
TokenStream* ts = tokenstream_open(buf); TokenStream* ts = tokenstream_open(buf);
if (ts == NULL) fail("could not create tokenstream"); if (ts == NULL) fail("could not create tokenstream");
@@ -109,9 +89,8 @@ static void test_tokenstream_whitespace_ignored(void) {
} }
static void test_tokenstream_void_function_signature(void) { static void test_tokenstream_void_function_signature(void) {
write_test_file("v0/test_token_tmp.c2", "void main()"); Buffer* buf = buffer_open_string("void main()");
Buffer* buf = buffer_open_file("v0/test_token_tmp.c2"); if (buf == NULL) fail("could not create buffer");
if (buf == NULL) fail("could not open file");
TokenStream* ts = tokenstream_open(buf); TokenStream* ts = tokenstream_open(buf);
if (ts == NULL) fail("could not create tokenstream"); if (ts == NULL) fail("could not create tokenstream");