@@ -38,6 +38,8 @@ class TestPath : public TestFixture {
3838 TEST_CASE (is_cpp);
3939 TEST_CASE (get_path_from_filename);
4040 TEST_CASE (join);
41+ TEST_CASE (getAbsoluteFilePath);
42+ TEST_CASE (stripDirectoryPart);
4143 }
4244
4345 void removeQuotationMarks () const {
@@ -93,18 +95,48 @@ class TestPath : public TestFixture {
9395 }
9496
9597 void getRelative () const {
96- const std::vector<std::string> basePaths = {
97- " " , // Don't crash with empty paths
98- " C:/foo" ,
99- " C:/bar/" ,
100- " C:/test.cpp"
101- };
102-
103- ASSERT_EQUALS (" x.c" , Path::getRelativePath (" C:/foo/x.c" , basePaths));
104- ASSERT_EQUALS (" y.c" , Path::getRelativePath (" C:/bar/y.c" , basePaths));
105- ASSERT_EQUALS (" foo/y.c" , Path::getRelativePath (" C:/bar/foo/y.c" , basePaths));
106- ASSERT_EQUALS (" C:/test.cpp" , Path::getRelativePath (" C:/test.cpp" , basePaths));
107- ASSERT_EQUALS (" C:/foobar/test.cpp" , Path::getRelativePath (" C:/foobar/test.cpp" , basePaths));
98+ {
99+ const std::vector<std::string> basePaths = {
100+ " " , // Don't crash with empty paths
101+ " C:/foo" ,
102+ " C:/bar/" ,
103+ " C:/test.cpp"
104+ };
105+
106+ ASSERT_EQUALS (" x.c" , Path::getRelativePath (" C:/foo/x.c" , basePaths));
107+ ASSERT_EQUALS (" y.c" , Path::getRelativePath (" C:/bar/y.c" , basePaths));
108+ ASSERT_EQUALS (" foo/y.c" , Path::getRelativePath (" C:/bar/foo/y.c" , basePaths));
109+ ASSERT_EQUALS (" C:/test.cpp" , Path::getRelativePath (" C:/test.cpp" , basePaths));
110+ ASSERT_EQUALS (" C:/foobar/test.cpp" , Path::getRelativePath (" C:/foobar/test.cpp" , basePaths));
111+ }
112+ {
113+ const std::vector<std::string> basePaths = {
114+ " " , // Don't crash with empty paths
115+ " C:\\ foo" ,
116+ " C:\\ bar\\ " ,
117+ " C:\\ test.cpp"
118+ };
119+
120+ ASSERT_EQUALS (" x.c" , Path::getRelativePath (" C:\\ foo\\ x.c" , basePaths));
121+ ASSERT_EQUALS (" y.c" , Path::getRelativePath (" C:\\ bar\\ y.c" , basePaths));
122+ ASSERT_EQUALS (" foo/y.c" , Path::getRelativePath (" C:\\ bar\\ foo\\ y.c" , basePaths));
123+ ASSERT_EQUALS (" C:/test.cpp" , Path::getRelativePath (" C:\\ test.cpp" , basePaths));
124+ ASSERT_EQUALS (" C:/foobar/test.cpp" , Path::getRelativePath (" C:\\ foobar\\ test.cpp" , basePaths));
125+ }
126+ {
127+ const std::vector<std::string> basePaths = {
128+ " " , // Don't crash with empty paths
129+ " /c/foo" ,
130+ " /c/bar/" ,
131+ " /c/test.cpp"
132+ };
133+
134+ ASSERT_EQUALS (" x.c" , Path::getRelativePath (" /c/foo/x.c" , basePaths));
135+ ASSERT_EQUALS (" y.c" , Path::getRelativePath (" /c/bar/y.c" , basePaths));
136+ ASSERT_EQUALS (" foo/y.c" , Path::getRelativePath (" /c/bar/foo\\ y.c" , basePaths));
137+ ASSERT_EQUALS (" /c/test.cpp" , Path::getRelativePath (" /c/test.cpp" , basePaths));
138+ ASSERT_EQUALS (" /c/foobar/test.cpp" , Path::getRelativePath (" /c/foobar/test.cpp" , basePaths));
139+ }
108140 }
109141
110142 void is_c () const {
@@ -141,14 +173,48 @@ class TestPath : public TestFixture {
141173 ASSERT_EQUALS (" /tmp/" , Path::getPathFromFilename (" /tmp/index.h" ));
142174 ASSERT_EQUALS (" a/b/c/" , Path::getPathFromFilename (" a/b/c/index.h" ));
143175 ASSERT_EQUALS (" a/b/c/" , Path::getPathFromFilename (" a/b/c/" ));
176+ ASSERT_EQUALS (" S:\\ tmp\\ " , Path::getPathFromFilename (" S:\\ tmp\\ index.h" ));
177+ ASSERT_EQUALS (" a\\ b\\ c\\ " , Path::getPathFromFilename (" a\\ b\\ c\\ index.h" ));
178+ ASSERT_EQUALS (" a\\ b\\ c\\ " , Path::getPathFromFilename (" a\\ b\\ c\\ " ));
179+ ASSERT_EQUALS (" S:\\ a\\ b\\ c\\ " , Path::getPathFromFilename (" S:\\ a\\ b\\ c\\ " ));
180+ ASSERT_EQUALS (" S:/tmp/" , Path::getPathFromFilename (" S:/tmp/index.h" ));
181+ ASSERT_EQUALS (" S:/a/b/c/" , Path::getPathFromFilename (" S:/a/b/c/index.h" ));
144182 }
145183
146184 void join () const {
147185 ASSERT_EQUALS (" a" , Path::join (" a" , " " ));
148186 ASSERT_EQUALS (" a" , Path::join (" " , " a" ));
149187 ASSERT_EQUALS (" a/b" , Path::join (" a" , " b" ));
150188 ASSERT_EQUALS (" a/b" , Path::join (" a/" , " b" ));
189+ ASSERT_EQUALS (" a/b" , Path::join (" a\\ " , " b" ));
151190 ASSERT_EQUALS (" /b" , Path::join (" a" , " /b" ));
191+ ASSERT_EQUALS (" /b" , Path::join (" a" , " \\ b" ));
192+ }
193+
194+ // TODO: this is quite messy - should Path::getAbsoluteFilePath() return normalized separators?
195+ void getAbsoluteFilePath () const {
196+ // Path::getAbsoluteFilePath() only works with existing paths on Linux
197+ #ifdef _WIN32
198+ const std::string cwd = Path::getCurrentPath ();
199+ ASSERT_EQUALS (Path::join (cwd, " a.h" ), Path::fromNativeSeparators (Path::getAbsoluteFilePath (" a.h" )));
200+ ASSERT_EQUALS (Path::join (cwd, " inc/a.h" ), Path::fromNativeSeparators (Path::getAbsoluteFilePath (" inc/a.h" )));
201+ const std::string cwd_down = Path::getPathFromFilename (cwd);
202+ ASSERT_EQUALS (Path::join (cwd_down, " a.h" ), Path::fromNativeSeparators (Path::getAbsoluteFilePath (" ../a.h" )));
203+ ASSERT_EQUALS (Path::join (cwd_down, " inc/a.h" ), Path::fromNativeSeparators (Path::getAbsoluteFilePath (" ../inc/a.h" )));
204+ ASSERT_EQUALS (Path::join (cwd_down, " inc/a.h" ), Path::fromNativeSeparators (Path::getAbsoluteFilePath (" ../inc/../inc/a.h" )));
205+ #endif
206+ }
207+
208+ void stripDirectoryPart () const {
209+ ASSERT_EQUALS (" a.h" , Path::stripDirectoryPart (" a.h" ));
210+ ASSERT_EQUALS (" a.h" , Path::stripDirectoryPart (" a/a.h" ));
211+ ASSERT_EQUALS (" a.h" , Path::stripDirectoryPart (" a/b/a.h" ));
212+ ASSERT_EQUALS (" a.h" , Path::stripDirectoryPart (" /mnt/a/b/a.h" ));
213+ ASSERT_EQUALS (" a.h" , Path::stripDirectoryPart (" a\\ a.h" ));
214+ ASSERT_EQUALS (" a.h" , Path::stripDirectoryPart (" a\\ b\\ a.h" ));
215+ ASSERT_EQUALS (" a.h" , Path::stripDirectoryPart (" S:\\ a\\ b\\ a.h" ));
216+ ASSERT_EQUALS (" a.h" , Path::stripDirectoryPart (" S:/a/b/a.h" ));
217+
152218 }
153219};
154220
0 commit comments