MalachScript/tests/ParserTests/ParserIntegrationTests.cpp

51 lines
2.9 KiB
C++

#include "../../extern/doctest.hpp"
#include "../../src/Parser/Lexer/Lexer.hpp"
#include "../../src/Parser/Parser.hpp"
using namespace MalachScript;
#define PARSE_TEST(name, scriptText, asserts) \
TEST_CASE(name) { \
Diagnostics::Logger diags; \
auto lexer = Parser::Lexer(name, scriptText, &diags); \
auto token = lexer.Lex(); \
auto script = Parser::Parser::Parse(token, &diags); \
asserts; \
delete script; \
}
PARSE_TEST("Parse class without definition", "class foobar;", {
REQUIRE(diags.GetMessages().empty());
REQUIRE(script->GetStatements().size() == 1);
REQUIRE(script->GetStatements()[0].get()->GetKind() == Parser::ParsedStatementKind::Class);
})
PARSE_TEST("Parse class with empty definition", "class foobar {}", {
REQUIRE(diags.GetMessages().empty());
REQUIRE(script->GetStatements().size() == 1);
REQUIRE(script->GetStatements()[0].get()->GetKind() == Parser::ParsedStatementKind::Class);
})
PARSE_TEST("Parse function without definition", "void foobar(int8 par1, bool &in x);", {
REQUIRE(diags.GetMessages().empty());
REQUIRE(script->GetStatements().size() == 1);
REQUIRE(script->GetStatements()[0].get()->GetKind() == Parser::ParsedStatementKind::Func);
})
PARSE_TEST("Parse class with virtprop", "class foobar { private bool foo { get; set; } }", {
REQUIRE(diags.GetMessages().empty());
REQUIRE(script->GetStatements().size() == 1);
auto firstStatement = script->GetStatements()[0].get();
REQUIRE(firstStatement->GetKind() == Parser::ParsedStatementKind::Class);
auto firstClassStatement =
dynamic_cast<const MalachScript::Parser::ParsedClassStatement*>(firstStatement)->GetBody()[0].get();
REQUIRE(firstClassStatement->GetKind() == Parser::ParsedStatementKind::VirtProp);
auto virtPropStatement = dynamic_cast<const MalachScript::Parser::ParsedVirtPropStatement*>(firstClassStatement);
REQUIRE(virtPropStatement->GetAccess() == MalachScript::AccessModifier::Private);
REQUIRE(virtPropStatement->GetIdentifier().GetString() == "foo");
REQUIRE(virtPropStatement->HasGet());
REQUIRE(virtPropStatement->HasSet());
REQUIRE(virtPropStatement->GetGetStatement() == nullptr);
REQUIRE(virtPropStatement->GetSetStatement() == nullptr);
})