104 lines
3.0 KiB
C++
104 lines
3.0 KiB
C++
#include "../external/acutest/include/acutest.h"
|
|
#include <string.h>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <thread>
|
|
#include <chrono>
|
|
#include "../include/HotReload.h"
|
|
#include "../include/ScriptEngine.h"
|
|
|
|
using namespace std::chrono_literals;
|
|
|
|
/* simple sanity test */
|
|
void test_one(void) {
|
|
TEST_CHECK(1 + 1 == 2);
|
|
}
|
|
|
|
void test_str(void) {
|
|
TEST_CHECK(strcmp("foo","foo") == 0);
|
|
}
|
|
|
|
void test_hotreload_directory_watch(void) {
|
|
std::filesystem::path tmpDir = std::filesystem::current_path() / "tmp_watch";
|
|
std::filesystem::create_directories(tmpDir);
|
|
|
|
std::filesystem::path f = tmpDir / "a.as";
|
|
std::ofstream ofs(f);
|
|
ofs << "// test" << std::endl;
|
|
ofs.close();
|
|
|
|
HotReload hr(tmpDir.string());
|
|
TEST_CHECK(hr.CheckForChanges() == false);
|
|
|
|
std::this_thread::sleep_for(1s);
|
|
std::ofstream ofs2(f, std::ios::app);
|
|
ofs2 << "// changed" << std::endl;
|
|
ofs2.close();
|
|
|
|
TEST_CHECK(hr.CheckForChanges() == true);
|
|
|
|
std::filesystem::remove(f);
|
|
std::filesystem::remove(tmpDir);
|
|
}
|
|
|
|
void test_scriptengine_compile_and_call(void) {
|
|
// Create a simple script file that defines Update(float) and a global
|
|
std::filesystem::path tmpDir = std::filesystem::current_path() / "tmp_script";
|
|
std::filesystem::create_directories(tmpDir);
|
|
std::filesystem::path script = tmpDir / "simple.as";
|
|
std::ofstream ofs(script);
|
|
ofs << "float g_x = 0;\n";
|
|
ofs << "void Update(float dt) { g_x += dt * 10.0f; }\n";
|
|
ofs.close();
|
|
|
|
ScriptEngine se;
|
|
TEST_CHECK(se.Initialize() == true);
|
|
bool ok = se.CompileScript(script.string());
|
|
TEST_CHECK(ok == true);
|
|
asIScriptFunction* upd = se.GetUpdateFunction();
|
|
TEST_CHECK(upd != nullptr);
|
|
|
|
// Call Update with dt=0.1
|
|
se.CallScriptFunction(upd, 0.1f);
|
|
|
|
se.Shutdown();
|
|
|
|
std::filesystem::remove(script);
|
|
std::filesystem::remove(tmpDir);
|
|
}
|
|
|
|
void test_scriptengine_include(void) {
|
|
std::filesystem::path tmpDir = std::filesystem::current_path() / "tmp_include";
|
|
std::filesystem::create_directories(tmpDir);
|
|
std::filesystem::path inc = tmpDir / "inc.as";
|
|
std::ofstream ofsinc(inc);
|
|
ofsinc << "void Included() { Print(\"Included called\"); }\n";
|
|
ofsinc.close();
|
|
|
|
std::filesystem::path main = tmpDir / "main.as";
|
|
std::ofstream ofsmain(main);
|
|
ofsmain << "#include \"inc.as\"\n";
|
|
ofsmain << "void Update(float dt) { Included(); }\n";
|
|
ofsmain.close();
|
|
|
|
ScriptEngine se;
|
|
TEST_CHECK(se.Initialize() == true);
|
|
// Compile should succeed and include callback should resolve inc.as
|
|
bool ok = se.CompileScript(main.string());
|
|
TEST_CHECK(ok == true);
|
|
se.Shutdown();
|
|
|
|
std::filesystem::remove(inc);
|
|
std::filesystem::remove(main);
|
|
std::filesystem::remove(tmpDir);
|
|
}
|
|
|
|
TEST_LIST = {
|
|
{ "one", test_one },
|
|
{ "str", test_str },
|
|
{ "hotreload_dir", test_hotreload_directory_watch },
|
|
{ "script_compile", test_scriptengine_compile_and_call },
|
|
{ "script_include", test_scriptengine_include },
|
|
{ NULL, NULL }
|
|
};
|