2019-08-10 14:45:15 +00:00
|
|
|
#ifdef TESTS_BUILD
|
|
|
|
#include <catch.hpp>
|
|
|
|
#include "../src/Script.hpp"
|
|
|
|
using namespace Porygon;
|
|
|
|
|
|
|
|
class ModuleHandler{
|
2019-08-11 10:32:18 +00:00
|
|
|
class Internal{
|
|
|
|
public:
|
|
|
|
unordered_map<string, Script*> MODULES;
|
2019-08-10 14:45:15 +00:00
|
|
|
|
2019-08-11 10:32:18 +00:00
|
|
|
Internal(){
|
|
|
|
MODULES = {
|
|
|
|
{"simple_return", Script::Create(u"return 500")},
|
2019-09-01 18:07:09 +00:00
|
|
|
{"simple_variables", Script::Create(u"foo = 50\nbar = \'test\'")},
|
|
|
|
{
|
|
|
|
"complex_module", Script::Create(uR"(
|
|
|
|
local module = {
|
|
|
|
function contains(table table, string s)
|
|
|
|
for _, v in table do
|
|
|
|
if (v == s) then return true end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
}
|
|
|
|
return module
|
|
|
|
)")
|
|
|
|
}
|
2019-08-11 10:32:18 +00:00
|
|
|
};
|
2019-08-10 14:45:15 +00:00
|
|
|
}
|
2019-08-11 10:32:18 +00:00
|
|
|
|
|
|
|
~Internal(){
|
|
|
|
for (const auto& v: MODULES){
|
|
|
|
delete v.second;
|
|
|
|
}
|
|
|
|
MODULES.clear();
|
|
|
|
}
|
|
|
|
};
|
2019-09-01 18:07:09 +00:00
|
|
|
public:
|
|
|
|
|
2019-08-17 12:42:48 +00:00
|
|
|
static Internal* _internal;
|
|
|
|
|
|
|
|
static Internal* GetInternal(){
|
|
|
|
if (!_internal)
|
|
|
|
_internal = new Internal();
|
|
|
|
return _internal;
|
|
|
|
}
|
2019-08-10 14:45:15 +00:00
|
|
|
|
2019-08-24 16:52:11 +00:00
|
|
|
inline static bool DoesModuleExist(const char* moduleName, size_t size){
|
2019-08-17 12:42:48 +00:00
|
|
|
return GetInternal()->MODULES.find(moduleName) != GetInternal()->MODULES.end();
|
2019-08-10 14:45:15 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 16:52:11 +00:00
|
|
|
inline static Script* ResolveModule(const char* moduleName, size_t size){
|
2019-09-01 18:07:09 +00:00
|
|
|
auto module = GetInternal()->MODULES[moduleName];
|
|
|
|
REQUIRE(!module->Diagnostics->HasErrors());
|
|
|
|
return module;
|
2019-08-10 14:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void Initialize(){
|
|
|
|
ScriptOptions::GetDefaultScriptOptions()->SetModuleExistsFunc(DoesModuleExist);
|
|
|
|
ScriptOptions::GetDefaultScriptOptions()->SetResolveModuleFunc(ResolveModule);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-17 12:42:48 +00:00
|
|
|
ModuleHandler::Internal* ModuleHandler::_internal = nullptr;
|
2019-08-10 14:45:15 +00:00
|
|
|
|
|
|
|
TEST_CASE( "Require simple return script", "[integration]" ) {
|
|
|
|
ModuleHandler::Initialize();
|
|
|
|
auto script = Script::Create(uR"(
|
|
|
|
return require("simple_return")
|
|
|
|
)");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
auto var = script->Evaluate();
|
|
|
|
REQUIRE(var->EvaluateInteger() == 500);
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
2019-08-11 10:32:18 +00:00
|
|
|
TEST_CASE( "Require simple variables script", "[integration]" ) {
|
|
|
|
ModuleHandler::Initialize();
|
|
|
|
auto script = Script::Create(uR"(
|
|
|
|
require("simple_variables")
|
|
|
|
)");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
script->Evaluate();
|
|
|
|
REQUIRE(script->HasVariable(u"foo"));
|
|
|
|
REQUIRE(script->HasVariable(u"bar"));
|
|
|
|
auto foo = script->GetVariable(u"foo");
|
|
|
|
auto bar = script->GetVariable(u"bar");
|
|
|
|
CHECK(foo->EvaluateInteger() == 50);
|
|
|
|
CHECK(bar->EvaluateString() == u"test");
|
|
|
|
|
|
|
|
delete foo;
|
|
|
|
delete bar;
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
2019-09-01 18:07:09 +00:00
|
|
|
TEST_CASE( "Require string table contains", "[integration]" ) {
|
|
|
|
ModuleHandler::Initialize();
|
|
|
|
auto script = Script::Create(uR"(
|
|
|
|
local list = require("complex_module")
|
|
|
|
return list.contains({"foo", "bar"}, "bar")
|
|
|
|
)");
|
|
|
|
REQUIRE(!script->Diagnostics -> HasErrors());
|
|
|
|
auto result = script->Evaluate();
|
|
|
|
CHECK(result->EvaluateBool());
|
|
|
|
|
|
|
|
delete script;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-10 14:45:15 +00:00
|
|
|
#endif
|