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 jmp_buf s_testJmp;
|
||||||
static const char* s_failMsg;
|
static const char* s_failMsg;
|
||||||
static char* s_logOutput = NULL;
|
static char* s_logOutput = NULL;
|
||||||
|
static const char* s_currentTestName = NULL;
|
||||||
|
static char* s_testSource = NULL;
|
||||||
|
|
||||||
void fail(const char* msg) {
|
void fail(const char* msg) {
|
||||||
s_failMsg = msg;
|
s_failMsg = msg;
|
||||||
@@ -29,7 +31,27 @@ void assert_log(const char* expected, const char* msg) {
|
|||||||
assert_str(expected, s_logOutput, 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");
|
const char* generate = getenv("GENERATE_GOLDEN");
|
||||||
if (generate && strcmp(generate, "1") == 0) {
|
if (generate && strcmp(generate, "1") == 0) {
|
||||||
FILE* f = fopen(filepath, "w");
|
FILE* f = fopen(filepath, "w");
|
||||||
@@ -52,23 +74,6 @@ void assert_log_file(const char* filepath, const char* msg) {
|
|||||||
free(content);
|
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) {
|
void assert_int(int expected, int actual, const char* msg) {
|
||||||
if (expected != actual) {
|
if (expected != actual) {
|
||||||
char buf[64];
|
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) {
|
static void log_append(const char* msg) {
|
||||||
size_t oldLen = s_logOutput ? strlen(s_logOutput) : 0;
|
size_t oldLen = s_logOutput ? strlen(s_logOutput) : 0;
|
||||||
size_t newLen = oldLen + strlen(msg) + 1;
|
size_t newLen = oldLen + strlen(msg) + 1;
|
||||||
@@ -141,6 +160,7 @@ static TestCase s_tests[] = {
|
|||||||
{"parser_public_imports", test_parser_public_imports},
|
{"parser_public_imports", test_parser_public_imports},
|
||||||
{"log_error", test_log_error},
|
{"log_error", test_log_error},
|
||||||
{"log_on_line", test_log_on_line},
|
{"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;
|
int failedCount = 0;
|
||||||
|
|
||||||
for (int i = 0; i < s_totalTests; i++) {
|
for (int i = 0; i < s_totalTests; i++) {
|
||||||
|
s_currentTestName = s_tests[i].name;
|
||||||
log_set_output(log_append);
|
log_set_output(log_append);
|
||||||
printf("%s...", s_tests[i].name);
|
printf("%s...", s_tests[i].name);
|
||||||
s_failMsg = NULL;
|
s_failMsg = NULL;
|
||||||
|
|
||||||
if (setjmp(s_testJmp) == 0) {
|
if (setjmp(s_testJmp) == 0) {
|
||||||
log_clear();
|
log_clear();
|
||||||
|
if (s_testSource) {
|
||||||
|
free(s_testSource);
|
||||||
|
s_testSource = NULL;
|
||||||
|
}
|
||||||
s_tests[i].func();
|
s_tests[i].func();
|
||||||
printf(" [OK]\n");
|
printf(" [OK]\n");
|
||||||
s_greenTests++;
|
s_greenTests++;
|
||||||
@@ -170,6 +195,8 @@ int main(int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (s_testSource) free(s_testSource);
|
||||||
|
|
||||||
if (failedCount > 0) {
|
if (failedCount > 0) {
|
||||||
printf("\nFailed tests:\n");
|
printf("\nFailed tests:\n");
|
||||||
for (int i = 0; i < failedCount; i++) {
|
for (int i = 0; i < failedCount; i++) {
|
||||||
|
|||||||
@@ -4,6 +4,10 @@
|
|||||||
#ifndef TEST_H
|
#ifndef TEST_H
|
||||||
#define TEST_H
|
#define TEST_H
|
||||||
|
|
||||||
|
#include "token.h"
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
typedef void (*Test)(void);
|
typedef void (*Test)(void);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -39,21 +43,16 @@ void assert_str(const char* expected, const char* actual, const char* msg);
|
|||||||
void assert_log(const char* expected, const char* msg);
|
void assert_log(const char* expected, const char* msg);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Asserts that the logged output matches the content of a file.
|
* Asserts that the logged output matches the content of the file `v0/tests/xyz.log`, where xyz is the test name.
|
||||||
* If GENERATE_GOLDEN=1, the file is overwritten with the actual output.
|
* If GENERATE_GOLDEN=1, the file is overwritten with the actual output.
|
||||||
*/
|
*/
|
||||||
void assert_log_file(const char* filepath, const char* msg);
|
void assert_log_file(const char* msg);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Asserts that two integers are equal.
|
* Asserts that two integers are equal.
|
||||||
*/
|
*/
|
||||||
void assert_int(int expected, int actual, 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.
|
* Asserts that a condition is true.
|
||||||
*/
|
*/
|
||||||
@@ -64,4 +63,10 @@ void assert_true(bool condition, const char* msg);
|
|||||||
*/
|
*/
|
||||||
void assert_false(bool condition, const char* msg);
|
void assert_false(bool condition, const char* msg);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the token stream used for this test.
|
||||||
|
* It reads from the `v0/tests/xyz.c2` file, where xyz is the test name.
|
||||||
|
*/
|
||||||
|
TokenStream* tokenstream_get_test(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+15
-6
@@ -22,7 +22,7 @@ static void test_log_error(void) {
|
|||||||
|
|
||||||
static void test_log_on_line(void) {
|
static void test_log_on_line(void) {
|
||||||
Location loc = {
|
Location loc = {
|
||||||
.filename = "test.c",
|
.filename = "v0/tests/log_on_line.c2",
|
||||||
.line_text = { "int main() []", 13 },
|
.line_text = { "int main() []", 13 },
|
||||||
.line = 1,
|
.line = 1,
|
||||||
.column_start = 12,
|
.column_start = 12,
|
||||||
@@ -30,9 +30,18 @@ static void test_log_on_line(void) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
log_on_line(&loc, 13, "unexpected token");
|
log_on_line(&loc, 13, "unexpected token");
|
||||||
assert_log_file("v0/tests/log_on_line.txt", "expected formatted error message");
|
assert_log_file("expected formatted error message");
|
||||||
|
}
|
||||||
log_clear();
|
|
||||||
log_on_line(&loc, 13, "unexpected token '%c'", 'x');
|
static void test_log_on_line_variadic(void) {
|
||||||
assert_log_file("v0/tests/log_on_line_variadic.txt", "expected formatted error message with variadic args");
|
Location loc = {
|
||||||
|
.filename = "v0/tests/log_on_line_variadic.c2",
|
||||||
|
.line_text = { "int main() []", 13 },
|
||||||
|
.line = 1,
|
||||||
|
.column_start = 12,
|
||||||
|
.column_end = 13
|
||||||
|
};
|
||||||
|
|
||||||
|
log_on_line(&loc, 13, "unexpected token '%c'", 'x');
|
||||||
|
assert_log_file("expected formatted error message with variadic args");
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-8
@@ -14,40 +14,40 @@ static void test_parser_module_name(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void test_parser_bad_module_name(void) {
|
static void test_parser_bad_module_name(void) {
|
||||||
TokenStream* ts = tokenstream_open("test.c", "import other_module;");
|
TokenStream* ts = tokenstream_get_test();
|
||||||
Module* m = parser_parse(ts);
|
Module* m = parser_parse(ts);
|
||||||
|
|
||||||
assert_log_file("v0/tests/bad_module_name.txt", "expected error to be logged for bad module name");
|
assert_log_file("expected error to be logged for bad module name");
|
||||||
|
|
||||||
parser_free(m);
|
parser_free(m);
|
||||||
tokenstream_close(ts);
|
tokenstream_close(ts);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_parser_missing_semicolon_module(void) {
|
static void test_parser_missing_semicolon_module(void) {
|
||||||
TokenStream* ts = tokenstream_open("test.c", "module my_module");
|
TokenStream* ts = tokenstream_get_test();
|
||||||
Module* m = parser_parse(ts);
|
Module* m = parser_parse(ts);
|
||||||
|
|
||||||
assert_log_file("v0/tests/missing_semicolon_module.txt", "expected error for missing semicolon");
|
assert_log_file("expected error for missing semicolon");
|
||||||
|
|
||||||
parser_free(m);
|
parser_free(m);
|
||||||
tokenstream_close(ts);
|
tokenstream_close(ts);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_parser_missing_semicolon_import(void) {
|
static void test_parser_missing_semicolon_import(void) {
|
||||||
TokenStream* ts = tokenstream_open("test.c", "module my_module; import other_module");
|
TokenStream* ts = tokenstream_get_test();
|
||||||
Module* m = parser_parse(ts);
|
Module* m = parser_parse(ts);
|
||||||
|
|
||||||
assert_log_file("v0/tests/missing_semicolon_import.txt", "expected error for missing semicolon");
|
assert_log_file("expected error for missing semicolon");
|
||||||
|
|
||||||
parser_free(m);
|
parser_free(m);
|
||||||
tokenstream_close(ts);
|
tokenstream_close(ts);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_parser_bad_import_name(void) {
|
static void test_parser_bad_import_name(void) {
|
||||||
TokenStream* ts = tokenstream_open("test.c", "module my_module; import ;");
|
TokenStream* ts = tokenstream_get_test();
|
||||||
Module* m = parser_parse(ts);
|
Module* m = parser_parse(ts);
|
||||||
|
|
||||||
assert_log_file("v0/tests/bad_import_name.txt", "expected error for bad import name");
|
assert_log_file("expected error for bad import name");
|
||||||
|
|
||||||
parser_free(m);
|
parser_free(m);
|
||||||
tokenstream_close(ts);
|
tokenstream_close(ts);
|
||||||
|
|||||||
+2
-2
@@ -82,11 +82,11 @@ static void test_tokenstream_void_function_signature(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void test_tokenstream_unknown_token(void) {
|
static void test_tokenstream_unknown_token(void) {
|
||||||
TokenStream* ts = tokenstream_open("test.c", "%");
|
TokenStream* ts = tokenstream_get_test();
|
||||||
|
|
||||||
if (tokenstream_next(ts).token != TOKEN_UNKNOWN) fail("expected TOKEN_UNKNOWN");
|
if (tokenstream_next(ts).token != TOKEN_UNKNOWN) fail("expected TOKEN_UNKNOWN");
|
||||||
|
|
||||||
assert_log_file("v0/tests/unknown_token.txt", "expected error message for unknown token");
|
assert_log_file("expected error message for unknown token");
|
||||||
|
|
||||||
tokenstream_close(ts);
|
tokenstream_close(ts);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +0,0 @@
|
|||||||
--- test.c ---
|
|
||||||
1| module my_module; import ;
|
|
||||||
^
|
|
||||||
expected module name to import
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
--- test.c ---
|
|
||||||
1| module my_module; import ;
|
|
||||||
^
|
|
||||||
expected module name to import
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
import ;
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
--- test.c ---
|
|
||||||
1| import other_module;
|
|
||||||
^^^^^^
|
|
||||||
expected 'module' keyword
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
import other_module;
|
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
int main() []
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
--- test.c ---
|
--- v0/tests/log_on_line.c2 ---
|
||||||
1| int main() []
|
1| int main() []
|
||||||
^^
|
^^
|
||||||
unexpected token
|
unexpected token
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
--- test.c ---
|
|
||||||
1| int main() []
|
|
||||||
^^
|
|
||||||
unexpected token
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
int main() []
|
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
int main() []
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
--- test.c ---
|
--- v0/tests/log_on_line_variadic.c2 ---
|
||||||
1| int main() []
|
1| int main() []
|
||||||
^^
|
^^
|
||||||
unexpected token 'x'
|
unexpected token 'x'
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
--- test.c ---
|
|
||||||
1| int main() []
|
|
||||||
^^
|
|
||||||
unexpected token 'x'
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
int main() []
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
--- test.c ---
|
|
||||||
1| module my_module; import other_module
|
|
||||||
^
|
|
||||||
expected ';' after import
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
--- test.c ---
|
|
||||||
1| module my_module; import other_module
|
|
||||||
^
|
|
||||||
expected ';' after import
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
module my_module; import other_module
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
--- test.c ---
|
|
||||||
1| module my_module
|
|
||||||
^
|
|
||||||
expected ';' after module name
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
--- test.c ---
|
|
||||||
1| module my_module
|
|
||||||
^
|
|
||||||
expected ';' after module name
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
module my_module
|
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
import ;
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
--- v0/tests/parser_bad_impo ---
|
||||||
|
1| import ;
|
||||||
|
^^^^^^
|
||||||
|
expected 'module' keyword
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
import other_module;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
--- test.c ---
|
--- v0/tests/parser_bad_modu ---
|
||||||
1| import other_module;
|
1| import other_module;
|
||||||
^^^^^^
|
^^^^^^
|
||||||
expected 'module' keyword
|
expected 'module' keyword
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
module my_module; import other_module
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
---
|
||||||
|
---
|
||||||
|
2|
|
||||||
|
^
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
module my_module
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
---
|
||||||
|
2|
|
||||||
|
^
|
||||||
|
expected ';' after module name
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
%
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
--- test.c ---
|
--- ---
|
||||||
1| %
|
1| %
|
||||||
^
|
^
|
||||||
unexpected token '%'
|
unexpected token '%'
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
--- test.c ---
|
|
||||||
1| %
|
|
||||||
^
|
|
||||||
unexpected token '%'
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
%
|
|
||||||
Reference in New Issue
Block a user