// // System.IO.Path Test Cases // // Authors: // Marcin Szczepanski (marcins@zipworld.com.au) // Gonzalo Paniagua Javier (gonzalo@ximian.com) // // (c) Marcin Szczepanski // (c) 2002 Ximian, Inc. (http://www.ximian.com) // #define NUNIT // Comment out this one if you wanna play with the test without using NUnit #if NUNIT using NUnit.Framework; #else using System.Reflection; #endif using System.IO; using System; using System.Text; namespace MonoTests.System.IO { enum OsType { Windows, Unix, Mac } #if NUNIT public class PathTest : TestCase { #else public class PathTest { #endif static string path1; static string path2; static string path3; static OsType OS; static char DSC = Path.DirectorySeparatorChar; #if NUNIT protected override void SetUp () { #else static PathTest () { #endif if ('/' == DSC) { OS = OsType.Unix; path1 = "/foo/test.txt"; path2 = "/etc"; path3 = "init.d"; } else if ('\\' == DSC) { OS = OsType.Windows; path1 = "c:\\foo\\test.txt"; path2 = Environment.GetEnvironmentVariable ("SYSTEMROOT"); path3 = "system32"; } else { OS = OsType.Mac; //FIXME: For Mac. figure this out when we need it path1 = "foo:test.txt"; path2 = "foo"; path3 = "bar"; } } bool Windows { get { return OS == OsType.Windows; } } bool Unix { get { return OS == OsType.Unix; } } bool Mac { get { return OS == OsType.Mac; } } public void TestChangeExtension () { string [] files = new string [3]; files [(int) OsType.Unix] = "/foo/test.doc"; files [(int) OsType.Windows] = "c:\\foo\\test.doc"; files [(int) OsType.Mac] = "foo:test.doc"; string testPath = Path.ChangeExtension (path1, "doc"); AssertEquals ("ChangeExtension #01", files [(int) OS], testPath); testPath = Path.ChangeExtension ("", ".extension"); AssertEquals ("ChangeExtension #02", String.Empty, testPath); testPath = Path.ChangeExtension (null, ".extension"); AssertEquals ("ChangeExtension #03", null, testPath); testPath = Path.ChangeExtension ("path", null); AssertEquals ("ChangeExtension #04", "path", testPath); testPath = Path.ChangeExtension ("path.ext", "doc"); AssertEquals ("ChangeExtension #05", "path.doc", testPath); testPath = Path.ChangeExtension ("path.ext1.ext2", "doc"); AssertEquals ("ChangeExtension #06", "path.ext1.doc", testPath); if (Windows) { try { testPath = Path.ChangeExtension ("<", ".extension"); Fail ("ChangeException Fail #01"); } catch (Exception e) { AssertEquals ("ChangeExtension Exc. #01", typeof (ArgumentException), e.GetType ()); } } } public void TestCombine () { string [] files = new string [3]; files [(int) OsType.Unix] = "/etc/init.d"; files [(int) OsType.Windows] = Environment.GetEnvironmentVariable ("SYSTEMROOT") + @"\system32"; files [(int) OsType.Mac] = "foo:bar"; string testPath = Path.Combine (path2, path3); AssertEquals ("Combine #01", files [(int) OS], testPath); testPath = Path.Combine ("one", ""); AssertEquals ("Combine #02", "one", testPath); testPath = Path.Combine ("", "one"); AssertEquals ("Combine #03", "one", testPath); string current = Directory.GetCurrentDirectory (); testPath = Path.Combine (current, "one"); string expected = current + DSC + "one"; AssertEquals ("Combine #04", expected, testPath); testPath = Path.Combine ("one", current); // LAMESPEC noted in Path.cs AssertEquals ("Combine #05", current, testPath); testPath = Path.Combine (current, expected); AssertEquals ("Combine #06", expected, testPath); testPath = DSC + "one"; testPath = Path.Combine (testPath, "two" + DSC); expected = DSC + "one" + DSC + "two" + DSC; AssertEquals ("Combine #06", expected, testPath); testPath = "one" + DSC; testPath = Path.Combine (testPath, DSC + "two"); expected = DSC + "two"; AssertEquals ("Combine #06", expected, testPath); testPath = "one" + DSC; testPath = Path.Combine (testPath, "two" + DSC); expected = "one" + DSC + "two" + DSC; AssertEquals ("Combine #07", expected, testPath); //TODO: Tests for UNC names try { testPath = Path.Combine ("one", null); Fail ("Combine Fail #01"); } catch (Exception e) { AssertEquals ("Combine Exc. #01", typeof (ArgumentNullException), e.GetType ()); } try { testPath = Path.Combine (null, "one"); Fail ("Combine Fail #02"); } catch (Exception e) { AssertEquals ("Combine Exc. #02", typeof (ArgumentNullException), e.GetType ()); } if (Windows) { try { testPath = Path.Combine ("a>", "one"); Fail ("Combine Fail #03"); } catch (Exception e) { AssertEquals ("Combine Exc. #03", typeof (ArgumentException), e.GetType ()); } try { testPath = Path.Combine ("one", "aaa<"); Fail ("Combine Fail #04"); } catch (Exception e) { AssertEquals ("Combine Exc. #04", typeof (ArgumentException), e.GetType ()); } } } public void TestDirectoryName () { string [] files = new string [3]; files [(int) OsType.Unix] = "/foo"; files [(int) OsType.Windows] = "c:\\foo"; files [(int) OsType.Mac] = "foo"; AssertEquals ("GetDirectoryName #01", null, Path.GetDirectoryName (null)); string testDirName = Path.GetDirectoryName (path1); AssertEquals ("GetDirectoryName #02", files [(int) OS], testDirName); testDirName = Path.GetDirectoryName (files [(int) OS] + DSC); AssertEquals ("GetDirectoryName #03", files [(int) OS], testDirName); try { testDirName = Path.GetDirectoryName (""); Fail ("GetDirectoryName Fail #01"); } catch (Exception e) { AssertEquals ("GetDirectoryName Exc. #01", typeof (ArgumentException), e.GetType ()); } if (Windows) { try { testDirName = Path.GetDirectoryName ("aaa>"); Fail ("GetDirectoryName Fail #02"); } catch (Exception e) { AssertEquals ("GetDirectoryName Exc. #02", typeof (ArgumentException), e.GetType ()); } } try { testDirName = Path.GetDirectoryName (" "); Fail ("GetDirectoryName Fail #03"); } catch (Exception e) { AssertEquals ("GetDirectoryName Exc. #03", typeof (ArgumentException), e.GetType ()); } } public void TestGetExtension () { string testExtn = Path.GetExtension (path1); AssertEquals ("GetExtension #01", ".txt", testExtn); testExtn = Path.GetExtension (path2); AssertEquals ("GetExtension #02", String.Empty, testExtn); testExtn = Path.GetExtension (String.Empty); AssertEquals ("GetExtension #03", String.Empty, testExtn); testExtn = Path.GetExtension (null); AssertEquals ("GetExtension #04", null, testExtn); testExtn = Path.GetExtension (" "); AssertEquals ("GetExtension #05", String.Empty, testExtn); testExtn = Path.GetExtension (path1 + ".doc"); AssertEquals ("GetExtension #06", ".doc", testExtn); testExtn = Path.GetExtension (path1 + ".doc" + DSC + "a.txt"); AssertEquals ("GetExtension #07", ".txt", testExtn); if (Windows) { try { testExtn = Path.GetExtension ("hi