0.2.0-dev Download ZIP
86 lines ·3.7 KB Raw
| 1 | using System.Diagnostics.CodeAnalysis; |
| 2 | |
| 3 | namespace EvilEngine.Mounts.Tests; |
| 4 | |
| 5 | public sealed class DirectoryQueryTests |
| 6 | { |
| 7 | private static MemorySource Install() |
| 8 | { |
| 9 | return new MemorySource("KSP", CaseRule.Insensitive) |
| 10 | .Add("GameData/Squad/squadcore.ksp", "UnityFS") |
| 11 | .Add("GameData/Squad/Parts/Aero/airbrake/part.cfg", "PART {}") |
| 12 | .Add("GameData/Squad/Parts/Engine/liquidEngine/part.cfg", "PART {}") |
| 13 | .Add("KSP_Data/level0", "level"); |
| 14 | } |
| 15 | |
| 16 | [Fact] |
| 17 | public void AnswersFromFilesWhenASourceKeepsNoFolderIndex() |
| 18 | { |
| 19 | IFileSource bare = new Bare(Install()); |
| 20 | |
| 21 | Assert.True(bare.DirectoryExists(VirtualPath.Root)); |
| 22 | Assert.True(bare.DirectoryExists("gamedata/squad/parts")); |
| 23 | Assert.False(bare.DirectoryExists("GameData/Squad/squadcore.ksp")); |
| 24 | Assert.False(bare.DirectoryExists("Ships")); |
| 25 | Assert.Equal(["GameData", "KSP_Data"], bare.EnumerateDirectories(VirtualPath.Root).Select(path => path.Value)); |
| 26 | Assert.Equal(["GameData/Squad/Parts/Aero", "GameData/Squad/Parts/Engine"], |
| 27 | bare.EnumerateDirectories("GameData/Squad/Parts").Select(path => path.Value)); |
| 28 | Assert.Empty(bare.EnumerateDirectories("KSP_Data")); |
| 29 | } |
| 30 | |
| 31 | [Fact] |
| 32 | public void ListsFoldersAboveAtAndBelowAPlacedTree() |
| 33 | { |
| 34 | IFileSource placed = new PlacedSource(new SubtreeSource(Install(), "GameData/Squad"), "GameData/Squad"); |
| 35 | |
| 36 | Assert.True(placed.DirectoryExists(VirtualPath.Root)); |
| 37 | Assert.True(placed.DirectoryExists("GameData")); |
| 38 | Assert.True(placed.DirectoryExists("gamedata/squad")); |
| 39 | Assert.True(placed.DirectoryExists("GameData/Squad/Parts/Aero")); |
| 40 | Assert.False(placed.DirectoryExists("GameData/Squad/Parts/Command")); |
| 41 | Assert.False(placed.DirectoryExists("KSP_Data")); |
| 42 | Assert.Equal(["GameData"], placed.EnumerateDirectories(VirtualPath.Root).Select(path => path.Value)); |
| 43 | Assert.Equal(["GameData/Squad"], placed.EnumerateDirectories("GameData").Select(path => path.Value)); |
| 44 | Assert.Equal(["GameData/Squad/Parts"], |
| 45 | placed.EnumerateDirectories("GameData/Squad").Select(path => path.Value)); |
| 46 | Assert.Equal(["GameData/Squad/Parts/Aero", "GameData/Squad/Parts/Engine"], |
| 47 | placed.EnumerateDirectories("GameData/Squad/Parts").Select(path => path.Value)); |
| 48 | Assert.Empty(placed.EnumerateDirectories("KSP_Data")); |
| 49 | } |
| 50 | |
| 51 | [Fact] |
| 52 | public void ListsFoldersInsideASubtreeAndALazySource() |
| 53 | { |
| 54 | IFileSource subtree = new SubtreeSource(Install(), "GameData"); |
| 55 | var lazy = new LazySource("lazy", CaseRule.Insensitive, Install); |
| 56 | |
| 57 | Assert.Equal(["Squad"], subtree.EnumerateDirectories(VirtualPath.Root).Select(path => path.Value)); |
| 58 | Assert.Equal(["Squad/Parts"], subtree.EnumerateDirectories("Squad").Select(path => path.Value)); |
| 59 | Assert.True(subtree.DirectoryExists("Squad/Parts/Engine")); |
| 60 | Assert.False(lazy.IsOpen); |
| 61 | Assert.True(lazy.DirectoryExists("KSP_Data")); |
| 62 | Assert.Equal(["GameData", "KSP_Data"], lazy.EnumerateDirectories(VirtualPath.Root).Select(path => path.Value)); |
| 63 | } |
| 64 | |
| 65 | private sealed class Bare(IFileSource inner) : IFileSource |
| 66 | { |
| 67 | public string Location => inner.Location; |
| 68 | |
| 69 | public CaseRule CaseRule => inner.CaseRule; |
| 70 | |
| 71 | public bool Exists(VirtualPath path) |
| 72 | { |
| 73 | return inner.Exists(path); |
| 74 | } |
| 75 | |
| 76 | public bool TryOpen(VirtualPath path, [NotNullWhen(true)] out Stream? stream) |
| 77 | { |
| 78 | return inner.TryOpen(path, out stream); |
| 79 | } |
| 80 | |
| 81 | public IEnumerable<FileEntry> Enumerate(VirtualPath directory, bool recursive) |
| 82 | { |
| 83 | return inner.Enumerate(directory, recursive); |
| 84 | } |
| 85 | } |
| 86 | } |