Skip to content

Commit 818e996

Browse files
committed
Load Enums By Name
Resolve lawrence-laz#2.
1 parent b2e2889 commit 818e996

File tree

6 files changed

+106
-1
lines changed

6 files changed

+106
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<SolutionConfiguration>
2+
<Settings>
3+
<AllowParallelTestExecution>True</AllowParallelTestExecution>
4+
<InstrumentationMode>Optimised</InstrumentationMode>
5+
<SolutionConfigured>True</SolutionConfigured>
6+
</Settings>
7+
</SolutionConfiguration>

src/Extensions.Configuration.Object/ObjectConfigurationProvider.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ private void LoadRecursively(string currentKey, object section)
4343
{
4444
base.Set(currentKey, section.ToString());
4545
}
46+
else if (section is Enum) // Enum.
47+
{
48+
base.Set(currentKey, Enum.GetName(section.GetType(), section));
49+
}
4650
else if (section is IDictionary dictionarySection) // Dictionary.
4751
{
4852
foreach (DictionaryEntry item in dictionarySection)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using AutoFixture.Xunit2;
2+
using FluentAssertions;
3+
using Microsoft.Extensions.Configuration;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using Microsoft.Extensions.Options;
6+
using Xunit;
7+
8+
namespace Extensions.Configuration.Object.UnitTests
9+
{
10+
public class EnumTest
11+
{
12+
public enum TestEnum
13+
{
14+
Undefined = 0,
15+
Foo,
16+
Bar
17+
}
18+
19+
public class TestOptions
20+
{
21+
public static string Test => "Test";
22+
23+
public TestEnum[] Property { get; set; }
24+
}
25+
26+
[Theory, AutoData]
27+
public void AddObject_WithEnum_ShoulBeSameInOptions(TestOptions expected)
28+
{
29+
// Arrange
30+
var configuration = new ConfigurationBuilder()
31+
.AddObject(expected)
32+
.Build();
33+
34+
var serviceProvider = new ServiceCollection()
35+
.Configure<TestOptions>(configuration)
36+
.BuildServiceProvider();
37+
38+
// Act
39+
var actual = serviceProvider.GetRequiredService<IOptions<TestOptions>>().Value;
40+
41+
// Assert
42+
actual.Should().BeEquivalentTo(expected);
43+
}
44+
}
45+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using FluentAssertions;
2+
using Microsoft.Extensions.Configuration;
3+
using Xunit;
4+
5+
namespace Extensions.Configuration.Object.UnitTests
6+
{
7+
public class NullValueTest
8+
{
9+
class TestOptions
10+
{
11+
public object Actual { get; set; }
12+
}
13+
14+
[Fact]
15+
public void AddObject_WithFieldNullValue_ShouldReturnNull()
16+
{
17+
// Arrange
18+
var configuration = new ConfigurationBuilder()
19+
.AddObject(new TestOptions { Actual = null })
20+
.Build();
21+
22+
// Act
23+
var actual = configuration["Actual"];
24+
25+
// Assert
26+
actual.Should().BeNull();
27+
}
28+
}
29+
}

test/Extensionsions.Configuration.Object.UnitTests/UnexpectedTest.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,23 @@ public void AddObject_WithNull_ShouldThrow()
1515
sut.Invoking(x => x.AddObject(null))
1616
.Should().ThrowExactly<ArgumentNullException>();
1717
}
18+
19+
[Fact]
20+
public void AddObject_OnNullConfigurationBuilder_ShouldThrow()
21+
{
22+
ConfigurationBuilder sut = null;
23+
24+
sut.Invoking(x => x.AddObject(default))
25+
.Should().ThrowExactly<ArgumentNullException>();
26+
}
27+
28+
[Fact]
29+
public void Build_WithConfigurationObjectNotSet_ShouldThrow()
30+
{
31+
var sut = new ObjectConfigurationSource();
32+
33+
sut.Invoking(x => x.Build(default))
34+
.Should().ThrowExactly<ArgumentNullException>();
35+
}
1836
}
1937
}

test/Extensionsions.Configuration.Object.UnitTests/UnitTests.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
<ItemGroup>
1111
<PackageReference Include="AutoFixture.Xunit2" Version="4.14.0" />
1212
<PackageReference Include="FluentAssertions" Version="5.10.3" />
13-
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.8" />
13+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.9" />
14+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.9" />
15+
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.1.9" />
1416
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
1517
<PackageReference Include="xunit" Version="2.4.1" />
1618
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">

0 commit comments

Comments
 (0)