FruityFoundation/Base.Tests/Structures/MaybeTests.cs
Kyle Ratti 479b30b60a
BREAKING CHANGE: chore: rename Maybe.Just to Maybe.Create
This was a bad AI suggestion and I regret the decision.
2024-04-30 13:18:51 -04:00

152 lines
3.5 KiB
C#

using FruityFoundation.Base.Structures;
using NUnit.Framework;
namespace Base.Tests.Structures;
public class MaybeTests
{
[Test]
public void TestBind_DoesNotBindEmptyValue()
{
var emptyMaybe = Maybe.Empty<int>();
var result = emptyMaybe.Bind(x => Maybe.Create(x + 1));
Assert.That(result, Is.InstanceOf<Maybe<int>>());
Assert.That(result.HasValue, Is.False);
}
[Test]
public void TestBind_BindsWhenHasValue()
{
var emptyMaybe = Maybe.Create<int>(1);
var result = emptyMaybe.Bind(x => Maybe.Create(x + 1));
Assert.That(result, Is.InstanceOf<Maybe<int>>());
Assert.That(result.HasValue, Is.True);
Assert.That(result.Value, Is.EqualTo(2));
}
[Test]
public void TestBind_BindsWhenHasValue_NewOutputType()
{
var maybe = Maybe.Create(25);
var result = maybe.Bind(x => x switch
{
25 => Maybe.Create("twenty-five"),
_ => Maybe.Empty<string>()
});
Assert.That(result, Is.InstanceOf<Maybe<string>>());
Assert.That(result.HasValue, Is.True);
}
[Test]
public void EmptyBind_DoesNotBindFactory_WhenHasValue()
{
var maybe = Maybe.Create(25);
var result = maybe.EmptyBind(() => Maybe.Create(30));
Assert.That(result, Is.InstanceOf<Maybe<int>>());
Assert.That(result.HasValue, Is.True);
Assert.That(result.Value, Is.EqualTo(25));
}
[Test]
public void EmptyBind_BindsFactory_WhenDoesNotHaveValue()
{
var maybe = Maybe.Empty<int>();
var result = maybe.EmptyBind(() => Maybe.Create(30));
Assert.That(result, Is.InstanceOf<Maybe<int>>());
Assert.That(result.HasValue, Is.True);
Assert.That(result.Value, Is.EqualTo(30));
}
[Test]
public void EmptyBind_DoesNotBindMaybe_WhenHasValue()
{
var maybe = Maybe.Create(25);
var result = maybe.EmptyBind(Maybe.Create(30));
Assert.That(result, Is.InstanceOf<Maybe<int>>());
Assert.That(result.HasValue, Is.True);
Assert.That(result.Value, Is.EqualTo(25));
}
[Test]
public void EmptyBind_BindsMaybe_WhenDoesNotHaveValue()
{
var maybe = Maybe.Empty<int>();
var result = maybe.EmptyBind(Maybe.Create(30));
Assert.That(result, Is.InstanceOf<Maybe<int>>());
Assert.That(result.HasValue, Is.True);
Assert.That(result.Value, Is.EqualTo(30));
}
[Test]
public void Cast_ToNullableReferenceType_ReturnsEmptyMaybe_WhenHasNoValue()
{
// Arrange
var maybe = Maybe.Empty<string>();
// Act
var result = maybe.Cast<string?>();
// Assert
Assert.That(result, Is.InstanceOf<Maybe<string?>>());
Assert.That(result.HasValue, Is.False);
}
[Test]
public void Cast_ToNullableReferenceType_ReturnsMaybe_WhenHasValue()
{
// Arrange
var maybe = Maybe.Create("banana");
// Act
var result = maybe.Cast<string?>();
// Assert
Assert.That(result, Is.InstanceOf<Maybe<string?>>());
Assert.That(result.HasValue, Is.True);
Assert.That(result.Value, Is.EqualTo("banana"));
}
[Test]
public void Cast_ToNullableValueType_ReturnsEmptyMaybe_WhenHasNoValue()
{
// Arrange
var maybe = Maybe.Empty<int>();
// Act
var result = maybe.Cast<string?>();
// Assert
Assert.That(result, Is.InstanceOf<Maybe<string?>>());
Assert.That(result.HasValue, Is.False);
}
[Test]
public void Cast_ToNullableValueType_ReturnsMaybe_WhenHasValue()
{
// Arrange
var maybe = Maybe.Create(25);
// Act
var result = maybe.Cast<int?>();
// Assert
Assert.That(result, Is.InstanceOf<Maybe<int?>>());
Assert.That(result.HasValue, Is.True);
Assert.That(result.Value, Is.EqualTo(25));
}
}