83 lines
2.5 KiB
C#
83 lines
2.5 KiB
C#
|
|
using System.Diagnostics;
|
|
using System.Reflection;
|
|
|
|
using CertMgr.Core;
|
|
using CertMgr.Core.Attributes;
|
|
using CertMgr.Core.Jobs;
|
|
using CertMgr.Core.Validation;
|
|
|
|
using NUnit.Framework;
|
|
|
|
namespace certmgrTest;
|
|
|
|
public class PropertyDescriptorTest
|
|
{
|
|
[Test]
|
|
public void First()
|
|
{
|
|
PropertyDescriptor? descriptor = GetProperty(typeof(TestSettings), nameof(TestSettings.StringItems));
|
|
|
|
if (descriptor == null)
|
|
{
|
|
throw new Exception(string.Format("property not found"));
|
|
}
|
|
|
|
IValueValidator? validator = descriptor.CustomValidator;
|
|
}
|
|
|
|
private static PropertyDescriptor? GetProperty(Type settingsType, string name)
|
|
{
|
|
PropertyDescriptor? result = null;
|
|
|
|
foreach (PropertyInfo propertyInfo in settingsType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
|
|
{
|
|
if (propertyInfo.Name != name)
|
|
{
|
|
continue;
|
|
}
|
|
SettingAttribute? settingAttribute = propertyInfo.GetCustomAttribute<SettingAttribute>();
|
|
if (settingAttribute is null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
result = new PropertyDescriptor(propertyInfo, settingAttribute, settingsType);
|
|
break;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
private sealed class TestSettings : JobSettings
|
|
{
|
|
[Setting("string-names", Validator = typeof(StringItemValidator))]
|
|
public IReadOnlyCollection<string>? StringItems { get; set; }
|
|
}
|
|
|
|
private sealed class StringItemValidator : IValueValidator<string>, IValueValidator<IEnumerable<string>>
|
|
{
|
|
public StringItemValidator(string settingName)
|
|
{
|
|
ValueName = settingName;
|
|
}
|
|
|
|
public string ValueName { [DebuggerStepThrough] get; }
|
|
|
|
Task<IValidationResult> IValueValidator<string>.ValidateAsync(string? settingValue, CancellationToken cancellationToken)
|
|
{
|
|
return Task.FromResult((IValidationResult)new ValidationResult(ValueName, true, "OK"));
|
|
}
|
|
|
|
Task<IValidationResult> IValueValidator.ValidateAsync(object? value, CancellationToken cancellationToken)
|
|
{
|
|
return Task.FromResult((IValidationResult)new ValidationResult(ValueName, true, "OK"));
|
|
}
|
|
|
|
Task<IValidationResult> IValueValidator<IEnumerable<string>>.ValidateAsync(IEnumerable<string>? settingValue, CancellationToken cancellationToken)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
}
|