From 39eafe23600a627bc8b19b662846936ee64e8f3f Mon Sep 17 00:00:00 2001 From: Karma Riuk Date: Mon, 7 Jul 2025 17:39:07 +0200 Subject: [PATCH] made tests for return statements --- test/parser/return.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 test/parser/return.cpp diff --git a/test/parser/return.cpp b/test/parser/return.cpp new file mode 100644 index 0000000..45d283b --- /dev/null +++ b/test/parser/return.cpp @@ -0,0 +1,39 @@ +#include "ast/ast.hpp" +#include "lexer/lexer.hpp" +#include "parser/parser.hpp" +#include "utils.hpp" + +#include +#include + +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(stmt)); + REQUIRE_MESSAGE( + let_stmt != nullptr, + "Couldn't cast statement to a return statement" + ); + } + + delete program; +}