Last active
December 30, 2015 23:09
-
-
Save toeb/7899256 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// concrete testclass implementing the abstract testclass bellow | |
[TestClass] | |
public class CoreFileSystemPhysicalReadonlyFileSystemTest : AbstractReadonlyFileSystemTest | |
{ | |
protected override void CreateFile(string path, string content = "") | |
{ | |
path = uut.NormalizePath(path); | |
using (var writer = new StreamWriter(File.OpenWrite(path))) | |
{ | |
writer.Write(content); | |
} | |
} | |
protected override void CreateDir(string path) | |
{ | |
path = uut.NormalizePath(path); | |
Directory.CreateDirectory(path); | |
} | |
protected override IReadonlyFileSystem CreateReadonlyFileSystem() | |
{ | |
return new PhysicalReadonlyFileSystem(); | |
} | |
protected override void DeleteDir(string path) | |
{ | |
Directory.Delete(path, true); | |
} | |
public override string NormalizePathForFs(string path) | |
{ | |
return Path.GetFullPath(path); | |
} | |
} | |
/// abstract test class | |
public abstract class AbstractReadonlyFileSystemTest | |
{ | |
protected IReadonlyFileSystem uut; | |
[TestInitialize] | |
public void InitReadonlyFileSystemTest() | |
{ | |
uut = CreateReadonlyFileSystem(); | |
try | |
{ | |
DeleteDir("testdir"); | |
} | |
catch (Exception e) { } | |
CreateDir("testdir"); | |
} | |
protected abstract void CreateFile(string path, string content); | |
protected abstract void DeleteDir(string path); | |
protected abstract void CreateDir(string path); | |
protected abstract IReadonlyFileSystem CreateReadonlyFileSystem(); | |
[TestMethod] | |
public void ShouldReturnTrueIfFileExists() | |
{ | |
CreateFile("testdir/file.txt", "test"); | |
var result = uut.IsFile("testdir/file.txt"); | |
Assert.IsTrue(result); | |
} | |
[TestMethod] | |
public void ShouldReturnFalseIfFileDoesNotExist() | |
{ | |
var result = uut.IsFile("testdir/file.txt"); | |
Assert.IsFalse(result); | |
} | |
[TestMethod] | |
public void ShouldReturnTruefIfDirectoryExists() | |
{ | |
CreateDir("testdir/testdir2"); | |
var result = uut.IsDirectory("testdir/testdir2"); | |
Assert.IsTrue(result); | |
} | |
[TestMethod] | |
public void ShouldReturnFalseIfDirectoryDoesNotExist() | |
{ | |
var result = uut.IsDirectory("testdir/testdir2"); | |
Assert.IsFalse(result); | |
} | |
[TestMethod] | |
public void ShouldReturnFalseIfPathExistsButIsNotADrectory() | |
{ | |
CreateFile("testdir/test", ""); | |
var result = uut.IsDirectory("testdir/test"); | |
Assert.IsFalse(result); | |
} | |
[TestMethod] | |
public void ShouldReturnFalseIfPathExistsButIsNotAFile() | |
{ | |
CreateDir("testdir/test"); | |
var result = uut.IsFile("testdir/test"); | |
Assert.IsFalse(result); | |
} | |
[TestMethod] | |
public void ShouldReturnAllFilesystemEntries() | |
{ | |
CreateDir("testdir/testdirect"); | |
CreateFile("testdir/testfile.txt", ""); | |
var entries = uut.GetEntries("testdir"); | |
Assert.IsNotNull(entries); | |
Assert.AreEqual(2, entries.Count()); | |
Assert.IsTrue(entries.Any(entry => entry.Contains("testdirect"))); | |
Assert.IsTrue(entries.Any(entry => entry.Contains("testfile.txt"))); | |
} | |
[TestMethod] | |
public void ShouldOpenFileToReadCorrectly() | |
{ | |
CreateFile("testdir/testfile.txt", "ShouldOpenFileToReadCorrectly"); | |
using (var reader = new StreamReader(uut.OpenRead("testdir/testfile.txt"))) | |
{ | |
var result = reader.ReadToEnd(); | |
Assert.AreEqual("ShouldOpenFileToReadCorrectly", result); | |
} | |
} | |
public abstract string NormalizePathForFs(string path); | |
[TestMethod] | |
public void ShouldNormalizePathCorrectly() | |
{ | |
var path = "unnormalized/path"; | |
var expected = NormalizePathForFs(path); | |
var actual = uut.NormalizePath(path); | |
Assert.AreEqual(expected, actual); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment