1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-16 19:29:56 +00:00
openmw/apps/openmw_test_suite/testing_util.hpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

97 lines
3.4 KiB
C++
Raw Normal View History

#ifndef TESTING_UTIL_H
#define TESTING_UTIL_H
#include <filesystem>
#include <initializer_list>
#include <sstream>
#include <components/misc/strings/conversion.hpp>
#include <components/vfs/archive.hpp>
#include <components/vfs/file.hpp>
#include <components/vfs/manager.hpp>
2023-06-01 16:28:32 +00:00
#include <components/vfs/pathutil.hpp>
namespace TestingOpenMW
{
inline std::filesystem::path outputFilePath(const std::string name)
{
std::filesystem::path dir("tests_output");
std::filesystem::create_directory(dir);
return dir / Misc::StringUtils::stringToU8String(name);
}
2023-02-25 19:03:39 +00:00
inline std::filesystem::path outputFilePathWithSubDir(const std::filesystem::path& subpath)
{
std::filesystem::path path("tests_output");
path /= subpath;
std::filesystem::create_directories(path.parent_path());
return path;
}
inline std::filesystem::path temporaryFilePath(const std::string name)
{
return std::filesystem::temp_directory_path() / name;
}
class VFSTestFile : public VFS::File
{
public:
explicit VFSTestFile(std::string content)
: mContent(std::move(content))
{
}
2022-04-15 00:15:39 +00:00
Files::IStreamPtr open() override { return std::make_unique<std::stringstream>(mContent, std::ios_base::in); }
2022-09-22 18:26:05 +00:00
std::filesystem::path getPath() override { return "TestFile"; }
2022-05-14 01:58:00 +00:00
private:
const std::string mContent;
};
struct VFSTestData : public VFS::Archive
{
VFS::FileMap mFiles;
explicit VFSTestData(VFS::FileMap&& files)
: mFiles(std::move(files))
{
}
void listResources(VFS::FileMap& out) override { out = mFiles; }
bool contains(VFS::Path::NormalizedView file) const override { return mFiles.contains(file); }
std::string getDescription() const override { return "TestData"; }
};
inline std::unique_ptr<VFS::Manager> createTestVFS(VFS::FileMap&& files)
{
2023-05-31 21:11:03 +00:00
auto vfs = std::make_unique<VFS::Manager>();
vfs->addArchive(std::make_unique<VFSTestData>(std::move(files)));
vfs->buildIndex();
return vfs;
}
inline std::unique_ptr<VFS::Manager> createTestVFS(
std::initializer_list<std::pair<std::string_view, VFS::File*>> files)
{
return createTestVFS(VFS::FileMap(files.begin(), files.end()));
}
#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)); \
2022-09-22 18:26:05 +00:00
}
}
#endif // TESTING_UTIL_H