PorygonLang/tests/TreeStringTests.cpp

286 lines
9.9 KiB
C++

#ifdef TESTS_BUILD
#include <catch.hpp>
#include <sstream>
#include "../src/ScriptTypes/ScriptType.hpp"
#include "../src/Binder/BoundStatements/BoundStatement.hpp"
using namespace Porygon;
using namespace Porygon::Binder;
using namespace Porygon::Utilities;
TEST_CASE( "Bad Statement To String", "[BoundTreeString]" ) {
std::stringstream stream;
auto s = new BoundBadStatement();
s->GetTreeString(stream, 1);
REQUIRE(stream.str() == "\tBadStatement");
delete s;
}
TEST_CASE( "Break Statement To String", "[BoundTreeString]" ) {
std::stringstream stream;
auto s = new BoundBreakStatement();
s->GetTreeString(stream, 1);
REQUIRE(stream.str() == "\tBreakStatement");
delete s;
}
TEST_CASE( "Block Statement To String", "[BoundTreeString]" ) {
std::stringstream stream;
auto s = new BoundBlockStatement({new BoundBreakStatement(), new BoundBreakStatement()});
s->GetTreeString(stream, 1);
REQUIRE(stream.str() == "\tBlockStatement\n\t\tBreakStatement\n\t\tBreakStatement");
delete s;
}
TEST_CASE( "Expression Statement To String", "[BoundTreeString]" ) {
std::stringstream stream;
auto s = new BoundExpressionStatement(new BoundNilExpression(0,0));
s->GetTreeString(stream, 1);
REQUIRE(stream.str() == "\tExpressionStatement\n\t\tNilExpression (nil)");
delete s;
}
TEST_CASE( "Assignment Statement To String", "[BoundTreeString]" ) {
std::stringstream stream;
auto key = new u16string(u"key");
const BoundVariableKey *keyObj = new BoundVariableKey(HashedString(key), 0, true, ScriptType::NilType);
auto s = new BoundAssignmentStatement(keyObj, new BoundLiteralIntegerExpression(5, 0,0));
s->GetTreeString(stream, 1);
REQUIRE(stream.str() == "\tAssignment -> global key\n\t\tLiteralInteger: 5 (numeric (Known integer))");
delete s;
}
TEST_CASE( "Index Assignment Statement To String", "[BoundTreeString]" ) {
std::stringstream stream;
auto s = new BoundIndexAssignmentStatement(new BoundNilExpression(0,0), new BoundNilExpression(0,0));
s->GetTreeString(stream, 1);
REQUIRE(stream.str() == "\tIndexAssignment\n\t\tNilExpression (nil)\n\t\tNilExpression (nil)");
delete s;
}
TEST_CASE( "Return Statement with expression To String", "[BoundTreeString]" ) {
std::stringstream stream;
auto s = new BoundReturnStatement(new BoundNilExpression(0,0));
s->GetTreeString(stream, 1);
REQUIRE(stream.str() == "\tReturnStatement\n\t\tNilExpression (nil)");
delete s;
}
TEST_CASE( "Return Statement without expression To String", "[BoundTreeString]" ) {
std::stringstream stream;
auto s = new BoundReturnStatement(nullptr);
s->GetTreeString(stream, 1);
REQUIRE(stream.str() == "\tReturnStatement");
delete s;
}
TEST_CASE( "Conditional To String", "[BoundTreeString]" ) {
std::stringstream stream;
auto s = new BoundConditionalStatement(new BoundLiteralBoolExpression(true,0,0),
new BoundBadStatement(), new BoundBadStatement());
s->GetTreeString(stream, 1);
REQUIRE(stream.str() ==
R"( ConditionalStatement
Condition:
LiteralBool: 1 (bool)
If True:
BadStatement
Else:
BadStatement)");
delete s;
}
TEST_CASE( "Numerical For to String", "[BoundTreeString]" ) {
std::stringstream stream;
auto key = new u16string(u"i");
const BoundVariableKey *keyObj = new BoundVariableKey(HashedString(key), 0, true, NumericScriptType::AwareInt);
auto s = new BoundNumericalForStatement(keyObj, new BoundLiteralIntegerExpression(0,0,0),
new BoundLiteralIntegerExpression(5,0,0),
new BoundLiteralIntegerExpression(1,0,0),
new BoundBadStatement());
s->GetTreeString(stream, 1);
REQUIRE(stream.str() ==
R"( NumericForLoopStatement
Start:
LiteralInteger: 0 (numeric (Known integer))
End:
LiteralInteger: 5 (numeric (Known integer))
Step:
LiteralInteger: 1 (numeric (Known integer))
Do:
BadStatement)");
delete s;
}
TEST_CASE( "Generic For to String", "[BoundTreeString]" ) {
std::stringstream stream;
auto key = new u16string(u"k");
const BoundVariableKey *keyObj = new BoundVariableKey(HashedString(key), 0, true, NumericScriptType::AwareInt);
auto valueKey = new u16string(u"v");
const BoundVariableKey *valueKeyObj = new BoundVariableKey(HashedString(valueKey), 0, true, NumericScriptType::AwareInt);
auto s = new BoundGenericForStatement(keyObj, valueKeyObj, new BoundNilExpression(0,0), new BoundBadStatement());
s->GetTreeString(stream, 1);
REQUIRE(stream.str() ==
R"( GenericForLoopStatement
Key: k (numeric (Known integer))
Value: v (numeric (Known integer))
Iterator:
NilExpression (nil)
Do:
BadStatement)");
delete s;
}
TEST_CASE( "While To String", "[BoundTreeString]" ) {
std::stringstream stream;
auto s = new BoundWhileStatement(new BoundLiteralBoolExpression(true,0,0),
new BoundBadStatement());
s->GetTreeString(stream, 1);
REQUIRE(stream.str() ==
R"( WhileStatement
Condition:
LiteralBool: 1 (bool)
While True:
BadStatement)");
delete s;
}
TEST_CASE( "Function Declaration To String", "[BoundTreeString]" ) {
std::stringstream stream;
auto t = make_shared<const GenericFunctionScriptType>();
auto key = new u16string(u"func");
const BoundVariableKey *keyObj = new BoundVariableKey(HashedString(key), 0, true, NumericScriptType::AwareInt);
auto s = new BoundFunctionDeclarationStatement(t, keyObj, new BoundBlockStatement({}));
s->GetTreeString(stream, 1);
REQUIRE(stream.str() ==
R"( FunctionDeclaration
Key: func
Type: function
Block:
BlockStatement)");
delete s;
}
TEST_CASE( "Bad Expression To String", "[BoundTreeString]" ) {
std::stringstream stream;
auto s = new BoundBadExpression(0,0);
s->GetTreeString(stream, 1);
REQUIRE(stream.str() == "\tBadExpression");
delete s;
}
TEST_CASE( "Literal Integer Expression To String", "[BoundTreeString]" ) {
std::stringstream stream;
auto s = new BoundLiteralIntegerExpression(684,0,0);
s->GetTreeString(stream, 1);
REQUIRE(stream.str() == "\tLiteralInteger: 684 (numeric (Known integer))");
delete s;
}
TEST_CASE( "Literal Float Expression To String", "[BoundTreeString]" ) {
std::stringstream stream;
auto s = new BoundLiteralFloatExpression(5.5,0,0);
s->GetTreeString(stream, 1);
REQUIRE(stream.str() == "\tLiteralFloat: 5.5 (numeric (Known float))");
delete s;
}
TEST_CASE( "Literal String Expression To String", "[BoundTreeString]" ) {
std::stringstream stream;
auto s = new BoundLiteralStringExpression(u"foobar",0,0);
s->GetTreeString(stream, 1);
REQUIRE(stream.str() == "\tLiteralString: \"foobar\" (string)");
delete s;
}
TEST_CASE( "Literal Bool Expression To String", "[BoundTreeString]" ) {
std::stringstream stream;
auto s = new BoundLiteralBoolExpression(true, 0,0);
s->GetTreeString(stream, 1);
REQUIRE(stream.str() == "\tLiteralBool: 1 (bool)");
delete s;
}
TEST_CASE( "Nil Expression To String", "[BoundTreeString]" ) {
std::stringstream stream;
auto s = new BoundNilExpression(0,0);
s->GetTreeString(stream, 1);
REQUIRE(stream.str() == "\tNilExpression (nil)");
delete s;
}
TEST_CASE( "Variable Expression To String", "[BoundTreeString]" ) {
std::stringstream stream;
auto type = ScriptType::BoolType;
auto key = new BoundVariableKey(HashedString(new u16string(u"var")), 0, false, NumericScriptType::AwareInt);
auto s = new BoundVariableExpression(key, type,0,0);
s->GetTreeString(stream, 1);
REQUIRE(stream.str() == "\tVariableExpression: var (bool)");
delete s;
}
TEST_CASE( "Binary Expression To String", "[BoundTreeString]" ) {
std::stringstream stream;
auto s = new BoundBinaryExpression(
new BoundLiteralIntegerExpression(5,0,0),
new BoundLiteralIntegerExpression(200,0,0),
BoundBinaryOperation ::Addition,
NumericScriptType::AwareInt, 0,0
);
s->GetTreeString(stream, 1);
REQUIRE(stream.str() == "\tBinaryExpression: addition (numeric (Known integer))\n\t\tLiteralInteger: 5 (numeric (Known integer))\n\t\tLiteralInteger: 200 (numeric (Known integer))");
delete s;
}
TEST_CASE( "Unary Expression To String", "[BoundTreeString]" ) {
std::stringstream stream;
auto s = new BoundUnaryExpression(
new BoundLiteralIntegerExpression(5,0,0),
BoundUnaryOperation ::Negation,
NumericScriptType::AwareInt, 0,0
);
s->GetTreeString(stream, 1);
REQUIRE(stream.str() == "\tUnaryExpression: negation (numeric (Known integer))\n\t\tLiteralInteger: 5 (numeric (Known integer))");
delete s;
}
TEST_CASE( "Index Expression To String", "[BoundTreeString]" ) {
std::stringstream stream;
auto s = new BoundIndexExpression(
new BoundBadExpression(0,0),
new BoundBadExpression(0,0),
ScriptType::NilType,
0,0
);
s->GetTreeString(stream, 1);
REQUIRE(stream.str() == "\tIndexExpression (nil)\n\t\tBadExpression\n\t\tBadExpression");
delete s;
}
TEST_CASE( "Period Index Expression To String", "[BoundTreeString]" ) {
std::stringstream stream;
auto s = new BoundPeriodIndexExpression(
new BoundBadExpression(0,0),
HashedString(new u16string(u"key")),
ScriptType::NilType,
0,0
);
s->GetTreeString(stream, 1);
REQUIRE(stream.str() == "\tPeriodIndex: key (nil)\n\t\tBadExpression");
delete s;
}
TEST_CASE( "Cast Expression To String", "[BoundTreeString]" ) {
std::stringstream stream;
auto s = new BoundCastExpression(
new BoundBadExpression(0,0),
ScriptType::BoolType
);
s->GetTreeString(stream, 1);
REQUIRE(stream.str() == "\tCastExpression (bool)\n\t\tBadExpression");
delete s;
}
#endif