Refactor test interface

This commit is contained in:
2026-04-29 10:59:06 +02:00
parent 34b7939f76
commit 3288efdfd7
5 changed files with 98 additions and 75 deletions
+10 -36
View File
@@ -6,16 +6,10 @@
static void test_parser_module_name(void) {
TokenStream* ts = tokenstream_open("test.c", "module my_module;");
Module* m = parser_parse(ts);
char* buf;
assert_not_null(m, "expected module to be parsed");
buf = malloc(m->name.text.length + 1);
memcpy(buf, m->name.text.data, m->name.text.length);
buf[m->name.text.length] = '\0';
assert_str("my_module", buf, "expected name 'my_module'");
free(buf);
parser_free(m);
assert_string("my_module", m->name.text, "expected name 'my_module'");
parser_free(m);
tokenstream_close(ts);
}
@@ -62,49 +56,29 @@ static void test_parser_bad_import_name(void) {
static void test_parser_imports(void) {
TokenStream* ts = tokenstream_open("test.c", "module my_module; import other_module;");
Module* m = parser_parse(ts);
char* buf;
assert_not_null(m, "expected module to be parsed");
buf = malloc(m->name.text.length + 1);
memcpy(buf, m->name.text.data, m->name.text.length);
buf[m->name.text.length] = '\0';
assert_str("my_module", buf, "expected name 'my_module'");
free(buf);
assert_string("my_module", m->name.text, "expected name 'my_module'");
assert_not_null(m->imports, "expected imports to be parsed");
assert_int(1, (int)m->import_count, "expected one import");
buf = malloc(m->imports[0].module_name.text.length + 1);
memcpy(buf, m->imports[0].module_name.text.data, m->imports[0].module_name.text.length);
buf[m->imports[0].module_name.text.length] = '\0';
assert_str("other_module", buf, "expected import name 'other_module'");
free(buf);
assert_string("other_module", m->imports[0].module_name.text, "expected import name 'other_module'");
assert_false(m->imports[0].is_public, "expected import to not be public");
parser_free(m);
parser_free(m);
tokenstream_close(ts);
}
static void test_parser_public_imports(void) {
TokenStream* ts = tokenstream_open("test.c", "module my_module; import public other_module;");
Module* m = parser_parse(ts);
char* buf;
assert_not_null(m, "expected module to be parsed");
buf = malloc(m->name.text.length + 1);
memcpy(buf, m->name.text.data, m->name.text.length);
buf[m->name.text.length] = '\0';
assert_str("my_module", buf, "expected name 'my_module'");
free(buf);
assert_string("my_module", m->name.text, "expected name 'my_module'");
assert_not_null(m->imports, "expected imports to be parsed");
assert_int(1, (int)m->import_count, "expected one import");
buf = malloc(m->imports[0].module_name.text.length + 1);
memcpy(buf, m->imports[0].module_name.text.data, m->imports[0].module_name.text.length);
buf[m->imports[0].module_name.text.length] = '\0';
assert_str("other_module", buf, "expected import name 'other_module'");
free(buf);
assert_string("other_module", m->imports[0].module_name.text, "expected import name 'other_module'");
assert_true(m->imports[0].is_public, "expected import to be public");
parser_free(m);
parser_free(m);
tokenstream_close(ts);
}