wabbajack/Wabbajack.Paths.Test/AbsolutePathTests.cs

118 lines
3.7 KiB
C#
Raw Normal View History

2021-09-27 12:42:46 +00:00
using System.Linq;
using Xunit;
2021-10-23 16:51:17 +00:00
namespace Wabbajack.Paths.Test;
public class AbsolutePathTests
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
[Fact]
public void CanParsePaths()
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
Assert.Equal(((AbsolutePath) @"c:\foo\bar").ToString(), ((AbsolutePath) @"c:\foo\bar").ToString());
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
[Fact]
public void CanGetParentPath()
{
Assert.Equal(((AbsolutePath) @"c:\foo").ToString(), ((AbsolutePath) @"c:\foo\bar").Parent.ToString());
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
[Fact]
public void ParentOfTopLevelPathThrows()
{
Assert.Throws<PathException>(() => ((AbsolutePath) @"c:\").Parent.ToString());
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
[Fact]
public void CanCreateRelativePathsFromAbolutePaths()
{
Assert.Equal((RelativePath) @"baz\qux.zip",
((AbsolutePath) @"\\foo\bar\baz\qux.zip").RelativeTo((AbsolutePath) @"\\foo\bar"));
Assert.Throws<PathException>(() =>
((AbsolutePath) @"\\foo\bar\baz\qux.zip").RelativeTo((AbsolutePath) @"\\z\bar"));
Assert.Throws<PathException>(() =>
((AbsolutePath) @"\\foo\bar\baz\qux.zip").RelativeTo((AbsolutePath) @"\\z\bar\buz"));
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
[Fact]
public void PathsAreEquatable()
{
Assert.Equal((AbsolutePath) @"c:\foo", (AbsolutePath) @"c:\foo");
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
Assert.True((AbsolutePath) @"c:\foo" == (AbsolutePath) @"c:\Foo");
Assert.False((AbsolutePath) @"c:\foo" != (AbsolutePath) @"c:\Foo");
Assert.NotEqual((AbsolutePath) @"c:\foo", (AbsolutePath) @"c:\bar");
Assert.NotEqual((AbsolutePath) @"c:\foo\bar", (AbsolutePath) @"c:\foo");
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
[Fact]
public void CanGetPathHashCodes()
{
Assert.Equal(@"c:\foo\bar.baz".ToAbsolutePath().GetHashCode(),
@"C:\Foo\Bar.bAz".ToAbsolutePath().GetHashCode());
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
[Fact]
public void CaseInsensitiveEquality()
{
Assert.Equal(@"c:\foo\bar.baz".ToAbsolutePath(), @"C:\Foo\Bar.bAz".ToAbsolutePath());
Assert.NotEqual(@"c:\foo\bar.baz".ToAbsolutePath(), (object) 42);
}
[Fact]
public void CanReplaceExtensions()
{
Assert.Equal(new Extension(".dds"), ((AbsolutePath) @"/foo/bar.dds").Extension);
Assert.Equal((RelativePath) "bar.dds", ((AbsolutePath) @"/foo/bar.dds").FileName);
Assert.Equal((AbsolutePath) @"/foo/bar.zip",
((AbsolutePath) @"/foo/bar.dds").ReplaceExtension(new Extension(".zip")));
Assert.Equal((AbsolutePath) @"/foo\bar.zip",
((AbsolutePath) @"/foo\bar").ReplaceExtension(new Extension(".zip")));
}
[Fact]
public void CanGetPathFormats()
{
Assert.Equal(PathFormat.Windows, ((AbsolutePath) @"c:\foo\bar").PathFormat);
Assert.Equal(PathFormat.Windows, ((AbsolutePath) @"\\foo\bar").PathFormat);
Assert.Equal(PathFormat.Unix, ((AbsolutePath) @"/foo/bar").PathFormat);
Assert.Throws<PathException>(() => ((AbsolutePath) @"c!\foo/bar").PathFormat);
}
[Fact]
public void CanCombinePaths()
{
Assert.Equal("/foo/bar/baz/qux",
((AbsolutePath) "/").Combine("foo", (RelativePath) "bar", "baz/qux").ToString());
Assert.Throws<PathException>(() => ((AbsolutePath) "/").Combine(42));
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
[Fact]
public void CanConvertPathsToStrings()
{
Assert.Equal("/foo/bar", ((AbsolutePath) "/foo/bar").ToString());
}
[Fact]
public void PathsAreComparable()
{
var data = new[]
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
(AbsolutePath) @"c:\a",
(AbsolutePath) @"c:\b\c",
(AbsolutePath) @"c:\d\e\f",
(AbsolutePath) @"c:\b"
};
var data2 = data.OrderBy(a => a).ToArray();
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
var data3 = new[]
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
(AbsolutePath) @"c:\a",
(AbsolutePath) @"c:\b",
(AbsolutePath) @"c:\b\c",
(AbsolutePath) @"c:\d\e\f"
};
Assert.Equal(data3, data2);
2021-09-27 12:42:46 +00:00
}
}