From 1b362140ae789cd79a3495b991bba6203957e507 Mon Sep 17 00:00:00 2001 From: elsid Date: Sun, 7 Sep 2025 17:42:48 +0200 Subject: [PATCH] Remove duplicated and leading slashes in normalizeFilenameInPlace To be consistent with correctResourcePath. --- apps/components_tests/vfs/testpathutil.cpp | 57 ++++++++++++++++++++-- components/misc/resourcehelpers.cpp | 9 ---- components/vfs/pathutil.hpp | 53 +++++++++++++++++--- 3 files changed, 99 insertions(+), 20 deletions(-) diff --git a/apps/components_tests/vfs/testpathutil.cpp b/apps/components_tests/vfs/testpathutil.cpp index 771036d60c..c30f563a4d 100644 --- a/apps/components_tests/vfs/testpathutil.cpp +++ b/apps/components_tests/vfs/testpathutil.cpp @@ -10,6 +10,49 @@ namespace VFS::Path { using namespace testing; + struct VFSPathIsNormalizedTest : TestWithParam> + { + }; + + TEST_P(VFSPathIsNormalizedTest, shouldReturnExpectedResult) + { + EXPECT_EQ(isNormalized(GetParam().first), GetParam().second); + } + + const std::pair isNormalizedTestParams[] = { + { std::string_view(), true }, + { "foo", true }, + { "foo/bar", true }, + { "foo/bar/baz", true }, + { "/foo", false }, + { "foo//", false }, + { "foo\\", false }, + { "Foo", false }, + }; + + INSTANTIATE_TEST_SUITE_P(IsNormalizedTestParams, VFSPathIsNormalizedTest, ValuesIn(isNormalizedTestParams)); + + TEST(VFSPathNormalizeFilenameInPlaceTest, shouldRemoveLeadingSeparators) + { + std::string value("//foo"); + normalizeFilenameInPlace(value); + EXPECT_EQ(value, "foo"); + } + + TEST(VFSPathNormalizeFilenameInPlaceTest, shouldRemoveDuplicatedSeparators) + { + std::string value("foo//bar///baz"); + normalizeFilenameInPlace(value); + EXPECT_EQ(value, "foo/bar/baz"); + } + + TEST(VFSPathNormalizeFilenameInPlaceTest, shouldRemoveDuplicatedLeadingSeparator) + { + std::string value("//foo"); + normalizeFilenameInPlace(value); + EXPECT_EQ(value, "foo"); + } + TEST(VFSPathNormalizedTest, shouldSupportDefaultConstructor) { const Normalized value; @@ -79,6 +122,13 @@ namespace VFS::Path EXPECT_EQ(value.value(), "foo/bar/baz"); } + TEST(VFSPathNormalizedTest, operatorDivShouldNormalizeSuffix) + { + Normalized value("foo/bar"); + value /= std::string_view("\\A\\\\B"); + EXPECT_EQ(value.value(), "foo/bar/a/b"); + } + TEST(VFSPathNormalizedTest, changeExtensionShouldReplaceAfterLastDot) { Normalized value("foo/bar.a"); @@ -86,11 +136,10 @@ namespace VFS::Path EXPECT_EQ(value.value(), "foo/bar.so"); } - TEST(VFSPathNormalizedTest, changeExtensionShouldNormalizeExtension) + TEST(VFSPathNormalizedTest, changeExtensionShouldThrowExceptionOnNotNormalizedExtension) { Normalized value("foo/bar.a"); - ASSERT_TRUE(value.changeExtension("SO")); - EXPECT_EQ(value.value(), "foo/bar.so"); + EXPECT_THROW(value.changeExtension("\\SO"), std::invalid_argument); } TEST(VFSPathNormalizedTest, changeExtensionShouldIgnorePathWithoutADot) @@ -116,7 +165,7 @@ namespace VFS::Path TEST(VFSPathNormalizedTest, changeExtensionShouldThrowExceptionOnExtensionWithSeparator) { Normalized value("foo.a"); - EXPECT_THROW(value.changeExtension("/so"), std::invalid_argument); + EXPECT_THROW(value.changeExtension("so/"), std::invalid_argument); } template diff --git a/components/misc/resourcehelpers.cpp b/components/misc/resourcehelpers.cpp index 9b0e81dc59..0a9ac84168 100644 --- a/components/misc/resourcehelpers.cpp +++ b/components/misc/resourcehelpers.cpp @@ -1,6 +1,5 @@ #include "resourcehelpers.hpp" -#include #include #include @@ -39,14 +38,6 @@ std::string Misc::ResourceHelpers::correctResourcePath(std::span