PathTest.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // System.IO.Path Test Cases
  3. //
  4. // Author: Marcin Szczepanski ([email protected])
  5. //
  6. // TODO: Add a more thorough workout for some
  7. // of the "trickier" functions.
  8. using NUnit.Framework;
  9. using System.IO;
  10. using System;
  11. using System.Text;
  12. namespace MonoTests.System.IO
  13. {
  14. public class PathTest : TestCase {
  15. string path1;
  16. string path2;
  17. string path3;
  18. public static ITest Suite {
  19. get {
  20. return new TestSuite(typeof(PathTest));
  21. }
  22. }
  23. public PathTest() : base ("MonoTests.System.IO.PathTest testcase") { }
  24. public PathTest( string name ): base(name) { }
  25. protected override void SetUp() {
  26. if ('/' == Path.VolumeSeparatorChar){
  27. path1 = "/foo/test.txt";
  28. path2 = "/etc";
  29. path3 = "init.d";
  30. } else {
  31. path1 = "c:\\foo\\test.txt";
  32. path2 = "c:\\winnt";
  33. path3 = "system32";
  34. }
  35. // For Mac. Figure this out when we need it
  36. //path1 = "foo:test.txt";
  37. //path2 = "foo";
  38. //path3 = "bar";
  39. }
  40. public void TestChangeExtension() {
  41. string testPath = Path.ChangeExtension( path1, "doc" );
  42. #if WINDOWS
  43. AssertEquals( "c:\\foo\\test.doc", testPath );
  44. #elif UNIX
  45. AssertEquals( "/foo/test.doc", testPath );
  46. #elif MAC
  47. AssertEquals( "foo:test.doc", testPath );
  48. #endif
  49. }
  50. public void TestCombine() {
  51. string testPath = Path.Combine( path2, path3 );
  52. #if WINDOWS
  53. AssertEquals( "c:\\winnt\\system32", testPath );
  54. #elif UNIX
  55. AssertEquals( "/etc/init.d", testPath );
  56. #elif MAC
  57. AssertEquals( "foo:bar", testPath );
  58. #endif
  59. }
  60. public void TestDirectoryName() {
  61. string testDirName = Path.GetDirectoryName( path1 );
  62. #if WINDOWS
  63. AssertEquals( "c:\\foo", testDirName );
  64. #elif UNIX
  65. AssertEquals( "/etc", testDirName );
  66. #elif MAC
  67. AssertEquals( "foo", testDirName );
  68. #endif
  69. }
  70. public void TestGetExtension() {
  71. string testExtn = Path.GetExtension( path1 );
  72. AssertEquals( ".txt", testExtn );
  73. testExtn = Path.GetExtension( path2 );
  74. AssertEquals ( "", testExtn );
  75. }
  76. public void TestGetFileName() {
  77. string testFileName = Path.GetFileName( path1 );
  78. AssertEquals( "test.txt", testFileName );
  79. }
  80. public void TestGetFileNameWithoutExtension() {
  81. string testFileName = Path.GetFileNameWithoutExtension( path1 );
  82. AssertEquals( "test", testFileName );
  83. }
  84. public void TestGetFullPath() {
  85. string testFullPath = Path.GetFullPath( "foo.txt" );
  86. // AssertEquals( "foo.txt", testFullPath );
  87. }
  88. public void TestGetTempPath() {
  89. string getTempPath = Path.GetTempPath();
  90. Assert ("Temp Path should not be empty", getTempPath != String.Empty);
  91. }
  92. public void TestGetTempFileName() {
  93. string getTempFileName = "";
  94. try {
  95. getTempFileName = Path.GetTempFileName();
  96. Assert ("Temp file name should not be empty", getTempFileName != String.Empty);
  97. Assert ("File should exist", File.Exists(getTempFileName));
  98. } finally {
  99. if (getTempFileName != null && getTempFileName != String.Empty){
  100. File.Delete(getTempFileName);
  101. }
  102. }
  103. }
  104. public void TestHasExtension() {
  105. AssertEquals( true, Path.HasExtension( "foo.txt" ) );
  106. AssertEquals( false, Path.HasExtension( "foo" ) );
  107. AssertEquals( true, Path.HasExtension( path1 ) );
  108. AssertEquals( false, Path.HasExtension( path2 ) );
  109. }
  110. public void TestRooted() {
  111. Assert ("Path should be rooted", Path.IsPathRooted(path2));
  112. Assert ("Path should NOT be rooted", !Path.IsPathRooted(path3));
  113. }
  114. }
  115. }