using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using GFramework.Core.Abstractions.Resource;
namespace GFramework.Core.Tests.Resource;
///
/// 为 ResourceManager 测试提供可控数据源的资源加载器。
///
public class TestResourceLoader : IResourceLoader
{
private readonly Dictionary _resourceData = new(StringComparer.Ordinal);
///
/// 同步加载指定路径的测试资源。
///
/// 资源路径。
/// 加载得到的测试资源。
/// 为 。
/// 为空字符串。
/// 指定路径的测试资源不存在。
public TestResource Load(string path)
{
ArgumentException.ThrowIfNullOrEmpty(path);
if (_resourceData.TryGetValue(path, out var content))
{
return new TestResource { Content = content };
}
throw new FileNotFoundException($"Resource not found: {path}");
}
///
/// 异步加载指定路径的测试资源。
///
/// 资源路径。
/// 加载得到的测试资源任务。
/// 为 。
/// 为空字符串。
/// 指定路径的测试资源不存在。
public Task LoadAsync(string path)
{
return Task.FromResult(Load(path));
}
///
/// 卸载已加载的测试资源。
///
/// 要标记为已释放的资源。
/// 为 。
public void Unload(TestResource resource)
{
ArgumentNullException.ThrowIfNull(resource);
resource.IsDisposed = true;
}
///
/// 判断当前加载器是否包含指定路径的测试资源。
///
/// 资源路径。
/// 存在对应测试资源时返回 ;否则返回 。
/// 为 。
/// 为空字符串。
public bool CanLoad(string path)
{
ArgumentException.ThrowIfNullOrEmpty(path);
return _resourceData.ContainsKey(path);
}
///
/// 向测试加载器注册一条可返回的资源数据。
///
/// 资源路径。
/// 资源内容。
///
/// 或 为 。
///
/// 为空字符串。
public void AddTestData(string path, string content)
{
ArgumentException.ThrowIfNullOrEmpty(path);
ArgumentNullException.ThrowIfNull(content);
_resourceData[path] = content;
}
}