Click here to Skip to main content
15,891,941 members
Articles / Desktop Programming / WPF

Catel - Part 4 of n: Unit testing with Catel

Rate me:
Please Sign up or sign in to vote.
4.55/5 (10 votes)
28 Jan 2011CPOL11 min read 49.1K   572   11  
This article explains how to write unit tests for MVVM using Catel.
using System;
using System.IO;
using System.Reflection;
using Catel.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Directory = Catel.IO.Directory;
using File = Catel.IO.File;
using Path = Catel.IO.Path;

namespace Catel.Test.IO
{
	/// <summary>
	/// Summary description for PathTest
	/// </summary>
	[TestClass]
	public class PathTest
	{
        #region Variables
        private string _testDirectory;
        #endregion

        #region Initialization & cleanup
        [TestInitialize]
        public void Initialize()
        {
            // Determine test directory
            _testDirectory = Path.Combine(System.IO.Path.GetTempPath(), "PathTest");

            // Delete directory, than create it
            if (Directory.Exists(_testDirectory)) System.IO.Directory.Delete(_testDirectory, true);
            Directory.CreateDirectory(_testDirectory);
        }

        [TestCleanup]
        public void CleanUp()
        {
            // Delete test directory
            if (!string.IsNullOrEmpty(_testDirectory)) System.IO.Directory.Delete(_testDirectory, true);
        }
        #endregion

		#region GetApplicationDataDirectory
		[TestMethod]
		public void GetApplicationData_EntryAssembly()
		{
			string expected = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
										   Assembly.GetExecutingAssembly().Company(), Assembly.GetExecutingAssembly().Product());

			string result = Path.GetApplicationDataDirectory();

			Assert.AreEqual(expected, result);
		}

		[TestMethod]
		public void GetApplicationDataDirectoryForAppOnly()
		{
			string expected = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
										   Assembly.GetExecutingAssembly().Product());

			string result = Path.GetApplicationDataDirectory(Assembly.GetExecutingAssembly().Product());

			Assert.AreEqual(expected, result);
		}

		[TestMethod]
		public void GetApplicationDataDirectory_CompanyAndApp()
		{
			string expected = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
										   Assembly.GetExecutingAssembly().Company(), Assembly.GetExecutingAssembly().Product());

			string result = Path.GetApplicationDataDirectory(Assembly.GetExecutingAssembly().Company(), Assembly.GetExecutingAssembly().Product());

			Assert.AreEqual(expected, result);
		}

		[TestMethod]
		public void GetApplicationDataDirectory_CompanyAndAppAndTestDirectoryCreation()
		{
			// Set up directory
			string directory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
											Assembly.GetExecutingAssembly().Company(), Assembly.GetExecutingAssembly().Product());

			// Make sure that the directory does not exist
			if (Directory.Exists(directory)) Directory.Delete(directory);

			// Now create the directory
			string result = Path.GetApplicationDataDirectory(Assembly.GetExecutingAssembly().Company(), Assembly.GetExecutingAssembly().Product());

			// Check if the directory exists
			Assert.AreEqual(directory, result);
			Assert.IsTrue(Directory.Exists(result));
		}
		#endregion

        #region GetDirectoryName
        [TestMethod]
        public void GetDirectoryName_NormalDirectory()
        {
            string result = Path.GetDirectoryName(@"C:\ParentDirectory\ChildDirectory");

            Assert.AreEqual(@"C:\ParentDirectory", result);
        }

        [TestMethod]
        public void GetDirectoryName_RootDirectory()
        {
            string result = Path.GetDirectoryName(@"C:\");

            Assert.AreEqual(string.Empty, result);
        }
        #endregion

        #region GetFileName
        [TestMethod]
		public void GetFileName_WithDirectory()
		{
			// Declare variables
			string input = @"C:\WINDOWS\notepad.exe";
			string expectedOutput = "notepad.exe";

			// Call method
			string output = Path.GetFileName(input);

			// Check result
			Assert.AreEqual(expectedOutput, output);
		}

		[TestMethod]
		public void GetFileName_WithoutDirectory()
		{
			// Declare variables
			string input = @"notepad.exe";
			string expectedOutput = "notepad.exe";

			// Call method
			string output = Path.GetFileName(input);

			// Check result
			Assert.AreEqual(expectedOutput, output);
		}

        [TestMethod]
        public void GetFileName_EmptyInput()
        {
            try
            {
                Path.GetFileName(null);
                Assert.Fail("Expected exception");
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("path", ex.ParamName);
            }

            try
            {
                Path.GetFileName(string.Empty);
                Assert.Fail("Expected exception");
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("path", ex.ParamName);
            } 
        }
		#endregion

		#region GetLongPathName
		[TestMethod]
		public void GetLongFileName_WithShortName()
		{
            // Declare variables
            string input = Path.Combine(_testDirectory, "verylo~1.ext");
            string expectedOutput = Path.Combine(_testDirectory, "verylongfilename.ext");

            // Create the file
            using (FileStream fileStream = File.Create(expectedOutput)) { }

			// Call method
			string output = Path.GetLongPathName(input);

			// Check result
			Assert.AreEqual(expectedOutput, output);
		}

		[TestMethod]
		public void GetLongFileName_WithLongName()
		{
			// Declare variables
			string input = @"C:\WINDOWS\system32\microsoft.managementconsole.dll";
			string expectedOutput = @"C:\WINDOWS\system32\microsoft.managementconsole.dll";

			// Call method
			string output = Path.GetLongPathName(input);

			// Check result
			Assert.AreEqual(expectedOutput, output);
		}

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void GetLongFileName_InvalidArgument()
        {
            Path.GetLongPathName("");
        }
		#endregion

		#region GetParentDirectory
		[TestMethod]
		public void GetParentDirectory_File()
		{
			// Declare variables
			string input = @"C:\MyPathThatDoesntExist\MyDirectory\MyFile.txt";
			string expectedOutput = @"C:\MyPathThatDoesntExist\MyDirectory";

			// Call method
			string output = Path.GetParentDirectory(input);

			// Check result
			Assert.AreEqual(expectedOutput, output);
		}

		[TestMethod]
		public void GetParentDirectory_DirectoryEndingWithSlash()
		{
			// Declare variables
			string input = @"C:\MyPathThatDoesntExist\MyDirectory\";
			string expectedOutput = @"C:\MyPathThatDoesntExist";

			// Call method
			string output = Path.GetParentDirectory(input);

			// Check result
			Assert.AreEqual(expectedOutput, output);
		}

		[TestMethod]
		public void GetParentDirectory_DirectoryNotEndingWithSlash()
		{
			// Declare variables
			string input = @"C:\MyPathThatDoesntExist\MyDirectory";
			string expectedOutput = @"C:\MyPathThatDoesntExist";

			// Call method
			string output = Path.GetParentDirectory(input);

			// Check result
			Assert.AreEqual(expectedOutput, output);
		}

		[TestMethod]
		public void GetParentDirectory_InvalidInput()
		{
			// Declare variables
			string input = @"abse";
			string expectedOutput = @"";

			// Call method
			string output = Path.GetParentDirectory(input);

			// Check result
			Assert.AreEqual(expectedOutput, output);
		}

        [TestMethod]
        public void GetParentDirectory_EmptyInput()
        {
            // Declare variables
            string input = @"";
            string expectedOutput = @"";

            // Call method
            string output = Path.GetParentDirectory(input);

            // Check result
            Assert.AreEqual(expectedOutput, output);
        }
		#endregion

		#region GetRelativePath
		[TestMethod]
		public void GetRelativePath_Root()
		{
			// Declare variables
			string file = @"C:\Windows\notepad.exe";
			string path = @"C:\";

			// Call method
			string relative = Path.GetRelativePath(file, path);

			// Validate
			Assert.AreEqual(@"Windows\notepad.exe".ToLower(), relative.ToLower());
		}

		[TestMethod]
		public void GetRelativePath_SingleDirectory()
		{
			// Declare variables
			string file = @"C:\Windows\notepad.exe";
			string path = @"C:\Windows\";

			// Call method
			string relative = Path.GetRelativePath(file, path);

			// Validate
			Assert.AreEqual(@"notepad.exe".ToLower(), relative.ToLower());
		}

		[TestMethod]
		public void GetRelativePath_SingleDirectoryNotEndingWithSlash()
		{
			// Declare variables
			string file = @"C:\Windows\notepad.exe";
			string path = @"C:\Windows";

			// Call method
			string relative = Path.GetRelativePath(file, path);

			// Validate
			Assert.AreEqual(@"notepad.exe".ToLower(), relative.ToLower());
		}

		[TestMethod]
		public void GetRelativePath_DeeperDirectory()
		{
			// Declare variables
			string file = @"C:\Windows\notepad.exe";
			string path = @"C:\Windows\Temp\";

			// Call method
			string relative = Path.GetRelativePath(file, path);

			// Validate
			Assert.AreEqual(@"..\notepad.exe".ToLower(), relative.ToLower());
		}

		[TestMethod]
		public void GetRelativePath_SameDirectoryLevelWithDifferentName()
		{
			// Declare variables
			string file = @"C:\Windows\MyTest\MyFile.exe";
			string path = @"C:\Windows\MyTes";

			// Call method
			string relative = Path.GetRelativePath(file, path);

			// Validate
			Assert.AreEqual(@"..\MyTest\MyFile.exe".ToLower(), relative.ToLower());
		}

        [TestMethod]
        public void GetRelativePath_EmptyBasePath()
        {
            // Get current directory
	        string currentWorkingDirectory = Environment.CurrentDirectory;
            Environment.CurrentDirectory = @"C:\Windows\System32";

			// Declare variables
			string file = @"C:\Windows\MyTest\MyFile.exe";

			// Call method
			string relative = Path.GetRelativePath(file);

			// Validate
            Assert.AreEqual(@"..\MyTest\MyFile.exe".ToLower(), relative.ToLower());

            // Restore current working directory
            Environment.CurrentDirectory = currentWorkingDirectory;
        }

        [TestMethod]
        public void GetRelativePath_DeepTree()
        {
            // Declare variables
            string file = @"C:\Windows\Level1_\Level2_";
            string path = @"C:\Windows\Level1\Level2";

            // Call method
            string relative = Path.GetRelativePath(file, path);

            // Validate
            Assert.AreEqual(@"..\..\Level1_\Level2_".ToLower(), relative.ToLower());
        }

        [TestMethod]
        public void GetRelativePath_HigherDirectory()
        {
            // Declare variables
            string file = @"C:\Windows\";
            string path = @"C:\Windows\Level1\Level2";

            // Call method
            string relative = Path.GetRelativePath(file, path);

            // Validate
            Assert.AreEqual(@"..\..".ToLower(), relative.ToLower());
        }

        [TestMethod]
        public void GetRelativePath_DifferentBaseDirectory()
        {
            // Declare variables
            string file = @"C:\Windows\notepad.exe";
            string path = @"C:\MyTest\";

            // Call method
            string relative = Path.GetRelativePath(file, path);

            // Validate
            Assert.AreEqual(@"..\Windows\notepad.exe".ToLower(), relative.ToLower());
        }

        [TestMethod]
        public void GetRelativePath_DifferentRoot()
        {
            // Declare variables
            string file = @"C:\Windows\notepad.exe";
            string path = @"D:\Windows\";

            // Call method
            string relative = Path.GetRelativePath(file, path);

            // Validate
            Assert.AreEqual(@"C:\Windows\notepad.exe".ToLower(), relative.ToLower());
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void GetRelativePath_InvalidInput()
        {
            Path.GetRelativePath(null, @"C:\test\");
        }
		#endregion

		#region GetFullPath
		[TestMethod]
		public void GetFullPath_FromRootDirectory()
		{
			// Declare variables
			string file = @"Windows\notepad.exe";
			string path = @"C:\";

			// Call method
			string full = Path.GetFullPath(file, path);

			// Validate
			Assert.AreEqual(@"C:\Windows\notepad.exe".ToLower(), full.ToLower());
		}

		[TestMethod]
		public void GetFullPath_FileNameOnly()
		{
			// Declare variables
			string file = @"notepad.exe";
			string path = @"C:\Windows\";

			// Call method
			string full = Path.GetFullPath(file, path);

			// Validate
			Assert.AreEqual(@"C:\Windows\notepad.exe".ToLower(), full.ToLower());
		}

		[TestMethod]
		public void GetFullPath_FileNameOnlyAndDirectoryWithoutTrailingSlash()
		{
			// Declare variables
			string file = @"notepad.exe";
			string path = @"C:\Windows";

			// Call method
			string full = Path.GetFullPath(file, path);

			// Validate
			Assert.AreEqual(@"C:\Windows\notepad.exe".ToLower(), full.ToLower());
		}

		[TestMethod]
		public void GetFullPath_RelativeDotsDirectory()
		{
			// Declare variables
			string file = @"..\notepad.exe";
			string path = @"C:\Windows\Temp\";

			// Call method
			string full = Path.GetFullPath(file, path);

			// Validate
			Assert.AreEqual(@"C:\Windows\notepad.exe".ToLower(), full.ToLower());
		}

		[TestMethod]
		public void GetFullPath_RelativeDotsAndNameDirectory()
		{
			// Declare variables
			string file = @"..\Windows\notepad.exe";
			string path = @"C:\Program Files\";

			// Call method
			string full = Path.GetFullPath(file, path);

			// Validate
			Assert.AreEqual(@"C:\Windows\notepad.exe".ToLower(), full.ToLower());
		}

		[TestMethod]
		public void GetFullPath_NoBasePath()
		{
			// Declare variables
			string file = @"..\Windows\notepad.exe";
			string path = @"";

			// Set current environment path
			string oldEnvironmentDirectory = Environment.CurrentDirectory;
			Environment.CurrentDirectory = @"C:\Program Files\";

		    try
		    {
                // Call method
                string full = Path.GetFullPath(file, path);
                Assert.Fail("Expected exception");
		    }
		    catch (ArgumentException ex)
		    {
		        Assert.AreEqual("basePath", ex.ParamName);
		    }
		}

        [TestMethod]
        public void GetFullPath_EmptyInput()
        {
            try
            {
                Path.GetFullPath(null);
                Assert.Fail("Expected exception");
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("path", ex.ParamName);
            }

            try
            {
                Path.GetFullPath(string.Empty);
                Assert.Fail("Expected exception");
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("path", ex.ParamName);
            }
        }
		#endregion

        #region GetWin32LongFileName
        [TestMethod]
        public void GetWin32LongFileName_NonWin32LongFileName()
        {
            // Declare variables
            string input = @"C:\Windows\notepad.exe";
            string expectedOutput = @"\\?\C:\Windows\notepad.exe";

            // Call method
            string output = Path.GetWin32LongFileName(input);

            // Check result
            Assert.AreEqual(expectedOutput, output);
        }

        [TestMethod]
        public void GetWin32LongFileName_Win32LongFileName()
        {
            // Declare variables
            string input = @"\\?\C:\Windows\notepad.exe";
            string expectedOutput = @"\\?\C:\Windows\notepad.exe";

            // Call method
            string output = Path.GetWin32LongFileName(input);

            // Check result
            Assert.AreEqual(expectedOutput, output);
        }
        #endregion

        #region AddWin32LongFileNamePrefix
        [TestMethod]
		public void AddWin32LongFileNamePrefix_WithoutPrefix()
		{
			// Declare variables
			string input = @"C:\Windows\notepad.exe";
			string expectedOutput = @"\\?\C:\Windows\notepad.exe";

			// Call method
			string output = Path.AddWin32LongFileNamePrefix(input);

			// Check result
			Assert.AreEqual(expectedOutput, output);
		}

		[TestMethod]
		public void AddWin32LongFileNamePrefix_WithPrefix()
		{
			// Declare variables
			string input = @"\\?\C:\Windows\notepad.exe";
			string expectedOutput = @"\\?\C:\Windows\notepad.exe";

			// Call method
			string output = Path.AddWin32LongFileNamePrefix(input);

			// Check result
			Assert.AreEqual(expectedOutput, output);
		}

		[TestMethod]
		public void AddWin32LongFileNamePrefix_WithDot()
		{
			// Declare variables
			string input = @"..\Windows\notepad.exe";
			string expectedOutput = @"..\Windows\notepad.exe";

			// Call method
			string output = Path.AddWin32LongFileNamePrefix(input);

			// Check result
			Assert.AreEqual(expectedOutput, output);
		}

		[TestMethod]
		public void AddWin32LongFileNamePrefix_UncPath()
		{
			// Declare variables
			string input = @"\\MyServer\C$\Windows\notepad.exe";
			string expectedOutput = @"\\?\UNC\MyServer\C$\Windows\notepad.exe";

			// Call method
			string output = Path.AddWin32LongFileNamePrefix(input);

			// Check result
			Assert.AreEqual(expectedOutput, output);
		}

		[TestMethod]
		public void AddWin32LongFileNamePrefix_UncPathWithPrefix()
		{
			// Declare variables
			string input = @"\\?\UNC\MyServer\C$\Windows\notepad.exe";
			string expectedOutput = @"\\?\UNC\MyServer\C$\Windows\notepad.exe";

			// Call method
			string output = Path.AddWin32LongFileNamePrefix(input);

			// Check result
			Assert.AreEqual(expectedOutput, output);
		}

        [TestMethod]
        public void AddWin32LongFileNamePrefix_UncWithEmptyInput()
        {
            try
            {
                Path.AddWin32LongFileNamePrefix(null);
                Assert.Fail("Expected exception");
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("filePath", ex.ParamName);
            }

            try
            {
                Path.AddWin32LongFileNamePrefix(string.Empty);
                Assert.Fail("Expected exception");
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("filePath", ex.ParamName);
            } 
        }
		#endregion

		#region RemoveWin32LongFileNamePrefix
		[TestMethod]
		public void RemoveWin32LongFileNamePrefix_WithPrefix()
		{
			// Declare variables
			string input = @"\\?\C:\Windows\notepad.exe";
			string expectedOutput = @"C:\Windows\notepad.exe";

			// Call method
			string output = Path.RemoveWin32LongFileNamePrefix(input);

			// Check result
			Assert.AreEqual(expectedOutput, output);
		}

		[TestMethod]
		public void RemoveWin32LongFileNamePrefix_WithoutPrefix()
		{
			// Declare variables
			string input = @"C:\Windows\notepad.exe";
			string expectedOutput = @"C:\Windows\notepad.exe";

			// Call method
			string output = Path.RemoveWin32LongFileNamePrefix(input);

			// Check result
			Assert.AreEqual(expectedOutput, output);
		}

		[TestMethod]
		public void RemoveWin32LongFileNamePrefix_UncPath()
		{
			// Declare variables
			string input = @"\\?\UNC\MyServer\C$\Windows\notepad.exe";
			string expectedOutput = @"\\MyServer\C$\Windows\notepad.exe";

			// Call method
			string output = Path.RemoveWin32LongFileNamePrefix(input);

			// Check result
			Assert.AreEqual(expectedOutput, output);
		}

		[TestMethod]
		public void RemoveWin32LongFileNamePrefix_UncPathWithoutPrefix()
		{
			// Declare variables
			string input = @"\\MyServer\C$\Windows\notepad.exe";
			string expectedOutput = @"\\MyServer\C$\Windows\notepad.exe";

			// Call method
			string output = Path.RemoveWin32LongFileNamePrefix(input);

			// Check result
			Assert.AreEqual(expectedOutput, output);
		}

        [TestMethod]
        public void RemoveWin32LongFileNamePrefix_UncWithEmptyInput()
        {
            try
            {
                Path.RemoveWin32LongFileNamePrefix(null);
                Assert.Fail("Expected exception");
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("filePath", ex.ParamName);
            }

            try
            {
                Path.RemoveWin32LongFileNamePrefix(string.Empty);
                Assert.Fail("Expected exception");
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("filePath", ex.ParamName);
            }
        }
		#endregion

		#region AppendTrailing
		[TestMethod]
		public void AppendTrailingBackslash_EmptyValue()
		{
            try
            {
                Path.AppendTrailingSlash(null);
                Assert.Fail("Expected exception");
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("path", ex.ParamName);
            }

            try
            {
                Path.AppendTrailingSlash(string.Empty);
                Assert.Fail("Expected exception");
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("path", ex.ParamName);
            } 
		}

		[TestMethod]
		public void AppendTrailingSlash_EmptyValue()
		{
            try
            {
                Path.AppendTrailingSlash(null, '/');
                Assert.Fail("Expected exception");
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("path", ex.ParamName);
            }

            try
            {
                Path.AppendTrailingSlash(string.Empty, '/');
                Assert.Fail("Expected exception");
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("path", ex.ParamName);
            } 
		}

		[TestMethod]
		public void AppendTrailingBackslash_WithoutTrailingBackslash()
		{
			// Declare variables
			string path = @"C:\Windows";

			// Call method
			string result = Path.AppendTrailingSlash(path);

			// Validate
			Assert.AreEqual(@"C:\Windows\", result);
		}

		[TestMethod]
		public void AppendTrailingSlash_WithoutTrailingSlash()
		{
			// Declare variables
			string path = @"http://www.catenalogic.com";

			// Call method
			string result = Path.AppendTrailingSlash(path, '/');

			// Validate
			Assert.AreEqual(@"http://www.catenalogic.com/", result);
		}

		[TestMethod]
		public void AppendTrailingBackslash_WithTrailingBackslash()
		{
			// Declare variables
			string path = @"C:\Windows\";

			// Call method
			string result = Path.AppendTrailingSlash(path);

			// Validate
			Assert.AreEqual(@"C:\Windows\", result);
		}

		[TestMethod]
		public void AppendTrailingSlash_WithTrailingSlash()
		{
			// Declare variables
			string path = @"http://www.catenalogic.com/";

			// Call method
			string result = Path.AppendTrailingSlash(path, '/');

			// Validate
			Assert.AreEqual(@"http://www.catenalogic.com/", result);
		}
		#endregion

		#region Combine
        [TestMethod]
        public void CombinePath_NoValues()
        {
            // Call method
            string result = Path.Combine();

            // Validate
            Assert.AreEqual(@"", result);
        }

		[TestMethod]
		public void CombinePath_EmptyValues()
		{
			// Declare variables
			string path1 = @"";
			string path2 = @"";
			string path3 = @"";

			// Call method
			string result = Path.Combine(path1, path2, path3);

			// Validate
			Assert.AreEqual(@"", result);
		}

		[TestMethod]
		public void CombinePath_OneNullValue()
		{
			// Declare variables
			string path1 = @"C:\";
			string path2 = null;
			string path3 = @"Program Files";

			// Call method
			string result = Path.Combine(path1, path2, path3);

			// Validate
			Assert.AreEqual(@"C:\Program Files", result);
		}

		[TestMethod]
		public void CombinePath_1Value()
		{
			// Declare variables
			string path1 = @"C:\Windows";

			// Call method
			string result = Path.Combine(path1);

			// Validate
			Assert.AreEqual(@"C:\Windows", result);
		}

		[TestMethod]
		public void CombinePath_2Values()
		{
			// Declare variables
			string path1 = @"C:\";
			string path2 = @"Windows";

			// Call method
			string result = Path.Combine(path1, path2);

			// Validate
			Assert.AreEqual(@"C:\Windows", result);
		}

		[TestMethod]
		public void CombinePath_3Values()
		{
			// Declare variables
			string path1 = @"C:\";
			string path2 = @"Windows";
			string path3 = @"System";

			// Call method
			string result = Path.Combine(path1, path2, path3);

			// Validate
			Assert.AreEqual(@"C:\Windows\System", result);
		}
		#endregion

		#region CombineUrls
        [TestMethod]
        public void CombineUrls_NoValues()
        {
            // Call method
            string result = Path.CombineUrls();

            // Validate
            Assert.AreEqual(@"", result);
        }

		[TestMethod]
		public void CombineUrls_1Value()
		{
			// Declare variables
			string path1 = @"http://www.catenalogic.com";

			// Call method
			string result = Path.CombineUrls(path1);

			// Validate
			Assert.AreEqual(@"http://www.catenalogic.com", result);
		}

		[TestMethod]
		public void CombineUrls_2Values()
		{
			// Declare variables
			string path1 = @"http://www.catenalogic.com";
			string path2 = @"products";

			// Call method
			string result = Path.CombineUrls(path1, path2);

			// Validate
			Assert.AreEqual(@"http://www.catenalogic.com/products", result);
		}

		[TestMethod]
		public void CombineUrls_3Values()
		{
			// Declare variables
			string path1 = @"http://www.catenalogic.com";
			string path2 = @"products";
			string path3 = @"updater";

			// Call method
			string result = Path.CombineUrls(path1, path2, path3);

			// Validate
			Assert.AreEqual(@"http://www.catenalogic.com/products/updater", result);
		}

		[TestMethod]
		public void CombineUrls_3ValuesAndGarbageSlashes()
		{
			// Declare variables
			string path1 = @"http://www.catenalogic.com/";
			string path2 = @"products\";
			string path3 = @"/updater";

			// Call method
			string result = Path.CombineUrls(path1, path2, path3);

			// Validate
			Assert.AreEqual(@"http://www.catenalogic.com/products/updater", result);
		}

		[TestMethod]
		public void CombineUrls_3ValuesAndRootedPath()
		{
			// Declare variables
			string path1 = @"/products";
			string path2 = @"updater\";
			string path3 = @"/default.aspx";

			// Call method
			string result = Path.CombineUrls(path1, path2, path3);

			// Validate
			Assert.AreEqual(@"/products/updater/default.aspx", result);
		}


        [TestMethod]
        public void CombineUrls_3ValuesAnd1Empty()
        {
            // Declare variables
            string path1 = @"/products";
            string path2 = @"";
            string path3 = @"/default.aspx";

            // Call method
            string result = Path.CombineUrls(path1, path2, path3);

            // Validate
            Assert.AreEqual(@"/products/default.aspx", result);
        }
		#endregion

        #region RemoveStartSlashes
        [TestMethod]
        public void RemoveStartSlashes_EmptyInput()
        {
            try
            {
                Path.RemoveStartSlashes(null);
                Assert.Fail("Expected exception");
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("value", ex.ParamName);
            }

            try
            {
                Path.RemoveStartSlashes(string.Empty);
                Assert.Fail("Expected exception");
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("value", ex.ParamName);
            }
        }

        [TestMethod]
        public void RemoveStartSlashes_StartingWithSlash()
        {
            string result = Path.RemoveStartSlashes(@"\withStartSlash");

            Assert.AreEqual(@"withStartSlash", result);
        }

        [TestMethod]
        public void RemoveStartSlashes_NotStartingWithSlash()
        {
            string result = Path.RemoveStartSlashes(@"withoutStartSlash");

            Assert.AreEqual(@"withoutStartSlash", result);
        }
        #endregion

        #region RemoveTrailingSlashes
        [TestMethod]
        public void RemoveTrailingSlashes_EmptyInput()
        {
            try
            {
                Path.RemoveTrailingSlashes(null);
                Assert.Fail("Expected exception");
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("value", ex.ParamName);
            }

            try
            {
                Path.RemoveTrailingSlashes(string.Empty);
                Assert.Fail("Expected exception");
            }
            catch (ArgumentException ex)
            {
                Assert.AreEqual("value", ex.ParamName);
            }
        }

        [TestMethod]
        public void RemoveTrailingSlashes_EndingWithSlash()
        {
            string result = Path.RemoveTrailingSlashes(@"withEndingSlash\");

            Assert.AreEqual(@"withEndingSlash", result);
        }

        [TestMethod]
        public void RemoveTrailingSlashes_NotRemoveTrailingSlashes_EndingWithSlash()
        {
            string result = Path.RemoveTrailingSlashes(@"withoutEndingSlash");

            Assert.AreEqual(@"withoutEndingSlash", result);
        }
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions