|
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Catel.Test.IO
{
[TestClass]
public class DirectoryTest
{
#region DirectoryExists
[TestMethod]
public void DirectoryExists_ExistingDirectory()
{
// Declare variables
string input = @"C:\Windows\";
bool expectedOutput = true;
// Call method
bool output = Catel.IO.Directory.Exists(input);
// Check result
Assert.AreEqual(expectedOutput, output);
}
[TestMethod]
public void DirectoryExists_NonExistingDirectory()
{
// Declare variables
string input = @"C:\MyWindows\";
bool expectedOutput = false;
// Call method
bool output = Catel.IO.Directory.Exists(input);
// Check result
Assert.AreEqual(expectedOutput, output);
}
[TestMethod]
public void DirectoryExists_Null()
{
try
{
Catel.IO.Directory.Exists(null);
Assert.Fail("Expected exception");
}
catch (ArgumentException ex)
{
Assert.AreEqual("directory", ex.ParamName);
}
}
[TestMethod]
public void DirectoryExists_EmptyParameter()
{
Assert.AreEqual(false, Catel.IO.Directory.Exists(string.Empty));
}
#endregion
#region DeleteDirectory
[TestMethod]
public void DeleteDirectory()
{
// Get temporary directory
string baseDirectory = Catel.IO.Path.Combine(System.IO.Path.GetTempPath(), "DeleteDirectoryTest");
// Create test directories (5 levels deep)
CreateDirectories(baseDirectory, 5);
// Now remove the directories again
bool output = Catel.IO.Directory.Delete(baseDirectory);
// Validate output
Assert.AreEqual(true, output);
Assert.AreEqual(false, Catel.IO.Directory.Exists(baseDirectory));
}
#endregion
#region Helper methods
/// <summary>
/// Creates a lot of directories with subdirectories and files.
/// </summary>
/// <param name="directory">The directory.</param>
/// <param name="depth">The number of subdirectories to create.</param>
public void CreateDirectories(string directory, int depth)
{
// Create directory
System.IO.Directory.CreateDirectory(directory);
// Create files in directory
CreateFilesInDirectory(directory);
// Should we still create directories?
if (depth > 0)
{
// Create 10 subdirectories
for (int i = 1; i < 5; i++)
{
// Create subdirectory
CreateDirectories(Catel.IO.Path.Combine(directory, i.ToString()), depth - 1);
}
}
}
/// <summary>
/// Creates 10 dummy files in the directory.
/// </summary>
/// <param name="directory">The directory.</param>
private void CreateFilesInDirectory(string directory)
{
// Create files
for (int i = 1; i <= 10; i++)
{
// Get filepath & create file
string filePath = Catel.IO.Path.Combine(directory, i + ".dat");
using (System.IO.FileStream fileStream = System.IO.File.Create(filePath))
{
// If you want, write something to the file
}
}
}
#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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.