@@ -701,6 +701,107 @@ TEST_F(test_file_canonical, canonical_path_win32_root) {
701701 }
702702}
703703#endif
704+
705+ #if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L
706+ TEST_F (test_file_canonical, parent_of_root_is_root_posix) {
707+ for (const char * root : {" /" , " //" }) {
708+ canonical_path p (root);
709+ EXPECT_FALSE (p.parent ());
710+ EXPECT_EQ (p.path (), root);
711+ }
712+ }
713+
714+ TEST_F (test_file_canonical, parent_of_non_root_removes_component_posix) {
715+ struct test_case {
716+ const std::string_view before;
717+ const std::string_view after;
718+ };
719+
720+ for (const test_case& tc : {
721+ test_case{" /hello" , " /" },
722+ test_case{" /dir/subdir" , " /dir" },
723+ test_case{" /a/b/c/d" , " /a/b/c" },
724+ test_case{" //implementation-defined" , " //" },
725+ test_case{" //implementation/defined" , " //implementation" },
726+ }) {
727+ canonical_path p (std::string (tc.before ));
728+ EXPECT_TRUE (p.parent ());
729+ EXPECT_EQ (p.path (), tc.after ) << " before = " << tc.before ;
730+ }
731+ }
732+
733+ TEST_F (test_file_canonical, append_component_posix) {
734+ struct test_case {
735+ const std::string_view before;
736+ const std::string_view component;
737+ const std::string_view after;
738+ };
739+
740+ for (const test_case& tc : {
741+ test_case{" /" , " hello" , " /hello" },
742+ test_case{" /dir" , " subdir" , " /dir/subdir" },
743+ test_case{" //" , " hello" , " //hello" },
744+ test_case{" //dir" , " subdir" , " //dir/subdir" },
745+ }) {
746+ canonical_path p (std::string (tc.before ));
747+ p.append_component (tc.component );
748+ EXPECT_EQ (p.path (), tc.after )
749+ << " before = " << tc.before << " \n component = " << tc.component ;
750+ }
751+ }
752+ #endif
753+
754+ #if defined(_WIN32)
755+ TEST_F (test_file_canonical, parent_of_root_is_root_win32) {
756+ for (const char * root : {R"( C:\)" , R"( \\?\X:\)" , R"( \\server\share)" }) {
757+ canonical_path p (root);
758+ EXPECT_FALSE (p.parent ());
759+ EXPECT_EQ (p.path (), root);
760+ }
761+ }
762+
763+ TEST_F (test_file_canonical, parent_of_non_root_removes_component_win32) {
764+ struct test_case {
765+ const std::string_view before;
766+ const std::string_view after;
767+ };
768+
769+ for (const test_case& tc : {
770+ test_case{R"( C:\hello)" , R"( C:\)" },
771+ test_case{R"( C:\dir\subdir)" , R"( C:\dir)" },
772+ test_case{R"( C:\a\b\c\d)" , R"( C:\a\b\c)" },
773+ test_case{R"( \\?\X:\hello)" , R"( \\?\X:\)" },
774+ test_case{R"( \\?\X:\dir\subdir)" , R"( \\?\X:\dir)" },
775+ test_case{R"( \\?\X:\a\b\c\d)" , R"( \\?\X:\a\b\c)" },
776+ test_case{R"( \\server\share\file)" , R"( \\server\share)" },
777+ }) {
778+ canonical_path p (std::string (tc.before ));
779+ EXPECT_TRUE (p.parent ());
780+ EXPECT_EQ (p.path (), tc.after ) << " before = " << tc.before ;
781+ }
782+ }
783+
784+ TEST_F (test_file_canonical, append_component_win32) {
785+ struct test_case {
786+ const std::string_view before;
787+ const std::string_view component;
788+ const std::string_view after;
789+ };
790+
791+ for (const test_case& tc : {
792+ test_case{R"( C:\)" , R"( hello)" , R"( C:\hello)" },
793+ test_case{R"( C:\dir)" , R"( subdir)" , R"( C:\dir\subdir)" },
794+ test_case{R"( \\?\X:\)" , R"( hello)" , R"( \\?\X:\hello)" },
795+ test_case{R"( \\?\X:\dir)" , R"( subdir)" , R"( \\?\X:\dir\subdir)" },
796+ test_case{R"( \\server\share)" , R"( dir)" , R"( \\server\share\dir)" },
797+ }) {
798+ canonical_path p (std::string (tc.before ));
799+ p.append_component (tc.component );
800+ EXPECT_EQ (p.path (), tc.after )
801+ << " before = " << tc.before << " \n component = " << tc.component ;
802+ }
803+ }
804+ #endif
704805}
705806}
706807
0 commit comments