From ed12c0a38e0f9eac2e895f566f80cf2549d0a23d Mon Sep 17 00:00:00 2001 From: Sebastiaan de Schaetzen Date: Fri, 24 Apr 2026 11:03:14 +0200 Subject: [PATCH] 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 , , 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> --- v0/test_token.c | 47 +++++++++++++---------------------------------- 1 file changed, 13 insertions(+), 34 deletions(-) diff --git a/v0/test_token.c b/v0/test_token.c index af8c14b..81d9325 100644 --- a/v0/test_token.c +++ b/v0/test_token.c @@ -1,30 +1,14 @@ #include "test.h" #include "token.h" -#include -#include -#include - -/* 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) { - Buffer* buf = buffer_open_file("v0/does_not_exist.c2"); - if (buf != NULL) fail("expected NULL for non-existent file"); - - TokenStream* ts = tokenstream_open(buf); + TokenStream* ts = tokenstream_open(NULL); if (ts != NULL) fail("expected NULL for NULL buffer"); } static void test_tokenstream_simple_keyword(void) { - write_test_file("v0/test_token_tmp.c2", "module"); - Buffer* buf = buffer_open_file("v0/test_token_tmp.c2"); - if (buf == NULL) fail("could not open file"); + 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"); @@ -39,9 +23,8 @@ static void test_tokenstream_simple_keyword(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_file("v0/test_token_tmp.c2"); - if (buf == NULL) fail("could not open file"); + 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"); @@ -58,9 +41,8 @@ static void test_tokenstream_keywords_and_symbols(void) { } static void test_tokenstream_parentheses_and_brackets(void) { - write_test_file("v0/test_token_tmp.c2", "()[]"); - Buffer* buf = buffer_open_file("v0/test_token_tmp.c2"); - if (buf == NULL) fail("could not open file"); + 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"); @@ -75,9 +57,8 @@ static void test_tokenstream_parentheses_and_brackets(void) { } static void test_tokenstream_comma(void) { - write_test_file("v0/test_token_tmp.c2", "a,b,c"); - Buffer* buf = buffer_open_file("v0/test_token_tmp.c2"); - if (buf == NULL) fail("could not open file"); + 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"); @@ -93,9 +74,8 @@ static void test_tokenstream_comma(void) { } static void test_tokenstream_whitespace_ignored(void) { - write_test_file("v0/test_token_tmp.c2", " module \n\t import ; "); - Buffer* buf = buffer_open_file("v0/test_token_tmp.c2"); - if (buf == NULL) fail("could not open file"); + 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"); @@ -109,9 +89,8 @@ static void test_tokenstream_whitespace_ignored(void) { } static void test_tokenstream_void_function_signature(void) { - write_test_file("v0/test_token_tmp.c2", "void main()"); - Buffer* buf = buffer_open_file("v0/test_token_tmp.c2"); - if (buf == NULL) fail("could not open file"); + 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");