From 59bd60c35194bf28f464752248289bb01f78cb42 Mon Sep 17 00:00:00 2001 From: Simon Oxtoby Date: Sat, 12 Mar 2022 14:07:00 +1000 Subject: [PATCH] Adding back ShouldBe expecting a nullable struct to be a non-nullable struct --- EasyAssertions/Assertions/ObjectAssertions.cs | 18 ++++++++ EasyAssertions/EasyAssertions.csproj | 2 +- ReleaseNotes.md | 6 ++- UnitTests/Assertions/ObjectAssertionTests.cs | 43 +++++++++++++++++++ 4 files changed, 67 insertions(+), 2 deletions(-) diff --git a/EasyAssertions/Assertions/ObjectAssertions.cs b/EasyAssertions/Assertions/ObjectAssertions.cs index b4a9e60..b65cad5 100644 --- a/EasyAssertions/Assertions/ObjectAssertions.cs +++ b/EasyAssertions/Assertions/ObjectAssertions.cs @@ -25,6 +25,24 @@ public static Actual ShouldBe(this TActual actual, } }); } + + /// + /// Asserts that a nullable value is equal to another value, using the default equality comparer. + /// + public static Actual ShouldBe(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(actual!.Value); + } /// /// Asserts that two values are equal, using the default equality comparer. diff --git a/EasyAssertions/EasyAssertions.csproj b/EasyAssertions/EasyAssertions.csproj index 3daf1a0..725cb43 100644 --- a/EasyAssertions/EasyAssertions.csproj +++ b/EasyAssertions/EasyAssertions.csproj @@ -2,7 +2,7 @@ netstandard2.0;net461 - 3.0.2 + 3.0.3 Simon Oxtoby diff --git a/ReleaseNotes.md b/ReleaseNotes.md index d9767c0..03c1da2 100644 --- a/ReleaseNotes.md +++ b/ReleaseNotes.md @@ -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. @@ -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. diff --git a/UnitTests/Assertions/ObjectAssertionTests.cs b/UnitTests/Assertions/ObjectAssertionTests.cs index b3c0e15..c899656 100644 --- a/UnitTests/Assertions/ObjectAssertionTests.cs +++ b/UnitTests/Assertions/ObjectAssertionTests.cs @@ -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 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(), Arg.Any()).Returns(ExpectedException); + + Assert.Throws(() => actualExpression.ShouldBe(expectedExpression)); + + Assert.AreEqual(nameof(actualExpression), TestExpression.GetActual()); + Assert.AreEqual(nameof(expectedExpression), TestExpression.GetExpected()); + } [Test] public void ShouldBeValue_SameValueReturnsActualValue()