MoonTools.NETPhysFS/UnitTests/Tests.cs

153 lines
4.2 KiB
C#
Raw Permalink Normal View History

2017-05-28 12:47:02 +00:00
using System;
using System.IO;
using System.Linq;
using FluentAssertions;
using NUnit.Framework;
2020-01-18 03:16:09 +00:00
using MoonTools.NETPhysFS;
2017-05-28 12:47:02 +00:00
namespace UnitTests
{
public class Tests
{
[Test]
public void IsInit()
2017-05-28 12:47:02 +00:00
{
using var pfs = new PhysFS("");
Assert.True(pfs.IsInit(), "PhysFS was not initialized");
2017-05-28 12:47:02 +00:00
}
[Theory]
[TestCase(3, 0, 2)]
public void VersionCheck(byte major, byte minor, byte patch)
2017-05-28 12:47:02 +00:00
{
using var pfs = new PhysFS("");
2020-01-18 03:16:09 +00:00
new MoonTools.NETPhysFS.Version() { major = major, minor = minor, patch = patch }.Should().BeEquivalentTo(pfs.GetLinkedVersion());
2017-05-28 12:47:02 +00:00
}
[Test]
public void DirSeparator()
2017-05-28 12:47:02 +00:00
{
using var pfs = new PhysFS("");
pfs.GetDirSeparator().Should().NotBeNullOrEmpty();
2017-05-28 12:47:02 +00:00
}
[Test]
public void PermitSymbolicLinks()
2017-05-28 12:47:02 +00:00
{
using var pfs = new PhysFS("");
pfs.SymbolicLinksPermitted().Should().BeFalse();
pfs.PermitSymbolicLinks(true);
pfs.SymbolicLinksPermitted().Should().BeTrue();
pfs.PermitSymbolicLinks(false);
pfs.SymbolicLinksPermitted().Should().BeFalse();
2017-05-28 12:47:02 +00:00
}
[Test]
public void Mounting()
2017-05-28 12:47:02 +00:00
{
using var pfs = new PhysFS("");
pfs.GetSearchPath().Should().BeEmpty();
pfs.Mount("./", "/", false);
pfs.GetSearchPath().Should().BeEquivalentTo(new string[] { "./" });
pfs.GetMountPoint("./").Should().Be("/");
pfs.IsDirectory("/").Should().BeTrue();
pfs.Mount("../", "foo", true);
pfs.GetSearchPath().Should().BeEquivalentTo(new string[] { "./", "../" });
pfs.GetMountPoint("../").Should().Be("foo/");
pfs.IsDirectory("/foo").Should().BeTrue();
pfs.Mount("../../", "bar", false);
pfs.GetSearchPath().Should().BeEquivalentTo(new string[] { "../../", "./", "../" });
pfs.GetMountPoint("../../").Should().Be("bar/");
pfs.IsDirectory("/bar").Should().BeTrue();
pfs.UnMount("../");
pfs.GetSearchPath().Should().BeEquivalentTo(new string[] { "../../", "./" });
2017-05-28 12:47:02 +00:00
}
[Test]
public void FileEnumeration()
2017-05-28 12:47:02 +00:00
{
using var pfs = new PhysFS("");
pfs.Mount("./", "/", false);
2017-05-28 12:47:02 +00:00
var effectiveFiles = Directory.GetFiles("./").Select(Path.GetFileName).ToArray();
Array.Sort(effectiveFiles);
var enumeratedFiles = pfs.EnumerateFiles("/").ToArray();
Array.Sort(enumeratedFiles);
2017-05-28 12:47:02 +00:00
enumeratedFiles.Should().BeEquivalentTo(effectiveFiles);
2017-05-28 12:47:02 +00:00
}
[Test]
public void DriveEnumeration()
2017-05-28 12:47:02 +00:00
{
using var pfs = new PhysFS("");
var effectiveCdDrives = DriveInfo.GetDrives()
.Where(x => x.DriveType == DriveType.CDRom)
.Select(x => x.RootDirectory.FullName)
2020-01-18 03:16:09 +00:00
.OrderBy(s => s)
.ToArray();
2017-05-28 12:47:02 +00:00
2020-01-18 03:16:09 +00:00
var enumeratedCdDrives = pfs.GetCdRomDirs().OrderBy(s => s);
enumeratedCdDrives.Should().BeEquivalentTo(effectiveCdDrives);
2017-05-28 12:47:02 +00:00
}
[Test]
public void UserDirectory()
2017-05-28 12:47:02 +00:00
{
using var pfs = new PhysFS("");
var userDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
var pfsUserDirectory = pfs.GetUserDir();
Path.GetPathRoot(pfsUserDirectory).Should().Be(Path.GetPathRoot(userDirectory));
2017-05-28 12:47:02 +00:00
}
[Test]
public void DirectoryManipulation()
2017-05-28 12:47:02 +00:00
{
using var pfs = new PhysFS("");
pfs.SetWriteDir("./");
pfs.GetWriteDir().Should().Be("./");
2017-05-28 12:47:02 +00:00
pfs.CreateDirectory("hello");
Directory.Exists("./hello").Should().BeTrue();
2017-05-28 12:47:02 +00:00
pfs.Delete("hello");
Directory.Exists("./hello").Should().BeFalse();
2017-05-28 12:47:02 +00:00
}
[Test]
public void FileManipulation()
2017-05-28 12:47:02 +00:00
{
using var pfs = new PhysFS("");
pfs.SetWriteDir("./");
pfs.Mount("./", "/", true);
using (var sw = new StreamWriter(pfs.OpenWrite("foo")))
{
sw.Write("hello, world! èòàùã こんにちは世界 你好世界");
}
Assert.True(File.Exists("./foo"));
var fileContent = File.ReadAllText("./foo");
using (var sr = new StreamReader(pfs.OpenRead("foo")))
2017-05-28 12:47:02 +00:00
{
sr.ReadToEnd().Should().BeEquivalentTo(fileContent);
2017-05-28 12:47:02 +00:00
}
using (var sw = new StreamWriter(pfs.OpenAppend("foo")))
{
sw.Write("foo");
}
File.ReadAllText("./foo").Should().BeEquivalentTo(fileContent + "foo");
pfs.Delete("foo");
Assert.False(File.Exists("./foo"));
2017-05-28 12:47:02 +00:00
}
}
}