-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from wemogy/main
Release
- Loading branch information
Showing
4 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/Wemogy.Configuration.Tests/ConfigurationExtensionsTests.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Extensions.Configuration; | ||
using Xunit; | ||
|
||
namespace Wemogy.Configuration.Tests; | ||
|
||
public class ConfigurationExtensionsTests | ||
{ | ||
[Fact] | ||
public void GetRequiredValue_ThrowsException_WhenKeyIsMissing() | ||
{ | ||
// Arrange | ||
var configuration = new ConfigurationBuilder().Build(); | ||
|
||
// Act | ||
void Act() => configuration.GetRequiredValue("MissingKey"); | ||
|
||
// Assert | ||
Assert.Throws<InvalidOperationException>((Action)Act); | ||
} | ||
|
||
[Fact] | ||
public void GetRequiredValue_ReturnsValue_WhenKeyIsPresent() | ||
{ | ||
// Arrange | ||
var configuration = new ConfigurationBuilder() | ||
.AddInMemoryCollection(new[] | ||
{ | ||
new KeyValuePair<string, string>("ExistingKey", "ExistingValue") | ||
}) | ||
.Build(); | ||
|
||
// Act | ||
var value = configuration.GetRequiredValue("ExistingKey"); | ||
|
||
// Assert | ||
Assert.Equal("ExistingValue", value); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace Wemogy.Configuration | ||
{ | ||
public static class ConfigurationExtensions | ||
{ | ||
public static string GetRequiredValue(this IConfiguration configuration, string key) | ||
{ | ||
var value = configuration[key]; | ||
|
||
if (string.IsNullOrWhiteSpace(value)) | ||
{ | ||
throw new InvalidOperationException($"Configuration value for key '{key}' is required."); | ||
} | ||
|
||
return value; | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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