diff --git a/StrongPaths.Test/Tests.cs b/StrongPaths.Test/Tests.cs index 286ad3a..f9f9065 100644 --- a/StrongPaths.Test/Tests.cs +++ b/StrongPaths.Test/Tests.cs @@ -1,7 +1,4 @@ namespace ktsu.io.StrongPaths.Test; - -using System.Reflection; - #pragma warning disable CS0219 #pragma warning disable CS1591 @@ -91,13 +88,6 @@ public void TestAnyAbsolutePath() [TestMethod] public void TestAnyDirectoryPath() => _ = (AnyDirectoryPath)FullyQualifiedPath; - [TestMethod] - public void TestAnyFilePath() - { - var path = (AnyFilePath)DotYeet; - _ = Assert.ThrowsException(action: () => _ = (AnyFilePath)Yeet); - } - [TestMethod] public void TestAnyRelativePath() { @@ -135,6 +125,19 @@ public void TestGetContents() { var path = (AnyDirectoryPath)FullyQualifiedPath; var contents = path.Contents; - Assert.IsTrue(contents.Contains((AbsoluteFilePath)Assembly.GetExecutingAssembly().Location)); + var filePath = (AbsoluteDirectoryPath)AppContext.BaseDirectory / (FileName)"ktsu.io.StrongPaths.Test.dll"; + Assert.IsTrue(contents.Contains(filePath)); + } + + [TestMethod] + public void TestWithFilePrefix() + { + var absDirectory = (AbsoluteDirectoryPath)FullyQualifiedPath; + var absFilePath = absDirectory / (FileName)DotYeet; + Assert.IsTrue(absFilePath.WithFilePrefix(Yeet) == absDirectory / (FileName)(Yeet + DotYeet)); + + var relDirectory = (RelativeDirectoryPath)Yeet; + var relFilePath = relDirectory / (FileName)DotYeet; + Assert.IsTrue(relFilePath.WithFilePrefix(Yeet) == relDirectory / (FileName)(Yeet + DotYeet)); } } diff --git a/StrongPaths.Test/Usings.cs b/StrongPaths.Test/Usings.cs deleted file mode 100644 index 540383d..0000000 --- a/StrongPaths.Test/Usings.cs +++ /dev/null @@ -1 +0,0 @@ -global using Microsoft.VisualStudio.TestTools.UnitTesting; diff --git a/StrongPaths/AnyAbsolutePath.cs b/StrongPaths/AnyAbsolutePath.cs index 4c22b9e..935b92d 100644 --- a/StrongPaths/AnyAbsolutePath.cs +++ b/StrongPaths/AnyAbsolutePath.cs @@ -10,6 +10,6 @@ public record AnyAbsolutePath : StrongPathAbstract : AnyAbsolutePath where TDerived : AnyAbsolutePath { - public static explicit operator AnyAbsolutePath(char[]? value) => FromCharArray(value: value); - public static explicit operator AnyAbsolutePath(string? value) => FromString(value: value); + public static explicit operator AnyAbsolutePath(char[]? value) => FromCharArray(value: MakeCanonical(value)); + public static explicit operator AnyAbsolutePath(string? value) => FromString(value: MakeCanonical(value)); } diff --git a/StrongPaths/AnyDirectoryPath.cs b/StrongPaths/AnyDirectoryPath.cs index d1d66c4..c4af0d4 100644 --- a/StrongPaths/AnyDirectoryPath.cs +++ b/StrongPaths/AnyDirectoryPath.cs @@ -39,6 +39,6 @@ public static Collection GetContents(AnyDirectoryPath directory public record AnyDirectoryPath : AnyDirectoryPath where TDerived : AnyDirectoryPath { - public static explicit operator AnyDirectoryPath(char[]? value) => FromCharArray(value: value); - public static explicit operator AnyDirectoryPath(string? value) => FromString(value: value); + public static explicit operator AnyDirectoryPath(char[]? value) => FromCharArray(value: MakeCanonical(value)); + public static explicit operator AnyDirectoryPath(string? value) => FromString(value: MakeCanonical(value)); } diff --git a/StrongPaths/AnyFileName.cs b/StrongPaths/AnyFileName.cs index 64a63fb..49f1778 100644 --- a/StrongPaths/AnyFileName.cs +++ b/StrongPaths/AnyFileName.cs @@ -10,6 +10,6 @@ public record AnyFileName : StrongPathAbstract; public record AnyFileName : AnyFileName where TDerived : AnyFileName { - public static explicit operator AnyFileName(char[]? value) => FromCharArray(value: value); - public static explicit operator AnyFileName(string? value) => FromString(value: value); + public static explicit operator AnyFileName(char[]? value) => FromCharArray(value: MakeCanonical(value)); + public static explicit operator AnyFileName(string? value) => FromString(value: MakeCanonical(value)); } diff --git a/StrongPaths/AnyFilePath.cs b/StrongPaths/AnyFilePath.cs index 56c2472..4f5ceec 100644 --- a/StrongPaths/AnyFilePath.cs +++ b/StrongPaths/AnyFilePath.cs @@ -36,6 +36,6 @@ public FileExtension FileExtension public record AnyFilePath : AnyFilePath where TDerived : AnyFilePath { - public static explicit operator AnyFilePath(char[]? value) => FromCharArray(value: value); - public static explicit operator AnyFilePath(string? value) => FromString(value: value); + public static explicit operator AnyFilePath(char[]? value) => FromCharArray(value: MakeCanonical(value)); + public static explicit operator AnyFilePath(string? value) => FromString(value: MakeCanonical(value)); } diff --git a/StrongPaths/AnyRelativePath.cs b/StrongPaths/AnyRelativePath.cs index 2f02a7c..9dbe9e4 100644 --- a/StrongPaths/AnyRelativePath.cs +++ b/StrongPaths/AnyRelativePath.cs @@ -64,6 +64,6 @@ public static TDest Make(AnyStrongPath from, AnyStrongPath to) public record AnyRelativePath : AnyRelativePath where TDerived : AnyRelativePath { - public static explicit operator AnyRelativePath(char[]? value) => FromCharArray(value: value); - public static explicit operator AnyRelativePath(string? value) => FromString(value: value); + public static explicit operator AnyRelativePath(char[]? value) => FromCharArray(value: MakeCanonical(value)); + public static explicit operator AnyRelativePath(string? value) => FromString(value: MakeCanonical(value)); } diff --git a/StrongPaths/AnyStrongPath.cs b/StrongPaths/AnyStrongPath.cs index d1d567a..00bde6b 100644 --- a/StrongPaths/AnyStrongPath.cs +++ b/StrongPaths/AnyStrongPath.cs @@ -3,6 +3,7 @@ namespace ktsu.io.StrongPaths; using System.Diagnostics.CodeAnalysis; +using ktsu.io.Extensions; using StrongStrings; public abstract record AnyStrongPath : StrongStringAbstract @@ -10,12 +11,23 @@ public abstract record AnyStrongPath : StrongStringAbstract IsDirectory || IsFile; public bool IsDirectory => Directory.Exists(path: WeakString); public bool IsFile => File.Exists(path: WeakString); + public static string MakeCanonical(string? input) + { + ArgumentNullException.ThrowIfNull(input); + return input.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar).RemoveSuffix(new string([Path.DirectorySeparatorChar])); + } + + public static char[]? MakeCanonical(char[]? input) + { + ArgumentNullException.ThrowIfNull(input); + return MakeCanonical(new string(input)).ToCharArray(); + } } [SuppressMessage(category: "Usage", checkId: "CA2225:Operator overloads have named alternates", Justification = "The base class already has these")] public abstract record AnyStrongPath : AnyStrongPath where TDerived : AnyStrongPath { - public static explicit operator AnyStrongPath(char[]? value) => FromCharArray(value: value); - public static explicit operator AnyStrongPath(string? value) => FromString(value: value); + public static explicit operator AnyStrongPath(char[]? value) => FromCharArray(value: MakeCanonical(value)); + public static explicit operator AnyStrongPath(string? value) => FromString(value: MakeCanonical(value)); } diff --git a/StrongPaths/CommonPaths.cs b/StrongPaths/CommonPaths.cs index d4635e2..e3811d2 100644 --- a/StrongPaths/CommonPaths.cs +++ b/StrongPaths/CommonPaths.cs @@ -3,12 +3,13 @@ namespace ktsu.io.StrongPaths; using System.Collections.ObjectModel; +using ktsu.io.Extensions; public sealed record class AbsoluteDirectoryPath : AbsolutePathAbstract { - public static AbsoluteFilePath operator /(AbsoluteDirectoryPath left, FileName right) => (AbsoluteFilePath)$"{left}/{right}"; - public static AbsoluteFilePath operator /(AbsoluteDirectoryPath left, RelativeFilePath right) => (AbsoluteFilePath)$"{left}/{right}"; - public static AbsoluteDirectoryPath operator /(AbsoluteDirectoryPath left, RelativeDirectoryPath right) => (AbsoluteDirectoryPath)$"{left}/{right}"; + public static AbsoluteFilePath operator /(AbsoluteDirectoryPath left, FileName right) => (AbsoluteFilePath)MakeCanonical($"{left}/{right}"); + public static AbsoluteFilePath operator /(AbsoluteDirectoryPath left, RelativeFilePath right) => (AbsoluteFilePath)MakeCanonical($"{left}/{right}"); + public static AbsoluteDirectoryPath operator /(AbsoluteDirectoryPath left, RelativeDirectoryPath right) => (AbsoluteDirectoryPath)MakeCanonical($"{left}/{right}"); public RelativeDirectoryPath RelativeTo(AnyStrongPath other) => AnyRelativePath.Make(from: other, to: this); public Collection Contents => AnyDirectoryPath.GetContents((AnyDirectoryPath)WeakString); public AbsoluteDirectoryPath Parent => (AbsoluteDirectoryPath)Path.GetDirectoryName(WeakString.Trim(Path.DirectorySeparatorChar).Trim(Path.AltDirectorySeparatorChar)); @@ -37,13 +38,19 @@ public FileExtension FileExtension public FileName FileName => (FileName)Path.GetFileName(WeakString); public AbsoluteDirectoryPath DirectoryPath => (AbsoluteDirectoryPath)Path.GetDirectoryName(WeakString); + + public AbsoluteFilePath WithFilePrefix(string filePrefix) + { + string directory = WeakString.RemoveSuffix(FileName); + return (AbsoluteFilePath)$"{directory}{filePrefix}{FileName}"; + } } public sealed record class RelativeDirectoryPath : RelativePathAbstract { - public static RelativeFilePath operator /(RelativeDirectoryPath left, FileName right) => (RelativeFilePath)$"{left}/{right}"; - public static RelativeFilePath operator /(RelativeDirectoryPath left, RelativeFilePath right) => (RelativeFilePath)$"{left}/{right}"; - public static RelativeDirectoryPath operator /(RelativeDirectoryPath left, RelativeDirectoryPath right) => (RelativeDirectoryPath)$"{left}/{right}"; + public static RelativeFilePath operator /(RelativeDirectoryPath left, FileName right) => (RelativeFilePath)MakeCanonical($"{left}/{right}"); + public static RelativeFilePath operator /(RelativeDirectoryPath left, RelativeFilePath right) => (RelativeFilePath)MakeCanonical($"{left}/{right}"); + public static RelativeDirectoryPath operator /(RelativeDirectoryPath left, RelativeDirectoryPath right) => (RelativeDirectoryPath)MakeCanonical($"{left}/{right}"); public RelativeDirectoryPath RelativeTo(AnyStrongPath other) => Make(from: other, to: this); public Collection Contents => AnyDirectoryPath.GetContents((AnyDirectoryPath)WeakString); public RelativeDirectoryPath Parent => (RelativeDirectoryPath)Path.GetDirectoryName(WeakString.Trim(Path.DirectorySeparatorChar).Trim(Path.AltDirectorySeparatorChar)); @@ -73,4 +80,10 @@ public FileExtension FileExtension public FileName FileName => (FileName)Path.GetFileName(WeakString); public RelativeDirectoryPath DirectoryPath => (RelativeDirectoryPath)Path.GetDirectoryName(WeakString); + + public RelativeFilePath WithFilePrefix(string filePrefix) + { + string directory = WeakString.RemoveSuffix(FileName); + return (RelativeFilePath)$"{directory}{filePrefix}{FileName}"; + } } diff --git a/StrongPaths/StrongPaths.csproj b/StrongPaths/StrongPaths.csproj index c2c528b..173f9cf 100644 --- a/StrongPaths/StrongPaths.csproj +++ b/StrongPaths/StrongPaths.csproj @@ -1,6 +1,7 @@ - + + diff --git a/VERSION b/VERSION index b0f3d96..9084fa2 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.0.8 +1.1.0