PorygonLang/tests/TreeStringTests.cpp

148 lines
4.9 KiB
C++

#ifdef TESTS_BUILD
#include <catch.hpp>
#include <sstream>
#include "../src/Binder/BoundStatements/BoundStatement.hpp"
#include "../src/Utilities/HashedString.hpp"
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);
auto s = new BoundAssignmentStatement(keyObj, new BoundLiteralIntegerExpression(5, 0,0));
s->GetTreeString(stream, 1);
REQUIRE(stream.str() == "\tAssignment -> key\n\t\tLiteralInteger: 5 (number)");
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);
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 (number)
End:
LiteralInteger: 5 (number)
Step:
LiteralInteger: 1 (number)
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);
auto valueKey = new u16string(u"v");
const BoundVariableKey *valueKeyObj = new BoundVariableKey(HashedString(valueKey), 0, true);
auto s = new BoundGenericForStatement(keyObj, valueKeyObj, new BoundNilExpression(0,0), new BoundBadStatement());
s->GetTreeString(stream, 1);
REQUIRE(stream.str() ==
R"( GenericForLoopStatement
Key: k
Value: v
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;
}
#endif