PorygonLang/tests/integration/ModuleTests.cpp

81 lines
2.2 KiB
C++
Raw Normal View History

2019-08-10 14:45:15 +00:00
#ifdef TESTS_BUILD
#include <catch.hpp>
#include "../src/Script.hpp"
using namespace Porygon;
class ModuleHandler{
class Internal{
public:
unordered_map<string, Script*> MODULES;
2019-08-10 14:45:15 +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
}
~Internal(){
for (const auto& v: MODULES){
delete v.second;
}
MODULES.clear();
}
};
static Internal* _internal;
static Internal* GetInternal(){
if (!_internal)
_internal = new Internal();
return _internal;
}
2019-08-10 14:45:15 +00:00
inline static bool DoesModuleExist(const char* moduleName, size_t size){
return GetInternal()->MODULES.find(moduleName) != GetInternal()->MODULES.end();
2019-08-10 14:45:15 +00:00
}
inline static Script* ResolveModule(const char* moduleName, size_t size){
return GetInternal()->MODULES[moduleName];
2019-08-10 14:45:15 +00:00
}
public:
static void Initialize(){
ScriptOptions::GetDefaultScriptOptions()->SetModuleExistsFunc(DoesModuleExist);
ScriptOptions::GetDefaultScriptOptions()->SetResolveModuleFunc(ResolveModule);
}
};
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;
}
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