fix: rename IDictionary.TryGet to TryGetValue

This resolves ambiguous invocation errors
This commit is contained in:
Kyle Ratti 2023-12-19 21:43:00 -05:00
parent d264505649
commit b958542534
No known key found for this signature in database
GPG Key ID: 4D429B6287C68DD9
2 changed files with 71 additions and 1 deletions

View File

@ -1,4 +1,6 @@
using System; using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using FruityFoundation.Base.Structures; using FruityFoundation.Base.Structures;
using NUnit.Framework; using NUnit.Framework;
@ -26,4 +28,72 @@ public class MaybeExtensionTests
Assert.IsNull(Maybe.Empty<int>().ToNullable()); Assert.IsNull(Maybe.Empty<int>().ToNullable());
Assert.IsNull(Maybe.Just(0, hasValue: _ => false).ToNullable()); Assert.IsNull(Maybe.Just(0, hasValue: _ => false).ToNullable());
} }
[Test]
public void IDictionaryTests_TryGetValue_ReturnsMaybeWithValue_ForValidKey()
{
var dict = new Dictionary<string, int>
{
{ "one", 1 },
{ "two", 2 },
{ "three", 3 }
};
var result = dict.TryGetValue("one");
Assert.That(result, Is.InstanceOf<Maybe<int>>());
Assert.That(result.HasValue, Is.True);
Assert.That(result.Value, Is.EqualTo(1));
}
[Test]
public void IDictionaryTests_TryGetValue_ReturnsEmptyMaybe_ForInvalidKey()
{
var dict = new Dictionary<string, int>
{
{ "one", 1 },
{ "two", 2 },
{ "three", 3 }
};
var result = dict.TryGetValue("eight");
Assert.That(result, Is.InstanceOf<Maybe<int>>());
Assert.That(result.HasValue, Is.False);
}
[Test]
public void IReadOnlyDictionaryTests_TryGetValue_ReturnsMaybeWithValue_ForValidKey()
{
var baseDict = new Dictionary<string, int>
{
{ "one", 1 },
{ "two", 2 },
{ "three", 3 }
};
IReadOnlyDictionary<string, int> dict = new ReadOnlyDictionary<string, int>(baseDict);
var result = dict.TryGet("one");
Assert.That(result, Is.InstanceOf<Maybe<int>>());
Assert.That(result.HasValue, Is.True);
Assert.That(result.Value, Is.EqualTo(1));
}
[Test]
public void IIReadOnlyDictionaryTests_TryGetValue_ReturnsEmptyMaybe_ForInvalidKey()
{
var baseDict = new Dictionary<string, int>
{
{ "one", 1 },
{ "two", 2 },
{ "three", 3 }
};
IReadOnlyDictionary<string, int> dict = new ReadOnlyDictionary<string, int>(baseDict);
var result = dict.TryGet("eight");
Assert.That(result, Is.InstanceOf<Maybe<int>>());
Assert.That(result.HasValue, Is.False);
}
} }

View File

@ -22,7 +22,7 @@ public static class MaybeExtensions
return Maybe.Empty<T>(); return Maybe.Empty<T>();
} }
public static Maybe<TValue> TryGet<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key) => public static Maybe<TValue> TryGetValue<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key) =>
dict.TryGetValue(key, out var value) ? Maybe.Just(value) : Maybe.Empty<TValue>(); dict.TryGetValue(key, out var value) ? Maybe.Just(value) : Maybe.Empty<TValue>();
public static Maybe<TValue> TryGet<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dict, TKey key) => public static Maybe<TValue> TryGet<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dict, TKey key) =>