2019-06-01 10:33:52 +00:00
|
|
|
#ifdef TESTS_BUILD
|
|
|
|
#include <catch.hpp>
|
|
|
|
#include "../src/Script.hpp"
|
|
|
|
|
|
|
|
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();
|
|
|
|
auto variable = script->GetVariable("add");
|
|
|
|
REQUIRE(variable != nullptr);
|
|
|
|
REQUIRE(variable->GetType()->GetClass() == TypeClass::Function);
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
auto variable = script->GetVariable("add");
|
|
|
|
REQUIRE(variable != nullptr);
|
|
|
|
REQUIRE(variable->GetType()->GetClass() == TypeClass::Function);
|
|
|
|
auto result = script->GetVariable("result");
|
|
|
|
REQUIRE(result->GetType()->GetClass() == TypeClass::Number);
|
|
|
|
REQUIRE(result->EvaluateInteger() == 3);
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
auto variable = script->GetVariable("add");
|
|
|
|
REQUIRE(variable != nullptr);
|
|
|
|
REQUIRE(variable->GetType()->GetClass() == TypeClass::Function);
|
|
|
|
auto result = script->GetVariable("result");
|
|
|
|
REQUIRE(result->GetType()->GetClass() == TypeClass::Number);
|
|
|
|
REQUIRE(result->EvaluateInteger() == 5);
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
REQUIRE(script->HasFunction("add"));
|
2019-06-05 17:18:11 +00:00
|
|
|
auto toAddVal = new IntegerEvalValue(5);
|
|
|
|
script->CallFunction("add", {toAddVal});
|
|
|
|
delete toAddVal;
|
|
|
|
toAddVal = new IntegerEvalValue(6);
|
|
|
|
script->CallFunction("add", {toAddVal});
|
|
|
|
delete toAddVal;
|
2019-06-05 16:44:23 +00:00
|
|
|
|
|
|
|
auto result = script->GetVariable("result");
|
|
|
|
REQUIRE(result->GetType()->GetClass() == TypeClass::Number);
|
|
|
|
REQUIRE(result->EvaluateInteger() == 11);
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
REQUIRE(script->HasFunction("add"));
|
|
|
|
auto toAddVal = new IntegerEvalValue(5);
|
|
|
|
auto toAddVal2 = new IntegerEvalValue(6);
|
|
|
|
auto result = script->CallFunction("add", {toAddVal, toAddVal2});
|
|
|
|
delete toAddVal;
|
|
|
|
delete toAddVal2;
|
|
|
|
|
|
|
|
REQUIRE(result->GetType()->GetClass() == TypeClass::Number);
|
|
|
|
REQUIRE(result->EvaluateInteger() == 11);
|
|
|
|
|
|
|
|
auto variable = script->GetVariable("val");
|
|
|
|
REQUIRE(variable->GetType()->GetClass() == TypeClass::Number);
|
|
|
|
REQUIRE(variable->EvaluateInteger() == 0);
|
|
|
|
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
2019-06-05 16:44:23 +00:00
|
|
|
|
2019-06-01 10:33:52 +00:00
|
|
|
#endif
|