Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Catel - Part 4 of n: Unit testing with Catel

, 28 Jan 2011 CPOL
This article explains how to write unit tests for MVVM using Catel.
Catel-04_01-unittesting.zip
src
Catel.Articles.04 - Unit testing
Models
Properties
Settings.settings
Resources
Images
add.png
delete.png
edit.png
group.png
UI
Data
Converters
ViewModels
Windows
Catel.Articles.04 - Unit testing.Test
Models
Properties
UI
ViewModels
Catel.Articles.Base
Data
Attributes
Properties
Settings.settings
Resources
Images
CatenaLogic.png
Preview.png
Run.png
ShowCode.png
UI
Controls
Helpers
ViewModels
Windows
Catel.Core
Attributes
ClassDiagrams
DataObjectBase.cd
SavableDataObjectBase.cd
Collections
Helpers
ComponentModel
Data
Attributes
Exceptions
Interfaces
Diagnostics
Extensions
Helpers
Exceptions
Helpers
IO
Exceptions
IoC
LLBLGen
Log4net
Appender
Extensions
Helpers
MVVM
Commands
Interfaces
Exceptions
Services
EventArgs
Exceptions
Interfaces
ViewModels
Attributes
Interfaces
Properties
Reflection
Exceptions
Extensions
Helpers
Runtime
Serialization
Attributes
Helpers
Security
Cryptography
Helpers
Catel.Examples.Models
Properties
Catel.Examples.PersonApplication
Properties
Settings.settings
Resources
Images
add.png
delete.png
edit.png
group.png
UI
Data
Converters
ViewModels
Windows
Catel.Examples.Silverlight
Properties
Resources
Images
add.png
delete.png
edit.png
group.png
UI
Data
Converters
Pages
ViewModels
Windows
Catel.Examples.Silverlight.Web
Catel.Examples.Silverlight.Web.csproj.user
ClientBin
Properties
Catel.FxCop
Catel.Silverlight
Diagnostics
log4net
Core
MVVM
Commands
Services
ViewModels
Properties
Catel.Core
Catel.Windows
Reflection
Themes
Generic
Assets
Old
Windows
Controls
Data
Converters
Helpers
Helpers
Catel.Silverlight.Test
Properties
Catel.Silverlight.Test.Web
Catel.Silverlight.Test.Web.csproj.user
ClientBin
Properties
Catel.snk
Catel.Templates.WpfApplication
Properties
Settings.settings
UI
Controls
ViewModels
Windows
Catel.Templates.WpfItemTemplates
Properties
UI
Controls
ViewModels
Windows
Catel.Test
Collections
Convert
Data
Helpers
IO
MVVM
UI
ViewModels
Properties
Reflection
Runtime
Serialization
Security
Cryptography
Test References
Catel.Windows.accessor
Windows
Data
Converters
Catel.vsmdi
Catel.Windows
ClassDiagrams
ViewModelBase.cd
Collections
Extensions
Helpers
MVVM
Commands
Services
Test
UI
ViewModels
Properties
Settings.settings
Resources
Images
Add.png
ClearOutput.png
Edit.png
Error.png
Loading.gif
Preview.png
Remove.png
Save.png
TipOfTheDay.png
Warning.png
Themes
Aero
ExpressionDark
Assets
Generic
Assets
Controls
Jetpack
Assets
background.png
Old
SunnyOrange
Assets
Windows
Controls
Extensions
LinkLabel
StackGrid
Data
Converters
Helpers
Documents
Extensions
Extensions
Helpers
Input
Markup
Media
Effects
EmptyEffect
EmptyEffect.fx
EmptyEffect.ps
EmptyEffect.fx
GrayscaleEffect
GrayscaleEffect.fx
GrayscaleEffect.ps
Extensions
Imaging
Extensions
Windows
DataWindow
TipOfTheDay
Local.testsettings
Settings.StyleCop
TraceAndTestImpact.testsettings
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)

Share

About the Author

Geert van Horrik
Software Developer CatenaLogic
Netherlands Netherlands
I am Geert van Horrik, and I have studied Computer Science in the Netherlands.
 
I love to write software using .NET (especially the combination of WPF and C#). I am also the lead developer of Catel, an open-source application development framework for WPF, Silverlight, WP7 and WinRT with the focus on MVVM.
 
I have my own company since January 1st 2007, called CatenaLogic. This company develops commercial and non-commercial software.
 
To download (or buy) applications I have written, visit my website: http://www.catenalogic.com
Follow on   Twitter

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150301.1 | Last Updated 28 Jan 2011
Article Copyright 2011 by Geert van Horrik
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid