added boolean literals and tests for them (no
parsing yet)
This commit is contained in:
@@ -35,6 +35,50 @@ TEST_SUITE("Parser: expression") {
|
||||
test_integer_literal(expression_stmt->expression, 5);
|
||||
};
|
||||
|
||||
TEST_CASE_FIXTURE(ParserFixture, "Simple statements with booleans") {
|
||||
SUBCASE("True literal") {
|
||||
setup("true;");
|
||||
REQUIRE(program->statements.size() == 1);
|
||||
|
||||
ast::expression_stmt* expression_stmt =
|
||||
cast<ast::expression_stmt>(program->statements[0]);
|
||||
|
||||
test_boolean_literal(expression_stmt->expression, true);
|
||||
}
|
||||
|
||||
SUBCASE("False literal") {
|
||||
setup("false;");
|
||||
REQUIRE(program->statements.size() == 1);
|
||||
|
||||
ast::expression_stmt* expression_stmt =
|
||||
cast<ast::expression_stmt>(program->statements[0]);
|
||||
|
||||
test_boolean_literal(expression_stmt->expression, false);
|
||||
}
|
||||
|
||||
SUBCASE("True let statement") {
|
||||
setup("let foo = true;");
|
||||
REQUIRE(program->statements.size() == 1);
|
||||
|
||||
ast::let_stmt* let_stmt =
|
||||
cast<ast::let_stmt>(program->statements[0]);
|
||||
|
||||
CHECK(let_stmt->name->value == "foo");
|
||||
test_boolean_literal(let_stmt->value, true);
|
||||
}
|
||||
|
||||
SUBCASE("False let statement") {
|
||||
setup("let bar = false;");
|
||||
REQUIRE(program->statements.size() == 1);
|
||||
|
||||
ast::let_stmt* let_stmt =
|
||||
cast<ast::let_stmt>(program->statements[0]);
|
||||
|
||||
CHECK(let_stmt->name->value == "bar");
|
||||
test_boolean_literal(let_stmt->value, false);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_FIXTURE(
|
||||
ParserFixture,
|
||||
"Simple expression statement with prefix before integer"
|
||||
@@ -90,7 +134,6 @@ TEST_SUITE("Parser: expression") {
|
||||
#undef CASE
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE_FIXTURE(ParserFixture, "Slightly more complex infix expression") {
|
||||
setup("5 - -15;");
|
||||
|
||||
|
@@ -1,5 +1,6 @@
|
||||
#include "utils.hpp"
|
||||
|
||||
#include "ast/expressions/boolean.hpp"
|
||||
#include "ast/expressions/identifier.hpp"
|
||||
#include "ast/expressions/infix.hpp"
|
||||
#include "ast/expressions/integer.hpp"
|
||||
@@ -36,6 +37,13 @@ void ParserFixture::setup(std::string source) {
|
||||
);
|
||||
}
|
||||
|
||||
void test_identifier(ast::expression* expr, std::string value) {
|
||||
ast::identifier* ident = cast<ast::identifier>(expr);
|
||||
|
||||
REQUIRE(ident->value == value);
|
||||
REQUIRE(ident->token_literal() == value);
|
||||
}
|
||||
|
||||
void test_integer_literal(ast::expression* expr, int value) {
|
||||
ast::integer_literal* int_lit = cast<ast::integer_literal>(expr);
|
||||
|
||||
@@ -45,16 +53,20 @@ void test_integer_literal(ast::expression* expr, int value) {
|
||||
REQUIRE(int_lit->token_literal() == oss.str());
|
||||
}
|
||||
|
||||
void test_identifier(ast::expression* expr, std::string value) {
|
||||
ast::identifier* ident = cast<ast::identifier>(expr);
|
||||
void test_boolean_literal(ast::expression* expr, bool value) {
|
||||
ast::boolean_literal* bool_lit = cast<ast::boolean_literal>(expr);
|
||||
|
||||
REQUIRE(ident->value == value);
|
||||
REQUIRE(ident->token_literal() == value);
|
||||
REQUIRE(bool_lit->value == value);
|
||||
std::ostringstream oss;
|
||||
oss << value;
|
||||
REQUIRE(bool_lit->token_literal() == oss.str());
|
||||
}
|
||||
|
||||
void test_literal_expression(ast::expression* exp, std::any& expected) {
|
||||
if (expected.type() == typeid(int))
|
||||
return test_integer_literal(exp, std::any_cast<int>(expected));
|
||||
if (expected.type() == typeid(bool))
|
||||
return test_boolean_literal(exp, std::any_cast<bool>(expected));
|
||||
if (expected.type() == typeid(std::string))
|
||||
return test_identifier(exp, std::any_cast<std::string>(expected));
|
||||
if (expected.type() == typeid(const char*))
|
||||
|
@@ -64,5 +64,6 @@ struct ParserFixture {
|
||||
|
||||
void test_identifier(ast::expression*, std::string);
|
||||
void test_integer_literal(ast::expression*, int);
|
||||
void test_boolean_literal(ast::expression*, bool);
|
||||
void test_literal_expression(ast::expression*, std::any&);
|
||||
void test_infix_expression(ast::expression*, std::any, std::string, std::any);
|
||||
|
Reference in New Issue
Block a user