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

Add Imply() to BooleanAssertions #2074

Merged
merged 20 commits into from Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions Src/FluentAssertions/Primitives/BooleanAssertions.cs
Expand Up @@ -118,6 +118,16 @@ public AndConstraint<TAssertions> NotBe(bool unexpected, string because = "", pa
return new AndConstraint<TAssertions>((TAssertions)this);
}

public AndConstraint<TAssertions> Imply(bool expectation, string because = "", params object[] becauseArgs)
IT-VBFK marked this conversation as resolved.
Show resolved Hide resolved
IT-VBFK marked this conversation as resolved.
Show resolved Hide resolved
{
Execute.Assertion
.ForCondition(!Subject.Value || expectation)
IT-VBFK marked this conversation as resolved.
Show resolved Hide resolved
.BecauseOf(because, becauseArgs)
.FailWith("Expected {context:boolean} ({0}) to imply {1} ({2}){reason}, but it did not.", Subject, nameof(expectation), expectation);
IT-VBFK marked this conversation as resolved.
Show resolved Hide resolved

return new AndConstraint<TAssertions>((TAssertions)this);
}

/// <inheritdoc/>
public override bool Equals(object obj) =>
throw new NotSupportedException("Equals is not part of Fluent Assertions. Did you mean Be() instead?");
Expand Down
Expand Up @@ -1816,6 +1816,7 @@ namespace FluentAssertions.Primitives
public FluentAssertions.AndConstraint<TAssertions> BeFalse(string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> BeTrue(string because = "", params object[] becauseArgs) { }
public override bool Equals(object obj) { }
public FluentAssertions.AndConstraint<TAssertions> Imply(bool expectation, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotBe(bool unexpected, string because = "", params object[] becauseArgs) { }
}
public class DateTimeAssertions : FluentAssertions.Primitives.DateTimeAssertions<FluentAssertions.Primitives.DateTimeAssertions>
Expand Down
Expand Up @@ -1841,6 +1841,7 @@ namespace FluentAssertions.Primitives
public FluentAssertions.AndConstraint<TAssertions> BeFalse(string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> BeTrue(string because = "", params object[] becauseArgs) { }
public override bool Equals(object obj) { }
public FluentAssertions.AndConstraint<TAssertions> Imply(bool expectation, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotBe(bool unexpected, string because = "", params object[] becauseArgs) { }
}
public class DateOnlyAssertions : FluentAssertions.Primitives.DateOnlyAssertions<FluentAssertions.Primitives.DateOnlyAssertions>
Expand Down
Expand Up @@ -1816,6 +1816,7 @@ namespace FluentAssertions.Primitives
public FluentAssertions.AndConstraint<TAssertions> BeFalse(string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> BeTrue(string because = "", params object[] becauseArgs) { }
public override bool Equals(object obj) { }
public FluentAssertions.AndConstraint<TAssertions> Imply(bool expectation, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotBe(bool unexpected, string because = "", params object[] becauseArgs) { }
}
public class DateTimeAssertions : FluentAssertions.Primitives.DateTimeAssertions<FluentAssertions.Primitives.DateTimeAssertions>
Expand Down
Expand Up @@ -1816,6 +1816,7 @@ namespace FluentAssertions.Primitives
public FluentAssertions.AndConstraint<TAssertions> BeFalse(string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> BeTrue(string because = "", params object[] becauseArgs) { }
public override bool Equals(object obj) { }
public FluentAssertions.AndConstraint<TAssertions> Imply(bool expectation, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotBe(bool unexpected, string because = "", params object[] becauseArgs) { }
}
public class DateTimeAssertions : FluentAssertions.Primitives.DateTimeAssertions<FluentAssertions.Primitives.DateTimeAssertions>
Expand Down
Expand Up @@ -1767,6 +1767,7 @@ namespace FluentAssertions.Primitives
public FluentAssertions.AndConstraint<TAssertions> BeFalse(string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> BeTrue(string because = "", params object[] becauseArgs) { }
public override bool Equals(object obj) { }
public FluentAssertions.AndConstraint<TAssertions> Imply(bool expectation, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotBe(bool unexpected, string because = "", params object[] becauseArgs) { }
}
public class DateTimeAssertions : FluentAssertions.Primitives.DateTimeAssertions<FluentAssertions.Primitives.DateTimeAssertions>
Expand Down
Expand Up @@ -1816,6 +1816,7 @@ namespace FluentAssertions.Primitives
public FluentAssertions.AndConstraint<TAssertions> BeFalse(string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> BeTrue(string because = "", params object[] becauseArgs) { }
public override bool Equals(object obj) { }
public FluentAssertions.AndConstraint<TAssertions> Imply(bool expectation, string because = "", params object[] becauseArgs) { }
public FluentAssertions.AndConstraint<TAssertions> NotBe(bool unexpected, string because = "", params object[] becauseArgs) { }
}
public class DateTimeAssertions : FluentAssertions.Primitives.DateTimeAssertions<FluentAssertions.Primitives.DateTimeAssertions>
Expand Down
313 changes: 180 additions & 133 deletions Tests/FluentAssertions.Specs/Primitives/BooleanAssertionSpecs.cs
Expand Up @@ -6,151 +6,198 @@ namespace FluentAssertions.Specs.Primitives;

public class BooleanAssertionSpecs
{
[Fact]
public void Should_succeed_when_asserting_boolean_value_true_is_true()
public class BeTrue
{
// Arrange
bool boolean = true;

// Act / Assert
boolean.Should().BeTrue();
}

[Fact]
public void Should_fail_when_asserting_boolean_value_false_is_true()
{
// Arrange
bool boolean = false;

// Act
Action action = () => boolean.Should().BeTrue();

// Assert
action.Should().Throw<XunitException>();
[Fact]
public void Should_succeed_when_asserting_boolean_value_true_is_true()
{
// Arrange
bool boolean = true;

// Act / Assert
boolean.Should().BeTrue();
}

[Fact]
public void Should_fail_when_asserting_boolean_value_false_is_true()
{
// Arrange
bool boolean = false;

// Act
Action action = () => boolean.Should().BeTrue();

// Assert
action.Should().Throw<XunitException>();
}

[Fact]
public void Should_fail_with_descriptive_message_when_asserting_boolean_value_false_is_true()
{
// Act
Action action = () =>
false.Should().BeTrue("because we want to test the failure {0}", "message");

// Assert
action
.Should().Throw<XunitException>()
.WithMessage("Expected boolean to be true because we want to test the failure message, but found False.");
}
}

[Fact]
public void Should_fail_with_descriptive_message_when_asserting_boolean_value_false_is_true()
public class BeFalse
{
// Act
Action action = () =>
false.Should().BeTrue("because we want to test the failure {0}", "message");

// Assert
action
.Should().Throw<XunitException>()
.WithMessage("Expected boolean to be true because we want to test the failure message, but found False.");
[Fact]
public void Should_succeed_when_asserting_boolean_value_false_is_false()
{
// Act
Action action = () =>
false.Should().BeFalse();

// Assert
action.Should().NotThrow();
}

[Fact]
public void Should_fail_when_asserting_boolean_value_true_is_false()
{
// Act
Action action = () =>
true.Should().BeFalse();

// Assert
action.Should().Throw<XunitException>();
}

[Fact]
public void Should_fail_with_descriptive_message_when_asserting_boolean_value_true_is_false()
{
// Act
Action action = () =>
true.Should().BeFalse("we want to test the failure {0}", "message");

// Assert
action.Should().Throw<XunitException>()
.WithMessage("Expected boolean to be false because we want to test the failure message, but found True.");
}
}

[Fact]
public void Should_succeed_when_asserting_boolean_value_false_is_false()
public class Be
{
// Act
Action action = () =>
false.Should().BeFalse();

// Assert
action.Should().NotThrow();
[Fact]
public void Should_succeed_when_asserting_boolean_value_to_be_equal_to_the_same_value()
{
// Act
Action action = () =>
false.Should().Be(false);

// Assert
action.Should().NotThrow();
}

[Fact]
public void Should_fail_when_asserting_boolean_value_to_be_equal_to_a_different_value()
{
// Act
Action action = () =>
false.Should().Be(true);

// Assert
action.Should().Throw<XunitException>();
}

[Fact]
public void Should_fail_with_descriptive_message_when_asserting_boolean_value_to_be_equal_to_a_different_value()
{
// Act
Action action = () =>
false.Should().Be(true, "because we want to test the failure {0}", "message");

// Assert
action.Should().Throw<XunitException>()
.WithMessage("*Expected*boolean*True*because we want to test the failure message, but found False.*");
}
}

[Fact]
public void Should_fail_when_asserting_boolean_value_true_is_false()
public class NotBe
{
// Act
Action action = () =>
true.Should().BeFalse();

// Assert
action.Should().Throw<XunitException>();
[Fact]
public void Should_succeed_when_asserting_boolean_value_not_to_be_equal_to_the_same_value()
{
// Act
Action action = () =>
true.Should().NotBe(false);

// Assert
action.Should().NotThrow();
}

[Fact]
public void Should_fail_when_asserting_boolean_value_not_to_be_equal_to_a_different_value()
{
// Act
Action action = () =>
true.Should().NotBe(true);

// Assert
action.Should().Throw<XunitException>();
}

[Fact]
public void Should_fail_with_descriptive_message_when_asserting_boolean_value_not_to_be_equal_to_a_different_value()
{
// Act
Action action = () =>
true.Should().NotBe(true, "because we want to test the failure {0}", "message");

// Assert
action.Should().Throw<XunitException>()
.WithMessage("*Expected*boolean*True*because we want to test the failure message, but found True.*");
}

[Fact]
public void Should_throw_a_helpful_error_when_accidentally_using_equals()
{
// Act
Action action = () => true.Should().Equals(true);

// Assert
action.Should().Throw<NotSupportedException>().WithMessage("Equals is not part of Fluent Assertions. Did you mean Be() instead?");
}
}

[Fact]
public void Should_fail_with_descriptive_message_when_asserting_boolean_value_true_is_false()
public class Imply
{
// Act
Action action = () =>
true.Should().BeFalse("we want to test the failure {0}", "message");

// Assert
action.Should().Throw<XunitException>()
.WithMessage("Expected boolean to be false because we want to test the failure message, but found True.");
}

[Fact]
public void Should_succeed_when_asserting_boolean_value_to_be_equal_to_the_same_value()
{
// Act
Action action = () =>
false.Should().Be(false);

// Assert
action.Should().NotThrow();
}

[Fact]
public void Should_fail_when_asserting_boolean_value_to_be_equal_to_a_different_value()
{
// Act
Action action = () =>
false.Should().Be(true);

// Assert
action.Should().Throw<XunitException>();
}

[Fact]
public void Should_fail_with_descriptive_message_when_asserting_boolean_value_to_be_equal_to_a_different_value()
{
// Act
Action action = () =>
false.Should().Be(true, "because we want to test the failure {0}", "message");

// Assert
action.Should().Throw<XunitException>()
.WithMessage("*Expected*boolean*True*because we want to test the failure message, but found False.*");
}

[Fact]
public void Should_succeed_when_asserting_boolean_value_not_to_be_equal_to_the_same_value()
{
// Act
Action action = () =>
true.Should().NotBe(false);

// Assert
action.Should().NotThrow();
}

[Fact]
public void Should_fail_when_asserting_boolean_value_not_to_be_equal_to_a_different_value()
{
// Act
Action action = () =>
true.Should().NotBe(true);

// Assert
action.Should().Throw<XunitException>();
}

[Fact]
public void Should_fail_with_descriptive_message_when_asserting_boolean_value_not_to_be_equal_to_a_different_value()
{
// Act
Action action = () =>
true.Should().NotBe(true, "because we want to test the failure {0}", "message");

// Assert
action.Should().Throw<XunitException>()
.WithMessage("*Expected*boolean*True*because we want to test the failure message, but found True.*");
}

[Fact]
public void Should_throw_a_helpful_error_when_accidentally_using_equals()
{
// Act
Action action = () => true.Should().Equals(true);

// Assert
action.Should().Throw<NotSupportedException>().WithMessage("Equals is not part of Fluent Assertions. Did you mean Be() instead?");
public static TheoryData<bool, bool> PassingImplications => new()
{
{ false, false },
{ false, true },
{ true, true }
};

public static TheoryData<bool, bool> NonPassingImplications => new()
{
{ true, false }
};

[Theory]
[MemberData(nameof(PassingImplications), MemberType = typeof(BooleanAssertionSpecs.Imply))]
IT-VBFK marked this conversation as resolved.
Show resolved Hide resolved
public void A_implies_B(bool a, bool b)
IT-VBFK marked this conversation as resolved.
Show resolved Hide resolved
{
// Act / Assert
a.Should().Imply(b);
}

[Theory]
[MemberData(nameof(NonPassingImplications), MemberType = typeof(BooleanAssertionSpecs.Imply))]
public void A_does_not_imply_B(bool a, bool b)
{
// Act
Action act = () => a.Should().Imply(b);
IT-VBFK marked this conversation as resolved.
Show resolved Hide resolved

// Assert
act.Should().Throw<XunitException>()
.WithMessage("Expected*to imply*but it did not*");
}
}
}