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")},
|
|
|
|
{"simple_variables", Script::Create(u"foo = 50\nbar = \'test\'")}
|
|
|
|
};
|
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-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-08-17 12:42:48 +00:00
|
|
|
return GetInternal()->MODULES[moduleName];
|
2019-08-10 14:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
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-08-10 14:45:15 +00:00
|
|
|
#endif
|