Skip to content

Commit

Permalink
Add tests for special file
Browse files Browse the repository at this point in the history
  • Loading branch information
withinboredom committed Nov 25, 2019
1 parent fd4d8ef commit e864caf
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions csharp/UnitTests/SpecialFileTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System.IO;
using System.Threading.Tasks;
using keyboards;
using NUnit.Framework;

namespace UnitTests
{
public class SpecialFileTests
{
private static string ExistsPath => Path.GetTempFileName();
private static string NoPermsPath
{
get
{
var path = Path.GetTempFileName();
File.SetAttributes(path, FileAttributes.ReadOnly);
return path;
}
}

private const string NoExistsPath = "noexists";

[Test]
public void CanCheckExists()
{
var exists = new SpecialFile(ExistsPath);
var noExists = new SpecialFile(NoExistsPath);

Assert.IsTrue(exists.Exists);
Assert.IsFalse(noExists.Exists);
}

[Test]
public void HasWritePermissionsWithWritableFile()
{
var canWrite = new SpecialFile(ExistsPath);

Assert.IsTrue(canWrite.HasPermission);
}

[Test]
public void NoPermissionsWithReadOnlyFile()
{
var noWrite = new SpecialFile(NoPermsPath);
Assert.IsFalse(noWrite.HasPermission);
}

[Test]
public void PermissionsToNonExistingFile()
{
var noExists = new SpecialFile(NoExistsPath);
Assert.IsTrue(noExists.HasPermission);
}

[Test]
public async Task CanWriteToRegularFile()
{
var exists = new SpecialFile(Path.GetTempFileName());
await exists.Commit("test");
Assert.AreEqual("test", await exists.Read());
Assert.AreEqual("test", exists.Contents);
Assert.AreEqual("test", (await exists.Lines())[0]);
}

[Test]
public async Task CannotWriteToUnwritableFile()
{
var no = new SpecialFile(NoPermsPath);
await no.Commit("test");
Assert.AreEqual("", no.Contents);
}

[Test]
public void CanDeleteNonExistentFile()
{
Assert.IsFalse(File.Exists(NoExistsPath));
var no = new SpecialFile(NoExistsPath);
no.Delete();
Assert.DoesNotThrow(() => no.Delete());
}

[Test]
public void CanDeleteRegularFile()
{
var path = ExistsPath;
Assert.IsTrue(File.Exists(path));
var file = new SpecialFile(path);
Assert.DoesNotThrow(() => file.Delete());
Assert.IsFalse(File.Exists(path));
}

[Test]
public void CanDeleteReadOnlyFile()
{
var path = NoPermsPath;
Assert.IsTrue(File.Exists(path));
var file = new SpecialFile(path);
Assert.DoesNotThrow(() => file.Delete());
Assert.IsTrue(File.Exists(path));
}
}
}

0 comments on commit e864caf

Please sign in to comment.