diff --git a/Makefile b/Makefile index 4523b15..479bc6c 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,8 @@ c2: v0/bin/c2 test:: +generate_golden:: + clean:: rm -f c2 diff --git a/v0/include.mk b/v0/include.mk index a507fea..529aca3 100644 --- a/v0/include.mk +++ b/v0/include.mk @@ -22,6 +22,9 @@ v0/bin/test: $(V0_SRC_OBJ_NO_MAIN) $(V0_TEST_OBJ) test:: v0/bin/test v0/bin/test +generate_golden:: v0/bin/test + GENERATE_GOLDEN=1 v0/bin/test + clean:: rm -f v0/bin/test v0/bin/c2 $(V0_SRC_OBJ) $(V0_TEST_OBJ) $(V0_SRC_DEPS) $(V0_TEST_DEPS) diff --git a/v0/test.c b/v0/test.c index e317795..120210e 100644 --- a/v0/test.c +++ b/v0/test.c @@ -29,6 +29,44 @@ 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) { + const char* generate = getenv("GENERATE_GOLDEN"); + if (generate && strcmp(generate, "1") == 0) { + FILE* f = fopen(filepath, "w"); + if (!f) { + fail("could not open golden file for writing"); + return; + } + fputs(s_logOutput ? s_logOutput : "", f); + fclose(f); + return; + } + + FILE* f = fopen(filepath, "r"); + if (!f) { + fail("could not open golden file for reading"); + return; + } + + fseek(f, 0, SEEK_END); + long size = ftell(f); + fseek(f, 0, SEEK_SET); + + char* content = malloc(size + 1); + if (!content) { + fclose(f); + fail("could not allocate memory for golden file content"); + return; + } + + fread(content, 1, size, f); + content[size] = '\0'; + fclose(f); + + assert_str(content, s_logOutput, msg); + free(content); +} + static void log_append(const char* msg) { size_t oldLen = s_logOutput ? strlen(s_logOutput) : 0; size_t newLen = oldLen + strlen(msg) + 1; @@ -75,7 +113,6 @@ static TestCase s_tests[] = { {"parser_module_name", test_parser_module_name}, {"log_error", test_log_error}, {"log_on_line", test_log_on_line}, - {"log_on_line_format", test_log_on_line_format}, }; diff --git a/v0/test.h b/v0/test.h index 1044a0a..2fee8fd 100644 --- a/v0/test.h +++ b/v0/test.h @@ -38,4 +38,10 @@ void assert_str(const char* expected, const char* actual, const char* msg); */ void assert_log(const char* expected, const char* msg); +/** + * Asserts that the logged output matches the content of a file. + * If GENERATE_GOLDEN=1, the file is overwritten with the actual output. + */ +void assert_log_file(const char* filepath, const char* msg); + #endif diff --git a/v0/test_log.c b/v0/test_log.c index b690330..f5a9d46 100644 --- a/v0/test_log.c +++ b/v0/test_log.c @@ -21,23 +21,10 @@ static void test_log_error(void) { } static void test_log_on_line(void) { - const char* expected = - "--- test.c ---\n" - "1| int main() []\n" - " ^^\n" - " unexpected token\n"; - log_on_line("test.c", "int main() []", 1, 12, 13, "unexpected token"); - assert_log(expected, "expected formatted error message"); -} - -static void test_log_on_line_format(void) { - const char* expected = - "--- test.c ---\n" - "1| int main() []\n" - " ^^\n" - " unexpected token 'x'\n"; + assert_log_file("v0/tests/log_on_line.txt", "expected formatted error message"); + log_clear(); log_on_line("test.c", "int main() []", 1, 12, 13, "unexpected token '%c'", 'x'); - assert_log(expected, "expected formatted error message with variadic args"); -} \ No newline at end of file + assert_log_file("v0/tests/log_on_line_variadic.txt", "expected formatted error message with variadic args"); +} diff --git a/v0/test_token.c b/v0/test_token.c index 1b8f015..e70b84c 100644 --- a/v0/test_token.c +++ b/v0/test_token.c @@ -86,12 +86,7 @@ static void test_tokenstream_unknown_token(void) { if (tokenstream_next(ts).token != TOKEN_UNKNOWN) fail("expected TOKEN_UNKNOWN"); - assert_log( - "--- test.c ---\n" - "1| %\n" - " ^\n" - " unexpected token '%'\n", - "expected error message for unknown token"); + assert_log_file("v0/tests/unknown_token.txt", "expected error message for unknown token"); tokenstream_close(ts); } diff --git a/v0/tests/log_on_line.txt b/v0/tests/log_on_line.txt new file mode 100644 index 0000000..d9e8ba1 --- /dev/null +++ b/v0/tests/log_on_line.txt @@ -0,0 +1,4 @@ +--- test.c --- +1| int main() [] + ^^ + unexpected token diff --git a/v0/tests/log_on_line_variadic.txt b/v0/tests/log_on_line_variadic.txt new file mode 100644 index 0000000..75be8b0 --- /dev/null +++ b/v0/tests/log_on_line_variadic.txt @@ -0,0 +1,4 @@ +--- test.c --- +1| int main() [] + ^^ + unexpected token 'x' diff --git a/v0/tests/unknown_token.txt b/v0/tests/unknown_token.txt new file mode 100644 index 0000000..c14067e --- /dev/null +++ b/v0/tests/unknown_token.txt @@ -0,0 +1,4 @@ +--- test.c --- +1| % + ^ + unexpected token '%'