2 // System.IO.Path Test Cases
5 // Marcin Szczepanski (marcins@zipworld.com.au)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // Ben Maurer (bmaurer@users.sf.net)
8 // Gilles Freart (gfr@skynet.be)
10 // (c) Marcin Szczepanski
11 // (c) 2002 Ximian, Inc. (http://www.ximian.com)
12 // (c) 2003 Ben Maurer
13 // (c) 2003 Gilles Freart
16 #define NUNIT // Comment out this one if you wanna play with the test without using NUnit
19 using NUnit.Framework;
21 using System.Reflection;
28 namespace MonoTests.System.IO
38 public class PathTest : TestCase
48 static char DSC = Path.DirectorySeparatorChar;
51 protected override void SetUp ()
59 path1 = "/foo/test.txt";
62 } else if ('\\' == DSC) {
64 path1 = "c:\\foo\\test.txt";
65 path2 = Environment.GetEnvironmentVariable ("SYSTEMROOT");
69 //FIXME: For Mac. figure this out when we need it
70 path1 = "foo:test.txt";
79 return OS == OsType.Windows;
86 return OS == OsType.Unix;
93 return OS == OsType.Mac;
97 public void TestChangeExtension ()
99 string [] files = new string [3];
100 files [(int) OsType.Unix] = "/foo/test.doc";
101 files [(int) OsType.Windows] = "c:\\foo\\test.doc";
102 files [(int) OsType.Mac] = "foo:test.doc";
104 string testPath = Path.ChangeExtension (path1, "doc");
105 AssertEquals ("ChangeExtension #01", files [(int) OS], testPath);
107 testPath = Path.ChangeExtension ("", ".extension");
108 AssertEquals ("ChangeExtension #02", String.Empty, testPath);
110 testPath = Path.ChangeExtension (null, ".extension");
111 AssertEquals ("ChangeExtension #03", null, testPath);
113 testPath = Path.ChangeExtension ("path", null);
114 AssertEquals ("ChangeExtension #04", "path", testPath);
116 testPath = Path.ChangeExtension ("path.ext", "doc");
117 AssertEquals ("ChangeExtension #05", "path.doc", testPath);
119 testPath = Path.ChangeExtension ("path.ext1.ext2", "doc");
120 AssertEquals ("ChangeExtension #06", "path.ext1.doc", testPath);
124 testPath = Path.ChangeExtension ("<", ".extension");
125 Fail ("ChangeException Fail #01");
126 } catch (Exception e) {
127 AssertEquals ("ChangeExtension Exc. #01", typeof (ArgumentException), e.GetType ());
132 public void TestCombine ()
134 string [] files = new string [3];
135 files [(int) OsType.Unix] = "/etc/init.d";
136 files [(int) OsType.Windows] = Environment.GetEnvironmentVariable ("SYSTEMROOT") + @"\system32";
137 files [(int) OsType.Mac] = "foo:bar";
139 string testPath = Path.Combine (path2, path3);
140 AssertEquals ("Combine #01", files [(int) OS], testPath);
142 testPath = Path.Combine ("one", "");
143 AssertEquals ("Combine #02", "one", testPath);
145 testPath = Path.Combine ("", "one");
146 AssertEquals ("Combine #03", "one", testPath);
148 string current = Directory.GetCurrentDirectory ();
149 testPath = Path.Combine (current, "one");
151 string expected = current + DSC + "one";
152 AssertEquals ("Combine #04", expected, testPath);
154 testPath = Path.Combine ("one", current);
155 // LAMESPEC noted in Path.cs
156 AssertEquals ("Combine #05", current, testPath);
158 testPath = Path.Combine (current, expected);
159 AssertEquals ("Combine #06", expected, testPath);
161 testPath = DSC + "one";
162 testPath = Path.Combine (testPath, "two" + DSC);
163 expected = DSC + "one" + DSC + "two" + DSC;
164 AssertEquals ("Combine #06", expected, testPath);
166 testPath = "one" + DSC;
167 testPath = Path.Combine (testPath, DSC + "two");
168 expected = DSC + "two";
169 AssertEquals ("Combine #06", expected, testPath);
171 testPath = "one" + DSC;
172 testPath = Path.Combine (testPath, "two" + DSC);
173 expected = "one" + DSC + "two" + DSC;
174 AssertEquals ("Combine #07", expected, testPath);
176 //TODO: Tests for UNC names
178 testPath = Path.Combine ("one", null);
179 Fail ("Combine Fail #01");
180 } catch (Exception e) {
181 AssertEquals ("Combine Exc. #01", typeof (ArgumentNullException), e.GetType ());
185 testPath = Path.Combine (null, "one");
186 Fail ("Combine Fail #02");
187 } catch (Exception e) {
188 AssertEquals ("Combine Exc. #02", typeof (ArgumentNullException), e.GetType ());
193 testPath = Path.Combine ("a>", "one");
194 Fail ("Combine Fail #03");
195 } catch (Exception e) {
196 AssertEquals ("Combine Exc. #03", typeof (ArgumentException), e.GetType ());
200 testPath = Path.Combine ("one", "aaa<");
201 Fail ("Combine Fail #04");
202 } catch (Exception e) {
203 AssertEquals ("Combine Exc. #04", typeof (ArgumentException), e.GetType ());
209 [ExpectedException (typeof(ArgumentException))]
210 public void EmptyDirectoryName ()
212 string testDirName = Path.GetDirectoryName ("");
215 public void TestDirectoryName ()
217 string [] files = new string [3];
218 files [(int) OsType.Unix] = "/foo";
219 files [(int) OsType.Windows] = "c:\\foo";
220 files [(int) OsType.Mac] = "foo";
222 AssertEquals ("GetDirectoryName #01", null, Path.GetDirectoryName (null));
223 string testDirName = Path.GetDirectoryName (path1);
224 AssertEquals ("GetDirectoryName #02", files [(int) OS], testDirName);
225 testDirName = Path.GetDirectoryName (files [(int) OS] + DSC);
226 AssertEquals ("GetDirectoryName #03", files [(int) OS], testDirName);
230 testDirName = Path.GetDirectoryName ("aaa>");
231 Fail ("GetDirectoryName Fail #02");
232 } catch (Exception e) {
233 AssertEquals ("GetDirectoryName Exc. #02", typeof (ArgumentException), e.GetType ());
238 testDirName = Path.GetDirectoryName (" ");
239 Fail ("GetDirectoryName Fail #03");
240 } catch (Exception e) {
241 AssertEquals ("GetDirectoryName Exc. #03", typeof (ArgumentException), e.GetType ());
245 public void TestGetExtension ()
247 string testExtn = Path.GetExtension (path1);
249 AssertEquals ("GetExtension #01", ".txt", testExtn);
251 testExtn = Path.GetExtension (path2);
252 AssertEquals ("GetExtension #02", String.Empty, testExtn);
254 testExtn = Path.GetExtension (String.Empty);
255 AssertEquals ("GetExtension #03", String.Empty, testExtn);
257 testExtn = Path.GetExtension (null);
258 AssertEquals ("GetExtension #04", null, testExtn);
260 testExtn = Path.GetExtension (" ");
261 AssertEquals ("GetExtension #05", String.Empty, testExtn);
263 testExtn = Path.GetExtension (path1 + ".doc");
264 AssertEquals ("GetExtension #06", ".doc", testExtn);
266 testExtn = Path.GetExtension (path1 + ".doc" + DSC + "a.txt");
267 AssertEquals ("GetExtension #07", ".txt", testExtn);
271 testExtn = Path.GetExtension ("hi<there.txt");
272 Fail ("GetExtension Fail #01");
273 } catch (Exception e) {
274 AssertEquals ("GetExtension Exc. #01", typeof (ArgumentException), e.GetType ());
279 public void TestGetFileName ()
281 string testFileName = Path.GetFileName (path1);
283 AssertEquals ("GetFileName #01", "test.txt", testFileName);
284 testFileName = Path.GetFileName (null);
285 AssertEquals ("GetFileName #02", null, testFileName);
286 testFileName = Path.GetFileName (String.Empty);
287 AssertEquals ("GetFileName #03", String.Empty, testFileName);
288 testFileName = Path.GetFileName (" ");
289 AssertEquals ("GetFileName #04", " ", testFileName);
293 testFileName = Path.GetFileName ("hi<");
294 Fail ("GetFileName Fail #01");
295 } catch (Exception e) {
296 AssertEquals ("GetFileName Exc. #01", typeof (ArgumentException), e.GetType ());
301 public void TestGetFileNameWithoutExtension ()
303 string testFileName = Path.GetFileNameWithoutExtension (path1);
305 AssertEquals ("GetFileNameWithoutExtension #01", "test", testFileName);
307 testFileName = Path.GetFileNameWithoutExtension (null);
308 AssertEquals ("GetFileNameWithoutExtension #02", null, testFileName);
310 testFileName = Path.GetFileNameWithoutExtension (String.Empty);
311 AssertEquals ("GetFileNameWithoutExtension #03", String.Empty, testFileName);
314 [Ignore("This does not work under windows. See ERROR comments below.")]
315 public void TestGetFullPath ()
317 string current = Directory.GetCurrentDirectory ();
319 string testFullPath = Path.GetFullPath ("foo.txt");
320 string expected = current + DSC + "foo.txt";
321 AssertEquals ("GetFullPath #01", expected, testFullPath);
323 testFullPath = Path.GetFullPath ("a//./.././foo.txt");
324 AssertEquals ("GetFullPath #02", expected, testFullPath);
325 string root = Windows ? "C:\\" : "/";
326 string [,] test = new string [,] {
327 {"root////././././././../root/././../root", "root"},
329 {"root/./", "root/"},
330 {"root/./", "root/"},
334 {"root/.hiddenfile", "root/.hiddenfile"},
335 {"root/. /", "root/. /"},
336 {"root/.. /", "root/.. /"},
337 {"root/..weirdname", "root/..weirdname"},
339 {"root/../a/b/../../..", ""},
343 {"root//dir", "root/dir"},
344 {"root/. /", "root/. /"},
345 {"root/.. /", "root/.. /"},
346 {"root/ . /", "root/ . /"},
347 {"root/ .. /", "root/ .. /"},
348 {"root/./", "root/"},
349 //ERROR! Paths are trimmed
350 {"root/.. /", "root/.. /"},
354 //ERROR! GetUpperBound (1) returns 1. GetUpperBound (0) == 23
355 //... so only the first test was being done.
356 for (int i = 0; i < test.GetUpperBound (1); i++) {
357 AssertEquals (String.Format ("GetFullPath #{0}", i), root + test [i, 1], Path.GetFullPath (root + test [i, 0]));
361 string uncroot = @"\\server\share\";
362 string [,] testunc = new string [,] {
363 {"root////././././././../root/././../root", "root"},
365 {"root/./", "root/"},
366 {"root/./", "root/"},
370 {"root/.hiddenfile", "root/.hiddenfile"},
371 {"root/. /", "root/. /"},
372 {"root/.. /", "root/.. /"},
373 {"root/..weirdname", "root/..weirdname"},
375 {"root/../a/b/../../..", ""},
379 {"root//dir", "root/dir"},
380 {"root/. /", "root/. /"},
381 {"root/.. /", "root/.. /"},
382 {"root/ . /", "root/ . /"},
383 {"root/ .. /", "root/ .. /"},
384 {"root/./", "root/"},
385 {"root/.. /", "root/.. /"},
388 for (int i = 0; i < test.GetUpperBound (1); i++) {
389 AssertEquals (String.Format ("GetFullPath UNC #{0}", i), uncroot + test [i, 1], Path.GetFullPath (uncroot + test [i, 0]));
394 testFullPath = Path.GetFullPath (null);
395 Fail ("GetFullPath Fail #01");
396 } catch (Exception e) {
397 AssertEquals ("GetFullPath Exc. #01", typeof (ArgumentNullException), e.GetType ());
401 testFullPath = Path.GetFullPath (String.Empty);
402 Fail ("GetFullPath Fail #02");
403 } catch (Exception e) {
404 AssertEquals ("GetFullPath Exc. #02", typeof (ArgumentException), e.GetType ());
408 public void TestGetPathRoot ()
413 current = Directory.GetCurrentDirectory ();
414 expected = current [0].ToString ();
416 current = @"J:\Some\Strange Directory\Name";
420 string pathRoot = Path.GetPathRoot (current);
421 AssertEquals ("GetPathRoot #01", expected, pathRoot);
423 pathRoot = Path.GetPathRoot ("hola");
424 AssertEquals ("GetPathRoot #02", String.Empty, pathRoot);
426 pathRoot = Path.GetPathRoot (null);
427 AssertEquals ("GetPathRoot #03", null, pathRoot);
430 public void TestGetTempPath ()
432 string getTempPath = Path.GetTempPath ();
433 Assert ("GetTempPath #01", getTempPath != String.Empty);
434 Assert ("GetTempPath #02", Path.IsPathRooted (getTempPath));
437 public void TestGetTempFileName ()
439 string getTempFileName = null;
441 getTempFileName = Path.GetTempFileName ();
442 Assert ("GetTempFileName #01", getTempFileName != String.Empty);
443 Assert ("GetTempFileName #02", File.Exists (getTempFileName));
445 if (getTempFileName != null && getTempFileName != String.Empty){
446 File.Delete (getTempFileName);
451 public void TestHasExtension ()
453 AssertEquals ("HasExtension #01", true, Path.HasExtension ("foo.txt"));
454 AssertEquals ("HasExtension #02", false, Path.HasExtension ("foo"));
455 AssertEquals ("HasExtension #03", true, Path.HasExtension (path1));
456 AssertEquals ("HasExtension #04", false, Path.HasExtension (path2));
459 public void TestRooted ()
461 Assert ("IsPathRooted #01", Path.IsPathRooted (path2));
462 Assert ("IsPathRooted #02", !Path.IsPathRooted (path3));
463 Assert ("IsPathRooted #03", !Path.IsPathRooted (null));
466 public void TestCanonicalizeDots ()
468 string current = Path.GetFullPath (".");
469 Assert ("TestCanonicalizeDotst #01", !current.EndsWith ("."));
470 string parent = Path.GetFullPath ("..");
471 Assert ("TestCanonicalizeDotst #02", !current.EndsWith (".."));
474 void Assert (string msg, bool result)
477 Console.WriteLine (msg);
480 void AssertEquals (string msg, object expected, object real)
482 if (expected == null && real == null)
485 if (expected != null && expected.Equals (real))
488 Console.WriteLine ("{0}: expected: '{1}', got: '{2}'", msg, expected, real);
491 void Fail (string msg)
493 Console.WriteLine ("Failed: {0}", msg);
498 PathTest p = new PathTest ();
499 Type t = p.GetType ();
500 MethodInfo [] methods = t.GetMethods ();
501 foreach (MethodInfo m in methods) {
502 if (m.Name.Substring (0, 4) == "Test") {