Refactor tests a bit more

This commit is contained in:
2026-04-29 13:25:41 +02:00
parent 98d58a2169
commit 1f40c8f5ee
+30 -29
View File
@@ -74,6 +74,7 @@ TokenStream* test_get_tokenstream(void) {
if (s_testSource) free(s_testSource); if (s_testSource) free(s_testSource);
s_testSource = read_file_content(filepath); s_testSource = read_file_content(filepath);
if (!s_testSource) { if (!s_testSource) {
puts(filepath);
fail("could not read test source file"); fail("could not read test source file");
free(filepath); free(filepath);
return NULL; return NULL;
@@ -184,38 +185,37 @@ typedef struct {
static int s_totalTests; static int s_totalTests;
static int s_greenTests; static int s_greenTests;
#define TEST(name) {#name, name},
static TestCase s_tests[] = { static TestCase s_tests[] = {
{"tokenstream_open_fail", test_tokenstream_open_fail}, TEST(test_log_error)
{"tokenstream_simple_keyword", test_tokenstream_simple_keyword}, TEST(test_log_on_line_variadic)
{"tokenstream_keywords_and_symbols", test_tokenstream_keywords_and_symbols}, TEST(test_log_on_line)
{"tokenstream_parentheses_and_brackets", test_tokenstream_parentheses_and_brackets}, TEST(test_parser_alias_and_import_mix)
{"tokenstream_comma", test_tokenstream_comma}, TEST(test_parser_alias_array)
{"tokenstream_whitespace_ignored", test_tokenstream_whitespace_ignored}, TEST(test_parser_alias_simple)
{"tokenstream_void_function_signature", test_tokenstream_void_function_signature}, TEST(test_parser_bad_import_name)
{"tokenstream_unknown_token", test_tokenstream_unknown_token}, TEST(test_parser_bad_module_name)
{"tokenstream_info", test_tokenstream_info}, TEST(test_parser_imports)
{"parser_module_name", test_parser_module_name}, TEST(test_parser_missing_semicolon_import)
{"parser_bad_module_name", test_parser_bad_module_name}, TEST(test_parser_missing_semicolon_module)
{"parser_missing_semicolon_module", test_parser_missing_semicolon_module}, TEST(test_parser_module_name)
{"parser_missing_semicolon_import", test_parser_missing_semicolon_import}, TEST(test_parser_public_imports)
{"parser_bad_import_name", test_parser_bad_import_name}, TEST(test_tokenstream_comma)
{"parser_imports", test_parser_imports}, TEST(test_tokenstream_info)
{"parser_public_imports", test_parser_public_imports}, TEST(test_tokenstream_keywords_and_symbols)
{"parser_alias_simple", test_parser_alias_simple}, TEST(test_tokenstream_open_fail)
{"parser_alias_array", test_parser_alias_array}, TEST(test_tokenstream_parentheses_and_brackets)
{"log_error", test_log_error}, TEST(test_tokenstream_simple_keyword)
{"log_on_line", test_log_on_line}, TEST(test_tokenstream_unknown_token)
{"log_on_line_variadic", test_log_on_line_variadic}, TEST(test_tokenstream_void_function_signature)
{"parser_alias_and_import_mix", test_parser_alias_and_import_mix}, TEST(test_tokenstream_whitespace_ignored)
}; };
int main(int argc, char** argv) { int main(int argc, char** argv) {
/* Declarations first for C89 */
const char** failedTests; const char** failedTests;
int failedCount; int failedCount;
int i;
int j;
(void)argc; (void)argc;
(void)argv; (void)argv;
@@ -223,12 +223,13 @@ int main(int argc, char** argv) {
s_totalTests = sizeof(s_tests) / sizeof(s_tests[0]); s_totalTests = sizeof(s_tests) / sizeof(s_tests[0]);
s_greenTests = 0; s_greenTests = 0;
/* Allocate failed tests array dynamically to avoid VLAs */ // Allocate failed tests array dynamically to avoid VLAs
failedTests = (const char**)malloc((s_totalTests + 1) * sizeof(const char*)); failedTests = (const char**)malloc((s_totalTests + 1) * sizeof(const char*));
failedCount = 0; failedCount = 0;
for (i = 0; i < s_totalTests; i++) { for (int i = 0; i < s_totalTests; i++) {
s_currentTestName = s_tests[i].name; // Add 5 to strip the 'test_' prefix.
s_currentTestName = s_tests[i].name + 5;
log_set_output(log_append); log_set_output(log_append);
printf("%s...", s_tests[i].name); printf("%s...", s_tests[i].name);
fflush(stdout); fflush(stdout);
@@ -264,7 +265,7 @@ int main(int argc, char** argv) {
if (failedCount > 0) { if (failedCount > 0) {
printf("\nFailed tests:\n"); printf("\nFailed tests:\n");
for (j = 0; j < failedCount; j++) { for (int j = 0; j < failedCount; j++) {
printf(" - %s\n", failedTests[j]); printf(" - %s\n", failedTests[j]);
} }
} }