using test suites
This commit is contained in:
@@ -57,66 +57,68 @@ void test_failing_let_parsing(
|
||||
delete program;
|
||||
}
|
||||
|
||||
TEST_CASE("Malformed let statement (checking for memory leaks)") {
|
||||
SUBCASE("Second token not identifier") {
|
||||
test_failing_let_parsing("let 5 = 5;", {token::type::IDENTIFIER});
|
||||
TEST_SUITE("Parser: let") {
|
||||
TEST_CASE("Malformed let statement (checking for memory leaks)") {
|
||||
SUBCASE("Second token not identifier") {
|
||||
test_failing_let_parsing("let 5 = 5;", {token::type::IDENTIFIER});
|
||||
}
|
||||
|
||||
SUBCASE("Third token not '='") {
|
||||
test_failing_let_parsing("let five ! 5;", {token::type::ASSIGN});
|
||||
}
|
||||
|
||||
SUBCASE("Missing both identifier and '='") {
|
||||
test_failing_let_parsing("let 5;", {token::type::IDENTIFIER});
|
||||
}
|
||||
|
||||
SUBCASE("Multiple parsing errors") {
|
||||
test_failing_let_parsing(
|
||||
"let 5; let ! = 5; let five = 5; let five 5; let;",
|
||||
{token::type::IDENTIFIER,
|
||||
token::type::IDENTIFIER,
|
||||
token::type::ASSIGN,
|
||||
token::type::IDENTIFIER},
|
||||
1
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
SUBCASE("Third token not '='") {
|
||||
test_failing_let_parsing("let five ! 5;", {token::type::ASSIGN});
|
||||
}
|
||||
|
||||
SUBCASE("Missing both identifier and '='") {
|
||||
test_failing_let_parsing("let 5;", {token::type::IDENTIFIER});
|
||||
}
|
||||
|
||||
SUBCASE("Multiple parsing errors") {
|
||||
test_failing_let_parsing(
|
||||
"let 5; let ! = 5; let five = 5; let five 5; let;",
|
||||
{token::type::IDENTIFIER,
|
||||
token::type::IDENTIFIER,
|
||||
token::type::ASSIGN,
|
||||
token::type::IDENTIFIER},
|
||||
1
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Parse let statement") {
|
||||
std::stringstream input("\
|
||||
TEST_CASE("Parse well formed let statements") {
|
||||
std::stringstream input("\
|
||||
let x = 5;\
|
||||
let y = 10;\
|
||||
let foobar = 103213;\
|
||||
");
|
||||
|
||||
lexer::lexer l{input};
|
||||
parser::parser p{l};
|
||||
lexer::lexer l{input};
|
||||
parser::parser p{l};
|
||||
|
||||
ast::program* program = p.parse_program();
|
||||
check_parser_errors(p.errors);
|
||||
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);
|
||||
REQUIRE_MESSAGE(
|
||||
program != nullptr,
|
||||
"parse_program() returned a null pointer"
|
||||
);
|
||||
REQUIRE(program->statements.size() == 3);
|
||||
|
||||
struct test {
|
||||
std::string expected_identifier;
|
||||
};
|
||||
struct test {
|
||||
std::string expected_identifier;
|
||||
};
|
||||
|
||||
test tests[]{
|
||||
"x",
|
||||
"y",
|
||||
"foobar",
|
||||
};
|
||||
test tests[]{
|
||||
"x",
|
||||
"y",
|
||||
"foobar",
|
||||
};
|
||||
|
||||
int i = 0;
|
||||
for (const auto& t : tests) {
|
||||
ast::statement* stmt = program->statements[i++];
|
||||
int i = 0;
|
||||
for (const auto& t : tests) {
|
||||
ast::statement* stmt = program->statements[i++];
|
||||
|
||||
test_let_statement(stmt, t.expected_identifier);
|
||||
test_let_statement(stmt, t.expected_identifier);
|
||||
}
|
||||
|
||||
delete program;
|
||||
}
|
||||
|
||||
delete program;
|
||||
}
|
||||
|
@@ -6,34 +6,36 @@
|
||||
#include <doctest.h>
|
||||
#include <sstream>
|
||||
|
||||
TEST_CASE("Parse return statement") {
|
||||
std::stringstream input("\
|
||||
TEST_SUITE("Parser: return") {
|
||||
TEST_CASE("Parse return statement") {
|
||||
std::stringstream input("\
|
||||
return 5;\
|
||||
return 10;\
|
||||
return 103213;\
|
||||
");
|
||||
|
||||
lexer::lexer l{input};
|
||||
parser::parser p{l};
|
||||
lexer::lexer l{input};
|
||||
parser::parser p{l};
|
||||
|
||||
ast::program* program = p.parse_program();
|
||||
check_parser_errors(p.errors);
|
||||
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::return_stmt* let_stmt;
|
||||
REQUIRE_NOTHROW(let_stmt = dynamic_cast<ast::return_stmt*>(stmt));
|
||||
REQUIRE_MESSAGE(
|
||||
let_stmt != nullptr,
|
||||
"Couldn't cast statement to a return statement"
|
||||
program != nullptr,
|
||||
"parse_program() returned a null pointer"
|
||||
);
|
||||
}
|
||||
REQUIRE(program->statements.size() == 3);
|
||||
|
||||
delete program;
|
||||
for (const auto stmt : program->statements) {
|
||||
REQUIRE(stmt->token_literal() == "return");
|
||||
ast::return_stmt* let_stmt;
|
||||
REQUIRE_NOTHROW(let_stmt = dynamic_cast<ast::return_stmt*>(stmt));
|
||||
REQUIRE_MESSAGE(
|
||||
let_stmt != nullptr,
|
||||
"Couldn't cast statement to a return statement"
|
||||
);
|
||||
}
|
||||
|
||||
delete program;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user