From e910c0134835fcb0c6988e1d0117118e7b684b5b Mon Sep 17 00:00:00 2001 From: Sebastiaan de Schaetzen Date: Sat, 25 Apr 2026 20:05:16 +0200 Subject: [PATCH] Refactor golden files to follow xyz_log_ and xyz_src_ naming convention --- v0/test.c | 22 ++++++++++++---------- v0/test.h | 5 +++++ v0/tests/bad_import_name_log_.txt | 4 ++++ v0/tests/bad_import_name_src_.c | 1 + v0/tests/bad_module_name_log_.txt | 4 ++++ v0/tests/bad_module_name_src_.c | 1 + v0/tests/log_on_line_log_.txt | 4 ++++ v0/tests/log_on_line_src_.c | 1 + v0/tests/log_on_line_variadic_log_.txt | 4 ++++ v0/tests/log_on_line_variadic_src_.c | 1 + v0/tests/missing_semicolon_import_log_.txt | 4 ++++ v0/tests/missing_semicolon_import_src_.c | 1 + v0/tests/missing_semicolon_module_log_.txt | 4 ++++ v0/tests/missing_semicolon_module_src_.c | 1 + v0/tests/unknown_token_log_.txt | 4 ++++ v0/tests/unknown_token_src_.c | 1 + 16 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 v0/tests/bad_import_name_log_.txt create mode 100644 v0/tests/bad_import_name_src_.c create mode 100644 v0/tests/bad_module_name_log_.txt create mode 100644 v0/tests/bad_module_name_src_.c create mode 100644 v0/tests/log_on_line_log_.txt create mode 100644 v0/tests/log_on_line_src_.c create mode 100644 v0/tests/log_on_line_variadic_log_.txt create mode 100644 v0/tests/log_on_line_variadic_src_.c create mode 100644 v0/tests/missing_semicolon_import_log_.txt create mode 100644 v0/tests/missing_semicolon_import_src_.c create mode 100644 v0/tests/missing_semicolon_module_log_.txt create mode 100644 v0/tests/missing_semicolon_module_src_.c create mode 100644 v0/tests/unknown_token_log_.txt create mode 100644 v0/tests/unknown_token_src_.c diff --git a/v0/test.c b/v0/test.c index 33b92a3..f5d5051 100644 --- a/v0/test.c +++ b/v0/test.c @@ -42,29 +42,31 @@ void assert_log_file(const char* filepath, const char* msg) { return; } - FILE* f = fopen(filepath, "r"); - if (!f) { + char* content = read_file_content(filepath); + if (!content) { fail("could not open golden file for reading"); return; } + assert_str(content, s_logOutput, 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); - fail("could not allocate memory for golden file content"); - return; + return NULL; } - fread(content, 1, size, f); content[size] = '\0'; fclose(f); - - assert_str(content, s_logOutput, msg); - free(content); + return content; } void assert_int(int expected, int actual, const char* msg) { @@ -177,4 +179,4 @@ int main(int argc, char** argv) { printf("\n%d/%d tests passed.\n", s_greenTests, s_totalTests); return failedCount > 0 ? 1 : 0; -} \ No newline at end of file +} diff --git a/v0/test.h b/v0/test.h index c49c994..a3ce3a9 100644 --- a/v0/test.h +++ b/v0/test.h @@ -49,6 +49,11 @@ void assert_log_file(const char* filepath, const char* msg); */ void assert_int(int expected, int actual, const char* msg); +/** + * Reads the content of a file into a newly allocated string. + */ +char* read_file_content(const char* filepath); + /** * Asserts that a condition is true. */ diff --git a/v0/tests/bad_import_name_log_.txt b/v0/tests/bad_import_name_log_.txt new file mode 100644 index 0000000..dff0f45 --- /dev/null +++ b/v0/tests/bad_import_name_log_.txt @@ -0,0 +1,4 @@ +--- test.c --- +1| module my_module; import ; + ^ + expected module name to import diff --git a/v0/tests/bad_import_name_src_.c b/v0/tests/bad_import_name_src_.c new file mode 100644 index 0000000..a55db2d --- /dev/null +++ b/v0/tests/bad_import_name_src_.c @@ -0,0 +1 @@ +import ; \ No newline at end of file diff --git a/v0/tests/bad_module_name_log_.txt b/v0/tests/bad_module_name_log_.txt new file mode 100644 index 0000000..26506fb --- /dev/null +++ b/v0/tests/bad_module_name_log_.txt @@ -0,0 +1,4 @@ +--- test.c --- +1| import other_module; + ^^^^^^ + expected 'module' keyword diff --git a/v0/tests/bad_module_name_src_.c b/v0/tests/bad_module_name_src_.c new file mode 100644 index 0000000..0d8a5fb --- /dev/null +++ b/v0/tests/bad_module_name_src_.c @@ -0,0 +1 @@ +import other_module; \ No newline at end of file diff --git a/v0/tests/log_on_line_log_.txt b/v0/tests/log_on_line_log_.txt new file mode 100644 index 0000000..d9e8ba1 --- /dev/null +++ b/v0/tests/log_on_line_log_.txt @@ -0,0 +1,4 @@ +--- test.c --- +1| int main() [] + ^^ + unexpected token diff --git a/v0/tests/log_on_line_src_.c b/v0/tests/log_on_line_src_.c new file mode 100644 index 0000000..8cac8ba --- /dev/null +++ b/v0/tests/log_on_line_src_.c @@ -0,0 +1 @@ +int main() [] \ No newline at end of file diff --git a/v0/tests/log_on_line_variadic_log_.txt b/v0/tests/log_on_line_variadic_log_.txt new file mode 100644 index 0000000..75be8b0 --- /dev/null +++ b/v0/tests/log_on_line_variadic_log_.txt @@ -0,0 +1,4 @@ +--- test.c --- +1| int main() [] + ^^ + unexpected token 'x' diff --git a/v0/tests/log_on_line_variadic_src_.c b/v0/tests/log_on_line_variadic_src_.c new file mode 100644 index 0000000..8cac8ba --- /dev/null +++ b/v0/tests/log_on_line_variadic_src_.c @@ -0,0 +1 @@ +int main() [] \ No newline at end of file diff --git a/v0/tests/missing_semicolon_import_log_.txt b/v0/tests/missing_semicolon_import_log_.txt new file mode 100644 index 0000000..d6d2947 --- /dev/null +++ b/v0/tests/missing_semicolon_import_log_.txt @@ -0,0 +1,4 @@ +--- test.c --- +1| module my_module; import other_module + ^ + expected ';' after import diff --git a/v0/tests/missing_semicolon_import_src_.c b/v0/tests/missing_semicolon_import_src_.c new file mode 100644 index 0000000..b4480e9 --- /dev/null +++ b/v0/tests/missing_semicolon_import_src_.c @@ -0,0 +1 @@ +module my_module; import other_module \ No newline at end of file diff --git a/v0/tests/missing_semicolon_module_log_.txt b/v0/tests/missing_semicolon_module_log_.txt new file mode 100644 index 0000000..585f78a --- /dev/null +++ b/v0/tests/missing_semicolon_module_log_.txt @@ -0,0 +1,4 @@ +--- test.c --- +1| module my_module + ^ + expected ';' after module name diff --git a/v0/tests/missing_semicolon_module_src_.c b/v0/tests/missing_semicolon_module_src_.c new file mode 100644 index 0000000..e8451c1 --- /dev/null +++ b/v0/tests/missing_semicolon_module_src_.c @@ -0,0 +1 @@ +module my_module \ No newline at end of file diff --git a/v0/tests/unknown_token_log_.txt b/v0/tests/unknown_token_log_.txt new file mode 100644 index 0000000..c14067e --- /dev/null +++ b/v0/tests/unknown_token_log_.txt @@ -0,0 +1,4 @@ +--- test.c --- +1| % + ^ + unexpected token '%' diff --git a/v0/tests/unknown_token_src_.c b/v0/tests/unknown_token_src_.c new file mode 100644 index 0000000..02691e3 --- /dev/null +++ b/v0/tests/unknown_token_src_.c @@ -0,0 +1 @@ +% \ No newline at end of file