This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using Cysharp.Threading.Tasks;
|
||||
|
||||
namespace ShrinkDataSaver.Tests
|
||||
{
|
||||
/// <summary>
|
||||
/// 内存模拟存储,用于单元测试,无需文件 I/O。
|
||||
/// </summary>
|
||||
public class MockStorageProvider : IStorageProvider
|
||||
{
|
||||
private readonly Dictionary<string, byte[]> _store = new();
|
||||
|
||||
public UniTask WriteAsync(string path, byte[] data, CancellationToken ct = default)
|
||||
{
|
||||
_store[NormalizePath(path)] = data;
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
public UniTask<byte[]> ReadAsync(string path, CancellationToken ct = default)
|
||||
{
|
||||
var key = NormalizePath(path);
|
||||
if (!_store.TryGetValue(key, out var data))
|
||||
throw new FileNotFoundException($"MockStorage: {key}");
|
||||
return UniTask.FromResult(data);
|
||||
}
|
||||
|
||||
public UniTask<bool> ExistsAsync(string path, CancellationToken ct = default)
|
||||
=> UniTask.FromResult(_store.ContainsKey(NormalizePath(path)));
|
||||
|
||||
public UniTask DeleteAsync(string path, CancellationToken ct = default)
|
||||
{
|
||||
_store.Remove(NormalizePath(path));
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
public UniTask<string[]> ListAsync(string prefix = "", CancellationToken ct = default)
|
||||
{
|
||||
var norm = NormalizePath(prefix);
|
||||
var results = _store.Keys
|
||||
.Where(k => string.IsNullOrEmpty(norm) || k.StartsWith(norm))
|
||||
.ToArray();
|
||||
return UniTask.FromResult(results);
|
||||
}
|
||||
|
||||
public void Clear() => _store.Clear();
|
||||
public int Count => _store.Count;
|
||||
|
||||
private static string NormalizePath(string path)
|
||||
=> path.Replace('\\', '/');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user