PathTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. //
  2. // System.IO.Path Test Cases
  3. //
  4. // Authors:
  5. // Marcin Szczepanski ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (c) Marcin Szczepanski
  9. // (c) 2002 Ximian, Inc. (http://www.ximian.com)
  10. //
  11. #define NUNIT // Comment out this one if you wanna play with the test without using NUnit
  12. #if NUNIT
  13. using NUnit.Framework;
  14. #else
  15. using System.Reflection;
  16. #endif
  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. #if NUNIT
  28. public class PathTest : TestCase
  29. {
  30. #else
  31. public class PathTest
  32. {
  33. #endif
  34. static string path1;
  35. static string path2;
  36. static string path3;
  37. static OsType OS;
  38. static char DSC = Path.DirectorySeparatorChar;
  39. #if NUNIT
  40. protected override void SetUp ()
  41. {
  42. #else
  43. static PathTest ()
  44. {
  45. #endif
  46. if ('/' == DSC) {
  47. OS = OsType.Unix;
  48. path1 = "/foo/test.txt";
  49. path2 = "/etc";
  50. path3 = "init.d";
  51. } else if ('\\' == DSC) {
  52. OS = OsType.Windows;
  53. path1 = "c:\\foo\\test.txt";
  54. path2 = Environment.GetEnvironmentVariable ("SYSTEMROOT");
  55. path3 = "system32";
  56. } else {
  57. OS = OsType.Mac;
  58. //FIXME: For Mac. figure this out when we need it
  59. path1 = "foo:test.txt";
  60. path2 = "foo";
  61. path3 = "bar";
  62. }
  63. }
  64. bool Windows
  65. {
  66. get {
  67. return OS == OsType.Windows;
  68. }
  69. }
  70. bool Unix
  71. {
  72. get {
  73. return OS == OsType.Unix;
  74. }
  75. }
  76. bool Mac
  77. {
  78. get {
  79. return OS == OsType.Mac;
  80. }
  81. }
  82. public void TestChangeExtension ()
  83. {
  84. string [] files = new string [3];
  85. files [(int) OsType.Unix] = "/foo/test.doc";
  86. files [(int) OsType.Windows] = "c:\\foo\\test.doc";
  87. files [(int) OsType.Mac] = "foo:test.doc";
  88. string testPath = Path.ChangeExtension (path1, "doc");
  89. AssertEquals ("ChangeExtension #01", files [(int) OS], testPath);
  90. testPath = Path.ChangeExtension ("", ".extension");
  91. AssertEquals ("ChangeExtension #02", String.Empty, testPath);
  92. testPath = Path.ChangeExtension (null, ".extension");
  93. AssertEquals ("ChangeExtension #03", null, testPath);
  94. testPath = Path.ChangeExtension ("path", null);
  95. AssertEquals ("ChangeExtension #04", "path", testPath);
  96. testPath = Path.ChangeExtension ("path.ext", "doc");
  97. AssertEquals ("ChangeExtension #05", "path.doc", testPath);
  98. testPath = Path.ChangeExtension ("path.ext1.ext2", "doc");
  99. AssertEquals ("ChangeExtension #06", "path.ext1.doc", testPath);
  100. if (Windows) {
  101. try {
  102. testPath = Path.ChangeExtension ("<", ".extension");
  103. Fail ("ChangeException Fail #01");
  104. } catch (Exception e) {
  105. AssertEquals ("ChangeExtension Exc. #01", typeof (ArgumentException), e.GetType ());
  106. }
  107. }
  108. }
  109. public void TestCombine ()
  110. {
  111. string [] files = new string [3];
  112. files [(int) OsType.Unix] = "/etc/init.d";
  113. files [(int) OsType.Windows] = Environment.GetEnvironmentVariable ("SYSTEMROOT") + @"\system32";
  114. files [(int) OsType.Mac] = "foo:bar";
  115. string testPath = Path.Combine (path2, path3);
  116. AssertEquals ("Combine #01", files [(int) OS], testPath);
  117. testPath = Path.Combine ("one", "");
  118. AssertEquals ("Combine #02", "one", testPath);
  119. testPath = Path.Combine ("", "one");
  120. AssertEquals ("Combine #03", "one", testPath);
  121. string current = Directory.GetCurrentDirectory ();
  122. testPath = Path.Combine (current, "one");
  123. string expected = current + DSC + "one";
  124. AssertEquals ("Combine #04", expected, testPath);
  125. testPath = Path.Combine ("one", current);
  126. // LAMESPEC noted in Path.cs
  127. AssertEquals ("Combine #05", current, testPath);
  128. testPath = Path.Combine (current, expected);
  129. AssertEquals ("Combine #06", expected, testPath);
  130. testPath = DSC + "one";
  131. testPath = Path.Combine (testPath, "two" + DSC);
  132. expected = DSC + "one" + DSC + "two" + DSC;
  133. AssertEquals ("Combine #06", expected, testPath);
  134. testPath = "one" + DSC;
  135. testPath = Path.Combine (testPath, DSC + "two");
  136. expected = DSC + "two";
  137. AssertEquals ("Combine #06", expected, testPath);
  138. testPath = "one" + DSC;
  139. testPath = Path.Combine (testPath, "two" + DSC);
  140. expected = "one" + DSC + "two" + DSC;
  141. AssertEquals ("Combine #07", expected, testPath);
  142. //TODO: Tests for UNC names
  143. try {
  144. testPath = Path.Combine ("one", null);
  145. Fail ("Combine Fail #01");
  146. } catch (Exception e) {
  147. AssertEquals ("Combine Exc. #01", typeof (ArgumentNullException), e.GetType ());
  148. }
  149. try {
  150. testPath = Path.Combine (null, "one");
  151. Fail ("Combine Fail #02");
  152. } catch (Exception e) {
  153. AssertEquals ("Combine Exc. #02", typeof (ArgumentNullException), e.GetType ());
  154. }
  155. if (Windows) {
  156. try {
  157. testPath = Path.Combine ("a>", "one");
  158. Fail ("Combine Fail #03");
  159. } catch (Exception e) {
  160. AssertEquals ("Combine Exc. #03", typeof (ArgumentException), e.GetType ());
  161. }
  162. try {
  163. testPath = Path.Combine ("one", "aaa<");
  164. Fail ("Combine Fail #04");
  165. } catch (Exception e) {
  166. AssertEquals ("Combine Exc. #04", typeof (ArgumentException), e.GetType ());
  167. }
  168. }
  169. }
  170. public void TestDirectoryName ()
  171. {
  172. string [] files = new string [3];
  173. files [(int) OsType.Unix] = "/foo";
  174. files [(int) OsType.Windows] = "c:\\foo";
  175. files [(int) OsType.Mac] = "foo";
  176. AssertEquals ("GetDirectoryName #01", null, Path.GetDirectoryName (null));
  177. string testDirName = Path.GetDirectoryName (path1);
  178. AssertEquals ("GetDirectoryName #02", files [(int) OS], testDirName);
  179. testDirName = Path.GetDirectoryName (files [(int) OS] + DSC);
  180. AssertEquals ("GetDirectoryName #03", files [(int) OS], testDirName);
  181. try {
  182. testDirName = Path.GetDirectoryName ("");
  183. Fail ("GetDirectoryName Fail #01");
  184. } catch (Exception e) {
  185. AssertEquals ("GetDirectoryName Exc. #01", typeof (ArgumentException), e.GetType ());
  186. }
  187. if (Windows) {
  188. try {
  189. testDirName = Path.GetDirectoryName ("aaa>");
  190. Fail ("GetDirectoryName Fail #02");
  191. } catch (Exception e) {
  192. AssertEquals ("GetDirectoryName Exc. #02", typeof (ArgumentException), e.GetType ());
  193. }
  194. }
  195. try {
  196. testDirName = Path.GetDirectoryName (" ");
  197. Fail ("GetDirectoryName Fail #03");
  198. } catch (Exception e) {
  199. AssertEquals ("GetDirectoryName Exc. #03", typeof (ArgumentException), e.GetType ());
  200. }
  201. }
  202. public void TestGetExtension ()
  203. {
  204. string testExtn = Path.GetExtension (path1);
  205. AssertEquals ("GetExtension #01", ".txt", testExtn);
  206. testExtn = Path.GetExtension (path2);
  207. AssertEquals ("GetExtension #02", String.Empty, testExtn);
  208. testExtn = Path.GetExtension (String.Empty);
  209. AssertEquals ("GetExtension #03", String.Empty, testExtn);
  210. testExtn = Path.GetExtension (null);
  211. AssertEquals ("GetExtension #04", null, testExtn);
  212. testExtn = Path.GetExtension (" ");
  213. AssertEquals ("GetExtension #05", String.Empty, testExtn);
  214. testExtn = Path.GetExtension (path1 + ".doc");
  215. AssertEquals ("GetExtension #06", ".doc", testExtn);
  216. testExtn = Path.GetExtension (path1 + ".doc" + DSC + "a.txt");
  217. AssertEquals ("GetExtension #07", ".txt", testExtn);
  218. if (Windows) {
  219. try {
  220. testExtn = Path.GetExtension ("hi<there.txt");
  221. Fail ("GetExtension Fail #01");
  222. } catch (Exception e) {
  223. AssertEquals ("GetExtension Exc. #01", typeof (ArgumentException), e.GetType ());
  224. }
  225. }
  226. }
  227. public void TestGetFileName ()
  228. {
  229. string testFileName = Path.GetFileName (path1);
  230. AssertEquals ("GetFileName #01", "test.txt", testFileName);
  231. testFileName = Path.GetFileName (null);
  232. AssertEquals ("GetFileName #02", null, testFileName);
  233. testFileName = Path.GetFileName (String.Empty);
  234. AssertEquals ("GetFileName #03", String.Empty, testFileName);
  235. testFileName = Path.GetFileName (" ");
  236. AssertEquals ("GetFileName #04", " ", testFileName);
  237. if (Windows) {
  238. try {
  239. testFileName = Path.GetFileName ("hi<");
  240. Fail ("GetFileName Fail #01");
  241. } catch (Exception e) {
  242. AssertEquals ("GetFileName Exc. #01", typeof (ArgumentException), e.GetType ());
  243. }
  244. }
  245. }
  246. public void TestGetFileNameWithoutExtension ()
  247. {
  248. string testFileName = Path.GetFileNameWithoutExtension (path1);
  249. AssertEquals ("GetFileNameWithoutExtension #01", "test", testFileName);
  250. testFileName = Path.GetFileNameWithoutExtension (null);
  251. AssertEquals ("GetFileNameWithoutExtension #02", null, testFileName);
  252. testFileName = Path.GetFileNameWithoutExtension (String.Empty);
  253. AssertEquals ("GetFileNameWithoutExtension #03", String.Empty, testFileName);
  254. }
  255. public void TestGetFullPath ()
  256. {
  257. string current = Directory.GetCurrentDirectory ();
  258. string testFullPath = Path.GetFullPath ("foo.txt");
  259. string expected = current + DSC + "foo.txt";
  260. AssertEquals ("GetFullPath #01", expected, testFullPath);
  261. try {
  262. testFullPath = Path.GetFullPath (null);
  263. Fail ("GetFullPath Fail #01");
  264. } catch (Exception e) {
  265. AssertEquals ("GetFullPath Exc. #01", typeof (ArgumentNullException), e.GetType ());
  266. }
  267. try {
  268. testFullPath = Path.GetFullPath (String.Empty);
  269. Fail ("GetFullPath Fail #02");
  270. } catch (Exception e) {
  271. AssertEquals ("GetFullPath Exc. #02", typeof (ArgumentException), e.GetType ());
  272. }
  273. }
  274. public void TestGetPathRoot ()
  275. {
  276. string current;
  277. string expected;
  278. if (!Windows){
  279. current = Directory.GetCurrentDirectory ();
  280. expected = current [0].ToString ();
  281. } else {
  282. current = @"J:\Some\Strange Directory\Name";
  283. expected = "J:\\";
  284. }
  285. string pathRoot = Path.GetPathRoot (current);
  286. AssertEquals ("GetPathRoot #01", expected, pathRoot);
  287. pathRoot = Path.GetPathRoot ("hola");
  288. AssertEquals ("GetPathRoot #02", String.Empty, pathRoot);
  289. pathRoot = Path.GetPathRoot (null);
  290. AssertEquals ("GetPathRoot #03", null, pathRoot);
  291. }
  292. public void TestGetTempPath ()
  293. {
  294. string getTempPath = Path.GetTempPath ();
  295. Assert ("GetTempPath #01", getTempPath != String.Empty);
  296. Assert ("GetTempPath #02", Path.IsPathRooted (getTempPath));
  297. }
  298. public void TestGetTempFileName ()
  299. {
  300. string getTempFileName = null;
  301. try {
  302. getTempFileName = Path.GetTempFileName ();
  303. Assert ("GetTempFileName #01", getTempFileName != String.Empty);
  304. Assert ("GetTempFileName #02", File.Exists (getTempFileName));
  305. } finally {
  306. if (getTempFileName != null && getTempFileName != String.Empty){
  307. File.Delete (getTempFileName);
  308. }
  309. }
  310. }
  311. public void TestHasExtension ()
  312. {
  313. AssertEquals ("HasExtension #01", true, Path.HasExtension ("foo.txt"));
  314. AssertEquals ("HasExtension #02", false, Path.HasExtension ("foo"));
  315. AssertEquals ("HasExtension #03", true, Path.HasExtension (path1));
  316. AssertEquals ("HasExtension #04", false, Path.HasExtension (path2));
  317. }
  318. public void TestRooted ()
  319. {
  320. Assert ("IsPathRooted #01", Path.IsPathRooted (path2));
  321. Assert ("IsPathRooted #02", !Path.IsPathRooted (path3));
  322. Assert ("IsPathRooted #03", !Path.IsPathRooted (null));
  323. }
  324. #if !NUNIT
  325. void Assert (string msg, bool result)
  326. {
  327. if (!result)
  328. Console.WriteLine (msg);
  329. }
  330. void AssertEquals (string msg, object expected, object real)
  331. {
  332. if (expected == null && real == null)
  333. return;
  334. if (expected != null && expected.Equals (real))
  335. return;
  336. Console.WriteLine ("{0}: expected: '{1}', got: '{2}'", msg, expected, real);
  337. }
  338. void Fail (string msg)
  339. {
  340. Console.WriteLine ("Failed: {0}", msg);
  341. }
  342. static void Main ()
  343. {
  344. PathTest p = new PathTest ();
  345. Type t = p.GetType ();
  346. MethodInfo [] methods = t.GetMethods ();
  347. foreach (MethodInfo m in methods) {
  348. if (m.Name.Substring (0, 4) == "Test") {
  349. m.Invoke (p, null);
  350. }
  351. }
  352. }
  353. #endif
  354. }
  355. }