Created
December 19, 2021 12:31
-
-
Save helto4real/3419db0823038a1c1709e5f009cf857c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Hosting; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.Logging; | |
using Microsoft.Extensions.Options; | |
using NetDaemon.AppModel.Common.Extensions; | |
using System.ComponentModel; | |
using System.Globalization; | |
namespace NetDaemon.AppModel.Tests.Config; | |
public class ConfigTests | |
{ | |
/// <summary> | |
/// This test is a showcase how to use custom type converters | |
/// </summary> | |
[Fact] | |
public void TestAddYamlConfigWithTypeConverterGetsSettingsCorrectly() | |
{ | |
// ARRANGE | |
var configurationBuilder = new ConfigurationBuilder() as IConfigurationBuilder; | |
configurationBuilder.AddYamlAppConfig( | |
Path.Combine(AppContext.BaseDirectory, | |
"Config/fixtures")); | |
var configRool = configurationBuilder.Build(); | |
var section = configRool.GetSection("TestConfigConverter"); | |
// ACT | |
var settings = section.Get<TestConvertedSettings>(); | |
// CHECK | |
settings!.Entity!.EntityId | |
.Should() | |
.Be("light.testlight"); | |
} | |
} | |
#region -- Test classes -- | |
internal class InjectMeWithConfigPlease | |
{ | |
public InjectMeWithConfigPlease(IAppConfig<TestSettings> settings) | |
{ | |
Settings = settings; | |
} | |
public IAppConfig<TestSettings> Settings { get; } | |
} | |
internal class TestConvertedSettings | |
{ | |
public TestEntity? Entity { get; set; } = null; | |
} | |
[TypeConverter(typeof(TestTypeConverter))] | |
internal class TestEntity | |
{ | |
public TestEntity( | |
string entityId | |
) | |
{ | |
EntityId = entityId; | |
} | |
public string EntityId { get; } | |
} | |
internal class TestTypeConverter : TypeConverter | |
{ | |
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) | |
{ | |
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); | |
} | |
public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value) | |
{ | |
var stringValue = value as string; | |
if (stringValue is not null) | |
return new TestEntity(stringValue); | |
return base.ConvertFrom(context, culture, value); | |
} | |
public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType) | |
{ | |
var casted = value as TestEntity; | |
if (destinationType == typeof(string) && casted is not null) | |
return casted; | |
return base.ConvertTo(context, culture, value, destinationType); | |
} | |
} | |
#endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment