feat(cordis): 完成阶段5访问介导与诊断
This commit is contained in:
@@ -250,6 +250,81 @@ namespace ShrinkContext.Tests
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>故意写入未声明键,验证 Provide 运行时契约。</summary>
|
||||
public sealed class UndeclaredProviderComponent : IShrinkComponent
|
||||
{
|
||||
public string Name => "undeclared-provider";
|
||||
public IReadOnlyList<string> Inject => Array.Empty<string>();
|
||||
public IReadOnlyList<string> Provide => Array.Empty<string>();
|
||||
|
||||
public UniTask ApplyAsync(ShrinkCtx ctx, object? config)
|
||||
{
|
||||
ctx.Set("not-declared", 1);
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class MetadataSuffixPolicy : IShrinkCoeffectAccessPolicy
|
||||
{
|
||||
public object? Resolve(ShrinkCoeffectAccessContext context, object? value)
|
||||
{
|
||||
var text = value?.ToString() ?? string.Empty;
|
||||
if (context.TryGetMetadata<string>("prefix", out var prefix))
|
||||
text = prefix + text;
|
||||
return context.TryGetMetadata<string>("suffix", out var suffix) ? text + suffix : text;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class PolicyProviderComponent : IShrinkComponent
|
||||
{
|
||||
private readonly string[] _provide;
|
||||
|
||||
public PolicyProviderComponent(string key)
|
||||
{
|
||||
Key = key;
|
||||
_provide = new[] { key };
|
||||
}
|
||||
|
||||
public string Key { get; }
|
||||
public string Name => "policy-provider";
|
||||
public IReadOnlyList<string> Inject => Array.Empty<string>();
|
||||
public IReadOnlyList<string> Provide => _provide;
|
||||
|
||||
public UniTask ApplyAsync(ShrinkCtx ctx, object? config)
|
||||
{
|
||||
ctx.Set(Key, config?.ToString() ?? "value", new MetadataSuffixPolicy());
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class PolicyConsumerComponent : IShrinkComponent
|
||||
{
|
||||
private readonly string[] _inject;
|
||||
private ShrinkCtx? _ctx;
|
||||
|
||||
public PolicyConsumerComponent(string key)
|
||||
{
|
||||
Key = key;
|
||||
_inject = new[] { key };
|
||||
}
|
||||
|
||||
public string Key { get; }
|
||||
public string Name => "policy-consumer";
|
||||
public int ApplyCount { get; private set; }
|
||||
public IReadOnlyList<string> Inject => _inject;
|
||||
public IReadOnlyList<string> Provide => Array.Empty<string>();
|
||||
|
||||
public UniTask ApplyAsync(ShrinkCtx ctx, object? config)
|
||||
{
|
||||
_ctx = ctx;
|
||||
ApplyCount++;
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
public string Read() => (_ctx ?? throw new InvalidOperationException("Consumer is not active."))
|
||||
.Get<string>(Key);
|
||||
}
|
||||
|
||||
/// <summary>测试辅助:同步等待 UniTask(测试保证全部延续同步完成,无死锁风险)。</summary>
|
||||
public static class TestAwait
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user