2019-09-02 18:48:52 +00:00
|
|
|
#ifdef TESTS_BUILD
|
|
|
|
#include <catch.hpp>
|
|
|
|
#include <sstream>
|
|
|
|
|
2019-09-05 14:41:12 +00:00
|
|
|
#include "../src/ScriptTypes/ScriptType.hpp"
|
2019-09-02 18:48:52 +00:00
|
|
|
#include "../src/Binder/BoundStatements/BoundStatement.hpp"
|
2019-09-05 10:22:10 +00:00
|
|
|
#include "../src/Utilities/HashedString.hpp"
|
2019-09-05 14:41:12 +00:00
|
|
|
using namespace Porygon;
|
2019-09-02 18:48:52 +00:00
|
|
|
using namespace Porygon::Binder;
|
2019-09-05 10:22:10 +00:00
|
|
|
using namespace Porygon::Utilities;
|
2019-09-02 18:48:52 +00:00
|
|
|
|
|
|
|
TEST_CASE( "Bad Statement To String", "[BoundTreeString]" ) {
|
|
|
|
std::stringstream stream;
|
|
|
|
auto s = new BoundBadStatement();
|
|
|
|
s->GetTreeString(stream, 1);
|
|
|
|
REQUIRE(stream.str() == "\tBadStatement");
|
2019-09-02 18:49:45 +00:00
|
|
|
delete s;
|
2019-09-02 18:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE( "Break Statement To String", "[BoundTreeString]" ) {
|
|
|
|
std::stringstream stream;
|
|
|
|
auto s = new BoundBreakStatement();
|
|
|
|
s->GetTreeString(stream, 1);
|
|
|
|
REQUIRE(stream.str() == "\tBreakStatement");
|
2019-09-02 18:49:45 +00:00
|
|
|
delete s;
|
2019-09-02 18:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
2019-09-02 18:49:45 +00:00
|
|
|
delete s;
|
2019-09-02 18:48:52 +00:00
|
|
|
}
|
|
|
|
|
2019-09-05 10:22:10 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-09-05 14:41:12 +00:00
|
|
|
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);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-09-05 10:22:10 +00:00
|
|
|
|
2019-09-02 18:48:52 +00:00
|
|
|
#endif
|