Refactor test interface

This commit is contained in:
2026-04-29 10:59:06 +02:00
parent 34b7939f76
commit 3288efdfd7
5 changed files with 98 additions and 75 deletions
+51 -38
View File
@@ -4,6 +4,7 @@
#include <string.h>
#include <stdlib.h>
#include "util.h"
#include "parser.h"
static jmp_buf s_testJmp;
static const char* s_failMsg;
@@ -16,22 +17,6 @@ void fail(const char* msg) {
longjmp(s_testJmp, 1);
}
void assert_not_null(void* ptr, const char* msg) {
if (ptr == NULL) {
fail(msg);
}
}
void assert_str(const char* expected, const char* actual, const char* msg) {
if (expected == NULL || actual == NULL || strcmp(expected, actual) != 0) {
fail(msg);
}
}
void assert_log(const char* expected, const char* msg) {
assert_str(expected, s_logOutput, msg);
}
char* read_file_content(const char* filepath) {
FILE* f;
long size;
@@ -53,6 +38,56 @@ char* read_file_content(const char* filepath) {
return content;
}
void assert_not_null(void* ptr, const char* msg) {
if (ptr == NULL) {
fail(msg);
}
}
void assert_string(const char* expected, String actual, const char* msg) {
if (expected == NULL || actual.data == NULL || strlen(expected) != actual.length || strncmp(expected, actual.data, actual.length) != 0) {
fail(msg);
}
}
void assert_str(const char* expected, const char* actual, const char* msg) {
if (expected == NULL || actual == NULL || strcmp(expected, actual) != 0) {
fail(msg);
}
}
TokenStream* tokenstream_get_test(void) {
char* filepath = NULL;
TokenStream* ts = 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");
free(filepath);
return NULL;
}
ts = tokenstream_open(filepath, s_testSource);
free(filepath);
return ts;
}
Module* test_get_ast(void) {
TokenStream* ts = tokenstream_get_test();
if (!ts) return NULL;
return parser_parse(ts);
}
void assert_log(const char* expected, const char* msg) {
assert_str(expected, s_logOutput, msg);
}
void assert_log_file(const char* msg) {
char* filepath = format_string("v0/tests/%s.log", s_currentTestName);
const char* generate;
@@ -109,28 +144,6 @@ void assert_false(bool condition, const char* msg) {
}
}
TokenStream* tokenstream_get_test(void) {
char* filepath = NULL;
TokenStream* ts = 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");
free(filepath);
return NULL;
}
ts = tokenstream_open(filepath, s_testSource);
free(filepath);
return ts;
}
static void log_append(const char* msg) {
size_t oldLen = s_logOutput ? strlen(s_logOutput) : 0;
size_t newLen = oldLen + strlen(msg) + 1;