PorygonLang/tests/integration/ModuleTests.cpp

46 lines
1.2 KiB
C++

#ifdef TESTS_BUILD
#include <catch.hpp>
#include "../src/Script.hpp"
using namespace Porygon;
class ModuleHandler{
static unordered_map<string, Script*> MODULES;
~ModuleHandler(){
for (auto v: MODULES){
delete v.second;
}
}
inline static bool DoesModuleExist(const string& moduleName){
return MODULES.find(moduleName) != MODULES.end();
}
inline static Script* ResolveModule(const string& moduleName){
return MODULES[moduleName];
}
public:
static void Initialize(){
ScriptOptions::GetDefaultScriptOptions()->SetModuleExistsFunc(DoesModuleExist);
ScriptOptions::GetDefaultScriptOptions()->SetResolveModuleFunc(ResolveModule);
}
};
unordered_map<string, Script*> ModuleHandler::MODULES = unordered_map<string, Script*>{
{"simple_return", Script::Create(u"return 500")}
};
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;
}
#endif