PathTest.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. //
  2. // System.IO.Path Test Cases
  3. //
  4. // Authors:
  5. // Marcin Szczepanski ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. // Ben Maurer ([email protected])
  8. // Gilles Freart ([email protected])
  9. // Atsushi Enomoto ([email protected])
  10. //
  11. // (c) Marcin Szczepanski
  12. // (c) 2002 Ximian, Inc. (http://www.ximian.com)
  13. // (c) 2003 Ben Maurer
  14. // (c) 2003 Gilles Freart
  15. //
  16. using NUnit.Framework;
  17. using System.IO;
  18. using System;
  19. using System.Text;
  20. namespace MonoTests.System.IO
  21. {
  22. enum OsType {
  23. Windows,
  24. Unix,
  25. Mac
  26. }
  27. [TestFixture]
  28. public class PathTest : Assertion
  29. {
  30. static string path1;
  31. static string path2;
  32. static string path3;
  33. static OsType OS;
  34. static char DSC = Path.DirectorySeparatorChar;
  35. [SetUp]
  36. public void SetUp ()
  37. {
  38. if ('/' == DSC) {
  39. OS = OsType.Unix;
  40. path1 = "/foo/test.txt";
  41. path2 = "/etc";
  42. path3 = "init.d";
  43. } else if ('\\' == DSC) {
  44. OS = OsType.Windows;
  45. path1 = "c:\\foo\\test.txt";
  46. path2 = Environment.GetEnvironmentVariable ("SYSTEMROOT");
  47. path3 = "system32";
  48. } else {
  49. OS = OsType.Mac;
  50. //FIXME: For Mac. figure this out when we need it
  51. path1 = "foo:test.txt";
  52. path2 = "foo";
  53. path3 = "bar";
  54. }
  55. }
  56. bool Windows
  57. {
  58. get {
  59. return OS == OsType.Windows;
  60. }
  61. }
  62. bool Unix
  63. {
  64. get {
  65. return OS == OsType.Unix;
  66. }
  67. }
  68. bool Mac
  69. {
  70. get {
  71. return OS == OsType.Mac;
  72. }
  73. }
  74. public void TestChangeExtension ()
  75. {
  76. string [] files = new string [3];
  77. files [(int) OsType.Unix] = "/foo/test.doc";
  78. files [(int) OsType.Windows] = "c:\\foo\\test.doc";
  79. files [(int) OsType.Mac] = "foo:test.doc";
  80. string testPath = Path.ChangeExtension (path1, "doc");
  81. AssertEquals ("ChangeExtension #01", files [(int) OS], testPath);
  82. testPath = Path.ChangeExtension ("", ".extension");
  83. AssertEquals ("ChangeExtension #02", String.Empty, testPath);
  84. testPath = Path.ChangeExtension (null, ".extension");
  85. AssertEquals ("ChangeExtension #03", null, testPath);
  86. testPath = Path.ChangeExtension ("path", null);
  87. AssertEquals ("ChangeExtension #04", "path", testPath);
  88. testPath = Path.ChangeExtension ("path.ext", "doc");
  89. AssertEquals ("ChangeExtension #05", "path.doc", testPath);
  90. testPath = Path.ChangeExtension ("path.ext1.ext2", "doc");
  91. AssertEquals ("ChangeExtension #06", "path.ext1.doc", testPath);
  92. testPath = Path.ChangeExtension ("hogehoge.xml", ".xsl");
  93. AssertEquals ("ChangeExtension #07", "hogehoge.xsl", testPath);
  94. testPath = Path.ChangeExtension ("hogehoge", ".xsl");
  95. AssertEquals ("ChangeExtension #08", "hogehoge.xsl", testPath);
  96. testPath = Path.ChangeExtension ("hogehoge.xml", "xsl");
  97. AssertEquals ("ChangeExtension #09", "hogehoge.xsl", testPath);
  98. testPath = Path.ChangeExtension ("hogehoge", "xsl");
  99. AssertEquals ("ChangeExtension #10", "hogehoge.xsl", testPath);
  100. testPath = Path.ChangeExtension ("hogehoge.xml", String.Empty);
  101. AssertEquals ("ChangeExtension #11", "hogehoge.", testPath);
  102. testPath = Path.ChangeExtension ("hogehoge", String.Empty);
  103. AssertEquals ("ChangeExtension #12", "hogehoge.", testPath);
  104. testPath = Path.ChangeExtension ("hogehoge.", null);
  105. AssertEquals ("ChangeExtension #13", "hogehoge", testPath);
  106. testPath = Path.ChangeExtension ("hogehoge", null);
  107. AssertEquals ("ChangeExtension #14", "hogehoge", testPath);
  108. testPath = Path.ChangeExtension (String.Empty, null);
  109. AssertEquals ("ChangeExtension #15", String.Empty, testPath);
  110. testPath = Path.ChangeExtension (String.Empty, "bashrc");
  111. AssertEquals ("ChangeExtension #16", String.Empty, testPath);
  112. testPath = Path.ChangeExtension (String.Empty, ".bashrc");
  113. AssertEquals ("ChangeExtension #17", String.Empty, testPath);
  114. testPath = Path.ChangeExtension (null, null);
  115. AssertNull ("ChangeExtension #18", testPath);
  116. }
  117. [Test]
  118. [ExpectedException (typeof (ArgumentException))]
  119. public void ChangeExtension_BadPath ()
  120. {
  121. if (!Windows) throw new ArgumentException ("Test Only On Windows");
  122. Path.ChangeExtension ("<", ".extension");
  123. }
  124. [Test]
  125. public void ChangeExtension_BadExtension ()
  126. {
  127. if (Windows) {
  128. string fn = Path.ChangeExtension ("file.ext", "<");
  129. AssertEquals ("Invalid filename", "file.<", fn);
  130. }
  131. }
  132. public void TestCombine ()
  133. {
  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";
  138. string testPath = Path.Combine (path2, path3);
  139. AssertEquals ("Combine #01", files [(int) OS], testPath);
  140. testPath = Path.Combine ("one", "");
  141. AssertEquals ("Combine #02", "one", testPath);
  142. testPath = Path.Combine ("", "one");
  143. AssertEquals ("Combine #03", "one", testPath);
  144. string current = Directory.GetCurrentDirectory ();
  145. testPath = Path.Combine (current, "one");
  146. string expected = current + DSC + "one";
  147. AssertEquals ("Combine #04", expected, testPath);
  148. testPath = Path.Combine ("one", current);
  149. // LAMESPEC noted in Path.cs
  150. AssertEquals ("Combine #05", current, testPath);
  151. testPath = Path.Combine (current, expected);
  152. AssertEquals ("Combine #06", expected, testPath);
  153. testPath = DSC + "one";
  154. testPath = Path.Combine (testPath, "two" + DSC);
  155. expected = DSC + "one" + DSC + "two" + DSC;
  156. AssertEquals ("Combine #06", expected, testPath);
  157. testPath = "one" + DSC;
  158. testPath = Path.Combine (testPath, DSC + "two");
  159. expected = DSC + "two";
  160. AssertEquals ("Combine #06", expected, testPath);
  161. testPath = "one" + DSC;
  162. testPath = Path.Combine (testPath, "two" + DSC);
  163. expected = "one" + DSC + "two" + DSC;
  164. AssertEquals ("Combine #07", expected, testPath);
  165. //TODO: Tests for UNC names
  166. try {
  167. testPath = Path.Combine ("one", null);
  168. Fail ("Combine Fail #01");
  169. } catch (Exception e) {
  170. AssertEquals ("Combine Exc. #01", typeof (ArgumentNullException), e.GetType ());
  171. }
  172. try {
  173. testPath = Path.Combine (null, "one");
  174. Fail ("Combine Fail #02");
  175. } catch (Exception e) {
  176. AssertEquals ("Combine Exc. #02", typeof (ArgumentNullException), e.GetType ());
  177. }
  178. if (Windows) {
  179. try {
  180. testPath = Path.Combine ("a>", "one");
  181. Fail ("Combine Fail #03");
  182. } catch (Exception e) {
  183. AssertEquals ("Combine Exc. #03", typeof (ArgumentException), e.GetType ());
  184. }
  185. try {
  186. testPath = Path.Combine ("one", "aaa<");
  187. Fail ("Combine Fail #04");
  188. } catch (Exception e) {
  189. AssertEquals ("Combine Exc. #04", typeof (ArgumentException), e.GetType ());
  190. }
  191. }
  192. }
  193. [Test]
  194. [ExpectedException (typeof(ArgumentException))]
  195. public void EmptyDirectoryName ()
  196. {
  197. string testDirName = Path.GetDirectoryName ("");
  198. }
  199. public void TestDirectoryName ()
  200. {
  201. string [] files = new string [3];
  202. files [(int) OsType.Unix] = "/foo";
  203. files [(int) OsType.Windows] = "c:\\foo";
  204. files [(int) OsType.Mac] = "foo";
  205. AssertEquals ("GetDirectoryName #01", null, Path.GetDirectoryName (null));
  206. string testDirName = Path.GetDirectoryName (path1);
  207. AssertEquals ("GetDirectoryName #02", files [(int) OS], testDirName);
  208. testDirName = Path.GetDirectoryName (files [(int) OS] + DSC);
  209. AssertEquals ("GetDirectoryName #03", files [(int) OS], testDirName);
  210. if (Windows) {
  211. try {
  212. testDirName = Path.GetDirectoryName ("aaa>");
  213. Fail ("GetDirectoryName Fail #02");
  214. } catch (Exception e) {
  215. AssertEquals ("GetDirectoryName Exc. #02", typeof (ArgumentException), e.GetType ());
  216. }
  217. }
  218. try {
  219. testDirName = Path.GetDirectoryName (" ");
  220. Fail ("GetDirectoryName Fail #03");
  221. } catch (Exception e) {
  222. AssertEquals ("GetDirectoryName Exc. #03", typeof (ArgumentException), e.GetType ());
  223. }
  224. }
  225. public void TestGetExtension ()
  226. {
  227. string testExtn = Path.GetExtension (path1);
  228. AssertEquals ("GetExtension #01", ".txt", testExtn);
  229. testExtn = Path.GetExtension (path2);
  230. AssertEquals ("GetExtension #02", String.Empty, testExtn);
  231. testExtn = Path.GetExtension (String.Empty);
  232. AssertEquals ("GetExtension #03", String.Empty, testExtn);
  233. testExtn = Path.GetExtension (null);
  234. AssertEquals ("GetExtension #04", null, testExtn);
  235. testExtn = Path.GetExtension (" ");
  236. AssertEquals ("GetExtension #05", String.Empty, testExtn);
  237. testExtn = Path.GetExtension (path1 + ".doc");
  238. AssertEquals ("GetExtension #06", ".doc", testExtn);
  239. testExtn = Path.GetExtension (path1 + ".doc" + DSC + "a.txt");
  240. AssertEquals ("GetExtension #07", ".txt", testExtn);
  241. testExtn = Path.GetExtension (".");
  242. AssertEquals ("GetExtension #08", String.Empty, testExtn);
  243. testExtn = Path.GetExtension ("end.");
  244. AssertEquals ("GetExtension #09", String.Empty, testExtn);
  245. testExtn = Path.GetExtension (".start");
  246. AssertEquals ("GetExtension #10", ".start", testExtn);
  247. testExtn = Path.GetExtension (".a");
  248. AssertEquals ("GetExtension #11", ".a", testExtn);
  249. testExtn = Path.GetExtension ("a.");
  250. AssertEquals ("GetExtension #12", String.Empty, testExtn);
  251. testExtn = Path.GetExtension ("a");
  252. AssertEquals ("GetExtension #13", String.Empty, testExtn);
  253. testExtn = Path.GetExtension ("makefile");
  254. AssertEquals ("GetExtension #14", String.Empty, testExtn);
  255. if (Windows) {
  256. try {
  257. testExtn = Path.GetExtension ("hi<there.txt");
  258. Fail ("GetExtension Fail #01");
  259. } catch (Exception e) {
  260. AssertEquals ("GetExtension Exc. #01", typeof (ArgumentException), e.GetType ());
  261. }
  262. }
  263. }
  264. public void TestGetFileName ()
  265. {
  266. string testFileName = Path.GetFileName (path1);
  267. AssertEquals ("GetFileName #01", "test.txt", testFileName);
  268. testFileName = Path.GetFileName (null);
  269. AssertEquals ("GetFileName #02", null, testFileName);
  270. testFileName = Path.GetFileName (String.Empty);
  271. AssertEquals ("GetFileName #03", String.Empty, testFileName);
  272. testFileName = Path.GetFileName (" ");
  273. AssertEquals ("GetFileName #04", " ", testFileName);
  274. if (Windows) {
  275. try {
  276. testFileName = Path.GetFileName ("hi<");
  277. Fail ("GetFileName Fail #01");
  278. } catch (Exception e) {
  279. AssertEquals ("GetFileName Exc. #01", typeof (ArgumentException), e.GetType ());
  280. }
  281. }
  282. }
  283. public void TestGetFileNameWithoutExtension ()
  284. {
  285. string testFileName = Path.GetFileNameWithoutExtension (path1);
  286. AssertEquals ("GetFileNameWithoutExtension #01", "test", testFileName);
  287. testFileName = Path.GetFileNameWithoutExtension (null);
  288. AssertEquals ("GetFileNameWithoutExtension #02", null, testFileName);
  289. testFileName = Path.GetFileNameWithoutExtension (String.Empty);
  290. AssertEquals ("GetFileNameWithoutExtension #03", String.Empty, testFileName);
  291. }
  292. [Ignore("This does not work under windows. See ERROR comments below.")]
  293. public void TestGetFullPath ()
  294. {
  295. string current = Directory.GetCurrentDirectory ();
  296. string testFullPath = Path.GetFullPath ("foo.txt");
  297. string expected = current + DSC + "foo.txt";
  298. AssertEquals ("GetFullPath #01", expected, testFullPath);
  299. testFullPath = Path.GetFullPath ("a//./.././foo.txt");
  300. AssertEquals ("GetFullPath #02", expected, testFullPath);
  301. string root = Windows ? "C:\\" : "/";
  302. string [,] test = new string [,] {
  303. {"root////././././././../root/././../root", "root"},
  304. {"root/", "root/"},
  305. {"root/./", "root/"},
  306. {"root/./", "root/"},
  307. {"root/../", ""},
  308. {"root/../", ""},
  309. {"root/../..", ""},
  310. {"root/.hiddenfile", "root/.hiddenfile"},
  311. {"root/. /", "root/. /"},
  312. {"root/.. /", "root/.. /"},
  313. {"root/..weirdname", "root/..weirdname"},
  314. {"root/..", ""},
  315. {"root/../a/b/../../..", ""},
  316. {"root/./..", ""},
  317. {"..", ""},
  318. {".", ""},
  319. {"root//dir", "root/dir"},
  320. {"root/. /", "root/. /"},
  321. {"root/.. /", "root/.. /"},
  322. {"root/ . /", "root/ . /"},
  323. {"root/ .. /", "root/ .. /"},
  324. {"root/./", "root/"},
  325. //ERROR! Paths are trimmed
  326. {"root/.. /", "root/.. /"},
  327. {".//", ""}
  328. };
  329. //ERROR! GetUpperBound (1) returns 1. GetUpperBound (0) == 23
  330. //... so only the first test was being done.
  331. for (int i = 0; i < test.GetUpperBound (1); i++) {
  332. AssertEquals (String.Format ("GetFullPath #{0}", i), root + test [i, 1], Path.GetFullPath (root + test [i, 0]));
  333. }
  334. if (Windows) {
  335. string uncroot = @"\\server\share\";
  336. string [,] testunc = new string [,] {
  337. {"root////././././././../root/././../root", "root"},
  338. {"root/", "root/"},
  339. {"root/./", "root/"},
  340. {"root/./", "root/"},
  341. {"root/../", ""},
  342. {"root/../", ""},
  343. {"root/../..", ""},
  344. {"root/.hiddenfile", "root/.hiddenfile"},
  345. {"root/. /", "root/. /"},
  346. {"root/.. /", "root/.. /"},
  347. {"root/..weirdname", "root/..weirdname"},
  348. {"root/..", ""},
  349. {"root/../a/b/../../..", ""},
  350. {"root/./..", ""},
  351. {"..", ""},
  352. {".", ""},
  353. {"root//dir", "root/dir"},
  354. {"root/. /", "root/. /"},
  355. {"root/.. /", "root/.. /"},
  356. {"root/ . /", "root/ . /"},
  357. {"root/ .. /", "root/ .. /"},
  358. {"root/./", "root/"},
  359. {"root/.. /", "root/.. /"},
  360. {".//", ""}
  361. };
  362. for (int i = 0; i < test.GetUpperBound (1); i++) {
  363. AssertEquals (String.Format ("GetFullPath UNC #{0}", i), uncroot + test [i, 1], Path.GetFullPath (uncroot + test [i, 0]));
  364. }
  365. }
  366. try {
  367. testFullPath = Path.GetFullPath (null);
  368. Fail ("GetFullPath Fail #01");
  369. } catch (Exception e) {
  370. AssertEquals ("GetFullPath Exc. #01", typeof (ArgumentNullException), e.GetType ());
  371. }
  372. try {
  373. testFullPath = Path.GetFullPath (String.Empty);
  374. Fail ("GetFullPath Fail #02");
  375. } catch (Exception e) {
  376. AssertEquals ("GetFullPath Exc. #02", typeof (ArgumentException), e.GetType ());
  377. }
  378. }
  379. public void TestGetFullPath2 ()
  380. {
  381. if (Windows) {
  382. AssertEquals ("GetFullPath w#01", @"Z:\", Path.GetFullPath ("Z:"));
  383. AssertEquals ("GetFullPath w#02", @"c:\abc\def", Path.GetFullPath (@"c:\abc\def"));
  384. Assert ("GetFullPath w#03", Path.GetFullPath (@"\").EndsWith (@"\"));
  385. // "\\\\" is not allowed
  386. Assert ("GetFullPath w#05", Path.GetFullPath ("/").EndsWith (@"\"));
  387. // "//" is not allowed
  388. Assert ("GetFullPath w#07", Path.GetFullPath ("readme.txt").EndsWith (@"\readme.txt"));
  389. Assert ("GetFullPath w#08", Path.GetFullPath ("c").EndsWith (@"\c"));
  390. Assert ("GetFullPath w#09", Path.GetFullPath (@"abc\def").EndsWith (@"abc\def"));
  391. Assert ("GetFullPath w#10", Path.GetFullPath (@"\abc\def").EndsWith (@"\abc\def"));
  392. AssertEquals ("GetFullPath w#11", @"\\abc\def", Path.GetFullPath (@"\\abc\def"));
  393. AssertEquals ("GetFullPath w#12", Directory.GetCurrentDirectory () + @"\abc\def", Path.GetFullPath (@"abc//def"));
  394. AssertEquals ("GetFullPath w#13", Directory.GetCurrentDirectory ().Substring (0,2) + @"\abc\def", Path.GetFullPath ("/abc/def"));
  395. AssertEquals ("GetFullPath w#14", @"\\abc\def", Path.GetFullPath ("//abc/def"));
  396. }
  397. }
  398. public void TestGetPathRoot ()
  399. {
  400. string current;
  401. string expected;
  402. if (!Windows){
  403. current = Directory.GetCurrentDirectory ();
  404. expected = current [0].ToString ();
  405. } else {
  406. current = @"J:\Some\Strange Directory\Name";
  407. expected = "J:\\";
  408. }
  409. string pathRoot = Path.GetPathRoot (current);
  410. AssertEquals ("GetPathRoot #01", expected, pathRoot);
  411. }
  412. [Test]
  413. public void TestGetPathRoot2 ()
  414. {
  415. // note: this method doesn't call Directory.GetCurrentDirectory so it can be
  416. // reused for partial trust unit tests in PathCas.cs
  417. string pathRoot = Path.GetPathRoot ("hola");
  418. AssertEquals ("GetPathRoot #02", String.Empty, pathRoot);
  419. pathRoot = Path.GetPathRoot (null);
  420. AssertEquals ("GetPathRoot #03", null, pathRoot);
  421. if (Windows) {
  422. AssertEquals ("GetPathRoot w#01", "z:", Path.GetPathRoot ("z:"));
  423. AssertEquals ("GetPathRoot w#02", "c:\\", Path.GetPathRoot ("c:\\abc\\def"));
  424. AssertEquals ("GetPathRoot w#03", "\\", Path.GetPathRoot ("\\"));
  425. AssertEquals ("GetPathRoot w#04", "\\\\", Path.GetPathRoot ("\\\\"));
  426. AssertEquals ("GetPathRoot w#05", "\\", Path.GetPathRoot ("/"));
  427. AssertEquals ("GetPathRoot w#06", "\\\\", Path.GetPathRoot ("//"));
  428. AssertEquals ("GetPathRoot w#07", String.Empty, Path.GetPathRoot ("readme.txt"));
  429. AssertEquals ("GetPathRoot w#08", String.Empty, Path.GetPathRoot ("c"));
  430. AssertEquals ("GetPathRoot w#09", String.Empty, Path.GetPathRoot ("abc\\def"));
  431. AssertEquals ("GetPathRoot w#10", "\\", Path.GetPathRoot ("\\abc\\def"));
  432. AssertEquals ("GetPathRoot w#11", "\\\\abc\\def", Path.GetPathRoot ("\\\\abc\\def"));
  433. AssertEquals ("GetPathRoot w#12", String.Empty, Path.GetPathRoot ("abc//def"));
  434. AssertEquals ("GetPathRoot w#13", "\\", Path.GetPathRoot ("/abc/def"));
  435. AssertEquals ("GetPathRoot w#14", "\\\\abc\\def", Path.GetPathRoot ("//abc/def"));
  436. } else {
  437. // TODO: Same tests for Unix.
  438. }
  439. }
  440. public void TestGetTempPath ()
  441. {
  442. string getTempPath = Path.GetTempPath ();
  443. Assert ("GetTempPath #01", getTempPath != String.Empty);
  444. Assert ("GetTempPath #02", Path.IsPathRooted (getTempPath));
  445. AssertEquals ("GetTempPath #03", Path.DirectorySeparatorChar, getTempPath [getTempPath.Length - 1]);
  446. }
  447. public void TestGetTempFileName ()
  448. {
  449. string getTempFileName = null;
  450. try {
  451. getTempFileName = Path.GetTempFileName ();
  452. Assert ("GetTempFileName #01", getTempFileName != String.Empty);
  453. Assert ("GetTempFileName #02", File.Exists (getTempFileName));
  454. } finally {
  455. if (getTempFileName != null && getTempFileName != String.Empty){
  456. File.Delete (getTempFileName);
  457. }
  458. }
  459. }
  460. public void TestHasExtension ()
  461. {
  462. AssertEquals ("HasExtension #01", true, Path.HasExtension ("foo.txt"));
  463. AssertEquals ("HasExtension #02", false, Path.HasExtension ("foo"));
  464. AssertEquals ("HasExtension #03", true, Path.HasExtension (path1));
  465. AssertEquals ("HasExtension #04", false, Path.HasExtension (path2));
  466. AssertEquals ("HasExtension #05", false, Path.HasExtension (null));
  467. AssertEquals ("HasExtension #06", false, Path.HasExtension (String.Empty));
  468. AssertEquals ("HasExtension #07", false, Path.HasExtension (" "));
  469. AssertEquals ("HasExtension #08", false, Path.HasExtension ("."));
  470. AssertEquals ("HasExtension #09", false, Path.HasExtension ("end."));
  471. AssertEquals ("HasExtension #10", true, Path.HasExtension (".start"));
  472. AssertEquals ("HasExtension #11", true, Path.HasExtension (".a"));
  473. AssertEquals ("HasExtension #12", false, Path.HasExtension ("a."));
  474. AssertEquals ("HasExtension #13", false, Path.HasExtension ("Makefile"));
  475. }
  476. public void TestRooted ()
  477. {
  478. Assert ("IsPathRooted #01", Path.IsPathRooted (path2));
  479. Assert ("IsPathRooted #02", !Path.IsPathRooted (path3));
  480. Assert ("IsPathRooted #03", !Path.IsPathRooted (null));
  481. Assert ("IsPathRooted #04", !Path.IsPathRooted (String.Empty));
  482. Assert ("IsPathRooted #05", !Path.IsPathRooted (" "));
  483. Assert ("IsPathRooted #06", Path.IsPathRooted ("/"));
  484. if (Windows)
  485. Assert ("IsPathRooted #07", Path.IsPathRooted ("\\"));
  486. else
  487. Assert ("IsPathRooted #07", !Path.IsPathRooted ("\\"));
  488. Assert ("IsPathRooted #08", Path.IsPathRooted ("//"));
  489. if (Windows)
  490. Assert ("IsPathRooted #09", Path.IsPathRooted ("\\\\"));
  491. else
  492. Assert ("IsPathRooted #09", !Path.IsPathRooted ("\\\\"));
  493. Assert ("IsPathRooted #10", !Path.IsPathRooted (":"));
  494. if (Windows)
  495. Assert ("IsPathRooted #11", Path.IsPathRooted ("z:"));
  496. else
  497. Assert ("IsPathRooted #11", !Path.IsPathRooted ("z:"));
  498. if (Windows) {
  499. Assert ("IsPathRooted #12", Path.IsPathRooted ("z:\\"));
  500. Assert ("IsPathRooted #13", Path.IsPathRooted ("z:\\topdir"));
  501. // This looks MS BUG. It is treated as absolute path
  502. Assert ("IsPathRooted #14", Path.IsPathRooted ("z:curdir"));
  503. Assert ("IsPathRooted #15", Path.IsPathRooted ("\\abc\\def"));
  504. }
  505. }
  506. public void TestCanonicalizeDots ()
  507. {
  508. string current = Path.GetFullPath (".");
  509. Assert ("TestCanonicalizeDotst #01", !current.EndsWith ("."));
  510. string parent = Path.GetFullPath ("..");
  511. Assert ("TestCanonicalizeDotst #02", !current.EndsWith (".."));
  512. }
  513. public void TestDirectoryNameBugs ()
  514. {
  515. if (Windows) {
  516. AssertEquals ("Win #01", "C:\\foo", Path.GetDirectoryName ("C:\\foo\\foo.txt"));
  517. } else {
  518. AssertEquals ("No win #01", "/etc", Path.GetDirectoryName ("/etc/hostname"));
  519. }
  520. }
  521. public void TestGetFullPathUnix ()
  522. {
  523. if (Windows)
  524. return;
  525. AssertEquals ("#01", "/", Path.GetFullPath ("/"));
  526. AssertEquals ("#02", "/hey", Path.GetFullPath ("/hey"));
  527. AssertEquals ("#03", Environment.CurrentDirectory, Path.GetFullPath ("."));
  528. AssertEquals ("#04", Path.Combine (Environment.CurrentDirectory, "hey"),
  529. Path.GetFullPath ("hey"));
  530. }
  531. }
  532. }