| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,76 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.IO; | ||
| 3 | + using System.Linq; | ||
| 4 | + using Microsoft.Extensions.Configuration; | ||
| 5 | + using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| 6 | + using TugDSC.Configuration.Binder; | ||
| 7 | + using TugDSC.Testing.MSTest; | ||
| 8 | + | ||
| 9 | + namespace TugDSC.Configuration.Tests | ||
| 10 | + { | ||
| 11 | + [TestClass] | ||
| 12 | + public class OfTypeTests | ||
| 13 | + { | ||
| 14 | + public abstract class Type1 | ||
| 15 | + { | ||
| 16 | + public string S1 { get; set; } | ||
| 17 | + | ||
| 18 | + public int I1 { get; set; } | ||
| 19 | + | ||
| 20 | + public bool B1 { get; set; } | ||
| 21 | + | ||
| 22 | + public IValueGetter ValueGetter | ||
| 23 | + { get; set; } | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + public class Type2 : Type1 {} | ||
| 27 | + | ||
| 28 | + public interface IValueGetter | ||
| 29 | + { | ||
| 30 | + string GetValue(); | ||
| 31 | + } | ||
| 32 | + | ||
| 33 | + public class BasicValueGetter : IValueGetter | ||
| 34 | + { | ||
| 35 | + public string Value | ||
| 36 | + { get; set; } | ||
| 37 | + | ||
| 38 | + public string GetValue() | ||
| 39 | + { | ||
| 40 | + return Value; | ||
| 41 | + } | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + public class ToUpperValueGetter : IValueGetter | ||
| 45 | + { | ||
| 46 | + public string Value | ||
| 47 | + { get; set; } | ||
| 48 | + | ||
| 49 | + public string GetValue() | ||
| 50 | + { | ||
| 51 | + return Value?.ToUpper(); | ||
| 52 | + } | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + [TestMethod] | ||
| 56 | + public void TestConfigurationExtendedBinder() | ||
| 57 | + { | ||
| 58 | + var dirPath = Path.GetDirectoryName(typeof(OfTypeTests).Assembly.Location); | ||
| 59 | + var jsonPath = Path.Combine(dirPath, "test1.json"); | ||
| 60 | + | ||
| 61 | + var config = new ConfigurationBuilder() | ||
| 62 | + .AddJsonFile(jsonPath) | ||
| 63 | + .Build(); | ||
| 64 | + | ||
| 65 | + | ||
| 66 | + Assert.That.ThrowsAny(() => config.GetSection("Type1Key0").GetExt<Type1>(), | ||
| 67 | + "cannot bind to interface or abstract without type hint"); | ||
| 68 | + | ||
| 69 | + var key1 = config.GetSection("Type1Key1").GetExt<Type1>(); | ||
| 70 | + var key2 = config.GetSection("Type1Key2").GetExt<Type1>(); | ||
| 71 | + | ||
| 72 | + Assert.AreEqual("FooBar", key1.ValueGetter.GetValue()); | ||
| 73 | + Assert.AreEqual("FOOBAR", key2.ValueGetter.GetValue()); | ||
| 74 | + } | ||
| 75 | + } | ||
| 76 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,26 @@ | |||
| 1 | + <Project Sdk="Microsoft.NET.Sdk"> | ||
| 2 | + | ||
| 3 | + <PropertyGroup> | ||
| 4 | + <TargetFrameworks>netcoreapp2.0</TargetFrameworks> | ||
| 5 | + | ||
| 6 | + <IsPackable>false</IsPackable> | ||
| 7 | + </PropertyGroup> | ||
| 8 | + | ||
| 9 | + <ItemGroup> | ||
| 10 | + <None Include="test1.json" CopyToOutputDirectory="PreserveNewest" /> | ||
| 11 | + </ItemGroup> | ||
| 12 | + | ||
| 13 | + <ItemGroup> | ||
| 14 | + <PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0" /> | ||
| 15 | + <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" /> | ||
| 16 | + <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" /> | ||
| 17 | + <PackageReference Include="MSTest.TestAdapter" Version="1.2.0" /> | ||
| 18 | + <PackageReference Include="MSTest.TestFramework" Version="1.2.0" /> | ||
| 19 | + </ItemGroup> | ||
| 20 | + | ||
| 21 | + <ItemGroup> | ||
| 22 | + <ProjectReference Include="..\..\..\..\src\TugDSC.Abstractions\TugDSC.Abstractions.csproj" /> | ||
| 23 | + <ProjectReference Include="..\..\..\..\src\testing\TugDSC.Testing.MSTest\TugDSC.Testing.MSTest.csproj" /> | ||
| 24 | + </ItemGroup> | ||
| 25 | + | ||
| 26 | + </Project> | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,45 @@ | |||
| 1 | + { | ||
| 2 | + "StrKey": "StrVal", | ||
| 3 | + | ||
| 4 | + "IntKey1": 199, | ||
| 5 | + "IntKey2": "299", | ||
| 6 | + | ||
| 7 | + "BoolKey1": true, | ||
| 8 | + "BoolKey2": "true", | ||
| 9 | + | ||
| 10 | + "StrListKey": [ | ||
| 11 | + "One", | ||
| 12 | + "Two", | ||
| 13 | + "Three" | ||
| 14 | + ], | ||
| 15 | + | ||
| 16 | + "Type1Key0": { | ||
| 17 | + "S1": "s1Value", | ||
| 18 | + "I1": "499", | ||
| 19 | + "B1": "true" | ||
| 20 | + }, | ||
| 21 | + | ||
| 22 | + "Type1Key1": { | ||
| 23 | + "$type": "TugDSC.Configuration.Tests.OfTypeTests+Type2, TugDSC.Configuration-tests", | ||
| 24 | + "S1": "s1Value", | ||
| 25 | + "I1": "499", | ||
| 26 | + "B1": "true", | ||
| 27 | + | ||
| 28 | + "ValueGetter": { | ||
| 29 | + "$type": "TugDSC.Configuration.Tests.OfTypeTests+BasicValueGetter, TugDSC.Configuration-tests", | ||
| 30 | + "Value": "FooBar" | ||
| 31 | + } | ||
| 32 | + }, | ||
| 33 | + | ||
| 34 | + "Type1Key2": { | ||
| 35 | + "$type": "TugDSC.Configuration.Tests.OfTypeTests+Type2, TugDSC.Configuration-tests", | ||
| 36 | + "S1": "s1Value", | ||
| 37 | + "I1": "499", | ||
| 38 | + "B1": "true", | ||
| 39 | + | ||
| 40 | + "ValueGetter": { | ||
| 41 | + "$type": "TugDSC.Configuration.Tests.OfTypeTests+ToUpperValueGetter, TugDSC.Configuration-tests", | ||
| 42 | + "Value": "FooBar" | ||
| 43 | + } | ||
| 44 | + } | ||
| 45 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments