PorygonLang/tests/integration/LoopTests.cpp

127 lines
3.1 KiB
C++
Raw Normal View History

2019-06-22 15:35:33 +00:00
#ifdef TESTS_BUILD
#include <catch.hpp>
#include "../src/Script.hpp"
using namespace Porygon;
TEST_CASE( "Numerical for loop without step", "[integration]" ) {
auto script = Script::Create(uR"(
result = 0
for i = 0,10 do
result = result + 3
end
)");
REQUIRE(!script->Diagnostics -> HasErrors());
script->Evaluate();
auto var = script->GetVariable(u"result");
REQUIRE(var->EvaluateInteger() == 33);
delete script;
}
TEST_CASE( "Numerical for loop with step", "[integration]" ) {
auto script = Script::Create(uR"(
result = 0
for i = 0,10,3 do
result = result + 3
end
)");
REQUIRE(!script->Diagnostics -> HasErrors());
script->Evaluate();
auto var = script->GetVariable(u"result");
REQUIRE(var->EvaluateInteger() == 12);
delete script;
}
TEST_CASE( "Numerical for loop with negative step", "[integration]" ) {
auto script = Script::Create(uR"(
result = 0
for i = 10,0,-1 do
result = result + 3
end
)");
REQUIRE(!script->Diagnostics -> HasErrors());
script->Evaluate();
auto var = script->GetVariable(u"result");
REQUIRE(var->EvaluateInteger() == 33);
delete script;
}
TEST_CASE( "Numerical for loop creates variable", "[integration]" ) {
auto script = Script::Create(uR"(
result = 0
for i = 0,5 do
result = result + i
end
)");
REQUIRE(!script->Diagnostics -> HasErrors());
script->Evaluate();
auto var = script->GetVariable(u"result");
REQUIRE(var->EvaluateInteger() == 15);
delete script;
}
2019-06-27 13:55:46 +00:00
TEST_CASE( "Numerical for loop, break", "[integration]" ) {
auto script = Script::Create(uR"(
result = 0
for i = 0,5 do
if i > 3 then break end
result = result + i
end
)");
REQUIRE(!script->Diagnostics -> HasErrors());
script->Evaluate();
auto var = script->GetVariable(u"result");
REQUIRE(var->EvaluateInteger() == 6);
delete script;
}
2019-06-26 14:19:34 +00:00
TEST_CASE( "Generic for loop over simple numerical table, get keys", "[integration]" ) {
auto script = Script::Create(uR"(
local table = {1, 3, 5, 7, 9}
result = 0
2019-06-26 14:19:34 +00:00
for i in table do
result = result + i
end
)");
REQUIRE(!script->Diagnostics -> HasErrors());
script->Evaluate();
auto var = script->GetVariable(u"result");
REQUIRE(var->EvaluateInteger() == 15);
2019-06-26 14:19:34 +00:00
delete script;
}
TEST_CASE( "Generic for loop over simple numerical table, get values", "[integration]" ) {
auto script = Script::Create(uR"(
local table = {1, 3, 5, 7, 9}
result = 0
2019-06-26 14:19:34 +00:00
for i,v in table do
result = result + v
end
)");
REQUIRE(!script->Diagnostics -> HasErrors());
script->Evaluate();
auto var = script->GetVariable(u"result");
REQUIRE(var->EvaluateInteger() == 25);
2019-06-26 14:19:34 +00:00
delete script;
}
2019-06-27 13:55:46 +00:00
TEST_CASE( "Generic for loop over simple numerical table, break", "[integration]" ) {
auto script = Script::Create(uR"(
local table = {1, 3, 5, 7, 9}
result = 0
for i,v in table do
if i > 3 then break end
result = result + v
end
)");
REQUIRE(!script->Diagnostics -> HasErrors());
script->Evaluate();
auto var = script->GetVariable(u"result");
REQUIRE(var->EvaluateInteger() == 9);
delete script;
}
2019-06-22 15:35:33 +00:00
#endif