made more helper functions for testing
This commit is contained in:
@@ -1,20 +1,9 @@
|
|||||||
#include "ast/expressions/identifier.hpp"
|
#include "ast/expressions/identifier.hpp"
|
||||||
#include "ast/expressions/infix.hpp"
|
#include "ast/expressions/infix.hpp"
|
||||||
#include "ast/expressions/integer.hpp"
|
|
||||||
#include "ast/expressions/prefix.hpp"
|
#include "ast/expressions/prefix.hpp"
|
||||||
#include "utils.hpp"
|
#include "utils.hpp"
|
||||||
|
|
||||||
#include <doctest.h>
|
#include <doctest.h>
|
||||||
#include <sstream>
|
|
||||||
|
|
||||||
void test_integer_literal(ast::expression* expr, int value) {
|
|
||||||
ast::integer_literal* int_lit = cast<ast::integer_literal>(expr);
|
|
||||||
|
|
||||||
REQUIRE(int_lit->value == value);
|
|
||||||
std::ostringstream oss;
|
|
||||||
oss << value;
|
|
||||||
REQUIRE(int_lit->token_literal() == oss.str());
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_SUITE("Parser: expression") {
|
TEST_SUITE("Parser: expression") {
|
||||||
TEST_CASE_FIXTURE(
|
TEST_CASE_FIXTURE(
|
||||||
@@ -78,15 +67,16 @@ TEST_SUITE("Parser: expression") {
|
|||||||
SUBCASE(name) { \
|
SUBCASE(name) { \
|
||||||
setup(input); \
|
setup(input); \
|
||||||
REQUIRE(program->statements.size() == 1); \
|
REQUIRE(program->statements.size() == 1); \
|
||||||
|
\
|
||||||
ast::expression_stmt* expression_stmt = \
|
ast::expression_stmt* expression_stmt = \
|
||||||
cast<ast::expression_stmt>(program->statements[0]); \
|
cast<ast::expression_stmt>(program->statements[0]); \
|
||||||
\
|
\
|
||||||
ast::infix_expr* infix_expr = \
|
test_infix_expression( \
|
||||||
cast<ast::infix_expr>(expression_stmt->expression); \
|
expression_stmt->expression, \
|
||||||
\
|
_left, \
|
||||||
test_integer_literal(infix_expr->left, _left); \
|
_op, \
|
||||||
REQUIRE(infix_expr->op == _op); \
|
_right \
|
||||||
test_integer_literal(infix_expr->right, _right); \
|
); \
|
||||||
}
|
}
|
||||||
CASE("Infix: '+'", "5 + 5;", 5, "+", 5);
|
CASE("Infix: '+'", "5 + 5;", 5, "+", 5);
|
||||||
CASE("Infix: '-'", "5- 5;", 5, "-", 5);
|
CASE("Infix: '-'", "5- 5;", 5, "-", 5);
|
||||||
@@ -96,6 +86,7 @@ TEST_SUITE("Parser: expression") {
|
|||||||
CASE("Infix: '>'", "25 > 15;", 25, ">", 15);
|
CASE("Infix: '>'", "25 > 15;", 25, ">", 15);
|
||||||
CASE("Infix: '=='", "5 == 5;", 5, "==", 5);
|
CASE("Infix: '=='", "5 == 5;", 5, "==", 5);
|
||||||
CASE("Infix: '!='", "15 != 5;", 15, "!=", 5);
|
CASE("Infix: '!='", "15 != 5;", 15, "!=", 5);
|
||||||
|
CASE("Infix: between identifiers", "alice * bob;", "alice", "*", "bob");
|
||||||
#undef CASE
|
#undef CASE
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,5 +1,9 @@
|
|||||||
#include "utils.hpp"
|
#include "utils.hpp"
|
||||||
|
|
||||||
|
#include "ast/expressions/identifier.hpp"
|
||||||
|
#include "ast/expressions/infix.hpp"
|
||||||
|
#include "ast/expressions/integer.hpp"
|
||||||
|
|
||||||
#include <doctest.h>
|
#include <doctest.h>
|
||||||
|
|
||||||
void check_parser_errors(const std::vector<ast::error::error*>& errors) {
|
void check_parser_errors(const std::vector<ast::error::error*>& errors) {
|
||||||
@@ -31,3 +35,43 @@ void ParserFixture::setup(std::string source) {
|
|||||||
"parse_program() returned a null pointer"
|
"parse_program() returned a null pointer"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_integer_literal(ast::expression* expr, int value) {
|
||||||
|
ast::integer_literal* int_lit = cast<ast::integer_literal>(expr);
|
||||||
|
|
||||||
|
REQUIRE(int_lit->value == value);
|
||||||
|
std::ostringstream oss;
|
||||||
|
oss << value;
|
||||||
|
REQUIRE(int_lit->token_literal() == oss.str());
|
||||||
|
}
|
||||||
|
|
||||||
|
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_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(std::string))
|
||||||
|
return test_identifier(exp, std::any_cast<std::string>(expected));
|
||||||
|
if (expected.type() == typeid(const char*))
|
||||||
|
return test_identifier(exp, std::any_cast<const char*>(expected));
|
||||||
|
|
||||||
|
FAIL(
|
||||||
|
"Type of exp not handled. Got: " * demangle(typeid(*exp).name())
|
||||||
|
* " as expression and " * demangle(expected.type().name())
|
||||||
|
* " as expected"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_infix_expression(
|
||||||
|
ast::expression* exp, std::any left, std::string op, std::any right
|
||||||
|
) {
|
||||||
|
ast::infix_expr* op_exp = cast<ast::infix_expr>(exp);
|
||||||
|
test_literal_expression(op_exp->left, left);
|
||||||
|
CHECK(op_exp->op == op);
|
||||||
|
test_literal_expression(op_exp->right, right);
|
||||||
|
}
|
||||||
|
@@ -2,6 +2,7 @@
|
|||||||
#include "lexer/lexer.hpp"
|
#include "lexer/lexer.hpp"
|
||||||
#include "parser/parser.hpp"
|
#include "parser/parser.hpp"
|
||||||
|
|
||||||
|
#include <any>
|
||||||
#include <cxxabi.h>
|
#include <cxxabi.h>
|
||||||
#include <doctest.h>
|
#include <doctest.h>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
@@ -60,3 +61,8 @@ struct ParserFixture {
|
|||||||
|
|
||||||
void setup(std::string);
|
void setup(std::string);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void test_identifier(ast::expression*, std::string);
|
||||||
|
void test_integer_literal(ast::expression*, int);
|
||||||
|
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