2023-12-24 21:11:25 -05:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using FruityFoundation.Base.Structures;
|
2023-08-01 22:12:54 -04:00
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
|
|
|
|
namespace Base.Tests.Structures;
|
|
|
|
|
|
|
|
|
|
public class NullableExtensionTests
|
|
|
|
|
{
|
|
|
|
|
[Test]
|
2024-04-30 13:39:53 -04:00
|
|
|
|
public void TestNullableStructOfNullToMaybe()
|
|
|
|
|
{
|
|
|
|
|
Assert.That(((int?)null).ToMaybe(), Is.EqualTo(Maybe.Empty<int>()));
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-01 22:12:54 -04:00
|
|
|
|
[Test]
|
2024-04-30 13:39:53 -04:00
|
|
|
|
public void TestNullableStructOfValueToMaybe()
|
|
|
|
|
{
|
|
|
|
|
Assert.That(((int?)25).ToMaybe(), Is.EqualTo(Maybe.Create(25)));
|
|
|
|
|
}
|
2023-12-24 21:11:25 -05:00
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestNullableReferenceOfNullAsMaybe()
|
|
|
|
|
{
|
|
|
|
|
string? nullableString = null;
|
|
|
|
|
|
|
|
|
|
var result = nullableString.AsMaybe();
|
|
|
|
|
|
|
|
|
|
Assert.That(result, Is.InstanceOf<Maybe<string>>());
|
|
|
|
|
Assert.That(result.HasValue, Is.False);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestNullableReferenceOfValueAsMaybe()
|
|
|
|
|
{
|
2024-04-30 13:41:02 -04:00
|
|
|
|
// ReSharper disable once SuggestVarOrType_BuiltInTypes
|
|
|
|
|
// ReSharper disable once ConvertToConstant.Local
|
|
|
|
|
// ReSharper disable once VariableCanBeNotNullable
|
2023-12-24 21:11:25 -05:00
|
|
|
|
string? nullableString = "banana";
|
|
|
|
|
|
|
|
|
|
var result = nullableString.AsMaybe();
|
|
|
|
|
|
|
|
|
|
Assert.That(result, Is.InstanceOf<Maybe<string>>());
|
|
|
|
|
Assert.That(result.HasValue, Is.True);
|
|
|
|
|
Assert.That(result.Value, Is.EqualTo("banana"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void TestNullableReferenceAsMaybeWorksWithChoose()
|
|
|
|
|
{
|
|
|
|
|
string?[] stringCollection =
|
|
|
|
|
[
|
|
|
|
|
"banana",
|
|
|
|
|
"bob",
|
|
|
|
|
null
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
var result = stringCollection
|
|
|
|
|
.Choose(x => x.AsMaybe())
|
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
Assert.That(result, Is.InstanceOf<string[]>());
|
|
|
|
|
Assert.That(result, Has.Length.EqualTo(2));
|
|
|
|
|
Assert.That(result[0], Is.EqualTo("banana"));
|
|
|
|
|
Assert.That(result[1], Is.EqualTo("bob"));
|
|
|
|
|
}
|
2023-08-01 22:12:54 -04:00
|
|
|
|
}
|