Skip to content

Commit

Permalink
Add test coverage for OccurrenceConstraint
Browse files Browse the repository at this point in the history
  • Loading branch information
eNeRGy164 committed Mar 17, 2022
1 parent 1cd3a08 commit ba5e038
Showing 1 changed file with 383 additions and 0 deletions.
383 changes: 383 additions & 0 deletions Tests/FluentAssertions.Specs/OccurrenceConstraintSpecs.cs
@@ -0,0 +1,383 @@
using System;
using FluentAssertions.Execution;
using Xunit;
using Xunit.Sdk;

namespace FluentAssertions.Specs
{
public class OccurrenceConstraintSpecs
{
#region At Least

[InlineData(0, true)]
[InlineData(1, false)]
[Theory]
public void At_least_once_should_be_bigger_than_zero(int actual, bool shouldThrow)
{
// Arrange
OccurrenceConstraint constraint = AtLeast.Once();

// Act
Action act = () => Execute.Assertion
.ForConstraint(constraint, actual)
.FailWith($"Expected occurrence to be {constraint.Mode} {constraint.ExpectedCount}, but it was {actual}");

// Assert
if (shouldThrow)
{
act.Should().Throw<XunitException>()
.WithMessage("Expected occurrence to be at least 1, but it was *");
}
else
{
act();
}
}

[InlineData(1, true)]
[InlineData(2, false)]
[Theory]
public void At_least_twice_should_be_bigger_than_one(int actual, bool shouldThrow)
{
// Arrange
OccurrenceConstraint constraint = AtLeast.Twice();

// Act
Action act = () => Execute.Assertion
.ForConstraint(constraint, actual)
.FailWith($"Expected occurrence to be {constraint.Mode} {constraint.ExpectedCount}, but it was {actual}");

// Assert
if (shouldThrow)
{
act.Should().Throw<XunitException>()
.WithMessage("Expected occurrence to be at least 2, but it was *");
}
else
{
act();
}
}

[InlineData(2, true)]
[InlineData(3, false)]
[Theory]
public void At_least_thrice_should_be_bigger_than_two(int actual, bool shouldThrow)
{
// Arrange
OccurrenceConstraint constraint = AtLeast.Thrice();

// Act
Action act = () => Execute.Assertion
.ForConstraint(constraint, actual)
.FailWith($"Expected occurrence to be {constraint.Mode} {constraint.ExpectedCount}, but it was {actual}");

// Assert
if (shouldThrow)
{
act.Should().Throw<XunitException>()
.WithMessage("Expected occurrence to be at least 3, but it was *");
}
else
{
act();
}
}

#endregion

#region At Most

[InlineData(1, false)]
[InlineData(2, true)]
[Theory]
public void At_most_once_should_be_smaller_than_two(int actual, bool shouldThrow)
{
// Arrange
OccurrenceConstraint constraint = AtMost.Once();

// Act
Action act = () => Execute.Assertion
.ForConstraint(constraint, actual)
.FailWith($"Expected occurrence to be {constraint.Mode} {constraint.ExpectedCount}, but it was {actual}");

// Assert
if (shouldThrow)
{
act.Should().Throw<XunitException>()
.WithMessage("Expected occurrence to be at most 1, but it was *");
}
else
{
act();
}
}

[InlineData(2, false)]
[InlineData(3, true)]
[Theory]
public void At_most_twice_should_be_smaller_than_three(int actual, bool shouldThrow)
{
// Arrange
OccurrenceConstraint constraint = AtMost.Twice();

// Act
Action act = () => Execute.Assertion
.ForConstraint(constraint, actual)
.FailWith($"Expected occurrence to be {constraint.Mode} {constraint.ExpectedCount}, but it was {actual}");

// Assert
if (shouldThrow)
{
act.Should().Throw<XunitException>()
.WithMessage("Expected occurrence to be at most 2, but it was *");
}
else
{
act();
}
}

[InlineData(3, false)]
[InlineData(4, true)]
[Theory]
public void At_most_thrice_should_be_smaller_than_four(int actual, bool shouldThrow)
{
// Arrange
OccurrenceConstraint constraint = AtMost.Thrice();

// Act
Action act = () => Execute.Assertion
.ForConstraint(constraint, actual)
.FailWith($"Expected occurrence to be {constraint.Mode} {constraint.ExpectedCount}, but it was {actual}");

// Assert
if (shouldThrow)
{
act.Should().Throw<XunitException>()
.WithMessage("Expected occurrence to be at most 3, but it was *");
}
else
{
act();
}
}

#endregion

#region Exactly

[InlineData(0, true)]
[InlineData(1, false)]
[InlineData(2, true)]
[Theory]
public void Exactly_once_should_be_one(int actual, bool shouldThrow)
{
// Arrange
OccurrenceConstraint constraint = Exactly.Once();

// Act
Action act = () => Execute.Assertion
.ForConstraint(constraint, actual)
.FailWith($"Expected occurrence to be {constraint.Mode} {constraint.ExpectedCount}, but it was {actual}");

// Assert
if (shouldThrow)
{
act.Should().Throw<XunitException>()
.WithMessage("Expected occurrence to be exactly 1, but it was *");
}
else
{
act();
}
}

[InlineData(1, true)]
[InlineData(2, false)]
[InlineData(3, true)]
[Theory]
public void Exactly_twice_should_be_two(int actual, bool shouldThrow)
{
// Arrange
OccurrenceConstraint constraint = Exactly.Twice();

// Act
Action act = () => Execute.Assertion
.ForConstraint(constraint, actual)
.FailWith($"Expected occurrence to be {constraint.Mode} {constraint.ExpectedCount}, but it was {actual}");

// Assert
if (shouldThrow)
{
act.Should().Throw<XunitException>()
.WithMessage("Expected occurrence to be exactly 2, but it was *");
}
else
{
act();
}
}

[InlineData(2, true)]
[InlineData(3, false)]
[InlineData(4, true)]
[Theory]
public void Exactly_thrice_should_be_three(int actual, bool shouldThrow)
{
// Arrange
OccurrenceConstraint constraint = Exactly.Thrice();

// Act
Action act = () => Execute.Assertion
.ForConstraint(constraint, actual)
.FailWith($"Expected occurrence to be {constraint.Mode} {constraint.ExpectedCount}, but it was {actual}");

// Assert
if (shouldThrow)
{
act.Should().Throw<XunitException>()
.WithMessage("Expected occurrence to be exactly 3, but it was *");
}
else
{
act();
}
}

#endregion

#region Less Than

[InlineData(1, false)]
[InlineData(2, true)]
[Theory]
public void Less_than_twice_should_be_smaller_than_two(int actual, bool shouldThrow)
{
// Arrange
OccurrenceConstraint constraint = LessThan.Twice();

// Act
Action act = () => Execute.Assertion
.ForConstraint(constraint, actual)
.FailWith($"Expected occurrence to be {constraint.Mode} {constraint.ExpectedCount}, but it was {actual}");

// Assert
if (shouldThrow)
{
act.Should().Throw<XunitException>()
.WithMessage("Expected occurrence to be less than 2, but it was *");
}
else
{
act();
}
}

[InlineData(2, false)]
[InlineData(3, true)]
[Theory]
public void Less_than_thrice_should_be_smaller_than_three(int actual, bool shouldThrow)
{
// Arrange
OccurrenceConstraint constraint = LessThan.Thrice();

// Act
Action act = () => Execute.Assertion
.ForConstraint(constraint, actual)
.FailWith($"Expected occurrence to be {constraint.Mode} {constraint.ExpectedCount}, but it was {actual}");

// Assert
if (shouldThrow)
{
act.Should().Throw<XunitException>()
.WithMessage("Expected occurrence to be less than 3, but it was *");
}
else
{
act();
}
}

#endregion

#region More Than

[InlineData(1, true)]
[InlineData(2, false)]
[Theory]
public void More_than_once_should_be_bigger_than_one(int actual, bool shouldThrow)
{
// Arrange
OccurrenceConstraint constraint = MoreThan.Once();

// Act
Action act = () => Execute.Assertion
.ForConstraint(constraint, actual)
.FailWith($"Expected occurrence to be {constraint.Mode} {constraint.ExpectedCount}, but it was {actual}");

// Assert
if (shouldThrow)
{
act.Should().Throw<XunitException>()
.WithMessage("Expected occurrence to be more than 1, but it was *");
}
else
{
act();
}
}

[InlineData(2, true)]
[InlineData(3, false)]
[Theory]
public void More_than_twice_should_be_bigger_than_two(int actual, bool shouldThrow)
{
// Arrange
OccurrenceConstraint constraint = MoreThan.Twice();

// Act
Action act = () => Execute.Assertion
.ForConstraint(constraint, actual)
.FailWith($"Expected occurrence to be {constraint.Mode} {constraint.ExpectedCount}, but it was {actual}");

// Assert
if (shouldThrow)
{
act.Should().Throw<XunitException>()
.WithMessage("Expected occurrence to be more than 2, but it was *");
}
else
{
act();
}
}

[InlineData(3, true)]
[InlineData(4, false)]
[Theory]
public void More_than_thrice_should_be_bigger_than_three(int actual, bool shouldThrow)
{
// Arrange
OccurrenceConstraint constraint = MoreThan.Thrice();

// Act
Action act = () => Execute.Assertion
.ForConstraint(constraint, actual)
.FailWith($"Expected occurrence to be {constraint.Mode} {constraint.ExpectedCount}, but it was {actual}");

// Assert
if (shouldThrow)
{
act.Should().Throw<XunitException>()
.WithMessage("Expected occurrence to be more than 3, but it was *");
}
else
{
act();
}
}

#endregion
}
}

0 comments on commit ba5e038

Please sign in to comment.