1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-03-27 03:40:24 +00:00

Fix unit tests

This commit is contained in:
Evil Eye 2023-06-01 18:28:32 +02:00
parent eeda48ec50
commit e791d2b498
3 changed files with 67 additions and 55 deletions

View file

@ -27,7 +27,7 @@ namespace
{ {
std::unique_ptr<VFS::Manager> mVFS = TestingOpenMW::createTestVFS({}); std::unique_ptr<VFS::Manager> mVFS = TestingOpenMW::createTestVFS({});
EXPECT_EQ(correctSoundPath("sound\\foo.wav", mVFS.get()), "sound/foo.mp3"); EXPECT_EQ(correctSoundPath("sound\\foo.wav", mVFS.get()), "sound/foo.mp3");
EXPECT_EQ(correctSoundPath("SOUND\\foo.WAV", mVFS.get()), "SOUND/foo.mp3"); EXPECT_EQ(correctSoundPath("SOUND\\foo.WAV", mVFS.get()), "sound/foo.mp3");
} }
namespace namespace

View file

@ -7,6 +7,7 @@
#include <components/misc/strings/conversion.hpp> #include <components/misc/strings/conversion.hpp>
#include <components/vfs/archive.hpp> #include <components/vfs/archive.hpp>
#include <components/vfs/manager.hpp> #include <components/vfs/manager.hpp>
#include <components/vfs/pathutil.hpp>
namespace TestingOpenMW namespace TestingOpenMW
{ {
@ -49,21 +50,25 @@ namespace TestingOpenMW
struct VFSTestData : public VFS::Archive struct VFSTestData : public VFS::Archive
{ {
std::map<std::string, VFS::File*> mFiles; std::map<std::string, VFS::File*, VFS::Path::PathLess> mFiles;
VFSTestData(std::map<std::string, VFS::File*> files) VFSTestData(std::map<std::string, VFS::File*, VFS::Path::PathLess> files)
: mFiles(std::move(files)) : mFiles(std::move(files))
{ {
} }
void listResources(std::map<std::string, VFS::File*>& out) override { out = mFiles; } void listResources(std::map<std::string, VFS::File*>& out) override
{
for (const auto& [key, value] : mFiles)
out.emplace(VFS::Path::normalizeFilename(key), value);
}
bool contains(const std::string& file) const override { return mFiles.count(file) != 0; } bool contains(const std::string& file) const override { return mFiles.count(file) != 0; }
std::string getDescription() const override { return "TestData"; } std::string getDescription() const override { return "TestData"; }
}; };
inline std::unique_ptr<VFS::Manager> createTestVFS(std::map<std::string, VFS::File*> files) inline std::unique_ptr<VFS::Manager> createTestVFS(std::map<std::string, VFS::File*, VFS::Path::PathLess> files)
{ {
auto vfs = std::make_unique<VFS::Manager>(); auto vfs = std::make_unique<VFS::Manager>();
vfs->addArchive(std::make_unique<VFSTestData>(std::move(files))); vfs->addArchive(std::make_unique<VFSTestData>(std::move(files)));

View file

@ -1,50 +1,57 @@
#ifndef OPENMW_COMPONENTS_RESOURCE_PATH_H #ifndef OPENMW_COMPONENTS_RESOURCE_PATH_H
#define OPENMW_COMPONENTS_RESOURCE_PATH_H #define OPENMW_COMPONENTS_RESOURCE_PATH_H
#include <components/misc/strings/lower.hpp> #include <components/misc/strings/lower.hpp>
#include <algorithm> #include <algorithm>
#include <string> #include <string>
#include <string_view> #include <string_view>
namespace VFS::Path namespace VFS::Path
{ {
inline constexpr char normalize(char c) inline constexpr char normalize(char c)
{ {
return c == '\\' ? '/' : Misc::StringUtils::toLower(c); return c == '\\' ? '/' : Misc::StringUtils::toLower(c);
} }
inline void normalizeFilenameInPlace(std::string& name) inline void normalizeFilenameInPlace(std::string& name)
{ {
for (char& ch : name) for (char& ch : name)
ch = normalize(ch); ch = normalize(ch);
} }
/// Normalize the given filename, making slashes/backslashes consistent, and lower-casing. /// Normalize the given filename, making slashes/backslashes consistent, and lower-casing.
[[nodiscard]] inline std::string normalizeFilename(std::string_view name) [[nodiscard]] inline std::string normalizeFilename(std::string_view name)
{ {
std::string out(name); std::string out(name);
normalizeFilenameInPlace(out); normalizeFilenameInPlace(out);
return out; return out;
} }
struct PathCharLess struct PathCharLess
{ {
bool operator()(char x, char y) const { return normalize(x) < normalize(y); } bool operator()(char x, char y) const { return normalize(x) < normalize(y); }
}; };
inline bool pathLess(std::string_view x, std::string_view y) inline bool pathLess(std::string_view x, std::string_view y)
{ {
return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end(), PathCharLess()); return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end(), PathCharLess());
} }
inline bool pathEqual(std::string_view x, std::string_view y) inline bool pathEqual(std::string_view x, std::string_view y)
{ {
if (std::size(x) != std::size(y)) if (std::size(x) != std::size(y))
return false; return false;
return std::equal( return std::equal(
std::begin(x), std::end(x), std::begin(y), [](char l, char r) { return normalize(l) == normalize(r); }); std::begin(x), std::end(x), std::begin(y), [](char l, char r) { return normalize(l) == normalize(r); });
} }
}
struct PathLess
#endif {
using is_transparent = void;
bool operator()(std::string_view left, std::string_view right) const { return pathLess(left, right); }
};
}
#endif