Skip to content

Feature: Add Maybe{T} assertions/test #3

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using CSharpFunctionalExtensions;
using Xunit;

namespace FluentAssertions.CSharpFunctionalExtensions.Spec
{
public class MaybeAssertionsSpec
{
[Fact]
public void When_maybe_is_expected_to_have_value_and_it_does_should_not_throw()
{
Maybe<string> x = "test";

x.Should().HaveValue("test");
}

[Fact]
public void When_maybe_is_expected_to_have_value_and_it_has_wrong_value_should_throw()
{
Maybe<string> x = "oops";

Action act = () => x.Should().HaveValue("test", "it is test");

act.Should().Throw<Exception>().WithMessage($"*value \"test\" because it is test, but with value \"oops\" it*");
}

[Fact]
public void When_maybe_is_expected_to_have_value_and_it_does_not_should_throw()
{
Maybe<string> x = null;

Action act = () => x.Should().HaveValue("test", "it is not None");

act.Should().Throw<Exception>().WithMessage($"*value \"test\" because it is not None*");
}

[Fact]
public void When_maybe_is_expected_to_have_no_value_and_it_has_none_should_not_throw()
{
Maybe<string> x = null;

x.Should().HaveNoValue();
}

[Fact]
public void When_maybe_is_expected_to_have_no_value_and_it_has_one_should_throw()
{
Maybe<string> x = "test";

Action act = () => x.Should().HaveNoValue("it is None");

act.Should().Throw<Exception>().WithMessage($"*maybe to have no value because it is None, but with value \"test\" it*");
}
}
}
51 changes: 51 additions & 0 deletions src/FluentAssertions.CSharpFunctionalExtensions/MaybeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using CSharpFunctionalExtensions;
using FluentAssertions.Execution;
using FluentAssertions.Primitives;

namespace FluentAssertions.CSharpFunctionalExtensions
{
public static class MaybeExtensions
{
public static MaybeAssertions<T> Should<T>(this Maybe<T> instance) => new MaybeAssertions<T>(instance);
}

public class MaybeAssertions<T> : ReferenceTypeAssertions<Maybe<T>, MaybeAssertions<T>>
{
public MaybeAssertions(Maybe<T> instance) => Subject = instance;

protected override string Identifier => "maybe{T}";

public AndConstraint<MaybeAssertions<T>> HaveValue(T value, string because = "", params object[] becauseArgs)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.Given(() => Subject)
.ForCondition(v => v.HasValue)
.FailWith(
"Expected a value {0}{reason}",
_ => value)
.Then
.Given(s => s.Value)
.ForCondition(v => v.Equals(value))
.FailWith(
"Expected {context:maybe} to have value {0}{reason}, but with value {1} it",
_ => value,
v => v);

return new AndConstraint<MaybeAssertions<T>>(this);
}

public AndConstraint<MaybeAssertions<T>> HaveNoValue(string because = "", params object[] becauseArgs)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.Given(() => Subject)
.ForCondition(v => v.HasNoValue)
.FailWith(
"Expected {context:maybe} to have no value{reason}, but with value {0} it",
v => v.HasNoValue ? default : v.Value);

return new AndConstraint<MaybeAssertions<T>>(this);
}
}
}