Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(GH-123) Implicit operators added for Maybe and MaybeStruct. #124

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion src/BBT.MaybePattern.Tests/MaybeStructTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ public void Should_Return_False_If_Input_Is_Not_Of_Same_Type()
{
// Arrange
var maybe = Maybe.NoneStruct<BaseStruct>();
var otherType = new BaseStruct();
var otherType = new ReferencedStruct();

// Act
var isEqual = maybe.Equals(otherType);
Expand Down Expand Up @@ -446,6 +446,36 @@ public void Should_Return_True_If_One_Is_Some_And_Other_Is_None_Maybe()
}
}

public sealed class TheImplicitOperator
{
[Fact]
public void Should_Return_Some_MaybeStruct_If_Assigned_With_Some_Struct()
{
// Arrange
var someStruct = new BaseStruct();

// Act
MaybeStruct<BaseStruct> maybeSomeStruct = someStruct;

// Assert
maybeSomeStruct.ShouldBeOfType<MaybeStruct<BaseStruct>>();
maybeSomeStruct.HasValue.ShouldBeTrue();
maybeSomeStruct.ValueOrException().ShouldBe(someStruct);
}

[Fact]
public void Should_Return_None_Maybe_If_Assigned_With_Null()
{
// Act
MaybeStruct<BaseStruct> maybeNone = null;

// Assert
maybeNone.ShouldBeOfType<MaybeStruct<BaseStruct>>();
maybeNone.HasValue.ShouldBeFalse();
maybeNone.ShouldBe(Maybe.NoneStruct<BaseStruct>());
}
}

public sealed class TheGetHashCodeMethod
{
[Fact]
Expand Down
30 changes: 30 additions & 0 deletions src/BBT.MaybePattern.Tests/MaybeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,36 @@ public void Should_Return_True_If_One_Is_Some_And_Other_Is_None_Maybe()
}
}

public sealed class TheImplicitOperator
{
[Fact]
public void Should_Return_Some_Maybe_If_Assigned_With_Some()
{
// Arrange
var some = new BaseClass();

// Act
Maybe<BaseClass> maybeSome = some;

// Assert
maybeSome.ShouldBeOfType<Maybe<BaseClass>>();
maybeSome.HasValue.ShouldBeTrue();
maybeSome.ValueOrException().ShouldBe(some);
}

[Fact]
public void Should_Return_None_Maybe_If_Assigned_With_Null()
{
// Act
Maybe<BaseClass> maybeNone = null;

// Assert
maybeNone.ShouldBeOfType<Maybe<BaseClass>>();
maybeNone.HasValue.ShouldBeFalse();
maybeNone.ShouldBe(Maybe.None<BaseClass>());
}
}

public sealed class TheGetHashCodeMethod
{
[Fact]
Expand Down
6 changes: 6 additions & 0 deletions src/BBT.MaybePattern/Maybe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ internal Maybe(SerializationInfo info, StreamingContext context)
/// </summary>
public bool HasValue => this.value != null;

/// <summary>
/// Implicit conversion from reference type to maybe.
/// </summary>
/// <param name="value">the reference type object or null.</param>
public static implicit operator Maybe<T>(T value) => new Maybe<T>(value);

/// <summary>
/// Checks whether the operands are equal.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions src/BBT.MaybePattern/MaybeStruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ internal MaybeStruct(SerializationInfo info, StreamingContext context)
/// </summary>
public bool HasValue => this.value.HasValue;

/// <summary>
/// Implicit conversion from struct to maybe.
/// </summary>
/// <param name="value">the struct or null.</param>
public static implicit operator MaybeStruct<T>(T? value) => new MaybeStruct<T>(value);

/// <summary>
/// Checks whether the operands are equal.
/// </summary>
Expand Down