Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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 @@ -38,11 +38,11 @@ public ChainedConfigurationProvider(ChainedConfigurationSource source)
/// </summary>
/// <param name="key">The key.</param>
/// <param name="value">When this method returns, contains the value.</param>
/// <returns><see langword="true"/> if a value for the specified key was found, otherwise <see langword="false"/>.</returns>
/// <returns><see langword="true"/> if the chained configuration has a non-<see langword="null"/> value for the specified key, otherwise <see langword="false"/>.</returns>
public bool TryGet(string key, out string? value)
{
value = _config[key];
return !string.IsNullOrEmpty(value);
return value is not null;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,38 @@ public void ChainedConfiguration_ReloadingOuterConfigurationRoot_RaisesSingleOut
Assert.Equal(0, innerNotifications);
}

[Fact]
public void ChainedConfiguration_TryGetReturnsTrueForEmptyStringValue()
{
IConfigurationProvider provider = new ChainedConfigurationSource
{
Configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string> { { "Key", string.Empty } })
.Build(),
ShouldDisposeConfiguration = false,
}
.Build(new ConfigurationBuilder());

Assert.True(provider.TryGet("Key", out string? value));
Assert.Equal(string.Empty, value);
}

[Fact]
public void ChainedConfiguration_TryGetReturnsFalseForMissingKey()
{
IConfigurationProvider provider = new ChainedConfigurationSource
{
Configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string> { { "OtherKey", "inner-value" } })
.Build(),
ShouldDisposeConfiguration = false,
}
.Build(new ConfigurationBuilder());

Assert.False(provider.TryGet("MissingKey", out string? value));
Assert.Null(value);
}

private class TestConfigurationProvider : ConfigurationProvider
{
public TestConfigurationProvider(string key, string value)
Expand Down
Loading