2019-06-01 10:33:52 +00:00
|
|
|
#ifdef TESTS_BUILD
|
|
|
|
#include <catch.hpp>
|
|
|
|
#include "../src/Script.hpp"
|
|
|
|
|
2019-06-17 16:35:12 +00:00
|
|
|
using namespace Porygon;
|
|
|
|
|
2019-06-01 10:33:52 +00:00
|
|
|
TEST_CASE( "Define script function", "[integration]" ) {
|
|
|
|
Script* script = Script::Create("function add(number a, number b) a + b end");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
script->Evaluate();
|
2019-06-15 15:20:27 +00:00
|
|
|
auto variable = script->GetVariable(u"add");
|
2019-06-01 10:33:52 +00:00
|
|
|
REQUIRE(variable != nullptr);
|
2019-06-13 14:26:10 +00:00
|
|
|
REQUIRE(variable->GetTypeClass() == TypeClass::Function);
|
2019-06-01 10:33:52 +00:00
|
|
|
delete script;
|
2019-07-27 15:59:42 +00:00
|
|
|
delete variable;
|
2019-06-01 10:33:52 +00:00
|
|
|
}
|
|
|
|
|
2019-06-01 17:20:31 +00:00
|
|
|
TEST_CASE( "Define script function and call", "[integration]" ) {
|
|
|
|
Script* script = Script::Create("function add(number a, number b) result = a + b end add(1, 2)");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
script->Evaluate();
|
2019-06-15 15:20:27 +00:00
|
|
|
auto variable = script->GetVariable(u"add");
|
2019-06-01 17:20:31 +00:00
|
|
|
REQUIRE(variable != nullptr);
|
2019-06-13 14:26:10 +00:00
|
|
|
REQUIRE(variable->GetTypeClass() == TypeClass::Function);
|
2019-06-15 15:20:27 +00:00
|
|
|
auto result = script->GetVariable(u"result");
|
2019-06-13 14:26:10 +00:00
|
|
|
REQUIRE(result->GetTypeClass() == TypeClass::Number);
|
2019-06-01 17:20:31 +00:00
|
|
|
REQUIRE(result->EvaluateInteger() == 3);
|
|
|
|
delete script;
|
2019-07-27 15:59:42 +00:00
|
|
|
delete variable;
|
|
|
|
delete result;
|
2019-06-01 17:20:31 +00:00
|
|
|
}
|
|
|
|
|
2019-06-01 19:38:39 +00:00
|
|
|
TEST_CASE( "Define script function and call multiple times", "[integration]" ) {
|
|
|
|
Script* script = Script::Create("result = 0 function add(number a) result = result + a end add(1) add(4)");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
script->Evaluate();
|
2019-06-15 15:20:27 +00:00
|
|
|
auto variable = script->GetVariable(u"add");
|
2019-06-01 19:38:39 +00:00
|
|
|
REQUIRE(variable != nullptr);
|
2019-06-13 14:26:10 +00:00
|
|
|
REQUIRE(variable->GetTypeClass() == TypeClass::Function);
|
2019-06-15 15:20:27 +00:00
|
|
|
auto result = script->GetVariable(u"result");
|
2019-06-13 14:26:10 +00:00
|
|
|
REQUIRE(result->GetTypeClass() == TypeClass::Number);
|
2019-06-01 19:38:39 +00:00
|
|
|
REQUIRE(result->EvaluateInteger() == 5);
|
|
|
|
delete script;
|
2019-07-27 15:59:42 +00:00
|
|
|
delete variable;
|
|
|
|
delete result;
|
2019-06-01 19:38:39 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 16:44:23 +00:00
|
|
|
TEST_CASE( "Define script function and call from extern", "[integration]" ) {
|
|
|
|
Script* script = Script::Create("result = 0 function add(number a) result = result + a end");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
script->Evaluate();
|
|
|
|
|
2019-06-15 15:20:27 +00:00
|
|
|
REQUIRE(script->HasFunction(u"add"));
|
2019-06-05 17:18:11 +00:00
|
|
|
auto toAddVal = new IntegerEvalValue(5);
|
2019-06-15 15:20:27 +00:00
|
|
|
script->CallFunction(u"add", {toAddVal});
|
2019-06-05 17:18:11 +00:00
|
|
|
delete toAddVal;
|
|
|
|
toAddVal = new IntegerEvalValue(6);
|
2019-06-15 15:20:27 +00:00
|
|
|
script->CallFunction(u"add", {toAddVal});
|
2019-06-05 17:18:11 +00:00
|
|
|
delete toAddVal;
|
2019-06-05 16:44:23 +00:00
|
|
|
|
2019-06-15 15:20:27 +00:00
|
|
|
auto result = script->GetVariable(u"result");
|
2019-06-13 14:26:10 +00:00
|
|
|
REQUIRE(result->GetTypeClass() == TypeClass::Number);
|
2019-06-05 16:44:23 +00:00
|
|
|
REQUIRE(result->EvaluateInteger() == 11);
|
|
|
|
delete script;
|
2019-07-27 15:59:42 +00:00
|
|
|
delete result;
|
2019-06-05 16:44:23 +00:00
|
|
|
}
|
|
|
|
|
2019-06-07 13:23:13 +00:00
|
|
|
TEST_CASE( "Define script function and return", "[integration]" ) {
|
|
|
|
Script* script = Script::Create(
|
|
|
|
"val = 0\n"
|
|
|
|
"function add(number a, number b) \n"
|
|
|
|
"return a + b \n"
|
|
|
|
"val = val + 1\n"
|
|
|
|
"end");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
script->Evaluate();
|
|
|
|
|
2019-06-15 15:20:27 +00:00
|
|
|
REQUIRE(script->HasFunction(u"add"));
|
2019-06-07 13:23:13 +00:00
|
|
|
auto toAddVal = new IntegerEvalValue(5);
|
|
|
|
auto toAddVal2 = new IntegerEvalValue(6);
|
2019-06-15 15:20:27 +00:00
|
|
|
auto result = script->CallFunction(u"add", {toAddVal, toAddVal2});
|
2019-06-07 13:23:13 +00:00
|
|
|
delete toAddVal;
|
|
|
|
delete toAddVal2;
|
|
|
|
|
2019-06-13 14:26:10 +00:00
|
|
|
REQUIRE(result->GetTypeClass() == TypeClass::Number);
|
2019-06-07 13:23:13 +00:00
|
|
|
REQUIRE(result->EvaluateInteger() == 11);
|
|
|
|
|
2019-06-15 15:20:27 +00:00
|
|
|
auto variable = script->GetVariable(u"val");
|
2019-06-13 14:26:10 +00:00
|
|
|
REQUIRE(variable->GetTypeClass() == TypeClass::Number);
|
2019-06-07 13:23:13 +00:00
|
|
|
REQUIRE(variable->EvaluateInteger() == 0);
|
|
|
|
|
|
|
|
delete script;
|
2019-07-27 15:59:42 +00:00
|
|
|
delete variable;
|
|
|
|
delete result;
|
2019-06-07 13:23:13 +00:00
|
|
|
}
|
|
|
|
|
2019-06-08 14:02:21 +00:00
|
|
|
TEST_CASE( "Functions can call themselves", "[integration]" ) {
|
|
|
|
Script* script = Script::Create(
|
2019-06-12 15:56:47 +00:00
|
|
|
R"(
|
|
|
|
val = 0
|
|
|
|
function add()
|
|
|
|
if val < 5 then
|
|
|
|
val = val + 1
|
|
|
|
add()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)");
|
2019-06-08 14:02:21 +00:00
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
script->Evaluate();
|
2019-06-15 15:20:27 +00:00
|
|
|
script->CallFunction(u"add", {});
|
2019-06-08 14:02:21 +00:00
|
|
|
|
2019-06-15 15:20:27 +00:00
|
|
|
auto variable = script->GetVariable(u"val");
|
2019-06-13 14:26:10 +00:00
|
|
|
REQUIRE(variable->GetTypeClass() == TypeClass::Number);
|
2019-06-08 14:02:21 +00:00
|
|
|
REQUIRE(variable->EvaluateInteger() == 5);
|
|
|
|
|
|
|
|
delete script;
|
2019-07-27 15:59:42 +00:00
|
|
|
delete variable;
|
2019-06-08 14:02:21 +00:00
|
|
|
}
|
2019-06-05 16:44:23 +00:00
|
|
|
|
2019-06-08 16:33:56 +00:00
|
|
|
TEST_CASE( "Functions respect scope", "[integration]" ) {
|
|
|
|
Script* script = Script::Create(
|
|
|
|
R"(
|
|
|
|
function test()
|
|
|
|
function foo()
|
|
|
|
local a = 10
|
|
|
|
end
|
|
|
|
if true then
|
|
|
|
if true then
|
|
|
|
local a = 50
|
|
|
|
foo()
|
|
|
|
result = a
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
test()
|
|
|
|
)"
|
|
|
|
);
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
script->Evaluate();
|
|
|
|
|
2019-06-15 15:20:27 +00:00
|
|
|
auto variable = script->GetVariable(u"result");
|
2019-06-13 14:26:10 +00:00
|
|
|
REQUIRE(variable->GetTypeClass() == TypeClass::Number);
|
2019-06-08 16:33:56 +00:00
|
|
|
REQUIRE(variable->EvaluateInteger() == 50);
|
|
|
|
|
|
|
|
delete script;
|
2019-07-27 15:59:42 +00:00
|
|
|
delete variable;
|
2019-06-08 16:33:56 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 14:21:21 +00:00
|
|
|
TEST_CASE( "Return doesn't parse next line expression", "[integration]" ) {
|
|
|
|
Script* script = Script::Create(
|
|
|
|
R"(
|
|
|
|
return
|
|
|
|
1 + 1
|
|
|
|
)"
|
|
|
|
);
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
auto result = script->Evaluate();
|
|
|
|
REQUIRE(result == nullptr);
|
|
|
|
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
2019-06-29 17:59:42 +00:00
|
|
|
TEST_CASE( "Allow declaration of multiple functions with different signatures", "[integration]" ) {
|
|
|
|
Script* script = Script::Create(
|
|
|
|
R"(
|
|
|
|
function add(number a, number b)
|
|
|
|
return a + b
|
|
|
|
end
|
|
|
|
|
|
|
|
function add(string a, string b)
|
|
|
|
return a + b
|
|
|
|
end
|
|
|
|
|
|
|
|
intResult = add(5, 500)
|
|
|
|
stringResult = add('foo', 'bar')
|
|
|
|
)"
|
|
|
|
);
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
script->Evaluate();
|
|
|
|
auto intVar = script -> GetVariable(u"intResult");
|
|
|
|
REQUIRE(intVar != nullptr);
|
|
|
|
CHECK(intVar->EvaluateInteger() == 505);
|
|
|
|
auto stringVar = script -> GetVariable(u"stringResult");
|
|
|
|
REQUIRE(stringVar != nullptr);
|
|
|
|
CHECK(stringVar->EvaluateString() == u"foobar");
|
|
|
|
|
|
|
|
delete script;
|
2019-07-27 15:59:42 +00:00
|
|
|
delete intVar;
|
|
|
|
delete stringVar;
|
2019-06-29 17:59:42 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 11:54:01 +00:00
|
|
|
TEST_CASE( "Malformed parameter logs error", "[integration]" ) {
|
|
|
|
Script* script = Script::Create(uR"(
|
|
|
|
function blockOwnCritical( move, target, hitNumber )
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
)");
|
|
|
|
REQUIRE(script->Diagnostics -> HasErrors());
|
|
|
|
delete script;
|
|
|
|
}
|
2019-06-08 16:33:56 +00:00
|
|
|
|
2019-06-01 10:33:52 +00:00
|
|
|
#endif
|