Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test coverage for
OccurrenceConstraint
- Loading branch information
Showing
1 changed file
with
383 additions
and
0 deletions.
There are no files selected for viewing
383 changes: 383 additions & 0 deletions
383
Tests/FluentAssertions.Specs/OccurrenceConstraintSpecs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |