2021-01-12 22:17:48 +00:00
|
|
|
#ifndef LUA_TESTING_UTIL_H
|
|
|
|
#define LUA_TESTING_UTIL_H
|
|
|
|
|
|
|
|
#include <sstream>
|
2021-12-26 20:49:20 +00:00
|
|
|
#include <sol/sol.hpp>
|
2021-01-12 22:17:48 +00:00
|
|
|
|
|
|
|
#include <components/vfs/archive.hpp>
|
|
|
|
#include <components/vfs/manager.hpp>
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2021-12-26 20:49:20 +00:00
|
|
|
template <typename T>
|
|
|
|
T get(sol::state& lua, const std::string& luaCode)
|
|
|
|
{
|
|
|
|
return lua.safe_script("return " + luaCode).get<T>();
|
|
|
|
}
|
|
|
|
|
2021-01-12 22:17:48 +00:00
|
|
|
class TestFile : public VFS::File
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit TestFile(std::string content) : mContent(std::move(content)) {}
|
|
|
|
|
|
|
|
Files::IStreamPtr open() override
|
|
|
|
{
|
2022-04-15 00:15:39 +00:00
|
|
|
return std::make_unique<std::stringstream>(mContent, std::ios_base::in);
|
2021-01-12 22:17:48 +00:00
|
|
|
}
|
|
|
|
|
2022-05-14 01:58:00 +00:00
|
|
|
std::string getPath() override
|
|
|
|
{
|
|
|
|
return "TestFile";
|
|
|
|
}
|
|
|
|
|
2021-01-12 22:17:48 +00:00
|
|
|
private:
|
|
|
|
const std::string mContent;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TestData : public VFS::Archive
|
|
|
|
{
|
|
|
|
std::map<std::string, VFS::File*> mFiles;
|
|
|
|
|
|
|
|
TestData(std::map<std::string, VFS::File*> files) : mFiles(std::move(files)) {}
|
|
|
|
|
|
|
|
void listResources(std::map<std::string, VFS::File*>& out, char (*normalize_function) (char)) override
|
|
|
|
{
|
|
|
|
out = mFiles;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool contains(const std::string& file, char (*normalize_function) (char)) const override
|
|
|
|
{
|
|
|
|
return mFiles.count(file) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getDescription() const override { return "TestData"; }
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
inline std::unique_ptr<VFS::Manager> createTestVFS(std::map<std::string, VFS::File*> files)
|
|
|
|
{
|
|
|
|
auto vfs = std::make_unique<VFS::Manager>(true);
|
|
|
|
vfs->addArchive(new TestData(std::move(files)));
|
|
|
|
vfs->buildIndex();
|
|
|
|
return vfs;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define EXPECT_ERROR(X, ERR_SUBSTR) try { X; FAIL() << "Expected error"; } \
|
2021-10-06 18:59:48 +00:00
|
|
|
catch (std::exception& e) { EXPECT_THAT(e.what(), ::testing::HasSubstr(ERR_SUBSTR)); }
|
2021-01-12 22:17:48 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // LUA_TESTING_UTIL_H
|