From 9c6407a1e335a68b38b764b2cf51f6ed582c6834 Mon Sep 17 00:00:00 2001 From: elsid Date: Mon, 8 Sep 2025 22:46:50 +0200 Subject: [PATCH] Cleanup resource helpers tests --- .../misc/testresourcehelpers.cpp | 229 ++++++++++++------ 1 file changed, 157 insertions(+), 72 deletions(-) diff --git a/apps/components_tests/misc/testresourcehelpers.cpp b/apps/components_tests/misc/testresourcehelpers.cpp index 95db3ce581..2e02590624 100644 --- a/apps/components_tests/misc/testresourcehelpers.cpp +++ b/apps/components_tests/misc/testresourcehelpers.cpp @@ -3,82 +3,167 @@ #include -namespace +namespace Misc::ResourceHelpers { - using namespace Misc::ResourceHelpers; - TEST(CorrectSoundPath, wav_files_not_overridden_with_mp3_in_vfs_are_not_corrected) - { - constexpr VFS::Path::NormalizedView path("sound/bar.wav"); - std::unique_ptr mVFS = TestingOpenMW::createTestVFS({ { path, nullptr } }); - EXPECT_EQ(correctSoundPath(path, *mVFS), "sound/bar.wav"); - } - - TEST(CorrectSoundPath, wav_files_overridden_with_mp3_in_vfs_are_corrected) - { - constexpr VFS::Path::NormalizedView mp3("sound/foo.mp3"); - std::unique_ptr mVFS = TestingOpenMW::createTestVFS({ { mp3, nullptr } }); - constexpr VFS::Path::NormalizedView wav("sound/foo.wav"); - EXPECT_EQ(correctSoundPath(wav, *mVFS), "sound/foo.mp3"); - } - - TEST(CorrectSoundPath, corrected_path_does_not_check_existence_in_vfs) - { - std::unique_ptr mVFS = TestingOpenMW::createTestVFS({}); - - { - constexpr VFS::Path::NormalizedView path("sound/foo.wav"); - EXPECT_EQ(correctSoundPath(path, *mVFS), "sound/foo.mp3"); - } - - auto correctESM4SoundPath = [](auto path, auto* vfs) { - return Misc::ResourceHelpers::correctResourcePath({ { "sound" } }, path, vfs, ".mp3"); - }; - - EXPECT_EQ(correctESM4SoundPath("foo.WAV", mVFS.get()), "sound\\foo.mp3"); - EXPECT_EQ(correctESM4SoundPath("SOUND/foo.WAV", mVFS.get()), "sound\\foo.mp3"); - EXPECT_EQ(correctESM4SoundPath("DATA\\SOUND\\foo.WAV", mVFS.get()), "sound\\foo.mp3"); - EXPECT_EQ(correctESM4SoundPath("\\Data/Sound\\foo.WAV", mVFS.get()), "sound\\foo.mp3"); - } - namespace { - std::string checkChangeExtensionToDds(std::string path) + using namespace ::testing; + + TEST(MiscResourceHelpersCorrectSoundPath, shouldKeepWavExtensionIfExistsInVfs) { - changeExtensionToDds(path); - return path; + constexpr VFS::Path::NormalizedView path("sound/foo.wav"); + const std::unique_ptr vfs = TestingOpenMW::createTestVFS({ { path, nullptr } }); + EXPECT_EQ(correctSoundPath(path, *vfs), "sound/foo.wav"); + } + + TEST(MiscResourceHelpersCorrectSoundPath, shouldFallbackToMp3IfWavDoesNotExistInVfs) + { + const std::unique_ptr vfs = TestingOpenMW::createTestVFS({}); + constexpr VFS::Path::NormalizedView path("sound/foo.wav"); + EXPECT_EQ(correctSoundPath(path, *vfs), "sound/foo.mp3"); + } + + TEST(MiscResourceHelpersCorrectSoundPath, shouldKeepWavExtensionIfBothExistsInVfs) + { + constexpr VFS::Path::NormalizedView wav("sound/foo.wav"); + constexpr VFS::Path::NormalizedView mp3("sound/foo.mp3"); + const std::unique_ptr vfs = TestingOpenMW::createTestVFS({ + { wav, nullptr }, + { mp3, nullptr }, + }); + EXPECT_EQ(correctSoundPath(wav, *vfs), "sound/foo.wav"); + } + + TEST(MiscResourceHelpersCorrectResourcePath, shouldFallbackToGivenExtentionIfDoesNotExistInVfs) + { + const std::unique_ptr vfs = TestingOpenMW::createTestVFS({}); + EXPECT_EQ(correctResourcePath({ { "sound" } }, "sound/foo.wav", vfs.get(), ".mp3"), "sound\\foo.mp3"); + } + + TEST(MiscResourceHelpersCorrectResourcePath, shouldFallbackToGivenExtentionIfBothExistInVfs) + { + constexpr VFS::Path::NormalizedView wav("sound/foo.wav"); + constexpr VFS::Path::NormalizedView mp3("sound/foo.mp3"); + const std::unique_ptr vfs = TestingOpenMW::createTestVFS({ + { wav, nullptr }, + { mp3, nullptr }, + }); + EXPECT_EQ(correctResourcePath({ { "sound" } }, wav.value(), vfs.get(), ".mp3"), "sound\\foo.mp3"); + } + + TEST(MiscResourceHelpersCorrectResourcePath, shouldKeepExtentionIfExistInVfs) + { + constexpr VFS::Path::NormalizedView wav("sound/foo.wav"); + const std::unique_ptr vfs = TestingOpenMW::createTestVFS({ + { wav, nullptr }, + }); + EXPECT_EQ(correctResourcePath({ { "sound" } }, wav.value(), vfs.get(), ".mp3"), "sound\\foo.wav"); + } + + TEST(MiscResourceHelpersCorrectResourcePath, shouldPrefixWithGivenTopDirectory) + { + const std::unique_ptr vfs = TestingOpenMW::createTestVFS({}); + EXPECT_EQ(correctResourcePath({ { "sound" } }, "foo.mp3", vfs.get(), ".mp3"), "sound\\foo.mp3"); + } + + TEST(MiscResourceHelpersCorrectResourcePath, shouldChangeTopDirectoryAndKeepExtensionIfOriginalExistInVfs) + { + constexpr VFS::Path::NormalizedView a("textures/foo.a"); + const std::unique_ptr vfs = TestingOpenMW::createTestVFS({ + { a, nullptr }, + }); + EXPECT_EQ(correctResourcePath({ { "textures", "bookart" } }, "bookart/foo.a", vfs.get(), ".b"), + "textures\\foo.a"); + } + + TEST(MiscResourceHelpersCorrectResourcePath, shouldChangeTopDirectoryAndChangeExtensionIfFallbackExistInVfs) + { + constexpr VFS::Path::NormalizedView b("textures/foo.b"); + const std::unique_ptr vfs = TestingOpenMW::createTestVFS({ + { b, nullptr }, + }); + EXPECT_EQ(correctResourcePath({ { "textures", "bookart" } }, "bookart/foo.a", vfs.get(), ".b"), + "textures\\foo.b"); + } + + TEST(MiscResourceHelpersCorrectResourcePath, shouldLowerCase) + { + const std::unique_ptr vfs = TestingOpenMW::createTestVFS({}); + EXPECT_EQ(correctResourcePath({ { "sound" } }, "SOUND\\Foo.MP3", vfs.get(), ".mp3"), "sound\\foo.mp3"); + } + + TEST(MiscResourceHelpersCorrectResourcePath, shouldRemoveLeadingSlash) + { + const std::unique_ptr vfs = TestingOpenMW::createTestVFS({}); + EXPECT_EQ(correctResourcePath({ { "sound" } }, "\\SOUND\\Foo.MP3", vfs.get(), ".mp3"), "sound\\foo.mp3"); + } + + TEST(MiscResourceHelpersCorrectResourcePath, shouldRemoveDuplicateSlashes) + { + const std::unique_ptr vfs = TestingOpenMW::createTestVFS({}); + EXPECT_EQ( + correctResourcePath({ { "sound" } }, "\\\\SOUND\\\\Foo.MP3", vfs.get(), ".mp3"), "sound\\foo.mp3"); + } + + TEST(MiscResourceHelpersCorrectResourcePath, shouldConvertToBackSlash) + { + const std::unique_ptr vfs = TestingOpenMW::createTestVFS({}); + EXPECT_EQ(correctResourcePath({ { "sound" } }, "SOUND/Foo.MP3", vfs.get(), ".mp3"), "sound\\foo.mp3"); + } + + struct MiscResourceHelpersCorrectResourcePathShouldRemoveExtraPrefix : TestWithParam + { + }; + + TEST_P(MiscResourceHelpersCorrectResourcePathShouldRemoveExtraPrefix, shouldMatchExpected) + { + const std::unique_ptr vfs = TestingOpenMW::createTestVFS({}); + EXPECT_EQ(correctResourcePath({ { "sound" } }, GetParam(), vfs.get(), ".mp3"), "sound\\foo.mp3"); + } + + const std::vector pathsWithPrefix = { + "data/sound/foo.mp3", + "data/notsound/sound/foo.mp3", + "data/soundnot/sound/foo.mp3", + "data/notsoundnot/sound/foo.mp3", + }; + + INSTANTIATE_TEST_SUITE_P( + PathsWithPrefix, MiscResourceHelpersCorrectResourcePathShouldRemoveExtraPrefix, ValuesIn(pathsWithPrefix)); + + TEST(MiscResourceHelpersChangeExtensionToDds, original_extension_with_same_size_as_dds) + { + std::string path = "texture/bar.tga"; + ASSERT_TRUE(changeExtensionToDds(path)); + EXPECT_EQ(path, "texture/bar.dds"); + } + + TEST(MiscResourceHelpersChangeExtensionToDds, original_extension_greater_than_dds) + { + std::string path = "texture/bar.jpeg"; + ASSERT_TRUE(changeExtensionToDds(path)); + EXPECT_EQ(path, "texture/bar.dds"); + } + + TEST(MiscResourceHelpersChangeExtensionToDds, original_extension_smaller_than_dds) + { + std::string path = "texture/bar.xx"; + ASSERT_TRUE(changeExtensionToDds(path)); + EXPECT_EQ(path, "texture/bar.dds"); + } + + TEST(MiscResourceHelpersChangeExtensionToDds, does_not_change_dds_extension) + { + std::string path = "texture/bar.dds"; + EXPECT_FALSE(changeExtensionToDds(path)); + EXPECT_EQ(path, "texture/bar.dds"); + } + + TEST(MiscResourceHelpersChangeExtensionToDds, does_not_change_when_no_extension) + { + std::string path = "texture/bar"; + EXPECT_FALSE(changeExtensionToDds(path)); + EXPECT_EQ(path, "texture/bar"); } } - - TEST(ChangeExtensionToDds, original_extension_with_same_size_as_dds) - { - EXPECT_EQ(checkChangeExtensionToDds("texture/bar.tga"), "texture/bar.dds"); - } - - TEST(ChangeExtensionToDds, original_extension_greater_than_dds) - { - EXPECT_EQ(checkChangeExtensionToDds("texture/bar.jpeg"), "texture/bar.dds"); - } - - TEST(ChangeExtensionToDds, original_extension_smaller_than_dds) - { - EXPECT_EQ(checkChangeExtensionToDds("texture/bar.xx"), "texture/bar.dds"); - } - - TEST(ChangeExtensionToDds, does_not_change_dds_extension) - { - std::string path = "texture/bar.dds"; - EXPECT_FALSE(changeExtensionToDds(path)); - } - - TEST(ChangeExtensionToDds, does_not_change_when_no_extension) - { - std::string path = "texture/bar"; - EXPECT_FALSE(changeExtensionToDds(path)); - } - - TEST(ChangeExtensionToDds, change_when_there_is_an_extension) - { - std::string path = "texture/bar.jpeg"; - EXPECT_TRUE(changeExtensionToDds(path)); - } }