Skip to content
Permalink
Browse files
Rewrite all tests as MSpec specs.
  • Loading branch information
forki committed Feb 19, 2011
1 parent 4f33b39 commit 9aaa3207fc5d2fc33fca9019dce7409071c81701
Show file tree
Hide file tree
Showing 33 changed files with 521 additions and 970 deletions.
@@ -1,14 +1,31 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Fake;
using Machine.Specifications;
using Microsoft.FSharp.Core;
using NUnit.Framework;

namespace Test.FAKECore.FileHandling
{
[TestFixture]
{
public class when_creating_test_directory_structure : BaseFunctions
{
Because of = CreateTestDirStructure;

It should_create_all_dirs = () => AllDirectories().Length.ShouldEqual(16);

It should_create_no_files = () => AllFiles().ShouldBeEmpty();
}

public class when_creating_test_file_structure : BaseFunctions
{
Because of = CreateTestFileStructure;

It should_create_all_dirs = () => AllDirectories().Length.ShouldEqual(16);

It should_create_all_files = () => AllFiles().Length.ShouldEqual(17);
}

public class BaseFunctions
{
/// <summary>
@@ -20,7 +37,6 @@ public static void CleanDir(string dir)
FileHelper.CleanDir(dir);
}


/// <summary>
/// Creates the test dir structure.
/// </summary>
@@ -40,18 +56,6 @@ public static void CreateTestDirStructure()
CleanDir(TestData.TestDir + "\\Dir8\\Sub1");
}

/// <summary>
/// Tests if dir is empty or doesn't exists.
/// </summary>
/// <param name="dir">The dir.</param>
public static void TestIfDirIsEmpty(string dir)
{
var di = new DirectoryInfo(dir);
if (!di.Exists) return;
Assert.AreEqual(0, Directory.GetDirectories(dir, "*", SearchOption.AllDirectories).Length);
Assert.AreEqual(0, Directory.GetFiles(dir, "*.*", SearchOption.AllDirectories).Length);
}

/// <summary>
/// Creates a test file.
/// </summary>
@@ -166,29 +170,14 @@ public static void CreateTestFileStructure()
}


/// <summary>
/// Tests the creating of directories.
/// </summary>
[Test]
public void TestDirStructure()
public static string[] AllDirectories()
{
CreateTestDirStructure();

Assert.AreEqual(16, Directory.GetDirectories(TestData.TestDir, "*", SearchOption.AllDirectories).Length);
Assert.AreEqual(0, Directory.GetFiles(TestData.TestDir, "*.*", SearchOption.AllDirectories).Length);
return Directory.GetDirectories(TestData.TestDir, "*", SearchOption.AllDirectories);
}


/// <summary>
/// Tests the creating of directories and files.
/// </summary>
[Test]
public void TestFileStructure()
protected static string[] AllFiles()
{
CreateTestFileStructure();

Assert.AreEqual(16, Directory.GetDirectories(TestData.TestDir, "*", SearchOption.AllDirectories).Length);
Assert.AreEqual(17, Directory.GetFiles(TestData.TestDir, "*.*", SearchOption.AllDirectories).Length);
return Directory.GetFiles(TestData.TestDir, "*.*", SearchOption.AllDirectories);
}
}
}
@@ -0,0 +1,30 @@
using System.IO;
using Machine.Specifications;

namespace Test.FAKECore.FileHandling
{
public class when_cleaning_empty_directory_after_creating_test_structure : BaseFunctions
{
Establish context = CreateTestDirStructure;

Because of = () => CleanDir(TestData.TestDir);

It should_cleaned_all_dirs = () => AllDirectories().ShouldBeEmpty();
It should_cleaned_all_files = () => AllFiles().ShouldBeEmpty();
It should_still_exist = () => new DirectoryInfo(TestData.TestDir).Exists.ShouldBeTrue();
}

public class when_cleaning_directory_after_creating_test_structure : BaseFunctions
{
Establish context = CreateTestFileStructure;

Because of = () => CleanDir(TestData.TestDir);

It should_be_writeable =
() => new DirectoryInfo(TestData.TestDir).Attributes.ShouldEqual(FileAttributes.Directory);

It should_cleaned_all_dirs = () => AllDirectories().ShouldBeEmpty();
It should_cleaned_all_files = () => AllFiles().ShouldBeEmpty();
It should_still_exist = () => new DirectoryInfo(TestData.TestDir).Exists.ShouldBeTrue();
}
}
@@ -0,0 +1,36 @@
using System.IO;
using Fake;
using Machine.Specifications;

namespace Test.FAKECore.FileHandling
{
public class when_copying_directory_with_subfolders_but_without_filter : BaseFunctions
{
Establish context = CreateTestFileStructure;

Because of = () => FileHelper.CopyDir(Target, TestData.SubDir7, AllFilesFunction);

It should_have_copied_all_files =
() => Directory.GetFiles(Target, "*.*", SearchOption.AllDirectories).Length.ShouldEqual(6);

It should_have_copied_the_subfolder =
() => Directory.GetDirectories(Target, "*", SearchOption.AllDirectories).Length.ShouldEqual(1);

private static readonly string Target = string.Format("{0}\\CopyTo", TestData.TestDir);
}

public class when_copying_directory_but_without_filter : BaseFunctions
{
Establish context = CreateTestFileStructure;

Because of = () => FileHelper.CopyDir(Target, TestData.SubDir1, AllFilesFunction);

It should_have_copied_all_files =
() => Directory.GetFiles(Target, "*.*", SearchOption.AllDirectories).Length.ShouldEqual(3);

It should_not_have_copied_a_subfolder =
() => Directory.GetDirectories(Target, "*", SearchOption.AllDirectories).ShouldBeEmpty();

private static readonly string Target = string.Format("{0}\\CopyTo", TestData.TestDir);
}
}
@@ -0,0 +1,22 @@
using System.IO;
using Fake;
using Machine.Specifications;

namespace Test.FAKECore.FileHandling
{
public class when_comparing_nonidentical_files
{
private static readonly FileInfo File1 = new FileInfo(@"TestData\AllObjects.txt");
private static readonly FileInfo File2 = new FileInfo(@"TestData\AllObjects_2.txt");

It should_not_be_identical = () => FileHelper.FilesAreEqual(File1, File2).ShouldBeFalse();
}

public class when_comparing_identical_files
{
private static readonly FileInfo File1 = new FileInfo(@"TestData\AllObjects.txt");
private static readonly FileInfo File2 = new FileInfo(@"TestData\AllObjects.txt");

It should_be_identical = () => FileHelper.FilesAreEqual(File1, File2).ShouldBeTrue();
}
}
@@ -0,0 +1,46 @@
using System.Linq;
using Fake;
using Machine.Specifications;

namespace Test.FAKECore.FileHandling
{
public class when_creating_some_directories_and_scanning_them : BaseFunctions
{
Establish context = CreateTestDirStructure;

It should_find_the_test_dir = () => Scan("Test").First().ShouldEqual(TestData.TestDir);
It should_find_only_the_test_dir = () => Scan("Test").Count.ShouldEqual(1);
It should_find_Dir2 = () => ScanCount("**/Dir2").ShouldEqual(1);
It should_find_Sub1_in_subfolder = () => ScanCount("**/**/Sub1").ShouldEqual(6);
It should_find_Sub1 = () => ScanCount("**/Sub1").ShouldEqual(6);
It should_find_Sub1_in_sub2 = () => ScanCount("**/Sub2/Sub1").ShouldEqual(1);
It should_find_Sub2 = () => ScanCount("**/Sub2").ShouldEqual(2);
It should_find_test_directory_in_base_directory = () => Scan(@"Test").First().ShouldEqual(TestData.TestDir);
}

public class when_creating_some_files_and_scanning_them : BaseFunctions
{
Establish context = CreateTestFileStructure;

It should_find_file1 = () => ScanCount("**/file1.nav").ShouldEqual(3);
It should_find_file1_with_every_extension = () => ScanCount("**/file1.*").ShouldEqual(5);
It should_find_file_x = () => ScanCount("**/file?.n??").ShouldEqual(11);
It should_find_file_x_in_subfolder = () => ScanCount("**/Sub1/**/file*.*").ShouldEqual(6);
It should_find_every_nav_file_in_every_folder = () => ScanCount("**/*.nav").ShouldEqual(7);
It should_not_find_a_nav_file_in_root = () => ScanCount("*.nav").ShouldEqual(0);
It should_find_all_files_in_test_folder = () => ScanCount("Test/*.*").ShouldEqual(5);
}

public class when_creating_some_files_and_scanning_them_with_concrete_path : BaseFunctions
{
Establish context = CreateTestFileStructure;

It should_find_file1 = () => ScanCount(FileSetHelper.DefaultBaseDir + "/**/file1.nav").ShouldEqual(3);
It should_find_file1_with_every_extension = () => ScanCount(FileSetHelper.DefaultBaseDir + "/**/file1.*").ShouldEqual(5);
It should_find_file_x = () => ScanCount(FileSetHelper.DefaultBaseDir + "/**/file?.n??").ShouldEqual(11);
It should_find_file_x_in_subfolder = () => ScanCount(FileSetHelper.DefaultBaseDir + "/Test/**/Sub1/**/file*.*").ShouldEqual(6);
It should_find_every_nav_file_in_every_folder = () => ScanCount(FileSetHelper.DefaultBaseDir + "/Test/**/*.nav").ShouldEqual(7);
It should_not_find_a_nav_file_in_root = () => ScanCount(FileSetHelper.DefaultBaseDir + "/*.nav").ShouldEqual(0);
It should_find_all_files_in_test_folder = () => ScanCount(FileSetHelper.DefaultBaseDir + "/Test/*.*").ShouldEqual(5);
}
}

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 9aaa320

Please sign in to comment.