Introduce golden file mechanism for tests
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
@@ -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},
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
+4
-17
@@ -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");
|
||||
}
|
||||
assert_log_file("v0/tests/log_on_line_variadic.txt", "expected formatted error message with variadic args");
|
||||
}
|
||||
|
||||
+1
-6
@@ -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);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
--- test.c ---
|
||||
1| int main() []
|
||||
^^
|
||||
unexpected token
|
||||
@@ -0,0 +1,4 @@
|
||||
--- test.c ---
|
||||
1| int main() []
|
||||
^^
|
||||
unexpected token 'x'
|
||||
@@ -0,0 +1,4 @@
|
||||
--- test.c ---
|
||||
1| %
|
||||
^
|
||||
unexpected token '%'
|
||||
Reference in New Issue
Block a user