Skip to content

Commit

Permalink
Adding back ShouldBe expecting a nullable struct to be a non-nullable…
Browse files Browse the repository at this point in the history
… struct
  • Loading branch information
soxtoby committed Mar 12, 2022
1 parent 2de375b commit 59bd60c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
18 changes: 18 additions & 0 deletions EasyAssertions/Assertions/ObjectAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ public static Actual<TActual> ShouldBe<TActual, TExpected>(this TActual actual,
}
});
}

/// <summary>
/// Asserts that a nullable value is equal to another value, using the default equality comparer.
/// </summary>
public static Actual<TActual> ShouldBe<TActual>(this TActual? actual, TActual expected, string? message = null)
where TActual : struct
{
actual.RegisterAssertion(c =>
{
if (!actual.HasValue)
throw c.StandardError.NotEqual(expected, actual, message);

if (!c.Test.ObjectsAreEqual(actual.Value, expected))
throw c.StandardError.NotEqual(expected, actual, message);
});

return new Actual<TActual>(actual!.Value);
}

/// <summary>
/// Asserts that two values are equal, using the default equality comparer.
Expand Down
2 changes: 1 addition & 1 deletion EasyAssertions/EasyAssertions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
<Version>3.0.2</Version>
<Version>3.0.3</Version>
<Authors>Simon Oxtoby</Authors>
<Company />
<Product />
Expand Down
6 changes: 5 additions & 1 deletion ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v3.0.3
## Bug Fixes
- Adding back `ShouldBe` expecting a nullable struct to be a non-nullable struct.

# v3.0.2
## Bug Fixes
- Removed the `ShouldBe` on nullable structs that was marked as `Obsolete` to push people to `ShouldBeValue` - the small improvement in intellisense isn't worth breaking existing code.
Expand All @@ -14,7 +18,7 @@
- Moved `ShouldBeValue` from `EnumAssertions` to `ObjectAssertions`.

# v3.0.0
**NOTE:** This release caused too many problems with the `class` constraint on `ShouldBe` - use v3.0.2 instead.
**NOTE:** This release caused too many problems with the `class` constraint on `ShouldBe` - use v3.0.3 instead.

## New Features
- Added nullable annotations everywhere.
Expand Down
43 changes: 43 additions & 0 deletions UnitTests/Assertions/ObjectAssertionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,49 @@ public void ShouldBe_CorrectlyRegistersAssertion()
Assert.AreEqual(nameof(actualExpression), TestExpression.GetActual());
Assert.AreEqual(nameof(expectedExpression), TestExpression.GetExpected());
}

[Test]
public void ShouldBe_NullableStruct_ValueEqualsExpected_ReturnsActualValue()
{
int? actual = 1;

Actual<int> result = actual.ShouldBe(1);

Assert.AreEqual(1, result.And);
}

[Test]
public void ShouldBe_NullableStruct_NoValue_FailsWithObjectsNotEqualMessage()
{
int? actual = null;
const int expected = 1;
Error.NotEqual(expected, actual, "foo").Returns(ExpectedException);

AssertThrowsExpectedError(() => actual.ShouldBe(expected, "foo"));
}

[Test]
public void ShouldBe_NullableStruct_ValueIsDifferent_FailsWithObjectsNotEqualMessage()
{
int? actual = 1;
const int expected = 2;
Error.NotEqual(expected, actual, "foo").Returns(ExpectedException);

AssertThrowsExpectedError(() => actual.ShouldBe(expected, "foo"));
}

[Test]
public void ShouldBe_NullableStruct_CorrectlyRegistersAssertion()
{
int? actualExpression = 1;
const int expectedExpression = 2;
Error.NotEqual(Arg.Any<object>(), Arg.Any<object>()).Returns(ExpectedException);

Assert.Throws<Exception>(() => actualExpression.ShouldBe(expectedExpression));

Assert.AreEqual(nameof(actualExpression), TestExpression.GetActual());
Assert.AreEqual(nameof(expectedExpression), TestExpression.GetExpected());
}

[Test]
public void ShouldBeValue_SameValueReturnsActualValue()
Expand Down

0 comments on commit 59bd60c

Please sign in to comment.