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; +}