2023-01-15 00:19:42 +00:00
|
|
|
using System.Collections.Generic;
|
2022-07-18 22:03:30 +00:00
|
|
|
using System.Linq;
|
|
|
|
|
2022-09-06 05:53:22 +00:00
|
|
|
using MfGames.Gallium;
|
|
|
|
using MfGames.Nitride.IO.Contents;
|
2022-07-18 22:03:30 +00:00
|
|
|
|
|
|
|
using Xunit;
|
|
|
|
using Xunit.Abstractions;
|
|
|
|
|
|
|
|
using Zio;
|
|
|
|
|
2023-01-15 00:19:42 +00:00
|
|
|
namespace MfGames.Nitride.IO.Tests.Paths;
|
2022-07-18 22:03:30 +00:00
|
|
|
|
|
|
|
public class GetEntityByPathTests : NitrideIOTestBase
|
|
|
|
{
|
|
|
|
public GetEntityByPathTests(ITestOutputHelper output)
|
|
|
|
: base(output)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void FoundFile()
|
|
|
|
{
|
|
|
|
// Set up the test.
|
|
|
|
using NitrideIOTestContext context = this.CreateContext();
|
|
|
|
|
|
|
|
// Set up the file.
|
|
|
|
IFileSystem fileSystem = context.FileSystem;
|
|
|
|
|
|
|
|
fileSystem.CreateFile("/b1.txt");
|
|
|
|
fileSystem.CreateFile("/c1.md");
|
|
|
|
|
|
|
|
// Set up the operation.
|
|
|
|
ReadFiles readFiles = context.Resolve<ReadFiles>();
|
|
|
|
|
|
|
|
// Read and replace the paths.
|
2023-01-15 00:19:42 +00:00
|
|
|
IOrderedEnumerable<string> output = NitrideIOEnumerableEntityExtensions
|
|
|
|
.GetEntityByPath(
|
|
|
|
readFiles.WithPattern("/**")
|
|
|
|
.Run(),
|
|
|
|
"/c1.md",
|
|
|
|
out Entity? found)
|
2022-07-18 22:03:30 +00:00
|
|
|
.Select(
|
|
|
|
x => x.Get<UPath>()
|
|
|
|
.ToString())
|
|
|
|
.OrderBy(x => x);
|
|
|
|
|
|
|
|
// Verify the results.
|
|
|
|
Assert.Equal(
|
|
|
|
new[]
|
|
|
|
{
|
|
|
|
"/b1.txt",
|
|
|
|
},
|
|
|
|
output);
|
|
|
|
|
|
|
|
Assert.NotNull(found);
|
|
|
|
Assert.Equal("/c1.md", found!.Get<UPath>());
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void FoundFileAndKeep()
|
|
|
|
{
|
|
|
|
// Set up the test.
|
|
|
|
using NitrideIOTestContext context = this.CreateContext();
|
|
|
|
|
|
|
|
// Set up the file.
|
|
|
|
IFileSystem fileSystem = context.FileSystem;
|
|
|
|
|
|
|
|
fileSystem.CreateFile("/b1.txt");
|
|
|
|
fileSystem.CreateFile("/c1.md");
|
|
|
|
|
|
|
|
// Set up the operation.
|
|
|
|
ReadFiles readFiles = context.Resolve<ReadFiles>();
|
|
|
|
|
|
|
|
// Read and replace the paths.
|
2023-01-15 00:19:42 +00:00
|
|
|
IOrderedEnumerable<string> output = NitrideIOEnumerableEntityExtensions
|
|
|
|
.GetEntityByPath(
|
|
|
|
(IEnumerable<Entity>)readFiles.WithPattern("/**")
|
|
|
|
.Run(),
|
|
|
|
(UPath)"/c1.md",
|
|
|
|
out Entity? found,
|
|
|
|
(IfFoundOutput)IfFoundOutput.ReturnInOutput)
|
2022-07-18 22:03:30 +00:00
|
|
|
.Select(
|
|
|
|
x => x.Get<UPath>()
|
|
|
|
.ToString())
|
|
|
|
.OrderBy(x => x);
|
|
|
|
|
|
|
|
// Verify the results.
|
|
|
|
Assert.Equal(
|
|
|
|
new[]
|
|
|
|
{
|
|
|
|
"/b1.txt",
|
|
|
|
"/c1.md",
|
|
|
|
},
|
|
|
|
output);
|
|
|
|
|
|
|
|
Assert.NotNull(found);
|
|
|
|
Assert.Equal("/c1.md", found!.Get<UPath>());
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void NotFoundFile()
|
|
|
|
{
|
|
|
|
// Set up the test.
|
|
|
|
using NitrideIOTestContext context = this.CreateContext();
|
|
|
|
|
|
|
|
// Set up the file.
|
|
|
|
IFileSystem fileSystem = context.FileSystem;
|
|
|
|
|
|
|
|
fileSystem.CreateFile("/b1.txt");
|
|
|
|
fileSystem.CreateFile("/c1.md");
|
|
|
|
|
|
|
|
// Set up the operation.
|
|
|
|
ReadFiles readFiles = context.Resolve<ReadFiles>();
|
|
|
|
|
|
|
|
// Read and replace the paths.
|
2023-01-15 00:19:42 +00:00
|
|
|
IOrderedEnumerable<string> output = NitrideIOEnumerableEntityExtensions
|
|
|
|
.GetEntityByPath(
|
|
|
|
readFiles.WithPattern("/**")
|
|
|
|
.Run(),
|
|
|
|
"/not-found.md",
|
|
|
|
out Entity? found)
|
2022-07-18 22:03:30 +00:00
|
|
|
.Select(
|
|
|
|
x => x.Get<UPath>()
|
|
|
|
.ToString())
|
|
|
|
.OrderBy(x => x);
|
|
|
|
|
|
|
|
// Verify the results.
|
|
|
|
Assert.Equal(
|
|
|
|
new[]
|
|
|
|
{
|
|
|
|
"/b1.txt",
|
|
|
|
"/c1.md",
|
|
|
|
},
|
|
|
|
output);
|
|
|
|
|
|
|
|
Assert.Null(found);
|
|
|
|
}
|
|
|
|
}
|