Skip to content

Commit

Permalink
Improve the DateOnly and TimeOnly code coverage (#2167)
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Delarosbil committed Apr 4, 2023
1 parent 293aec1 commit b1f5945
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
69 changes: 69 additions & 0 deletions Tests/FluentAssertions.Specs/Primitives/DateOnlyAssertionSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ public void Should_succeed_when_asserting_nullable_dateonly_value_with_value_to_
dateOnly.Should().NotBeNull();
}

[Fact]
public void Should_succeed_when_asserting_nullable_dateonly_value_with_null_to_be_null()
{
// Arrange
DateOnly? dateOnly = null;

// Act/Assert
dateOnly.Should().BeNull();
}

public class Be
{
[Fact]
Expand Down Expand Up @@ -602,6 +612,65 @@ public void When_asserting_subject_null_dateonly_should_not_have_month_should_th
}
}

public class NotBe
{
[Fact]
public void Different_dateonly_values_are_valid()
{
// Arrange
DateOnly date = new(2020, 06, 04);
DateOnly otherDate = new(2020, 06, 05);

// Act & Assert
date.Should().NotBe(otherDate);
}

[Fact]
public void Different_dateonly_values_with_different_nullability_are_valid()
{
// Arrange
DateOnly date = new(2020, 06, 04);
DateOnly? otherDate = new(2020, 07, 05);

// Act & Assert
date.Should().NotBe(otherDate);
}

[Fact]
public void Same_dateonly_values_are_invalid()
{
// Arrange
DateOnly date = new(2020, 06, 04);
DateOnly sameDate = new(2020, 06, 04);

// Act
Action act =
() => date.Should().NotBe(sameDate, "because we want to test the failure {0}", "message");

// Assert
act.Should().Throw<XunitException>()
.WithMessage(
"Expected date not to be <2020-06-04> because we want to test the failure message, but it is.");
}

[Fact]
public void Same_dateonly_values_with_different_nullability_are_invalid()
{
// Arrange
DateOnly date = new(2020, 06, 04);
DateOnly? sameDate = new(2020, 06, 04);

// Act
Action act =
() => date.Should().NotBe(sameDate, "because we want to test the failure {0}", "message");

// Assert
act.Should().Throw<XunitException>()
.WithMessage(
"Expected date not to be <2020-06-04> because we want to test the failure message, but it is.");
}
}

public class HaveDay
{
[Fact]
Expand Down
69 changes: 69 additions & 0 deletions Tests/FluentAssertions.Specs/Primitives/TimeOnlyAssertionSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ public void Should_succeed_when_asserting_nullable_timeonly_value_with_value_to_
timeOnly.Should().NotBeNull();
}

[Fact]
public void Should_succeed_when_asserting_nullable_timeonly_value_with_null_to_be_null()
{
// Arrange
TimeOnly? timeOnly = null;

// Act/Assert
timeOnly.Should().BeNull();
}

public class Be
{
[Fact]
Expand Down Expand Up @@ -1291,6 +1301,65 @@ public void When_a_value_is_one_of_the_specified_values_it_should_succeed_when_t
}
}

public class NotBe
{
[Fact]
public void Different_timeonly_values_are_valid()
{
// Arrange
TimeOnly time = new(19, 06, 04);
TimeOnly otherTime = new(20, 06, 05);

// Act & Assert
time.Should().NotBe(otherTime);
}

[Fact]
public void Different_timeonly_values_with_different_nullability_are_valid()
{
// Arrange
TimeOnly time = new(19, 06, 04);
TimeOnly? otherTime = new(19, 07, 05);

// Act & Assert
time.Should().NotBe(otherTime);
}

[Fact]
public void Same_timeonly_values_are_invalid()
{
// Arrange
TimeOnly time = new(19, 06, 04);
TimeOnly sameTime = new(19, 06, 04);

// Act
Action act =
() => time.Should().NotBe(sameTime, "because we want to test the failure {0}", "message");

// Assert
act.Should().Throw<XunitException>()
.WithMessage(
"Expected time not to be <19:06:04.000> because we want to test the failure message, but it is.");
}

[Fact]
public void Same_timeonly_values_with_different_nullability_are_invalid()
{
// Arrange
TimeOnly time = new(19, 06, 04);
TimeOnly? sameTime = new(19, 06, 04);

// Act
Action act =
() => time.Should().NotBe(sameTime, "because we want to test the failure {0}", "message");

// Assert
act.Should().Throw<XunitException>()
.WithMessage(
"Expected time not to be <19:06:04.000> because we want to test the failure message, but it is.");
}
}

public class AndChaining
{
[Fact]
Expand Down

0 comments on commit b1f5945

Please sign in to comment.