Refactor tests

This commit is contained in:
2026-04-29 13:09:14 +02:00
parent f0621a8076
commit 98d58a2169
13 changed files with 69 additions and 105 deletions
+38 -22
View File
@@ -1,10 +1,12 @@
#include "test.h"
#include "util.h"
#include "parser.h"
#include <setjmp.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "util.h"
#include "parser.h"
static jmp_buf s_testJmp;
static const char* s_failMsg;
@@ -12,6 +14,9 @@ static char* s_logOutput = NULL;
static const char* s_currentTestName = NULL;
static char* s_testSource = NULL;
static Module* s_currentModule = NULL;
static TokenStream* s_currentTokenStream = NULL;
void fail(const char* msg) {
s_failMsg = msg;
longjmp(s_testJmp, 1);
@@ -56,32 +61,34 @@ void assert_str(const char* expected, const char* actual, const char* msg) {
}
}
TokenStream* tokenstream_get_test(void) {
char* filepath = NULL;
TokenStream* ts = NULL;
TokenStream* test_get_tokenstream(void) {
if (s_currentTokenStream == NULL) {
char* filepath = NULL;
filepath = format_string("v0/tests/%s.c2", s_currentTestName);
if (!filepath) {
fail("out of memory");
return NULL;
}
if (s_testSource) free(s_testSource);
s_testSource = read_file_content(filepath);
if (!s_testSource) {
fail("could not read test source file");
filepath = format_string("v0/tests/%s.c2", s_currentTestName);
if (!filepath) {
fail("out of memory");
return NULL;
}
if (s_testSource) free(s_testSource);
s_testSource = read_file_content(filepath);
if (!s_testSource) {
fail("could not read test source file");
free(filepath);
return NULL;
}
s_currentTokenStream = tokenstream_open(filepath, s_testSource);
free(filepath);
return NULL;
}
ts = tokenstream_open(filepath, s_testSource);
free(filepath);
return ts;
return s_currentTokenStream;
}
Module* test_get_ast(void) {
TokenStream* ts = tokenstream_get_test();
if (!ts) return NULL;
return parser_parse(ts);
if (s_currentModule == NULL) {
s_currentModule = parser_parse(test_get_tokenstream());
}
return s_currentModule;
}
void assert_log(const char* expected, const char* msg) {
@@ -240,6 +247,15 @@ int main(int argc, char** argv) {
printf(" [FAIL]: %s\n", s_failMsg ? s_failMsg : "");
failedTests[failedCount++] = s_tests[i].name;
}
if (s_currentModule) {
parser_free(s_currentModule);
s_currentModule = NULL;
}
if (s_currentTokenStream) {
tokenstream_close(s_currentTokenStream);
s_currentTokenStream = NULL;
}
fflush(stdout);
}