-
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 #22 from futurum-dev/feature/improve-README.md
Improve README.md, Code tidy and Fix issue with 'OptionEnumerableExtensions.Map'
- Loading branch information
Showing
8 changed files
with
848 additions
and
238 deletions.
There are no files selected for viewing
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,42 @@ | ||
namespace Futurum.Core.Option; | ||
|
||
public static partial class OptionDictionaryExtensions | ||
{ | ||
/// <summary> | ||
/// Get the value associated with the specified key as an Option. | ||
/// <list type="bullet"> | ||
/// <item> | ||
/// <description> | ||
/// If the key is found, return the value as <see cref="Option{T}"/>. | ||
/// </description> | ||
/// </item> | ||
/// <item> | ||
/// <description> | ||
/// If the key is not found, return the value as <see cref="Option{T}"/> <see cref="Option{T}.None"/>. | ||
/// </description> | ||
/// </item> | ||
/// </list> | ||
/// </summary> | ||
public static Option<TValue> TryGetValue<TKey, TValue>(this Dictionary<TKey, TValue> source, TKey key) | ||
where TKey : notnull => | ||
source.TryGetValue(key, out var value) ? Option<TValue>.From(value) : Option<TValue>.None; | ||
|
||
/// <summary> | ||
/// Get the value associated with the specified key as an Option. | ||
/// <list type="bullet"> | ||
/// <item> | ||
/// <description> | ||
/// If the key is found, return the value as <see cref="Option{T}"/>. | ||
/// </description> | ||
/// </item> | ||
/// <item> | ||
/// <description> | ||
/// If the key is not found, return the value as <see cref="Option{T}"/> <see cref="Option{T}.None"/>. | ||
/// </description> | ||
/// </item> | ||
/// </list> | ||
/// </summary> | ||
public static Option<TValue> TryGetValue<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> source, TKey key) | ||
where TKey : notnull => | ||
source.TryGetValue(key, out var value) ? Option<TValue>.From(value) : Option<TValue>.None; | ||
} |
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
64 changes: 64 additions & 0 deletions
64
test/Futurum.Core.Tests/Option/OptionTests.Dictionary.TryGetValue.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,64 @@ | ||
using System.Collections.ObjectModel; | ||
using System.Linq; | ||
|
||
using Futurum.Core.Option; | ||
using Futurum.Core.Tests.Helper.Option; | ||
|
||
using Xunit; | ||
|
||
namespace Futurum.Core.Tests.Option; | ||
|
||
public class OptionDictionaryExtensionsTryGetValueTests | ||
{ | ||
private const string ErrorMessage = "ERROR_MESSAGE"; | ||
|
||
public class Dictionary | ||
{ | ||
[Fact] | ||
public void HasNoValue() | ||
{ | ||
var values = Enumerable.Range(0, 10) | ||
.ToDictionary(x => x, x => x); | ||
|
||
var option = values.TryGetValue(100); | ||
|
||
option.ShouldBeHasNoValue(); | ||
} | ||
|
||
[Fact] | ||
public void HasValue() | ||
{ | ||
var values = Enumerable.Range(0, 10) | ||
.ToDictionary(x => x, x => x); | ||
|
||
var option = values.TryGetValue(2); | ||
|
||
option.ShouldBeHasValueWithValue(2); | ||
} | ||
} | ||
|
||
public class IReadOnlyDictionary | ||
{ | ||
[Fact] | ||
public void HasNoValue() | ||
{ | ||
var values = new ReadOnlyDictionary<int, int>(Enumerable.Range(0, 10) | ||
.ToDictionary(x => x, x => x)); | ||
|
||
var option = values.TryGetValue(100); | ||
|
||
option.ShouldBeHasNoValue(); | ||
} | ||
|
||
[Fact] | ||
public void HasValue() | ||
{ | ||
var values = new ReadOnlyDictionary<int, int>(Enumerable.Range(0, 10) | ||
.ToDictionary(x => x, x => x)); | ||
|
||
var option = values.TryGetValue(2); | ||
|
||
option.ShouldBeHasValueWithValue(2); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.