You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
commander/MockFileSystemService.cs

81 lines
2.9 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

namespace CommanderApp;
public class MockFileSystemService : IFileSystemService
{
public string MockRootPath { get; set; } = "/mock/root";
private readonly Dictionary<string, List<FileSystemItem>> _mockDirectories = new();
public MockFileSystemService()
{
// Инициализируем mock данными
SetupMockData();
}
private void SetupMockData()
{
_mockDirectories["/mock/root"] = new List<FileSystemItem>
{
new FileSystemItem { Name = "Documents", FullName = "/mock/root/Documents", IsDirectory = true },
new FileSystemItem { Name = "Pictures", FullName = "/mock/root/Pictures", IsDirectory = true },
new FileSystemItem { Name = "readme.txt", FullName = "/mock/root/readme.txt", IsDirectory = false },
new FileSystemItem { Name = "..", FullName = "/mock", IsDirectory = true }
};
_mockDirectories["/mock/root/Documents"] = new List<FileSystemItem>
{
new FileSystemItem { Name = "Project1", FullName = "/mock/root/Documents/Project1", IsDirectory = true },
new FileSystemItem { Name = "Project2", FullName = "/mock/root/Documents/Project2", IsDirectory = true },
new FileSystemItem { Name = "notes.txt", FullName = "/mock/root/Documents/notes.txt", IsDirectory = false },
new FileSystemItem { Name = "..", FullName = "/mock/root", IsDirectory = true }
};
_mockDirectories["/mock/root/Pictures"] = new List<FileSystemItem>
{
new FileSystemItem { Name = "vacation.jpg", FullName = "/mock/root/Pictures/vacation.jpg", IsDirectory = false },
new FileSystemItem { Name = "..", FullName = "/mock/root", IsDirectory = true }
};
}
public string GetRootPath()
{
return MockRootPath;
}
public IEnumerable<FileSystemItem> GetDirectoryContents(string path)
{
if (_mockDirectories.ContainsKey(path))
{
return _mockDirectories[path];
}
// Если директории нет в mock данных, возвращаем пустой список с ".."
var parent = GetParentPath(path);
if (parent != null)
{
return new List<FileSystemItem>
{
new FileSystemItem { Name = "..", FullName = parent, IsDirectory = true }
};
}
return Enumerable.Empty<FileSystemItem>();
}
// Метод для добавления mock данных в тестах
public void AddMockDirectory(string path, List<FileSystemItem> items)
{
_mockDirectories[path] = items;
}
private string GetParentPath(string path)
{
var separator = '/';
var lastSeparator = path.LastIndexOf(separator);
if (lastSeparator > 0)
{
return path.Substring(0, lastSeparator);
}
return null;
}
}