From 1689c59546a862cc5b9ee2276f080bd7a71a7015 Mon Sep 17 00:00:00 2001 From: elsid Date: Sat, 3 Feb 2024 14:09:50 +0100 Subject: [PATCH] Add tests for VFS::Path::Normalized --- apps/openmw_test_suite/CMakeLists.txt | 2 + apps/openmw_test_suite/vfs/testpathutil.cpp | 99 +++++++++++++++++++++ components/vfs/pathutil.hpp | 22 +++-- 3 files changed, 114 insertions(+), 9 deletions(-) create mode 100644 apps/openmw_test_suite/vfs/testpathutil.cpp diff --git a/apps/openmw_test_suite/CMakeLists.txt b/apps/openmw_test_suite/CMakeLists.txt index 967511953d..71da2de590 100644 --- a/apps/openmw_test_suite/CMakeLists.txt +++ b/apps/openmw_test_suite/CMakeLists.txt @@ -97,6 +97,8 @@ file(GLOB UNITTEST_SRC_FILES esmterrain/testgridsampling.cpp resource/testobjectcache.cpp + + vfs/testpathutil.cpp ) source_group(apps\\openmw_test_suite FILES openmw_test_suite.cpp ${UNITTEST_SRC_FILES}) diff --git a/apps/openmw_test_suite/vfs/testpathutil.cpp b/apps/openmw_test_suite/vfs/testpathutil.cpp new file mode 100644 index 0000000000..811b2b3691 --- /dev/null +++ b/apps/openmw_test_suite/vfs/testpathutil.cpp @@ -0,0 +1,99 @@ +#include + +#include + +#include + +namespace VFS::Path +{ + namespace + { + using namespace testing; + + TEST(NormalizedTest, shouldSupportDefaultConstructor) + { + const Normalized value; + EXPECT_EQ(value.value(), ""); + } + + TEST(NormalizedTest, shouldSupportConstructorFromString) + { + const std::string string("Foo\\Bar/baz"); + const Normalized value(string); + EXPECT_EQ(value.value(), "foo/bar/baz"); + } + + TEST(NormalizedTest, shouldSupportConstructorFromConstCharPtr) + { + const char* const ptr = "Foo\\Bar/baz"; + const Normalized value(ptr); + EXPECT_EQ(value.value(), "foo/bar/baz"); + } + + TEST(NormalizedTest, shouldSupportConstructorFromStringView) + { + const std::string_view view = "Foo\\Bar/baz"; + const Normalized value(view); + EXPECT_EQ(value.view(), "foo/bar/baz"); + } + + TEST(NormalizedTest, supportMovingValueOut) + { + Normalized value("Foo\\Bar/baz"); + EXPECT_EQ(std::move(value).value(), "foo/bar/baz"); + EXPECT_EQ(value.value(), ""); + } + + TEST(NormalizedTest, isNotEqualToNotNormalized) + { + const Normalized value("Foo\\Bar/baz"); + EXPECT_NE(value.value(), "Foo\\Bar/baz"); + } + + TEST(NormalizedTest, shouldSupportOperatorLeftShiftToOStream) + { + const Normalized value("Foo\\Bar/baz"); + std::stringstream stream; + stream << value; + EXPECT_EQ(stream.str(), "foo/bar/baz"); + } + + template + struct NormalizedOperatorsTest : Test + { + }; + + TYPED_TEST_SUITE_P(NormalizedOperatorsTest); + + TYPED_TEST_P(NormalizedOperatorsTest, supportsEqual) + { + const Normalized normalized("a/foo/bar/baz"); + const TypeParam otherEqual{ "a/foo/bar/baz" }; + const TypeParam otherNotEqual{ "b/foo/bar/baz" }; + EXPECT_EQ(normalized, otherEqual); + EXPECT_EQ(otherEqual, normalized); + EXPECT_NE(normalized, otherNotEqual); + EXPECT_NE(otherNotEqual, normalized); + } + + TYPED_TEST_P(NormalizedOperatorsTest, supportsLess) + { + const Normalized normalized("b/foo/bar/baz"); + const TypeParam otherEqual{ "b/foo/bar/baz" }; + const TypeParam otherLess{ "a/foo/bar/baz" }; + const TypeParam otherGreater{ "c/foo/bar/baz" }; + EXPECT_FALSE(normalized < otherEqual); + EXPECT_FALSE(otherEqual < normalized); + EXPECT_LT(otherLess, normalized); + EXPECT_FALSE(normalized < otherLess); + EXPECT_LT(normalized, otherGreater); + EXPECT_FALSE(otherGreater < normalized); + } + + REGISTER_TYPED_TEST_SUITE_P(NormalizedOperatorsTest, supportsEqual, supportsLess); + + using StringTypes = Types; + + INSTANTIATE_TYPED_TEST_SUITE_P(Typed, NormalizedOperatorsTest, StringTypes); + } +} diff --git a/components/vfs/pathutil.hpp b/components/vfs/pathutil.hpp index 0856bfffa2..6ee33f64d2 100644 --- a/components/vfs/pathutil.hpp +++ b/components/vfs/pathutil.hpp @@ -96,22 +96,26 @@ namespace VFS::Path friend bool operator==(const Normalized& lhs, const Normalized& rhs) = default; - template - friend bool operator==(const Normalized& lhs, const T& rhs) + friend bool operator==(const Normalized& lhs, const auto& rhs) { return lhs.mValue == rhs; } + +#if defined(_MSC_VER) && _MSC_VER <= 1935 + friend bool operator==(const auto& lhs, const Normalized& rhs) { - return lhs.mValue == rhs; + return lhs == rhs.mValue; + } +#endif + + friend bool operator<(const Normalized& lhs, const Normalized& rhs) + { + return lhs.mValue < rhs.mValue; } - friend bool operator<(const Normalized& lhs, const Normalized& rhs) { return lhs.mValue < rhs.mValue; } - - template - friend bool operator<(const Normalized& lhs, const T& rhs) + friend bool operator<(const Normalized& lhs, const auto& rhs) { return lhs.mValue < rhs; } - template - friend bool operator<(const T& lhs, const Normalized& rhs) + friend bool operator<(const auto& lhs, const Normalized& rhs) { return lhs < rhs.mValue; }