moved the utils for the parser to a global utils

folder for the tests (that utils is includable
only by the tests and not the src code, I added a
compiler flag only for the tests in the makefile,
but the compiler_flags.txt is global for the lsp,
gotta be careful with that)
This commit is contained in:
Karma Riuk
2025-07-19 13:27:25 +02:00
parent d1dd5f9dab
commit c943380d58
11 changed files with 419 additions and 338 deletions

View File

@@ -9,7 +9,7 @@
TEST_SUITE("Parser: if") {
TEST_CASE("Malformed if then else (checking for memory leaks)") {
SUBCASE("Missing opening paren") {
test_failing_parsing(
test::utils::test_failing_parsing(
"if x > 15) {\
return x;\
}",
@@ -18,7 +18,7 @@ TEST_SUITE("Parser: if") {
}
SUBCASE("Missing closing paren") {
test_failing_parsing(
test::utils::test_failing_parsing(
"if (x > 15 {\
return x;\
}",
@@ -27,7 +27,7 @@ TEST_SUITE("Parser: if") {
}
SUBCASE("Missing opening brace") {
test_failing_parsing(
test::utils::test_failing_parsing(
"if (x > 15) \
return x;\
}",
@@ -36,7 +36,7 @@ TEST_SUITE("Parser: if") {
}
SUBCASE("Missing closing brace") {
test_failing_parsing(
test::utils::test_failing_parsing(
"if (x > 15) { \
return x;\
",
@@ -45,7 +45,10 @@ TEST_SUITE("Parser: if") {
}
}
TEST_CASE_FIXTURE(ParserFixture, "Parse well formed simple if ") {
TEST_CASE_FIXTURE(
test::utils::ParserFixture,
"Parse well formed simple if "
) {
setup("\
if (x > 15) {\
return x;\
@@ -55,21 +58,23 @@ if (x > 15) {\
CHECK(program->statements[0]->token_literal() == "if");
ast::expression_stmt* expr_stmt =
cast<ast::expression_stmt>(program->statements[0]);
test::utils::cast<ast::expression_stmt>(program->statements[0]);
ast::if_then_else* if_stmt =
cast<ast::if_then_else>(expr_stmt->expression);
test::utils::cast<ast::if_then_else>(expr_stmt->expression);
// condition
ast::infix_expr* cond = cast<ast::infix_expr>(if_stmt->condition);
test_infix_expression(cond, "x", ">", 15);
ast::infix_expr* cond =
test::utils::cast<ast::infix_expr>(if_stmt->condition);
test::utils::test_infix_expression(cond, "x", ">", 15);
CHECK(if_stmt->condition->str() == "(x > 15)");
// consequence
REQUIRE(if_stmt->consequence->statements.size() == 1);
ast::return_stmt* ret_stmt =
cast<ast::return_stmt>(if_stmt->consequence->statements[0]);
test_identifier(ret_stmt->value, "x");
ast::return_stmt* ret_stmt = test::utils::cast<ast::return_stmt>(
if_stmt->consequence->statements[0]
);
test::utils::test_identifier(ret_stmt->value, "x");
CHECK(if_stmt->consequence->str() == "{return x;}");
// alternative
@@ -79,7 +84,10 @@ if (x > 15) {\
CHECK(if_stmt->str() == "if (x > 15){return x;}");
}
TEST_CASE_FIXTURE(ParserFixture, "Parse well formed if then else") {
TEST_CASE_FIXTURE(
test::utils::ParserFixture,
"Parse well formed if then else"
) {
setup("\
if (x < 5) {\
return 15;\
@@ -92,34 +100,38 @@ if (x < 5) {\
CHECK(program->statements[0]->token_literal() == "if");
ast::expression_stmt* expr_stmt =
cast<ast::expression_stmt>(program->statements[0]);
test::utils::cast<ast::expression_stmt>(program->statements[0]);
ast::if_then_else* if_stmt =
cast<ast::if_then_else>(expr_stmt->expression);
test::utils::cast<ast::if_then_else>(expr_stmt->expression);
// condition
ast::infix_expr* cond = cast<ast::infix_expr>(if_stmt->condition);
test_infix_expression(cond, "x", "<", 5);
ast::infix_expr* cond =
test::utils::cast<ast::infix_expr>(if_stmt->condition);
test::utils::test_infix_expression(cond, "x", "<", 5);
CHECK(if_stmt->condition->str() == "(x < 5)");
// consequence
REQUIRE(if_stmt->consequence->statements.size() == 1);
ast::return_stmt* ret_stmt =
cast<ast::return_stmt>(if_stmt->consequence->statements[0]);
test_integer_literal(ret_stmt->value, 15);
ast::return_stmt* ret_stmt = test::utils::cast<ast::return_stmt>(
if_stmt->consequence->statements[0]
);
test::utils::test_integer_literal(ret_stmt->value, 15);
CHECK(if_stmt->consequence->str() == "{return 15;}");
// alternative
REQUIRE(if_stmt->alternative != nullptr);
REQUIRE(if_stmt->alternative->statements.size() == 2);
ast::let_stmt* let_stmt =
cast<ast::let_stmt>(if_stmt->alternative->statements[0]);
test_identifier(let_stmt->name, "b");
test_integer_literal(let_stmt->value, 1);
ast::let_stmt* let_stmt = test::utils::cast<ast::let_stmt>(
if_stmt->alternative->statements[0]
);
test::utils::test_identifier(let_stmt->name, "b");
test::utils::test_integer_literal(let_stmt->value, 1);
ast::return_stmt* ret_stmt2 =
cast<ast::return_stmt>(if_stmt->alternative->statements[1]);
test_identifier(ret_stmt2->value, "x");
ast::return_stmt* ret_stmt2 = test::utils::cast<ast::return_stmt>(
if_stmt->alternative->statements[1]
);
test::utils::test_identifier(ret_stmt2->value, "x");
CHECK(if_stmt->alternative->str() == "{let b = 1;return x;}");
// full string