made tests for return statements

This commit is contained in:
Karma Riuk
2025-07-07 17:39:07 +02:00
parent 7b916b2a0b
commit 39eafe2360

39
test/parser/return.cpp Normal file
View File

@@ -0,0 +1,39 @@
#include "ast/ast.hpp"
#include "lexer/lexer.hpp"
#include "parser/parser.hpp"
#include "utils.hpp"
#include <doctest.h>
#include <sstream>
TEST_CASE("Parse return statement") {
std::stringstream input("\
return 5;\
return 10;\
return 103213;\
");
lexer::lexer l{input};
parser::parser p{l};
ast::program* program = p.parse_program();
check_parser_errors(p.errors);
REQUIRE_MESSAGE(
program != nullptr,
"parse_program() returned a null pointer"
);
REQUIRE(program->statements.size() == 3);
for (const auto stmt : program->statements) {
REQUIRE(stmt->token_literal() == "return");
ast::let* let_stmt;
REQUIRE_NOTHROW(let_stmt = dynamic_cast<ast::let*>(stmt));
REQUIRE_MESSAGE(
let_stmt != nullptr,
"Couldn't cast statement to a return statement"
);
}
delete program;
}