mirror of https://github.com/OpenMW/openmw.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
2.0 KiB
C++
78 lines
2.0 KiB
C++
3 years ago
|
#ifndef TESTING_UTIL_H
|
||
|
#define TESTING_UTIL_H
|
||
4 years ago
|
|
||
3 years ago
|
#include <filesystem>
|
||
4 years ago
|
#include <sstream>
|
||
|
|
||
|
#include <components/vfs/archive.hpp>
|
||
|
#include <components/vfs/manager.hpp>
|
||
|
|
||
3 years ago
|
namespace TestingOpenMW
|
||
4 years ago
|
{
|
||
|
|
||
3 years ago
|
inline std::string outputFilePath(const std::string name)
|
||
3 years ago
|
{
|
||
3 years ago
|
std::filesystem::path dir("tests_output");
|
||
|
std::filesystem::create_directory(dir);
|
||
|
return (dir / name).string();
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
inline std::string temporaryFilePath(const std::string name)
|
||
|
{
|
||
|
return (std::filesystem::temp_directory_path() / name).string();
|
||
|
}
|
||
|
|
||
|
class VFSTestFile : public VFS::File
|
||
4 years ago
|
{
|
||
|
public:
|
||
3 years ago
|
explicit VFSTestFile(std::string content) : mContent(std::move(content)) {}
|
||
4 years ago
|
|
||
|
Files::IStreamPtr open() override
|
||
|
{
|
||
3 years ago
|
return std::make_unique<std::stringstream>(mContent, std::ios_base::in);
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
std::string getPath() override
|
||
|
{
|
||
|
return "TestFile";
|
||
|
}
|
||
|
|
||
4 years ago
|
private:
|
||
|
const std::string mContent;
|
||
|
};
|
||
|
|
||
3 years ago
|
struct VFSTestData : public VFS::Archive
|
||
4 years ago
|
{
|
||
|
std::map<std::string, VFS::File*> mFiles;
|
||
|
|
||
3 years ago
|
VFSTestData(std::map<std::string, VFS::File*> files) : mFiles(std::move(files)) {}
|
||
4 years ago
|
|
||
|
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);
|
||
3 years ago
|
vfs->addArchive(new VFSTestData(std::move(files)));
|
||
4 years ago
|
vfs->buildIndex();
|
||
|
return vfs;
|
||
|
}
|
||
|
|
||
|
#define EXPECT_ERROR(X, ERR_SUBSTR) try { X; FAIL() << "Expected error"; } \
|
||
3 years ago
|
catch (std::exception& e) { EXPECT_THAT(e.what(), ::testing::HasSubstr(ERR_SUBSTR)); }
|
||
4 years ago
|
|
||
|
}
|
||
|
|
||
3 years ago
|
#endif // TESTING_UTIL_H
|