Some fixes for statements to string, added more tests
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2019-09-05 12:22:10 +02:00
parent fb142c7f25
commit 256969e912
3 changed files with 166 additions and 48 deletions

View File

@@ -3,7 +3,9 @@
#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;
@@ -29,4 +31,118 @@ TEST_CASE( "Block Statement To String", "[BoundTreeString]" ) {
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