Implement tokenstream_get_test and simplified assert_log_file using test names
This commit is contained in:
@@ -7,6 +7,8 @@
|
||||
static jmp_buf s_testJmp;
|
||||
static const char* s_failMsg;
|
||||
static char* s_logOutput = NULL;
|
||||
static const char* s_currentTestName = NULL;
|
||||
static char* s_testSource = NULL;
|
||||
|
||||
void fail(const char* msg) {
|
||||
s_failMsg = msg;
|
||||
@@ -29,7 +31,27 @@ void assert_log(const char* expected, const char* msg) {
|
||||
assert_str(expected, s_logOutput, msg);
|
||||
}
|
||||
|
||||
void assert_log_file(const char* filepath, const char* msg) {
|
||||
char* read_file_content(const char* filepath) {
|
||||
FILE* f = fopen(filepath, "r");
|
||||
if (!f) return NULL;
|
||||
fseek(f, 0, SEEK_END);
|
||||
long size = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
char* content = malloc(size + 1);
|
||||
if (!content) {
|
||||
fclose(f);
|
||||
return NULL;
|
||||
}
|
||||
fread(content, 1, size, f);
|
||||
content[size] = '\0';
|
||||
fclose(f);
|
||||
return content;
|
||||
}
|
||||
|
||||
void assert_log_file(const char* msg) {
|
||||
char filepath[256];
|
||||
snprintf(filepath, sizeof(filepath), "v0/tests/%s.log", s_currentTestName);
|
||||
|
||||
const char* generate = getenv("GENERATE_GOLDEN");
|
||||
if (generate && strcmp(generate, "1") == 0) {
|
||||
FILE* f = fopen(filepath, "w");
|
||||
@@ -52,23 +74,6 @@ void assert_log_file(const char* filepath, const char* msg) {
|
||||
free(content);
|
||||
}
|
||||
|
||||
char* read_file_content(const char* filepath) {
|
||||
FILE* f = fopen(filepath, "r");
|
||||
if (!f) return NULL;
|
||||
fseek(f, 0, SEEK_END);
|
||||
long size = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
char* content = malloc(size + 1);
|
||||
if (!content) {
|
||||
fclose(f);
|
||||
return NULL;
|
||||
}
|
||||
fread(content, 1, size, f);
|
||||
content[size] = '\0';
|
||||
fclose(f);
|
||||
return content;
|
||||
}
|
||||
|
||||
void assert_int(int expected, int actual, const char* msg) {
|
||||
if (expected != actual) {
|
||||
char buf[64];
|
||||
@@ -89,6 +94,20 @@ void assert_false(bool condition, const char* msg) {
|
||||
}
|
||||
}
|
||||
|
||||
TokenStream* tokenstream_get_test(void) {
|
||||
char filepath[256];
|
||||
snprintf(filepath, sizeof(filepath), "v0/tests/%s.c2", s_currentTestName);
|
||||
|
||||
if (s_testSource) free(s_testSource);
|
||||
s_testSource = read_file_content(filepath);
|
||||
if (!s_testSource) {
|
||||
fail("could not read test source file");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return tokenstream_open(filepath, s_testSource);
|
||||
}
|
||||
|
||||
static void log_append(const char* msg) {
|
||||
size_t oldLen = s_logOutput ? strlen(s_logOutput) : 0;
|
||||
size_t newLen = oldLen + strlen(msg) + 1;
|
||||
@@ -141,6 +160,7 @@ static TestCase s_tests[] = {
|
||||
{"parser_public_imports", test_parser_public_imports},
|
||||
{"log_error", test_log_error},
|
||||
{"log_on_line", test_log_on_line},
|
||||
{"log_on_line_variadic", test_log_on_line_variadic},
|
||||
};
|
||||
|
||||
|
||||
@@ -155,12 +175,17 @@ int main(int argc, char** argv) {
|
||||
int failedCount = 0;
|
||||
|
||||
for (int i = 0; i < s_totalTests; i++) {
|
||||
s_currentTestName = s_tests[i].name;
|
||||
log_set_output(log_append);
|
||||
printf("%s...", s_tests[i].name);
|
||||
s_failMsg = NULL;
|
||||
|
||||
if (setjmp(s_testJmp) == 0) {
|
||||
log_clear();
|
||||
if (s_testSource) {
|
||||
free(s_testSource);
|
||||
s_testSource = NULL;
|
||||
}
|
||||
s_tests[i].func();
|
||||
printf(" [OK]\n");
|
||||
s_greenTests++;
|
||||
@@ -170,6 +195,8 @@ int main(int argc, char** argv) {
|
||||
}
|
||||
}
|
||||
|
||||
if (s_testSource) free(s_testSource);
|
||||
|
||||
if (failedCount > 0) {
|
||||
printf("\nFailed tests:\n");
|
||||
for (int i = 0; i < failedCount; i++) {
|
||||
|
||||
Reference in New Issue
Block a user