Reduce branching in SearchInSortVector
[TortoiseGit.git] / test / UnitTests / PathUtilsTest.cpp
blob1d96d9579f3c31c3cb2b66f423536373c4e7eaa4
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2015-2017 - TortoiseGit
4 // Copyright (C) 2003-2008, 2013-2014 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "stdafx.h"
22 #include "PathUtils.h"
24 TEST(CPathUtils, UnescapeTest)
26 CString test(L"file:///d:/REpos1/uCOS-100/Trunk/name%20with%20spaces/NewTest%20%%20NewTest");
27 CString test2 = CPathUtils::PathUnescape(test);
28 EXPECT_STREQ(L"file:///d:/REpos1/uCOS-100/Trunk/name with spaces/NewTest % NewTest", test2);
29 CStringA test3 = CPathUtils::PathEscape("file:///d:/REpos1/uCOS-100/Trunk/name with spaces/NewTest % NewTest");
30 EXPECT_STREQ("file:///d:/REpos1/uCOS-100/Trunk/name%20with%20spaces/NewTest%20%%20NewTest", test3);
33 TEST(CPathUtils, GetFileNameFromPath)
35 CString test(L"d:\\test\\filename.ext");
36 EXPECT_STREQ(L"filename.ext", CPathUtils::GetFileNameFromPath(test));
37 test = L"filename.ext";
38 EXPECT_STREQ(L"filename.ext", CPathUtils::GetFileNameFromPath(test));
39 test = L"d:/test/filename";
40 EXPECT_STREQ(L"filename", CPathUtils::GetFileNameFromPath(test));
41 test = L"d:\\test\\filename";
42 EXPECT_STREQ(L"filename", CPathUtils::GetFileNameFromPath(test));
43 test = L"filename";
44 EXPECT_STREQ(L"filename", CPathUtils::GetFileNameFromPath(test));
45 test.Empty();
46 EXPECT_STREQ(L"", CPathUtils::GetFileNameFromPath(test));
49 TEST(CPathUtils, ExtTest)
51 CString test(L"d:\\test\\filename.ext");
52 EXPECT_STREQ(L".ext", CPathUtils::GetFileExtFromPath(test));
53 test = L"filename.ext";
54 EXPECT_STREQ(L".ext", CPathUtils::GetFileExtFromPath(test));
55 test = L"d:\\test\\filename";
56 EXPECT_STREQ(L"", CPathUtils::GetFileExtFromPath(test));
57 test = L"filename";
58 EXPECT_STREQ(L"", CPathUtils::GetFileExtFromPath(test));
59 test.Empty();
60 EXPECT_STREQ(L"", CPathUtils::GetFileExtFromPath(test));
63 TEST(CPathUtils, ParseTests)
65 CString test(L"test 'd:\\testpath with spaces' test");
66 EXPECT_STREQ(L"d:\\testpath with spaces", CPathUtils::ParsePathInString(test));
67 test = L"d:\\testpath with spaces";
68 EXPECT_STREQ(L"d:\\testpath with spaces", CPathUtils::ParsePathInString(test));
71 TEST(CPathUtils, MakeSureDirectoryPathExists)
73 CAutoTempDir tmpDir;
74 EXPECT_TRUE(PathIsDirectory(tmpDir.GetTempDir()));
75 EXPECT_FALSE(PathFileExists(tmpDir.GetTempDir() + L"\\sub"));
76 EXPECT_FALSE(PathIsDirectory(tmpDir.GetTempDir() + L"\\sub"));
78 EXPECT_TRUE(CPathUtils::MakeSureDirectoryPathExists(tmpDir.GetTempDir() + L"\\sub\\sub\\dir"));
79 EXPECT_TRUE(PathIsDirectory(tmpDir.GetTempDir() + L"\\sub"));
80 EXPECT_TRUE(PathIsDirectory(tmpDir.GetTempDir() + L"\\sub\\sub"));
81 EXPECT_TRUE(PathIsDirectory(tmpDir.GetTempDir() + L"\\sub\\sub\\dir"));
83 EXPECT_TRUE(CPathUtils::MakeSureDirectoryPathExists(tmpDir.GetTempDir() + L"\\sub/asub/adir"));
84 EXPECT_TRUE(PathIsDirectory(tmpDir.GetTempDir() + L"\\sub"));
85 EXPECT_TRUE(PathIsDirectory(tmpDir.GetTempDir() + L"\\sub\\asub"));
86 EXPECT_TRUE(PathIsDirectory(tmpDir.GetTempDir() + L"\\sub\\asub\\adir"));
89 TEST(CPathUtils, EnsureTrailingPathDelimiter)
91 CString tPath;
92 CPathUtils::EnsureTrailingPathDelimiter(tPath);
93 EXPECT_STREQ(tPath, L"");
95 tPath = L"C:";
96 CPathUtils::EnsureTrailingPathDelimiter(tPath);
97 EXPECT_STREQ(tPath, L"C:\\");
99 tPath = L"C:\\";
100 CPathUtils::EnsureTrailingPathDelimiter(tPath);
101 EXPECT_STREQ(tPath, L"C:\\");
103 tPath = L"C:\\my\\path";
104 CPathUtils::EnsureTrailingPathDelimiter(tPath);
105 EXPECT_STREQ(tPath, L"C:\\my\\path\\");
107 tPath = L"C:\\my\\path\\";
108 CPathUtils::EnsureTrailingPathDelimiter(tPath);
109 EXPECT_STREQ(tPath, L"C:\\my\\path\\");
112 TEST(CPathUtils, BuildPathWithPathDelimiter)
114 EXPECT_STREQ(CPathUtils::BuildPathWithPathDelimiter(L""), L"");
115 EXPECT_STREQ(CPathUtils::BuildPathWithPathDelimiter(L"C:"), L"C:\\");
116 EXPECT_STREQ(CPathUtils::BuildPathWithPathDelimiter(L"C:\\"), L"C:\\");
117 EXPECT_STREQ(CPathUtils::BuildPathWithPathDelimiter(L"C:\\my\\path"), L"C:\\my\\path\\");
118 EXPECT_STREQ(CPathUtils::BuildPathWithPathDelimiter(L"C:\\my\\path\\"), L"C:\\my\\path\\");
121 TEST(CPathUtils, TrimTrailingPathDelimiter)
123 CString tPath;
124 CPathUtils::TrimTrailingPathDelimiter(tPath);
125 EXPECT_STREQ(tPath, L"");
127 tPath = L"C:";
128 CPathUtils::TrimTrailingPathDelimiter(tPath);
129 EXPECT_STREQ(tPath, L"C:");
131 tPath = L"C:\\";
132 CPathUtils::TrimTrailingPathDelimiter(tPath);
133 EXPECT_STREQ(tPath, L"C:");
135 tPath = L"C:\\my\\path";
136 CPathUtils::TrimTrailingPathDelimiter(tPath);
137 EXPECT_STREQ(tPath, L"C:\\my\\path");
139 tPath = L"C:\\my\\path\\";
140 CPathUtils::TrimTrailingPathDelimiter(tPath);
141 EXPECT_STREQ(tPath, L"C:\\my\\path");
143 tPath = L"C:\\my\\path\\\\";
144 CPathUtils::TrimTrailingPathDelimiter(tPath);
145 EXPECT_STREQ(tPath, L"C:\\my\\path");
148 TEST(CPathUtils, ExpandFileName)
150 EXPECT_STREQ(CPathUtils::ExpandFileName(L"C:\\my\\path\\da\\da\\da"), L"C:\\my\\path\\da\\da\\da");
151 EXPECT_STREQ(CPathUtils::ExpandFileName(L"C:\\my\\path\\da\\da\\da\\"), L"C:\\my\\path\\da\\da\\da\\");
153 EXPECT_STREQ(CPathUtils::ExpandFileName(L"C:\\my\\path\\\\da\\da\\da"), L"C:\\my\\path\\da\\da\\da");
154 EXPECT_STREQ(CPathUtils::ExpandFileName(L"C:\\my\\path\\.\\da\\da\\da"), L"C:\\my\\path\\da\\da\\da");
155 EXPECT_STREQ(CPathUtils::ExpandFileName(L"C:\\my\\path\\.\\da\\da\\da\\"), L"C:\\my\\path\\da\\da\\da\\");
157 EXPECT_STREQ(CPathUtils::ExpandFileName(L"C:\\my\\path\\..\\da\\da\\da"), L"C:\\my\\da\\da\\da");
158 EXPECT_STREQ(CPathUtils::ExpandFileName(L"C:\\my\\path\\..\\da\\da\\da\\"), L"C:\\my\\da\\da\\da\\");
160 // "\\.\\C:\\"
161 EXPECT_STREQ(CPathUtils::ExpandFileName(L"\\\\.\\C:\\my\\path\\da\\da\\da"), L"\\\\.\\C:\\my\\path\\da\\da\\da");
162 EXPECT_STREQ(CPathUtils::ExpandFileName(L"\\\\.\\C:\\my\\path\\da\\da\\da\\"), L"\\\\.\\C:\\my\\path\\da\\da\\da\\");
164 EXPECT_STREQ(CPathUtils::ExpandFileName(L"\\\\.\\C:\\my\\path\\\\da\\da\\da"), L"\\\\.\\C:\\my\\path\\da\\da\\da");
165 EXPECT_STREQ(CPathUtils::ExpandFileName(L"\\\\.\\C:\\my\\path\\.\\da\\da\\da"), L"\\\\.\\C:\\my\\path\\da\\da\\da");
166 EXPECT_STREQ(CPathUtils::ExpandFileName(L"\\\\.\\C:\\my\\path\\.\\da\\da\\da\\"), L"\\\\.\\C:\\my\\path\\da\\da\\da\\");
168 EXPECT_STREQ(CPathUtils::ExpandFileName(L"\\\\.\\C:\\my\\path\\..\\da\\da\\da"), L"\\\\.\\C:\\my\\da\\da\\da");
169 EXPECT_STREQ(CPathUtils::ExpandFileName(L"\\\\.\\C:\\my\\path\\..\\da\\da\\da\\"), L"\\\\.\\C:\\my\\da\\da\\da\\");
171 // UNC paths
172 EXPECT_STREQ(CPathUtils::ExpandFileName(L"\\\\DACOMPUTER\\my\\path\\.\\da\\da\\da"), L"\\\\DACOMPUTER\\my\\path\\da\\da\\da");
173 EXPECT_STREQ(CPathUtils::ExpandFileName(L"\\\\DACOMPUTER\\my\\path\\.\\da\\da\\da\\"), L"\\\\DACOMPUTER\\my\\path\\da\\da\\da\\");
175 EXPECT_STREQ(CPathUtils::ExpandFileName(L"\\\\DACOMPUTER\\my\\path\\..\\da\\da\\da"), L"\\\\DACOMPUTER\\my\\da\\da\\da");
176 EXPECT_STREQ(CPathUtils::ExpandFileName(L"\\\\DACOMPUTER\\my\\path\\..\\da\\da\\da\\"), L"\\\\DACOMPUTER\\my\\da\\da\\da\\");
179 TEST(CPathUtils, IsSamePath)
181 EXPECT_TRUE(CPathUtils::IsSamePath(L"", L""));
183 EXPECT_TRUE(CPathUtils::IsSamePath(L"C:\\my\\path\\da\\da\\da", L"C:\\my\\path\\da\\da\\da"));
184 EXPECT_TRUE(CPathUtils::IsSamePath(L"C:\\my\\path\\da\\da\\da", L"c:\\my\\pAth\\DA\\da\\da"));
185 EXPECT_TRUE(CPathUtils::IsSamePath(L"C:\\my\\path\\da\\da\\da", L"C:\\my\\path\\da\\da\\da\\"));
186 EXPECT_TRUE(CPathUtils::IsSamePath(L"C:\\my\\path\\da\\da\\da", L"C:\\my\\path\\da\\da\\da\\."));
187 EXPECT_TRUE(CPathUtils::IsSamePath(L"C:\\my\\path\\da\\da\\da", L"C:\\my\\path\\da\\da\\da\\.\\"));
188 EXPECT_TRUE(CPathUtils::IsSamePath(L"C:\\my\\path\\da\\da\\da", L"C:\\my\\path\\.\\.\\da\\da\\da"));
189 EXPECT_TRUE(CPathUtils::IsSamePath(L"C:\\my\\path\\da\\da\\da", L"C:\\my\\path\\..\\path\\da\\da\\da"));
190 EXPECT_TRUE(CPathUtils::IsSamePath(L"C:\\my\\path\\da\\da\\da", L"C:\\my\\path\\..\\path\\da\\da\\da\\bla\\.."));
192 EXPECT_FALSE(CPathUtils::IsSamePath(L"C:\\my\\path\\da\\da\\da", L"C:\\this\\is\\a\\new\\path"));
193 EXPECT_FALSE(CPathUtils::IsSamePath(L"C:\\my\\path\\da\\da\\da", L"C:\\my\\path\\da\\da\\da\\.."));
194 EXPECT_FALSE(CPathUtils::IsSamePath(L"C:\\my\\path\\da\\da\\da", L"C:\\my\\path\\da\\da\\da\\..\\"));
195 EXPECT_FALSE(CPathUtils::IsSamePath(L"C:\\my\\path\\da\\da\\da", L"C:\\my\\path\\..\\path\\da\\da\\da\\.\\da"));