Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@ private static void BindInstance(
return;
}

if (config == null)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously the entire block below was wrapped in a null check - 35f043f#diff-2e6f75e1d4f577e1c8c1b6a32d158ce7812a38ad8fcd3b4d762157b92c03e890L308-R315

Since this method already has multiple returns, I find it more readable to early-exit rather than count curly-brackets/indentation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good to me.

{
return;
}

var section = config as IConfigurationSection;
string? configValue = section?.Value;
if (configValue != null && TryConvertValue(type, configValue, section?.Path, out object? convertedValue, out Exception? error))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.Extensions.Configuration;
#endif
using Microsoft.Extensions.Configuration.Test;
using Moq;
using Xunit;

namespace Microsoft.Extensions
Expand Down Expand Up @@ -2404,5 +2405,27 @@ public void SharedChildInstance()
config.GetSection("A").Bind(instance);
Assert.Equal("localhost", instance.ConnectionString);
}

[Fact]
public void CanBindToMockConfiugrationSection()
{
double expectedLatitude = 42.0002d;
var mockConfSection = new Mock<IConfigurationSection>();
var latitudeSection = new Mock<IConfigurationSection>();
latitudeSection.Setup(m => m.Value).Returns(expectedLatitude.ToString());

// only mock one of the two properties, the other will return a null section.
// runtime binder uses GetSection
mockConfSection.Setup(config => config.GetSection(nameof(GeolocationClass.Latitude))).Returns(latitudeSection.Object);
// source gen uses indexer
mockConfSection.Setup(config => config[nameof(GeolocationClass.Latitude)]).Returns(latitudeSection.Object?.Value);
mockConfSection.Setup(config => config.GetChildren()).Returns(new[] { latitudeSection.Object });

GeolocationClass result = new();
mockConfSection.Object.Bind(result);

Assert.Equal(expectedLatitude, result.Latitude);
Assert.Equal(default(double), result.Longitude);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Options.ConfigurationExtensions\src\Microsoft.Extensions.Options.ConfigurationExtensions.csproj" SkipUseReferenceAssembly="true" />
<ProjectReference Include="..\..\src\Microsoft.Extensions.Configuration.Binder.csproj" SkipUseReferenceAssembly="true" />
<ProjectReference Include="..\..\gen\Microsoft.Extensions.Configuration.Binder.SourceGeneration.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" />
<PackageReference Include="Moq" Version="$(MoqVersion)" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Configuration.Json\src\Microsoft.Extensions.Configuration.Json.csproj" SkipUseReferenceAssembly="true" />
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.FileProviders.Abstractions\src\Microsoft.Extensions.FileProviders.Abstractions.csproj" SkipUseReferenceAssembly="true" />
<ProjectReference Include="..\..\src\Microsoft.Extensions.Configuration.Binder.csproj" SkipUseReferenceAssembly="true" />
<PackageReference Include="Moq" Version="$(MoqVersion)" />
</ItemGroup>

</Project>