2023-08-01 22:12:54 -04:00
|
|
|
|
using System;
|
2023-12-19 21:43:00 -05:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.ObjectModel;
|
2023-08-01 22:12:54 -04:00
|
|
|
|
using FruityFoundation.Base.Structures;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
|
|
|
|
namespace Base.Tests.Structures;
|
|
|
|
|
|
|
|
|
|
public class MaybeExtensionTests
|
|
|
|
|
{
|
|
|
|
|
[Test]
|
|
|
|
|
public void EnumerableFirstOrEmptyTests()
|
|
|
|
|
{
|
|
|
|
|
Assert.AreEqual(Maybe.Empty<string>(), Array.Empty<string>().FirstOrEmpty());
|
2024-04-30 13:18:51 -04:00
|
|
|
|
Assert.AreEqual(Maybe.Create<string>("banana"), new[] { "banana" }.FirstOrEmpty());
|
2023-08-01 22:12:54 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestToMaybe()
|
|
|
|
|
{
|
|
|
|
|
Assert.AreEqual(Maybe.Empty<int>(), Maybe.Empty<int>());
|
2024-04-30 13:18:51 -04:00
|
|
|
|
Assert.AreNotEqual(Maybe.Create(293921), Maybe.Create(2));
|
2023-08-01 22:12:54 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void MaybeNullableTests()
|
|
|
|
|
{
|
|
|
|
|
Assert.IsNull(Maybe.Empty<int>().ToNullable());
|
2024-04-30 13:18:51 -04:00
|
|
|
|
Assert.IsNull(Maybe.Create(0, hasValue: _ => false).ToNullable());
|
2023-08-01 22:12:54 -04:00
|
|
|
|
}
|
2023-12-19 21:43:00 -05:00
|
|
|
|
|
|
|
|
|
[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);
|
|
|
|
|
}
|
2023-08-01 22:12:54 -04:00
|
|
|
|
}
|