From dce101c7cbf267355968636a2106923b378c642a Mon Sep 17 00:00:00 2001 From: pmosk Date: Wed, 9 Aug 2023 18:45:08 +0400 Subject: [PATCH 1/3] Add SourceException --- .../AssertHelper/AssertHelper.cs | 10 +- .../Failure.Tests/Failure.Tests.csproj | 16 +- .../Failure.Tests/Source/FailureTestSource.cs | 15 ++ .../Failure.Tests/Source/Source.Equal.cs | 79 +++++++++ .../Failure.Tests/Source/Source.Unequal.cs | 61 +++++++ .../FailureTest.Deconstruct.Exception.cs | 46 +++++ .../Test.Failure/FailureTest.Deconstruct.cs | 15 +- .../FailureTest.Equals.Equality.cs | 104 +---------- .../FailureTest.Equals.Inequality.cs | 106 +---------- .../Test.Failure/FailureTest.Equals.Object.cs | 165 ++---------------- .../Test.Failure/FailureTest.Equals.Other.cs | 99 +---------- .../Test.Failure/FailureTest.Equals.Static.cs | 102 +---------- .../Test.Failure/FailureTest.Factory.cs | 12 +- .../Test.Failure/FailureTest.GetHashCode.cs | 103 ++--------- .../FailureTest.MapFailureCode.cs | 57 +++--- .../Test.Failure/FailureTest.ToString.cs | 54 +++--- .../Failure.Tests/Test.Failure/FailureTest.cs | 2 +- .../FailureStaticTest.Equals.cs | 104 +---------- .../FailureStaticTest.Factory.Exception.cs | 38 ++++ ...ailureStaticTest.Factory.Unit.Exception.cs | 41 +++++ .../FailureStaticTest.Factory.Unit.cs | 26 +-- .../FailureStaticTest.Factory.cs | 38 +--- .../Test.FailureStatic/FailureStaticTest.cs | 2 +- .../Failure.T/Failure.T.Deconstruct.cs | 7 + .../Failure.T/Failure.T.Equality.Common.cs | 4 + .../Failure.T/Failure.T.Equality.Equals.cs | 3 +- .../Failure.T.Equality.GetHashCode.cs | 14 +- .../Failure.T/Failure.T.MapFailureCode.cs | 5 +- .../Failure/Failure.T/Failure.T.ToString.cs | 22 ++- .../Failure/Failure.T/Failure.T.cs | 6 +- .../Failure/Failure.csproj | 2 +- .../Failure/Failure/Failure.Factory.cs | 16 ++ 32 files changed, 506 insertions(+), 868 deletions(-) create mode 100644 src/core-taggeds-failure/Failure.Tests/Source/FailureTestSource.cs create mode 100644 src/core-taggeds-failure/Failure.Tests/Source/Source.Equal.cs create mode 100644 src/core-taggeds-failure/Failure.Tests/Source/Source.Unequal.cs create mode 100644 src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Deconstruct.Exception.cs create mode 100644 src/core-taggeds-failure/Failure.Tests/Test.FailureStatic/FailureStaticTest.Factory.Exception.cs create mode 100644 src/core-taggeds-failure/Failure.Tests/Test.FailureStatic/FailureStaticTest.Factory.Unit.Exception.cs diff --git a/src/core-taggeds-failure/Failure.Tests/AssertHelper/AssertHelper.cs b/src/core-taggeds-failure/Failure.Tests/AssertHelper/AssertHelper.cs index ca0f959e..c9e8a525 100644 --- a/src/core-taggeds-failure/Failure.Tests/AssertHelper/AssertHelper.cs +++ b/src/core-taggeds-failure/Failure.Tests/AssertHelper/AssertHelper.cs @@ -1,15 +1,17 @@ -using Xunit; +using System; +using Xunit; namespace PrimeFuncPack.Core.Tests; internal static class AssertHelper { public static void AssertEqualFailures( - (TFailureCode FailureCode, string FailureMessage) expected, - (TFailureCode FailureCode, string FailureMessage) actual) + (TFailureCode FailureCode, string FailureMessage, Exception? SourceException) expected, + (TFailureCode FailureCode, string FailureMessage, Exception? SourceException) actual) where TFailureCode : struct { - Assert.Equal(expected.FailureCode, actual.FailureCode); + Assert.StrictEqual(expected.FailureCode, actual.FailureCode); Assert.Equal(expected.FailureMessage, actual.FailureMessage); + Assert.Equal(expected.SourceException, actual.SourceException); } } diff --git a/src/core-taggeds-failure/Failure.Tests/Failure.Tests.csproj b/src/core-taggeds-failure/Failure.Tests/Failure.Tests.csproj index d0303cb9..25adb5d6 100644 --- a/src/core-taggeds-failure/Failure.Tests/Failure.Tests.csproj +++ b/src/core-taggeds-failure/Failure.Tests/Failure.Tests.csproj @@ -13,22 +13,22 @@ PrimeFuncPack.Core.Failure.Tests + + + + - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all - - - - diff --git a/src/core-taggeds-failure/Failure.Tests/Source/FailureTestSource.cs b/src/core-taggeds-failure/Failure.Tests/Source/FailureTestSource.cs new file mode 100644 index 00000000..51c47bff --- /dev/null +++ b/src/core-taggeds-failure/Failure.Tests/Source/FailureTestSource.cs @@ -0,0 +1,15 @@ +using System; + +namespace PrimeFuncPack.Core.Tests; + +internal static partial class FailureTestSource +{ + private sealed class SomeException : Exception + { + public static readonly SomeException SomeInstance; + + static SomeException() + => + SomeInstance = new(); + } +} \ No newline at end of file diff --git a/src/core-taggeds-failure/Failure.Tests/Source/Source.Equal.cs b/src/core-taggeds-failure/Failure.Tests/Source/Source.Equal.cs new file mode 100644 index 00000000..6643448e --- /dev/null +++ b/src/core-taggeds-failure/Failure.Tests/Source/Source.Equal.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using PrimeFuncPack.UnitTest; + +namespace PrimeFuncPack.Core.Tests; + +partial class FailureTestSource +{ + public static IEnumerable EqualPairTestData + => + new[] + { + new object[] + { + new Failure(), + default(Failure) + }, + new object[] + { + new Failure(), + new Failure(default, null) + }, + new object[] + { + default(Failure), + new Failure(default, string.Empty) + { + SourceException = null + } + }, + new object[] + { + new Failure(default, null), + new Failure() + }, + new object[] + { + new Failure(default, string.Empty), + default(Failure) + }, + new object[] + { + new Failure(SomeFailureCode.First, null) + { + SourceException = SomeException.SomeInstance + }, + new Failure(SomeFailureCode.First, null) + { + SourceException = SomeException.SomeInstance + } + }, + new object[] + { + new Failure(SomeFailureCode.Second, string.Empty), + new Failure(SomeFailureCode.Second, null) + }, + new object[] + { + new Failure(SomeFailureCode.Third, null), + new Failure(SomeFailureCode.Third, string.Empty) + }, + new object[] + { + new Failure(SomeFailureCode.Unknown, string.Empty), + new Failure(SomeFailureCode.Unknown, string.Empty) + }, + new object[] + { + new Failure(SomeFailureCode.First, TestData.SomeString) + { + SourceException = SomeException.SomeInstance + }, + new Failure(SomeFailureCode.First, TestData.SomeString) + { + SourceException = SomeException.SomeInstance + } + } + }; +} \ No newline at end of file diff --git a/src/core-taggeds-failure/Failure.Tests/Source/Source.Unequal.cs b/src/core-taggeds-failure/Failure.Tests/Source/Source.Unequal.cs new file mode 100644 index 00000000..ef018d6f --- /dev/null +++ b/src/core-taggeds-failure/Failure.Tests/Source/Source.Unequal.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using PrimeFuncPack.UnitTest; + +namespace PrimeFuncPack.Core.Tests; + +partial class FailureTestSource +{ + public static IEnumerable UnequalPairTestData + => + new[] + { + new object[] + { + new Failure(), + new Failure(SomeFailureCode.First, null) + }, + new object[] + { + default(Failure), + new Failure(SomeFailureCode.Second, TestData.WhiteSpaceString) + }, + new object[] + { + new Failure(SomeFailureCode.Third, null), + default(Failure) + }, + new object[] + { + new Failure(SomeFailureCode.First, TestData.TabString), + new Failure() + }, + new object[] + { + new Failure(SomeFailureCode.First, TestData.SomeString), + new Failure(SomeFailureCode.Second, TestData.SomeString) + }, + new object[] + { + new Failure(SomeFailureCode.First, TestData.SomeString) + { + SourceException = SomeException.SomeInstance + }, + new Failure(SomeFailureCode.First, TestData.UpperSomeString) + { + SourceException = SomeException.SomeInstance + } + }, + new object[] + { + new Failure(SomeFailureCode.First, TestData.SomeString) + { + SourceException = new SomeException() + }, + new Failure(SomeFailureCode.First, TestData.SomeString) + { + SourceException = new SomeException() + } + } + }; +} \ No newline at end of file diff --git a/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Deconstruct.Exception.cs b/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Deconstruct.Exception.cs new file mode 100644 index 00000000..c158dc06 --- /dev/null +++ b/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Deconstruct.Exception.cs @@ -0,0 +1,46 @@ +using System; +using Xunit; +using static PrimeFuncPack.Core.Tests.AssertHelper; +using static PrimeFuncPack.UnitTest.TestData; + +namespace PrimeFuncPack.Core.Tests; + +partial class FailureTest +{ + [Theory] + [InlineData(int.MinValue, null, EmptyString)] + [InlineData(Zero, EmptyString, EmptyString)] + [InlineData(PlusFifteen, SomeString, SomeString)] + public static void DeconstructWithSourceException_SourceExceptionIsNull_ExpectCorrectValue( + int sourceFailureCode, string? sourceFailureMessage, string expectedFailureMessage) + { + var source = new Failure(sourceFailureCode, sourceFailureMessage); + + var (actualFailureCode, actualFailureMessage, actualException) = source; + + AssertEqualFailures( + (sourceFailureCode, expectedFailureMessage, null), + (actualFailureCode, actualFailureMessage, actualException)); + } + + [Theory] + [InlineData(SomeFailureCode.First, null, EmptyString)] + [InlineData(SomeFailureCode.Unknown, EmptyString, EmptyString)] + [InlineData(SomeFailureCode.Second, SomeString, SomeString)] + public static void DeconstructWithSourceException_SourceExceptionIsNotNull_ExpectCorrectValue( + SomeFailureCode sourceFailureCode, string? sourceFailureMessage, string expectedFailureMessage) + { + var sourceException = new Exception(); + + var source = new Failure(sourceFailureCode, sourceFailureMessage) + { + SourceException = sourceException + }; + + var (actualFailureCode, actualFailureMessage, actualException) = source; + + AssertEqualFailures( + (sourceFailureCode, expectedFailureMessage, sourceException), + (actualFailureCode, actualFailureMessage, actualException)); + } +} diff --git a/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Deconstruct.cs b/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Deconstruct.cs index 13d4260a..7292987b 100644 --- a/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Deconstruct.cs +++ b/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Deconstruct.cs @@ -13,7 +13,7 @@ partial class FailureTest [InlineData(Zero)] [InlineData(PlusFifteen)] [InlineData(int.MaxValue)] - public void Deconstruct_SourceFailureMessageIsNull_ExpectFailureCodeIsEqualToSourceAndMessageIsEmpty( + public static void Deconstruct_SourceFailureMessageIsNull_ExpectFailureCodeIsEqualToSourceAndMessageIsEmpty( int sourceFailureCode) { var source = new Failure(sourceFailureCode, null); @@ -21,8 +21,8 @@ public void Deconstruct_SourceFailureMessageIsNull_ExpectFailureCodeIsEqualToSou var (actualFailureCode, actualFailureMessage) = source; AssertEqualFailures( - (source.FailureCode, source.FailureMessage), - (actualFailureCode, actualFailureMessage)); + (source.FailureCode, source.FailureMessage, null), + (actualFailureCode, actualFailureMessage, null)); } [Theory] @@ -36,16 +36,15 @@ public void Deconstruct_SourceFailureMessageIsNull_ExpectFailureCodeIsEqualToSou [InlineData(SomeFailureCode.Second, LowerSomeString)] [InlineData(SomeFailureCode.First, SomeString)] [InlineData(SomeFailureCode.Third, UpperSomeString)] - public void Deconstruct_SourceFailureMessageIsNotNull_ExpectFailureCodeAndMessageAreEqualToSource( - SomeFailureCode sourceFailureCode, - string sourceFailureMessage) + public static void Deconstruct_SourceFailureMessageIsNotNull_ExpectFailureCodeAndMessageAreEqualToSource( + SomeFailureCode sourceFailureCode, string sourceFailureMessage) { var source = new Failure(sourceFailureCode, sourceFailureMessage); var (actualFailureCode, actualFailureMessage) = source; AssertEqualFailures( - (source.FailureCode, source.FailureMessage), - (actualFailureCode, actualFailureMessage)); + (source.FailureCode, source.FailureMessage, null), + (actualFailureCode, actualFailureMessage, null)); } } diff --git a/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Equals.Equality.cs b/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Equals.Equality.cs index 45db3746..3aaf3760 100644 --- a/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Equals.Equality.cs +++ b/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Equals.Equality.cs @@ -1,113 +1,25 @@ using System; -using PrimeFuncPack.UnitTest; using Xunit; -using static PrimeFuncPack.UnitTest.TestData; namespace PrimeFuncPack.Core.Tests; partial class FailureTest { - [Fact] - public void Equality_FailureAIsDefaultAndFailureBIsDefault_ExpectTrue() - { - var failureA = new Failure(); - var failureB = default(Failure); - - var actual = failureA == failureB; - Assert.True(actual); - } - - [Theory] - [InlineData(null)] - [InlineData(EmptyString)] - public void Equality_FailureAIsDefaultAndFailureBCodeIsDefaultAndFailureBMessageIsNullOrEmpty_ExpectTrue( - string? failureMessage) - { - var failureA = new Failure(); - var failureB = new Failure(default, failureMessage); - - var actual = failureA == failureB; - Assert.True(actual); - } - [Theory] - [InlineData(null)] - [InlineData(EmptyString)] - public void Equality_FailureACodeIsDefaultAndFailureAMessageIsNullOrEmptyAndFailureBIsDefault_ExpectTrue( - string? failureMessage) + [MemberData(nameof(FailureTestSource.EqualPairTestData), MemberType = typeof(FailureTestSource))] + public static void Equality_LeftIsEqualToRight_ExpectTrue( + Failure left, Failure right) { - var failureA = new Failure(default, failureMessage); - var failureB = new Failure(); - - var actual = failureA == failureB; + var actual = left == right; Assert.True(actual); } [Theory] - [InlineData(Zero, null, null)] - [InlineData(Zero, EmptyString, null)] - [InlineData(Zero, null, EmptyString)] - [InlineData(Zero, EmptyString, EmptyString)] - [InlineData(byte.MinValue, UpperSomeString, UpperSomeString)] - [InlineData(byte.MaxValue, SomeString, SomeString)] - public void Equality_FailureACodeIsEqualToFailureBCodeAndMessagesAreNullOrEmpty_ExpectTrue( - byte failureCode, - string? failureAMessage, - string? failureBMessage) + [MemberData(nameof(FailureTestSource.UnequalPairTestData), MemberType = typeof(FailureTestSource))] + public static void Equality_LeftIsNotEqualToRight_ExpectTrue( + Failure left, Failure right) { - var failureA = new Failure(failureCode, failureAMessage); - var failureB = new Failure(failureCode, failureBMessage); - - var actual = failureA == failureB; - Assert.True(actual); - } - - [Theory] - [InlineData(SomeFailureCode.Third, null)] - [InlineData(SomeFailureCode.Unknown, WhiteSpaceString)] - [InlineData(SomeFailureCode.Unknown, TabString)] - [InlineData(SomeFailureCode.Unknown, LowerSomeString)] - public void Equality_FailureAIsDefaultAndFailureBCodeIsNotDefaultOrFailureBMessageIsNotEmpty_ExpectFalse( - SomeFailureCode failureBCode, - string? failureBMessage) - { - var failureA = new Failure(); - var failureB = new Failure(failureBCode, failureBMessage); - - var actual = failureA == failureB; - Assert.False(actual); - } - - [Theory] - [InlineData(SomeFailureCode.Second, null)] - [InlineData(SomeFailureCode.Unknown, WhiteSpaceString)] - [InlineData(SomeFailureCode.Unknown, TabString)] - public void Equality_FailureACodeIsNotDefaultOrFailureAMessageIsNotEmptyAndFailureBIsDefault_ExpectFalse( - SomeFailureCode failureACode, - string? failureAMessage) - { - var failureA = new Failure(failureACode, failureAMessage); - var failureB = default(Failure); - - var actual = failureA == failureB; - Assert.False(actual); - } - - [Theory] - [InlineData(Zero, null, Zero, WhiteSpaceString)] - [InlineData(Zero, EmptyString, Zero, TabString)] - [InlineData(Zero, LowerSomeString, Zero, SomeString)] - [InlineData(PlusFifteen, WhiteSpaceString, PlusFifteen, TabString)] - [InlineData(Zero, SomeString, int.MaxValue, SomeString)] - [InlineData(int.MinValue, UpperSomeString, MinusFifteen, UpperSomeString)] - public void Equality_FailureACodeIsNotEqualToFailureBCodeAndFailureAMessageIsNotEqualToFailureBMessage_ExpectFalse( - int failureACode, string? failureAMessage, - int failureBCode, string? failureBMessage) - { - var failureA = new Failure(failureACode, failureAMessage); - var failureB = new Failure(failureBCode, failureBMessage); - - var actual = failureA == failureB; + var actual = left == right; Assert.False(actual); } } diff --git a/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Equals.Inequality.cs b/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Equals.Inequality.cs index a33349ee..73e011c8 100644 --- a/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Equals.Inequality.cs +++ b/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Equals.Inequality.cs @@ -1,115 +1,25 @@ using System; -using PrimeFuncPack.UnitTest; using Xunit; -using static PrimeFuncPack.UnitTest.TestData; namespace PrimeFuncPack.Core.Tests; partial class FailureTest { - [Fact] - public void Inequality_FailureAIsDefaultAndFailureBIsDefault_ExpectFalse() - { - var failureA = default(Failure); - var failureB = new Failure(); - - var actual = failureA != failureB; - Assert.False(actual); - } - - [Theory] - [InlineData(null)] - [InlineData(EmptyString)] - public void Inequality_FailureAIsDefaultAndFailureBCodeIsDefaultAndFailureBMessageIsNullOrEmpty_ExpectFalse( - string? failureMessage) - { - var failureA = new Failure(); - var failureB = new Failure(default, failureMessage); - - var actual = failureA != failureB; - Assert.False(actual); - } - [Theory] - [InlineData(null)] - [InlineData(EmptyString)] - public void Inequality_FailureACodeIsDefaultAndFailureAMessageIsNullOrEmptyAndFailureBIsDefault_ExpectFalse( - string? failureMessage) + [MemberData(nameof(FailureTestSource.EqualPairTestData), MemberType = typeof(FailureTestSource))] + public static void Inequality_LeftIsEqualToRight_ExpectFalse( + Failure left, Failure right) { - var failureA = new Failure(default, failureMessage); - var failureB = new Failure(); - - var actual = failureA != failureB; + var actual = left != right; Assert.False(actual); } [Theory] - [InlineData(SomeFailureCode.Unknown, null, null)] - [InlineData(SomeFailureCode.Unknown, EmptyString, null)] - [InlineData(SomeFailureCode.Unknown, null, EmptyString)] - [InlineData(SomeFailureCode.Unknown, EmptyString, EmptyString)] - [InlineData(SomeFailureCode.First, UpperSomeString, UpperSomeString)] - [InlineData(SomeFailureCode.Third, SomeString, SomeString)] - public void Inequality_FailureACodeIsEqualToFailureBCodeAndMessagesAreNullOrEmpty_ExpectFalse( - SomeFailureCode failureCode, - string? failureAMessage, - string? failureBMessage) + [MemberData(nameof(FailureTestSource.UnequalPairTestData), MemberType = typeof(FailureTestSource))] + public static void Inequality_LeftIsNotEqualToRight_ExpectTrue( + Failure left, Failure right) { - var failureA = new Failure(failureCode, failureAMessage); - var failureB = new Failure(failureCode, failureBMessage); - - var actual = failureA != failureB; - Assert.False(actual); - } - - [Theory] - [InlineData(PlusFifteen, null)] - [InlineData(Zero, WhiteSpaceString)] - [InlineData(Zero, TabString)] - [InlineData(Zero, LowerSomeString)] - public void Inequality_FailureAIsDefaultAndFailureBCodeIsNotDefaultOrFailureBMessageIsNotEmpty_ExpectTrue( - int failureBCode, - string? failureBMessage) - { - var failureA = new Failure(); - var failureB = new Failure(failureBCode, failureBMessage); - - var actual = failureA != failureB; - Assert.True(actual); - } - - [Theory] - [InlineData(long.MinValue, null)] - [InlineData(Zero, WhiteSpaceString)] - [InlineData(Zero, TabString)] - [InlineData(Zero, SomeString)] - [InlineData(long.MinValue, LowerSomeString)] - public void Inequality_FailureACodeIsNotDefaultOrFailureAMessageIsNotEmptyAndFailureBIsDefault_ExpectTrue( - long failureACode, - string? failureAMessage) - { - var failureA = new Failure(failureACode, failureAMessage); - var failureB = new Failure(); - - var actual = failureA != failureB; - Assert.True(actual); - } - - [Theory] - [InlineData(SomeFailureCode.Unknown, null, SomeFailureCode.Unknown, WhiteSpaceString)] - [InlineData(SomeFailureCode.Unknown, EmptyString, SomeFailureCode.Unknown, TabString)] - [InlineData(SomeFailureCode.Unknown, LowerSomeString, SomeFailureCode.Unknown, SomeString)] - [InlineData(SomeFailureCode.First, WhiteSpaceString, SomeFailureCode.First, TabString)] - [InlineData(SomeFailureCode.Unknown, SomeString, SomeFailureCode.Second, SomeString)] - [InlineData(SomeFailureCode.Third, UpperSomeString, SomeFailureCode.First, UpperSomeString)] - public void Inequality_FailureACodeIsNotEqualToFailureBCodeAndFailureAMessageIsNotEqualToFailureBMessage_ExpectTrue( - SomeFailureCode failureACode, string? failureAMessage, - SomeFailureCode failureBCode, string? failureBMessage) - { - var failureA = new Failure(failureACode, failureAMessage); - var failureB = new Failure(failureBCode, failureBMessage); - - var actual = failureA != failureB; + var actual = left != right; Assert.True(actual); } } diff --git a/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Equals.Object.cs b/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Equals.Object.cs index d490271f..b5342af9 100644 --- a/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Equals.Object.cs +++ b/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Equals.Object.cs @@ -1,90 +1,30 @@ using System; using Xunit; -using static PrimeFuncPack.UnitTest.TestData; namespace PrimeFuncPack.Core.Tests; partial class FailureTest { - [Fact] - public void EqualsObject_SourceIsDefaultAndObjIsDefaultFailureSameType_ExpectTrue() - { - var source = default(Failure); - object? obj = new Failure(); - - var actual = source.Equals(obj); - Assert.True(actual); - } - - [Theory] - [InlineData(null)] - [InlineData(EmptyString)] - public void EqualsObject_SourceIsDefaultAndObjCodeIsDefaultAndObjMessageIsNullOrEmptyAndTypesAreSame_ExpectTrue( - string? failureMessage) - { - var source = new Failure(); - object? obj = new Failure(default, failureMessage); - - var actual = source.Equals(obj); - Assert.True(actual); - } - - [Theory] - [InlineData(null)] - [InlineData(EmptyString)] - public void EqualsObject_SourceCodeIsDefaultAndSourceMessageIsNullOrEmptyAndObjIsDefaultAndTypesAreSame_ExpectTrue( - string? failureMessage) - { - var source = new Failure(default, failureMessage); - object? obj = default(Failure); - - var actual = source.Equals(obj); - Assert.True(actual); - } - [Theory] - [InlineData(Zero, null, null)] - [InlineData(Zero, EmptyString, null)] - [InlineData(Zero, null, EmptyString)] - [InlineData(Zero, EmptyString, EmptyString)] - [InlineData(MinusFifteen, LowerSomeString, LowerSomeString)] - [InlineData(int.MaxValue, SomeString, SomeString)] - public void EqualsObject_SourceCodeIsEqualToObjCodeAndMessagesAreNullOrEmptyAndTypesAreSame_ExpectTrue( - int failureCode, - string? sourceMessage, - string? objMessage) + [MemberData(nameof(FailureTestSource.EqualPairTestData), MemberType = typeof(FailureTestSource))] + public static void EqualsObject_SourceIsEqualToObjectAndTypesAreSame_ExpectTrue( + Failure source, object? obj) { - var source = new Failure(failureCode, sourceMessage); - object? obj = new Failure(failureCode, objMessage); - var actual = source.Equals(obj); Assert.True(actual); } - [Theory] - [InlineData(SomeFailureCode.Unknown, null)] - [InlineData(SomeFailureCode.Unknown, EmptyString)] - [InlineData(SomeFailureCode.Unknown, WhiteSpaceString)] - [InlineData(SomeFailureCode.Unknown, TabString)] - [InlineData(SomeFailureCode.Unknown, SomeString)] - [InlineData(SomeFailureCode.First, null)] - [InlineData(SomeFailureCode.Second, EmptyString)] - [InlineData(SomeFailureCode.Third, WhiteSpaceString)] - [InlineData(SomeFailureCode.Third, TabString)] - [InlineData(SomeFailureCode.Second, LowerSomeString)] - [InlineData(SomeFailureCode.First, SomeString)] - public void EqualsObject_ObjIsNull_ExpectFalse( - SomeFailureCode failureCode, - string? failureMessage) + [Fact] + public static void EqualsObject_ObjIsNull_ExpectFalse() { - var source = new Failure(failureCode, failureMessage); + var source = new Failure(SomeFailureCode.Unknown, null); var actual = source.Equals(null); Assert.False(actual); } [Fact] - public void EqualsObject_SourceIsDefaultAndObjIsDefaultFailureNotSameType_ExpectFalse() + public static void EqualsObject_SourceTypeIsNotEqualToObjectType_ExpectFalse() { var source = default(Failure); object? obj = default(Failure); @@ -94,95 +34,10 @@ public void EqualsObject_SourceIsDefaultAndObjIsDefaultFailureNotSameType_Expect } [Theory] - [InlineData(null)] - [InlineData(EmptyString)] - public void EqualsObject_SourceIsDefaultAndObjCodeIsDefaultAndObjMessageIsNullOrEmptyAndTypesAreNotSame_ExpectFalse( - string? failureMessage) + [MemberData(nameof(FailureTestSource.UnequalPairTestData), MemberType = typeof(FailureTestSource))] + public static void EqualsObject_SourceIsNotEqualToObjectAndTypesAreSame_ExpectFalse( + Failure source, Failure obj) { - var source = new Failure(); - object? obj = new Failure(default, failureMessage); - - var actual = source.Equals(obj); - Assert.False(actual); - } - - [Theory] - [InlineData(null)] - [InlineData(EmptyString)] - public void EqualsObject_SourceCodeIsDefaultAndSourceMessageIsNullOrEmptyAndObjIsDefaultAndTypesAreNotSame_ExpectFalse( - string? failureMessage) - { - var source = new Failure(default, failureMessage); - object? obj = default(Failure); - - var actual = source.Equals(obj); - Assert.False(actual); - } - - [Theory] - [InlineData(Zero, null, null)] - [InlineData(Zero, EmptyString, null)] - [InlineData(Zero, null, EmptyString)] - [InlineData(Zero, EmptyString, EmptyString)] - [InlineData(MinusFifteen, LowerSomeString, LowerSomeString)] - [InlineData(int.MaxValue, SomeString, SomeString)] - public void EqualsObject_SourceCodeIsSameAsObjCodeIsDefaultAndMessagesAreNullOrEmptyAndTypesAreNotSame_ExpectFalse( - int failureCode, - string? sourceMessage, - string? objMessage) - { - var source = new Failure(failureCode, sourceMessage); - object? obj = new Failure(failureCode, objMessage); - - var actual = source.Equals(obj); - Assert.False(actual); - } - - [Theory] - [InlineData(PlusFifteen, null)] - [InlineData(Zero, ThreeWhiteSpacesString)] - [InlineData(Zero, TabString)] - public void EqualsObject_SourceIsDefaultAndObjCodeIsNotDefaultOrObjMessageIsNotEmptyAndTypesAreNotSame_ExpectFalse( - int objCode, - string? objMessage) - { - var source = default(Failure); - object? obj = new Failure(objCode, objMessage); - - var actual = source.Equals(obj); - Assert.False(actual); - } - - [Theory] - [InlineData(int.MinValue, null)] - [InlineData(MinusFifteen, WhiteSpaceString)] - [InlineData(PlusFifteen, TabString)] - [InlineData(int.MinValue, SomeString)] - public void EqualsObject_SourceCodeIsNotDefaultOrSourceMessageIsNotEmptyAndObjIsDefaultAndTypesAreNotSame_ExpectFalse( - int sourceCode, - string? sourceMessage) - { - var source = new Failure(sourceCode, sourceMessage); - object? obj = new Failure(); - - var actual = source.Equals(obj); - Assert.False(actual); - } - - [Theory] - [InlineData(SomeFailureCode.Unknown, null, SomeFailureCode.Unknown, WhiteSpaceString)] - [InlineData(SomeFailureCode.Unknown, EmptyString, SomeFailureCode.Unknown, TabString)] - [InlineData(SomeFailureCode.Third, WhiteSpaceString, SomeFailureCode.Third, TabString)] - [InlineData(SomeFailureCode.Unknown, LowerSomeString, SomeFailureCode.Unknown, SomeString)] - [InlineData(SomeFailureCode.Second, SomeString, SomeFailureCode.Unknown, SomeString)] - [InlineData(SomeFailureCode.Third, UpperSomeString, SomeFailureCode.First, UpperSomeString)] - public void EqualsObject_SourceCodeIsNotEqualToObjCodeAndSourceMessageIsNotEqualToObjMessageAndTypesAreSame_ExpectFalse( - SomeFailureCode sourceCode, string? sourceMessage, - SomeFailureCode objCode, string? objMessage) - { - var source = new Failure(sourceCode, sourceMessage); - object? obj = new Failure(objCode, objMessage); - var actual = source.Equals(obj); Assert.False(actual); } diff --git a/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Equals.Other.cs b/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Equals.Other.cs index 8d961205..7db5bcbd 100644 --- a/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Equals.Other.cs +++ b/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Equals.Other.cs @@ -1,111 +1,24 @@ using System; -using PrimeFuncPack.UnitTest; using Xunit; -using static PrimeFuncPack.UnitTest.TestData; namespace PrimeFuncPack.Core.Tests; partial class FailureTest { - [Fact] - public void EqualsOther_SourceIsDefaultAndOtherIsDefault_ExpectTrue() - { - var source = new Failure(); - var other = default(Failure); - - var actual = source.Equals(other); - Assert.True(actual); - } - - [Theory] - [InlineData(null)] - [InlineData(EmptyString)] - public void EqualsOther_SourceIsDefaultAndOtherCodeIsDefaultAndOtherMessageIsNullOrEmpty_ExpectTrue( - string? otherMessage) - { - var source = default(Failure); - var other = new Failure(0, otherMessage); - - var actual = source.Equals(other); - Assert.True(actual); - } - - [Theory] - [InlineData(null)] - [InlineData(EmptyString)] - public void EqualsOther_SourceCodeIsDefaultAndSourceMessageIsNullOrEmptyAndOtherIsDefault_ExpectTrue( - string? sourceMessage) - { - var source = new Failure(default, sourceMessage); - var other = new Failure(); - - var actual = source.Equals(other); - Assert.True(actual); - } - [Theory] - [InlineData(SomeFailureCode.Unknown, null, null)] - [InlineData(SomeFailureCode.Unknown, EmptyString, null)] - [InlineData(SomeFailureCode.Unknown, null, EmptyString)] - [InlineData(SomeFailureCode.Unknown, EmptyString, EmptyString)] - [InlineData(SomeFailureCode.First, LowerSomeString, LowerSomeString)] - [InlineData(SomeFailureCode.Second, SomeString, SomeString)] - public void EqualsOther_SourceCodeIsEqualToOtherCodeAndMessagesAreNullOrEmpty_ExpectTrue( - SomeFailureCode failureCode, - string? sourceMessage, - string? otherMessage) + [MemberData(nameof(FailureTestSource.EqualPairTestData), MemberType = typeof(FailureTestSource))] + public static void EqualsOther_SourceIsEqualToOther_ExpectTrue( + Failure source, Failure other) { - var source = new Failure(failureCode, sourceMessage); - var other = new Failure(failureCode, otherMessage); - var actual = source.Equals(other); Assert.True(actual); } [Theory] - [InlineData(long.MaxValue, null)] - [InlineData(Zero, ThreeWhiteSpacesString)] - [InlineData(Zero, TabString)] - public void EqualsOther_SourceIsDefaultAndOtherCodeIsNotDefaultOrOtherMessageIsNotEmpty_ExpectFalse( - long otherCode, - string? otherMessage) - { - var source = new Failure(); - var other = new Failure(otherCode, otherMessage); - - var actual = source.Equals(other); - Assert.False(actual); - } - - [Theory] - [InlineData(SomeFailureCode.Third, null)] - [InlineData(SomeFailureCode.Unknown, WhiteSpaceString)] - [InlineData(SomeFailureCode.Unknown, TabString)] - public void EqualsOther_SourceCodeIsNotDefaultOrSourceMessageIsNotEmptyAndOtherIsDefault_ExpectFalse( - SomeFailureCode sourceCode, - string? sourceMessage) + [MemberData(nameof(FailureTestSource.UnequalPairTestData), MemberType = typeof(FailureTestSource))] + public static void EqualsOther_SourceIsNotEqualToOther_ExpectFalse( + Failure source, Failure other) { - var source = new Failure(sourceCode, sourceMessage); - var other = default(Failure); - - var actual = source.Equals(other); - Assert.False(actual); - } - - [Theory] - [InlineData(Zero, null, Zero, WhiteSpaceString)] - [InlineData(Zero, EmptyString, Zero, TabString)] - [InlineData(Zero, LowerSomeString, Zero, SomeString)] - [InlineData(PlusFifteen, WhiteSpaceString, PlusFifteen, TabString)] - [InlineData(Zero, SomeString, MinusFifteen, SomeString)] - [InlineData(PlusFifteen, UpperSomeString, MinusFifteen, UpperSomeString)] - public void EqualsOther_SourceCodeIsNotEqualToOtherCodeAndSourceMessageIsNotEqualToOtherMessage_ExpectFalse( - int sourceCode, string? sourceMessage, - int otherCode, string? otherMessage) - { - var source = new Failure(sourceCode, sourceMessage); - var other = new Failure(otherCode, otherMessage); - var actual = source.Equals(other); Assert.False(actual); } diff --git a/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Equals.Static.cs b/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Equals.Static.cs index e3dd0a1f..a4cdf1c3 100644 --- a/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Equals.Static.cs +++ b/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Equals.Static.cs @@ -1,111 +1,25 @@ using System; using Xunit; -using static PrimeFuncPack.UnitTest.TestData; namespace PrimeFuncPack.Core.Tests; partial class FailureTest { - [Fact] - public void EqualsStatic_FailureAIsDefaultAndFailureBIsDefault_ExpectTrue() - { - var failureA = default(Failure); - var failureB = new Failure(); - - var actual = Failure.Equals(failureA, failureB); - Assert.True(actual); - } - - [Theory] - [InlineData(null)] - [InlineData(EmptyString)] - public void EqualsStatic_FailureAIsDefaultAndFailureBCodeIsDefaultAndFailureBMessageIsNullOrEmpty_ExpectTrue( - string? failureMessage) - { - var failureA = new Failure(); - var failureB = new Failure(default, failureMessage); - - var actual = Failure.Equals(failureA, failureB); - Assert.True(actual); - } - [Theory] - [InlineData(null)] - [InlineData(EmptyString)] - public void EqualsStatic_FailureACodeIsDefaultAndFailureAMessageIsNullOrEmptyAndFailureBIsDefault_ExpectTrue( - string? failureMessage) + [MemberData(nameof(FailureTestSource.EqualPairTestData), MemberType = typeof(FailureTestSource))] + public static void EqualsStatic_LeftIsEqualToRight_ExpectTrue( + Failure left, Failure right) { - var failureA = new Failure(SomeFailureCode.Unknown, failureMessage); - var failureB = default(Failure); - - var actual = Failure.Equals(failureA, failureB); + var actual = Failure.Equals(left, right); Assert.True(actual); } [Theory] - [InlineData(Zero, null, null)] - [InlineData(Zero, EmptyString, null)] - [InlineData(Zero, null, EmptyString)] - [InlineData(Zero, EmptyString, EmptyString)] - [InlineData(PlusFifteen, LowerSomeString, LowerSomeString)] - [InlineData(MinusFifteen, SomeString, SomeString)] - public void EqualsStatic_FailureACodeIsEqualToFailureBCodeAndMessagesAreNullOrEmpty_ExpectTrue( - int failureCode, - string? failureAMessage, - string? failureBMessage) + [MemberData(nameof(FailureTestSource.UnequalPairTestData), MemberType = typeof(FailureTestSource))] + public static void EqualsStatic_LeftIsNotEqualToRight_ExpectFalse( + Failure left, Failure right) { - var failureA = new Failure(failureCode, failureAMessage); - var failureB = new Failure(failureCode, failureBMessage); - - var actual = Failure.Equals(failureA, failureB); - Assert.True(actual); - } - - [Theory] - [InlineData(SomeFailureCode.First, null)] - [InlineData(SomeFailureCode.Unknown, WhiteSpaceString)] - [InlineData(SomeFailureCode.Unknown, TabString)] - public void EqualsStatic_FailureAIsDefaultAndFailureBCodeIsNotDefaultOrFailureBMessageIsNotEmpty_ExpectFalse( - SomeFailureCode failureBCode, - string? failureBMessage) - { - var failureA = new Failure(); - var failureB = new Failure(failureBCode, failureBMessage); - - var actual = Failure.Equals(failureA, failureB); - Assert.False(actual); - } - - [Theory] - [InlineData(PlusFifteen, null)] - [InlineData(Zero, WhiteSpaceString)] - [InlineData(Zero, TabString)] - public void EqualsStatic_FailureACodeIsNotDefaultOrFailureAMessageIsNotEmptyAndFailureBIsDefault_ExpectFalse( - int failureACode, - string? failureAMessage) - { - var failureA = new Failure(failureACode, failureAMessage); - var failureB = new Failure(); - - var actual = Failure.Equals(failureA, failureB); - Assert.False(actual); - } - - [Theory] - [InlineData(SomeFailureCode.Unknown, null, SomeFailureCode.Unknown, WhiteSpaceString)] - [InlineData(SomeFailureCode.Unknown, EmptyString, SomeFailureCode.Unknown, TabString)] - [InlineData(SomeFailureCode.Unknown, LowerSomeString, SomeFailureCode.Unknown, SomeString)] - [InlineData(SomeFailureCode.Second, WhiteSpaceString, SomeFailureCode.Second, TabString)] - [InlineData(SomeFailureCode.First, SomeString, SomeFailureCode.Unknown, SomeString)] - [InlineData(SomeFailureCode.Second, UpperSomeString, SomeFailureCode.First, UpperSomeString)] - public void EqualsStatic_FailureACodeIsNotEqualToFailureBCodeAndFailureAMessageIsNotEqualToFailureBMessage_ExpectFalse( - SomeFailureCode failureACode, string? failureAMessage, - SomeFailureCode failureBCode, string? failureBMessage) - { - var failureA = new Failure(failureACode, failureAMessage); - var failureB = new Failure(failureBCode, failureBMessage); - - var actual = Failure.Equals(failureA, failureB); + var actual = Failure.Equals(left, right); Assert.False(actual); } } diff --git a/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Factory.cs b/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Factory.cs index 29069255..08fa8101 100644 --- a/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Factory.cs +++ b/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.Factory.cs @@ -13,14 +13,14 @@ partial class FailureTest [InlineData(Zero)] [InlineData(PlusFifteen)] [InlineData(int.MaxValue)] - public void Constructor_SourceFailureMessageIsNull_ExpectFailureCodeIsEqualToSourceAndMessageIsEmpty( + public static void Constructor_SourceFailureMessageIsNull_ExpectFailureCodeIsEqualToSourceAndMessageIsEmpty( int sourceFailureCode) { var actual = new Failure(sourceFailureCode, null); AssertEqualFailures( - (sourceFailureCode, EmptyString), - (actual.FailureCode, actual.FailureMessage)); + (sourceFailureCode, EmptyString, null), + (actual.FailureCode, actual.FailureMessage, actual.SourceException)); } [Theory] @@ -34,14 +34,14 @@ public void Constructor_SourceFailureMessageIsNull_ExpectFailureCodeIsEqualToSou [InlineData(SomeFailureCode.Second, LowerSomeString)] [InlineData(SomeFailureCode.First, SomeString)] [InlineData(SomeFailureCode.Third, UpperSomeString)] - public void Constructor_SourceFailureMessageIsNotNull_ExpectFailureCodeAndMessageAreEqualToSource( + public static void Constructor_SourceFailureMessageIsNotNull_ExpectFailureCodeAndMessageAreEqualToSource( SomeFailureCode sourceFailureCode, string sourceFailureMessage) { var actual = new Failure(sourceFailureCode, sourceFailureMessage); AssertEqualFailures( - (sourceFailureCode, sourceFailureMessage), - (actual.FailureCode, actual.FailureMessage)); + (sourceFailureCode, sourceFailureMessage, null), + (actual.FailureCode, actual.FailureMessage, actual.SourceException)); } } diff --git a/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.GetHashCode.cs b/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.GetHashCode.cs index e01c6866..0a949f79 100644 --- a/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.GetHashCode.cs +++ b/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.GetHashCode.cs @@ -6,97 +6,22 @@ namespace PrimeFuncPack.Core.Tests; partial class FailureTest { - [Fact] - public void GetHashCode_FirstIsDefaultAndSecondIsDefault_ExpectHashCodesAreEqual() - { - var first = new Failure(); - var second = default(Failure); - - var firstHashCode = first.GetHashCode(); - var secondHashCode = second.GetHashCode(); - - Assert.Equal(firstHashCode, secondHashCode); - } - - [Theory] - [InlineData(null)] - [InlineData(EmptyString)] - public void GetHashCode_FirstIsDefaultAndSecondCodeIsDefaultAndSecondMessageIsNullOrEmpty_ExpectHashCodesAreEqual( - string? failureMessage) - { - var first = new Failure(); - var second = new Failure(default, failureMessage); - - var firstHashCode = first.GetHashCode(); - var secondHashCode = second.GetHashCode(); - - Assert.Equal(firstHashCode, secondHashCode); - } - - [Theory] - [InlineData(null)] - [InlineData(EmptyString)] - public void GetHashCode_FirstCodeIsDefaultAndFirstMessageIsNullOrEmptyAndSecondIsDefault_ExpectHashCodesAreEqual( - string? failureMessage) - { - var first = new Failure(SomeFailureCode.Unknown, failureMessage); - var second = new Failure(); - - var firstHashCode = first.GetHashCode(); - var secondHashCode = second.GetHashCode(); - - Assert.Equal(firstHashCode, secondHashCode); - } - [Theory] - [InlineData(SomeFailureCode.Unknown, null, null)] - [InlineData(SomeFailureCode.Unknown, EmptyString, null)] - [InlineData(SomeFailureCode.Unknown, null, EmptyString)] - [InlineData(SomeFailureCode.Unknown, WhiteSpaceString, WhiteSpaceString)] - [InlineData(SomeFailureCode.Second, UpperSomeString, UpperSomeString)] - [InlineData(SomeFailureCode.First, SomeString, SomeString)] - public void GetHashCode_FirstCodeIsEqualToSecondCodeAndMessagesAreNullOrEmpty_ExpectHashCodesAreEqual( - SomeFailureCode failureCode, - string? firstMessage, - string? secondMessage) + [MemberData(nameof(FailureTestSource.EqualPairTestData), MemberType = typeof(FailureTestSource))] + public static void GetHashCode_FirstIsEqualToSecondAndTypesAreSame_ExpectHashCodesAreEqual( + Failure first, Failure second) { - var first = new Failure(failureCode, firstMessage); - var second = new Failure(failureCode, secondMessage); - var firstHashCode = first.GetHashCode(); var secondHashCode = second.GetHashCode(); Assert.Equal(firstHashCode, secondHashCode); } - [Theory] - [InlineData(MinusFifteen, null)] - [InlineData(PlusFifteen, WhiteSpaceString)] - [InlineData(PlusFifteen, TabString)] - [InlineData(MinusFifteen, SomeString)] - public void GetHashCode_FirstIsDefaultAndSecondCodeIsNotDefaultOrSecondMessageIsNotEmpty_ExpectHashCodesAreNotEqual( - int secondCode, - string? secondMessage) - { - var first = default(Failure); - var second = new Failure(secondCode, secondMessage); - - var firstHashCode = first.GetHashCode(); - var secondHashCode = second.GetHashCode(); - - Assert.NotEqual(firstHashCode, secondHashCode); - } - - [Theory] - [InlineData(PlusFifteen, null)] - [InlineData(Zero, WhiteSpaceString)] - [InlineData(Zero, TabString)] - public void GetHashCode_FirstCodeIsNotDefaultOrFirstMessageIsNotEmptyAndSecondIsDefault_ExpectHashCodesAreNotEqual( - int firstCode, - string? firstMessage) + [Fact] + public static void GetHashCode_FirstTypeIsNotEqualToSecondType_ExpectHashCodesAreNotEqual() { - var first = new Failure(firstCode, firstMessage); - var second = new Failure(); + var first = new Failure(PlusFifteen, SomeString); + var second = new Failure(PlusFifteen, SomeString); var firstHashCode = first.GetHashCode(); var secondHashCode = second.GetHashCode(); @@ -105,18 +30,10 @@ public void GetHashCode_FirstCodeIsNotDefaultOrFirstMessageIsNotEmptyAndSecondIs } [Theory] - [InlineData(SomeFailureCode.Unknown, null, SomeFailureCode.Unknown, WhiteSpaceString)] - [InlineData(SomeFailureCode.Unknown, LowerSomeString, SomeFailureCode.Unknown, SomeString)] - [InlineData(SomeFailureCode.Second, WhiteSpaceString, SomeFailureCode.Second, TabString)] - [InlineData(SomeFailureCode.First, SomeString, SomeFailureCode.Unknown, SomeString)] - [InlineData(SomeFailureCode.Second, UpperSomeString, SomeFailureCode.First, UpperSomeString)] - public void GetHashCode_FirstCodeIsNotEqualToSecondOneAndFirstMessageIsNotEqualToSecondOne_ExpectHashCodesAreNotEqual( - SomeFailureCode firstCode, string? firstMessage, - SomeFailureCode secondCode, string? secondMessage) + [MemberData(nameof(FailureTestSource.UnequalPairTestData), MemberType = typeof(FailureTestSource))] + public static void GetHashCode_FirstIsNotEqualToSecondAndTypesAreSame_ExpectHashCodesAreNotEqual( + Failure first, Failure second) { - var first = new Failure(firstCode, firstMessage); - var second = new Failure(secondCode, secondMessage); - var firstHashCode = first.GetHashCode(); var secondHashCode = second.GetHashCode(); diff --git a/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.MapFailureCode.cs b/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.MapFailureCode.cs index 0254245a..59d52e6a 100644 --- a/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.MapFailureCode.cs +++ b/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.MapFailureCode.cs @@ -10,7 +10,7 @@ partial class FailureTest [Theory] [InlineData(true)] [InlineData(false)] - public void MapFailureCode_MapFailureCodeIsNull_ExpectArgumentNullException( + public static void MapFailureCode_MapFailureCodeIsNull_ExpectArgumentNullException( bool isNotDefault) { var source = isNotDefault ? new Failure(SomeFailureCode.First, SomeString) : default; @@ -24,7 +24,7 @@ public void MapFailureCode_MapFailureCodeIsNull_ExpectArgumentNullException( [InlineData(SomeFailureCode.First)] [InlineData(SomeFailureCode.Second)] [InlineData(SomeFailureCode.Third)] - public void MapFailureCode_SourceIsDefault_ExpectFailureCodeIsMappedAndFailureMessageIsEmpty( + public static void MapFailureCode_SourceIsDefault_ExpectFailureCodeIsMappedAndFailureMessageIsEmpty( SomeFailureCode mappedFailureCode) { var source = default(Failure); @@ -32,51 +32,44 @@ public void MapFailureCode_SourceIsDefault_ExpectFailureCodeIsMappedAndFailureMe var actual = source.MapFailureCode(_ => mappedFailureCode); AssertEqualFailures( - (mappedFailureCode, EmptyString), - (actual.FailureCode, actual.FailureMessage)); + (mappedFailureCode, EmptyString, null), + (actual.FailureCode, actual.FailureMessage, actual.SourceException)); } [Theory] - [InlineData(int.MinValue)] - [InlineData(MinusFifteen)] - [InlineData(Zero)] - [InlineData(PlusFifteen)] - [InlineData(int.MaxValue)] - public void MapFailureCode_SourceFailureMessageIsNull_ExpectFailureCodeIsMappedAndFailureMessageIsEmpty( - int mappedFailureCode) + [InlineData(int.MinValue, EmptyString, EmptyString)] + [InlineData(MinusFifteen, SomeString, SomeString)] + [InlineData(Zero, null, EmptyString)] + public static void MapFailureCode_SourceExceptionIsNull_ExpectFailureCodeIsMappedAndSourceExceptionIsNull( + int mappedFailureCode, string? sourceFailureMessage, string expectedFailureMessage) { - var source = new Failure(decimal.One, null); + var source = new Failure(decimal.One, sourceFailureMessage); var actual = source.MapFailureCode(_ => mappedFailureCode); AssertEqualFailures( - (mappedFailureCode, EmptyString), - (actual.FailureCode, actual.FailureMessage)); + (mappedFailureCode, expectedFailureMessage, null), + (actual.FailureCode, actual.FailureMessage, actual.SourceException)); } [Theory] - [InlineData(SomeFailureCode.Unknown, EmptyString, Zero)] - [InlineData(SomeFailureCode.Unknown, WhiteSpaceString, Zero)] - [InlineData(SomeFailureCode.Unknown, TabString, Zero)] - [InlineData(SomeFailureCode.Unknown, LowerSomeString, Zero)] - [InlineData(SomeFailureCode.Unknown, SomeString, Zero)] - [InlineData(SomeFailureCode.Unknown, SomeString, PlusFifteen)] - [InlineData(SomeFailureCode.First, EmptyString, int.MinValue)] - [InlineData(SomeFailureCode.Second, WhiteSpaceString, MinusFifteen)] - [InlineData(SomeFailureCode.Third, TabString, PlusFifteen)] - [InlineData(SomeFailureCode.First, SomeString, Zero)] - [InlineData(SomeFailureCode.Third, UpperSomeString, int.MaxValue)] - public void MapFailureCode_SourceFailureMessageIsNotNull_ExpectFailureCodeIsMappedAndFailureMessageIsEqualToSource( - SomeFailureCode sourceFailureCode, - string sourceFailureMessage, - int mappedFailureCode) + [InlineData(SomeFailureCode.Unknown, null, EmptyString)] + [InlineData(SomeFailureCode.First, EmptyString, EmptyString)] + [InlineData(SomeFailureCode.Second, SomeString, SomeString)] + public static void MapFailureCode_SourceExceptionIsNotNull_ExpectFailureCodeIsMappedAndSourceExceptionIsSame( + SomeFailureCode mappedFailureCode, string? sourceFailureMessage, string expectedFailureMessage) { - var source = new Failure(sourceFailureCode, sourceFailureMessage); + var sourceException = new Exception("Some error message"); + + var source = new Failure(MinusFifteen, sourceFailureMessage) + { + SourceException = sourceException + }; var actual = source.MapFailureCode(_ => mappedFailureCode); AssertEqualFailures( - (mappedFailureCode, sourceFailureMessage), - (actual.FailureCode, actual.FailureMessage)); + (mappedFailureCode, expectedFailureMessage, sourceException), + (actual.FailureCode, actual.FailureMessage, actual.SourceException)); } } diff --git a/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.ToString.cs b/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.ToString.cs index 268dc059..ecc689ed 100644 --- a/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.ToString.cs +++ b/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.ToString.cs @@ -8,14 +8,14 @@ namespace PrimeFuncPack.Core.Tests; partial class FailureTest { [Fact] - public void ToString_FailureIsDefault() + public static void ToString_FailureIsDefault() { var failure = default(Failure); var actual = failure.ToString(); var expected = string.Format( CultureInfo.InvariantCulture, - "Failure<{0}>:{{ \"FailureCode\": {1}, \"FailureMessage\": \"{2}\" }}", + "Failure<{0}>:{{ \"FailureCode\": {1}, \"FailureMessage\": \"{2}\", \"SourceException\": null }}", typeof(SomeFailureCode).Name, SomeFailureCode.Unknown, string.Empty); @@ -24,48 +24,52 @@ public void ToString_FailureIsDefault() } [Theory] - [InlineData(Zero, null)] - [InlineData(PlusFifteen, null)] - [InlineData(Zero, EmptyString)] - [InlineData(MinusFifteen, EmptyString)] - [InlineData(Zero, SomeString)] - [InlineData(int.MaxValue, LowerSomeString)] - public void ToString_FailureIsNotDefault( - int failureCode, - string? failureMessage) + [InlineData(Zero, null, EmptyString)] + [InlineData(Zero, EmptyString, EmptyString)] + [InlineData(Zero, SomeString, SomeString)] + public static void ToString_SourceExceptionIsNull( + int failureCode, string? sourceFailureMessage, string expectedFailureMessage) { - var sourceFailure = new Failure(failureCode, failureMessage); + var sourceFailure = new Failure(failureCode, sourceFailureMessage) + { + SourceException = null + }; + var actual = sourceFailure.ToString(); var expected = string.Format( CultureInfo.InvariantCulture, - "Failure<{0}>:{{ \"FailureCode\": {1}, \"FailureMessage\": \"{2}\" }}", + "Failure<{0}>:{{ \"FailureCode\": {1}, \"FailureMessage\": \"{2}\", \"SourceException\": null }}", typeof(int).Name, failureCode, - failureMessage); + expectedFailureMessage); Assert.Equal(expected, actual); } [Theory] - [InlineData(SomeFailureCode.Unknown, WhiteSpaceString)] - [InlineData(SomeFailureCode.First, TabString)] - [InlineData(SomeFailureCode.Unknown, SomeString)] - [InlineData(SomeFailureCode.Second, LowerSomeString)] - [InlineData(SomeFailureCode.Third, UpperSomeString)] - public void ToString_FailureMessageIsNotEmpty( - SomeFailureCode failureCode, - string failureMessage) + [InlineData(SomeFailureCode.Unknown, null, EmptyString)] + [InlineData(SomeFailureCode.First, EmptyString, EmptyString)] + [InlineData(SomeFailureCode.Unknown, SomeString, SomeString)] + public static void ToString_SourceExceptionIsNotNull( + SomeFailureCode failureCode, string? sourceFailureMessage, string expectedFailureMessage) { - var sourceFailure = new Failure(failureCode, failureMessage); + var sourceException = new InvalidOperationException("Some error message"); + + var sourceFailure = new Failure(failureCode, sourceFailureMessage) + { + SourceException = sourceException + }; + var actual = sourceFailure.ToString(); var expected = string.Format( CultureInfo.InvariantCulture, - "Failure<{0}>:{{ \"FailureCode\": {1}, \"FailureMessage\": \"{2}\" }}", + "Failure<{0}>:{{ \"FailureCode\": {1}, \"FailureMessage\": \"{2}\", \"SourceException\": \"{3}\" }}", typeof(SomeFailureCode).Name, failureCode, - failureMessage); + expectedFailureMessage, + sourceException.ToString()); Assert.Equal(expected, actual); } diff --git a/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.cs b/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.cs index 0e3e5dc5..1200ab67 100644 --- a/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.cs +++ b/src/core-taggeds-failure/Failure.Tests/Test.Failure/FailureTest.cs @@ -1,5 +1,5 @@ namespace PrimeFuncPack.Core.Tests; -public sealed partial class FailureTest +public static partial class FailureTest { } diff --git a/src/core-taggeds-failure/Failure.Tests/Test.FailureStatic/FailureStaticTest.Equals.cs b/src/core-taggeds-failure/Failure.Tests/Test.FailureStatic/FailureStaticTest.Equals.cs index ab9e32d2..6becdb3f 100644 --- a/src/core-taggeds-failure/Failure.Tests/Test.FailureStatic/FailureStaticTest.Equals.cs +++ b/src/core-taggeds-failure/Failure.Tests/Test.FailureStatic/FailureStaticTest.Equals.cs @@ -1,113 +1,25 @@ using System; using Xunit; -using static PrimeFuncPack.UnitTest.TestData; namespace PrimeFuncPack.Core.Tests; partial class FailureStaticTest { - [Fact] - public void Equals_FailureAIsDefaultAndFailureBIsDefault_ExpectTrue() - { - var failureA = default(Failure); - var failureB = new Failure(); - - var actual = Failure.Equals(failureA, failureB); - Assert.True(actual); - } - - [Theory] - [InlineData(null)] - [InlineData(EmptyString)] - public void Equals_FailureAIsDefaultAndFailureBCodeIsDefaultAndFailureBMessageIsNullOrEmpty_ExpectTrue( - string? failureMessage) - { - var failureA = default(Failure); - var failureB = new Failure(default, failureMessage); - - var actual = Failure.Equals(failureA, failureB); - Assert.True(actual); - } - [Theory] - [InlineData(null)] - [InlineData(EmptyString)] - public void Equals_FailureACodeIsDefaultAndFailureAMessageIsNullOrEmptyAndFailureBIsDefault_ExpectTrue( - string? failureMessage) + [MemberData(nameof(FailureTestSource.EqualPairTestData), MemberType = typeof(FailureTestSource))] + public static void Equals_LeftIsEqualToRight_ExpectTrue( + Failure left, Failure right) { - var failureA = new Failure(default, failureMessage); - var failureB = new Failure(); - - var actual = Failure.Equals(failureA, failureB); + var actual = Failure.Equals(left, right); Assert.True(actual); } [Theory] - [InlineData(Zero, null, null)] - [InlineData(Zero, EmptyString, null)] - [InlineData(Zero, null, EmptyString)] - [InlineData(Zero, EmptyString, EmptyString)] - [InlineData(PlusFifteen, LowerSomeString, LowerSomeString)] - [InlineData(MinusFifteen, SomeString, SomeString)] - public void Equals_FailureACodeIsEqualToFailureBCodeAndMessagesAreNullOrEmpty_ExpectTrue( - int failureCode, - string? failureAMessage, - string? failureBMessage) + [MemberData(nameof(FailureTestSource.UnequalPairTestData), MemberType = typeof(FailureTestSource))] + public static void Equals_LeftIsNotEqualToRight_ExpectFalse( + Failure left, Failure right) { - var failureA = new Failure(failureCode, failureAMessage); - var failureB = new Failure(failureCode, failureBMessage); - - var actual = Failure.Equals(failureA, failureB); - Assert.True(actual); - } - - [Theory] - [InlineData(SomeFailureCode.First, null)] - [InlineData(SomeFailureCode.Unknown, WhiteSpaceString)] - [InlineData(SomeFailureCode.Unknown, TabString)] - public void Equals_FailureAIsDefaultAndFailureBCodeIsNotDefaultOrFailureBMessageIsNotEmpty_ExpectFalse( - SomeFailureCode failureBCode, - string? failureBMessage) - { - var failureA = new Failure(); - var failureB = new Failure(failureBCode, failureBMessage); - - var actual = Failure.Equals(failureA, failureB); - Assert.False(actual); - } - - [Theory] - [InlineData(SomeFailureCode.Third, null)] - [InlineData(SomeFailureCode.Unknown, WhiteSpaceString)] - [InlineData(SomeFailureCode.Unknown, TabString)] - [InlineData(SomeFailureCode.Unknown, SomeString)] - [InlineData(SomeFailureCode.First, SomeString)] - public void Equals_FailureACodeIsNotDefaultOrFailureAMessageIsNotEmptyAndFailureBIsDefault_ExpectFalse( - SomeFailureCode failureACode, - string? failureAMessage) - { - var failureA = new Failure(failureACode, failureAMessage); - var failureB = default(Failure); - - var actual = Failure.Equals(failureA, failureB); - Assert.False(actual); - } - - [Theory] - [InlineData(Zero, null, Zero, WhiteSpaceString)] - [InlineData(Zero, EmptyString, Zero, TabString)] - [InlineData(Zero, LowerSomeString, Zero, SomeString)] - [InlineData(PlusFifteen, WhiteSpaceString, PlusFifteen, TabString)] - [InlineData(MinusFifteen, SomeString, Zero, SomeString)] - [InlineData(int.MaxValue, UpperSomeString, PlusFifteen, UpperSomeString)] - public void Equals_FailureACodeIsNotEqualToFailureBCodeAndFailureAMessageIsNotEqualToFailureBMessage_ExpectFalse( - int failureACode, string? failureAMessage, - int failureBCode, string? failureBMessage) - { - var failureA = new Failure(failureACode, failureAMessage); - var failureB = new Failure(failureBCode, failureBMessage); - - var actual = Failure.Equals(failureA, failureB); + var actual = Failure.Equals(left, right); Assert.False(actual); } } diff --git a/src/core-taggeds-failure/Failure.Tests/Test.FailureStatic/FailureStaticTest.Factory.Exception.cs b/src/core-taggeds-failure/Failure.Tests/Test.FailureStatic/FailureStaticTest.Factory.Exception.cs new file mode 100644 index 00000000..d21734a8 --- /dev/null +++ b/src/core-taggeds-failure/Failure.Tests/Test.FailureStatic/FailureStaticTest.Factory.Exception.cs @@ -0,0 +1,38 @@ +using System; +using Xunit; +using static PrimeFuncPack.Core.Tests.AssertHelper; +using static PrimeFuncPack.UnitTest.TestData; + +namespace PrimeFuncPack.Core.Tests; + +partial class FailureStaticTest +{ + [Theory] + [InlineData(SomeFailureCode.First, null, EmptyString)] + [InlineData(SomeFailureCode.Second, EmptyString, EmptyString)] + [InlineData(SomeFailureCode.Unknown, SomeString, SomeString)] + public static void CreateWithSourceException_SourceExceptionIsNull_ExpectActualExceptionIsNull( + SomeFailureCode sourceFailureCode, string? sourceFailureMessage, string expectedFailureMessage) + { + var actual = Failure.Create(sourceFailureCode, sourceFailureMessage, null); + + AssertEqualFailures( + (sourceFailureCode, expectedFailureMessage, null), + (actual.FailureCode, actual.FailureMessage, actual.SourceException)); + } + + [Theory] + [InlineData(SomeFailureCode.First, null, EmptyString)] + [InlineData(SomeFailureCode.Second, EmptyString, EmptyString)] + [InlineData(SomeFailureCode.Unknown, SomeString, SomeString)] + public static void CreateWithSourceException_SourceExceptionIsNull_ExpectActualExceptionIsSame( + SomeFailureCode sourceFailureCode, string? sourceFailureMessage, string expectedFailureMessage) + { + var sourceException = new InvalidOperationException("Some Exception Message"); + var actual = Failure.Create(sourceFailureCode, sourceFailureMessage, sourceException); + + AssertEqualFailures( + (sourceFailureCode, expectedFailureMessage, sourceException), + (actual.FailureCode, actual.FailureMessage, actual.SourceException)); + } +} diff --git a/src/core-taggeds-failure/Failure.Tests/Test.FailureStatic/FailureStaticTest.Factory.Unit.Exception.cs b/src/core-taggeds-failure/Failure.Tests/Test.FailureStatic/FailureStaticTest.Factory.Unit.Exception.cs new file mode 100644 index 00000000..a6141080 --- /dev/null +++ b/src/core-taggeds-failure/Failure.Tests/Test.FailureStatic/FailureStaticTest.Factory.Unit.Exception.cs @@ -0,0 +1,41 @@ +using System; +using Xunit; +using static PrimeFuncPack.Core.Tests.AssertHelper; +using static PrimeFuncPack.UnitTest.TestData; + +namespace PrimeFuncPack.Core.Tests; + +partial class FailureStaticTest +{ + [Theory] + [InlineData(null, EmptyString)] + [InlineData(EmptyString, EmptyString)] + [InlineData(SomeString, SomeString)] + public static void CreateUnitFailureCodeWithSourceException_SourceExceptionIsNull_ExpectActualExceptionIsNull( + string? sourceFailureMessage, string expectedFailureMessage) + { + var actual = Failure.Create(sourceFailureMessage, null); + + AssertEqualFailures( + (Unit.Value, expectedFailureMessage, null), + (actual.FailureCode, actual.FailureMessage, actual.SourceException)); + } + + [Theory] + [InlineData(null, EmptyString)] + [InlineData(EmptyString, EmptyString)] + [InlineData(SomeString, SomeString)] + public static void CreateUnitFailureCodeWithSourceException_SourceExceptionIsNotNull_ExpectActualExceptionIsSame( + string? sourceFailureMessage, string expectedFailureMessage) + { + var sourceException = new Exception( + message: "Some error message", + innerException: new InvalidOperationException("Some inner error text")); + + var actual = Failure.Create(sourceFailureMessage, sourceException); + + AssertEqualFailures( + (Unit.Value, expectedFailureMessage, sourceException), + (actual.FailureCode, actual.FailureMessage, actual.SourceException)); + } +} diff --git a/src/core-taggeds-failure/Failure.Tests/Test.FailureStatic/FailureStaticTest.Factory.Unit.cs b/src/core-taggeds-failure/Failure.Tests/Test.FailureStatic/FailureStaticTest.Factory.Unit.cs index f522bd04..e3fc81d6 100644 --- a/src/core-taggeds-failure/Failure.Tests/Test.FailureStatic/FailureStaticTest.Factory.Unit.cs +++ b/src/core-taggeds-failure/Failure.Tests/Test.FailureStatic/FailureStaticTest.Factory.Unit.cs @@ -7,29 +7,17 @@ namespace PrimeFuncPack.Core.Tests; partial class FailureStaticTest { - [Fact] - public void CreateUnitFailureCode_SourceFailureMessageIsNull_ExpectFailureCodeIsUnitAndMessageIsEmpty() - { - var actual = Failure.Create(null); - - AssertEqualFailures( - (Unit.Value, EmptyString), - (actual.FailureCode, actual.FailureMessage)); - } - [Theory] - [InlineData(EmptyString)] - [InlineData(WhiteSpaceString)] - [InlineData(TabString)] - [InlineData(LowerSomeString)] - [InlineData(SomeString)] - public void CreateUnitFailureCode_SourceFailureMessageIsNotNull_ExpectFailureCodeIsUnitAndMessageIsEqualToSource( - string sourceFailureMessage) + [InlineData(null, EmptyString)] + [InlineData(EmptyString, EmptyString)] + [InlineData(SomeString, SomeString)] + public static void CreateUnitFailureCode_ExpectActualValues( + string? sourceFailureMessage, string expectedFailureMessage) { var actual = Failure.Create(sourceFailureMessage); AssertEqualFailures( - (Unit.Value, sourceFailureMessage), - (actual.FailureCode, actual.FailureMessage)); + (Unit.Value, expectedFailureMessage, null), + (actual.FailureCode, actual.FailureMessage, actual.SourceException)); } } diff --git a/src/core-taggeds-failure/Failure.Tests/Test.FailureStatic/FailureStaticTest.Factory.cs b/src/core-taggeds-failure/Failure.Tests/Test.FailureStatic/FailureStaticTest.Factory.cs index e737d277..6e7f0fb5 100644 --- a/src/core-taggeds-failure/Failure.Tests/Test.FailureStatic/FailureStaticTest.Factory.cs +++ b/src/core-taggeds-failure/Failure.Tests/Test.FailureStatic/FailureStaticTest.Factory.cs @@ -8,40 +8,16 @@ namespace PrimeFuncPack.Core.Tests; partial class FailureStaticTest { [Theory] - [InlineData(int.MinValue)] - [InlineData(MinusFifteen)] - [InlineData(Zero)] - [InlineData(PlusFifteen)] - [InlineData(int.MaxValue)] - public void Create_SourceFailureMessageIsNull_ExpectFailureCodeIsEqualToSourceAndMessageIsEmpty( - int sourceFailureCode) - { - var actual = Failure.Create(sourceFailureCode, null); - - AssertEqualFailures( - (sourceFailureCode, EmptyString), - (actual.FailureCode, actual.FailureMessage)); - } - - [Theory] - [InlineData(SomeFailureCode.Unknown, EmptyString)] - [InlineData(SomeFailureCode.Unknown, WhiteSpaceString)] - [InlineData(SomeFailureCode.Unknown, TabString)] - [InlineData(SomeFailureCode.Unknown, SomeString)] - [InlineData(SomeFailureCode.First, EmptyString)] - [InlineData(SomeFailureCode.Second, WhiteSpaceString)] - [InlineData(SomeFailureCode.Third, TabString)] - [InlineData(SomeFailureCode.Second, LowerSomeString)] - [InlineData(SomeFailureCode.First, SomeString)] - [InlineData(SomeFailureCode.Third, UpperSomeString)] - public void Create_SourceFailureMessageIsNotNull_ExpectFailureCodeAndMessageAreEqualToSource( - SomeFailureCode sourceFailureCode, - string sourceFailureMessage) + [InlineData(SomeFailureCode.First, null, EmptyString)] + [InlineData(SomeFailureCode.Second, EmptyString, EmptyString)] + [InlineData(SomeFailureCode.Unknown, SomeString, SomeString)] + public static void Create_ExpectActualValues( + SomeFailureCode sourceFailureCode, string? sourceFailureMessage, string expectedFailureMessage) { var actual = Failure.Create(sourceFailureCode, sourceFailureMessage); AssertEqualFailures( - (sourceFailureCode, sourceFailureMessage), - (actual.FailureCode, actual.FailureMessage)); + (sourceFailureCode, expectedFailureMessage, null), + (actual.FailureCode, actual.FailureMessage, actual.SourceException)); } } diff --git a/src/core-taggeds-failure/Failure.Tests/Test.FailureStatic/FailureStaticTest.cs b/src/core-taggeds-failure/Failure.Tests/Test.FailureStatic/FailureStaticTest.cs index 4ba82b6f..ca009d0e 100644 --- a/src/core-taggeds-failure/Failure.Tests/Test.FailureStatic/FailureStaticTest.cs +++ b/src/core-taggeds-failure/Failure.Tests/Test.FailureStatic/FailureStaticTest.cs @@ -1,5 +1,5 @@ namespace PrimeFuncPack.Core.Tests; -public sealed partial class FailureStaticTest +public static partial class FailureStaticTest { } diff --git a/src/core-taggeds-failure/Failure/Failure.T/Failure.T.Deconstruct.cs b/src/core-taggeds-failure/Failure/Failure.T/Failure.T.Deconstruct.cs index 23050514..41580a02 100644 --- a/src/core-taggeds-failure/Failure/Failure.T/Failure.T.Deconstruct.cs +++ b/src/core-taggeds-failure/Failure/Failure.T/Failure.T.Deconstruct.cs @@ -7,4 +7,11 @@ public void Deconstruct(out TFailureCode failureCode, out string failureMessage) failureCode = FailureCode; failureMessage = FailureMessage; } + + public void Deconstruct(out TFailureCode failureCode, out string failureMessage, out Exception? sourceException) + { + failureCode = FailureCode; + failureMessage = FailureMessage; + sourceException = SourceException; + } } diff --git a/src/core-taggeds-failure/Failure/Failure.T/Failure.T.Equality.Common.cs b/src/core-taggeds-failure/Failure/Failure.T/Failure.T.Equality.Common.cs index f1e312f1..bb68578f 100644 --- a/src/core-taggeds-failure/Failure/Failure.T/Failure.T.Equality.Common.cs +++ b/src/core-taggeds-failure/Failure/Failure.T/Failure.T.Equality.Common.cs @@ -11,4 +11,8 @@ private static EqualityComparer FailureCodeComparer private static StringComparer FailureMessageComparer => StringComparer.Ordinal; + + private static EqualityComparer SourceExceptionComparer + => + EqualityComparer.Default; } diff --git a/src/core-taggeds-failure/Failure/Failure.T/Failure.T.Equality.Equals.cs b/src/core-taggeds-failure/Failure/Failure.T/Failure.T.Equality.Equals.cs index 2cee4aa1..829b879d 100644 --- a/src/core-taggeds-failure/Failure/Failure.T/Failure.T.Equality.Equals.cs +++ b/src/core-taggeds-failure/Failure/Failure.T/Failure.T.Equality.Equals.cs @@ -5,5 +5,6 @@ partial struct Failure public bool Equals(Failure other) => FailureCodeComparer.Equals(FailureCode, other.FailureCode) && - FailureMessageComparer.Equals(FailureMessage, other.FailureMessage); + FailureMessageComparer.Equals(FailureMessage, other.FailureMessage) && + SourceExceptionComparer.Equals(SourceException, other.SourceException); } diff --git a/src/core-taggeds-failure/Failure/Failure.T/Failure.T.Equality.GetHashCode.cs b/src/core-taggeds-failure/Failure/Failure.T/Failure.T.Equality.GetHashCode.cs index f32264f6..a7052e37 100644 --- a/src/core-taggeds-failure/Failure/Failure.T/Failure.T.Equality.GetHashCode.cs +++ b/src/core-taggeds-failure/Failure/Failure.T/Failure.T.Equality.GetHashCode.cs @@ -6,11 +6,21 @@ namespace System; partial struct Failure { public override int GetHashCode() - => - HashCode.Combine( + { + if (SourceException is not null) + { + return HashCode.Combine( + EqualityContractHashCode(), + FailureCodeComparer.GetHashCode(FailureCode), + FailureMessageComparer.GetHashCode(FailureMessage), + SourceExceptionComparer.GetHashCode(SourceException)); + } + + return HashCode.Combine( EqualityContractHashCode(), FailureCodeComparer.GetHashCode(FailureCode), FailureMessageComparer.GetHashCode(FailureMessage)); + } [MethodImpl(MethodImplOptions.AggressiveInlining)] private static int EqualityContractHashCode() diff --git a/src/core-taggeds-failure/Failure/Failure.T/Failure.T.MapFailureCode.cs b/src/core-taggeds-failure/Failure/Failure.T/Failure.T.MapFailureCode.cs index 11ca205b..b82f8737 100644 --- a/src/core-taggeds-failure/Failure/Failure.T/Failure.T.MapFailureCode.cs +++ b/src/core-taggeds-failure/Failure/Failure.T/Failure.T.MapFailureCode.cs @@ -19,5 +19,8 @@ private Failure InnerMapFailureCode( new( mapFailureCode.Invoke(FailureCode), failureMessage, // pass the inner state - default); + default) + { + SourceException = SourceException + }; } diff --git a/src/core-taggeds-failure/Failure/Failure.T/Failure.T.ToString.cs b/src/core-taggeds-failure/Failure/Failure.T/Failure.T.ToString.cs index 777e4a3b..76fffbd7 100644 --- a/src/core-taggeds-failure/Failure/Failure.T/Failure.T.ToString.cs +++ b/src/core-taggeds-failure/Failure/Failure.T/Failure.T.ToString.cs @@ -1,3 +1,4 @@ +using System.Text; using static System.FormattableString; namespace System; @@ -6,6 +7,23 @@ partial struct Failure { public override string ToString() => - Invariant( - $"Failure<{typeof(TFailureCode).Name}>:{{ \"FailureCode\": {FailureCode}, \"FailureMessage\": \"{FailureMessage}\" }}"); + new StringBuilder( + "Failure<") + .Append( + typeof(TFailureCode).Name) + .Append( + ">:{ \"FailureCode\": ") + .Append( + FailureCode.ToString()) + .Append( + ", \"FailureMessage\": \"") + .Append( + FailureMessage) + .Append( + "\", \"SourceException\": ") + .Append( + SourceException is null ? "null" : Invariant($"\"{SourceException}\"")) + .Append( + " }") + .ToString(); } diff --git a/src/core-taggeds-failure/Failure/Failure.T/Failure.T.cs b/src/core-taggeds-failure/Failure/Failure.T/Failure.T.cs index c8d06016..518289c4 100644 --- a/src/core-taggeds-failure/Failure/Failure.T/Failure.T.cs +++ b/src/core-taggeds-failure/Failure/Failure.T/Failure.T.cs @@ -16,12 +16,14 @@ private Failure(TFailureCode failureCode, string? failureMessage, int _) this.failureCode = failureCode; this.failureMessage = failureMessage; + SourceException = null; } public Failure(TFailureCode failureCode, [AllowNull] string failureMessage) { this.failureCode = failureCode; this.failureMessage = string.IsNullOrEmpty(failureMessage) ? null : failureMessage; + SourceException = null; Debug.Assert(this.failureMessage is null || this.failureMessage.Length != default); } @@ -33,4 +35,6 @@ public TFailureCode FailureCode public string FailureMessage => failureMessage ?? string.Empty; -} + + public Exception? SourceException { get; init; } +} \ No newline at end of file diff --git a/src/core-taggeds-failure/Failure/Failure.csproj b/src/core-taggeds-failure/Failure/Failure.csproj index 2a89d315..422a0c1c 100644 --- a/src/core-taggeds-failure/Failure/Failure.csproj +++ b/src/core-taggeds-failure/Failure/Failure.csproj @@ -17,7 +17,7 @@ PrimeFuncPack Core.Failure is a core library for .NET consisting of Failure type targeted for use in functional programming. System PrimeFuncPack.Core.Failure - 2.0.2-rc.2 + 2.1.0-preview.1 diff --git a/src/core-taggeds-failure/Failure/Failure/Failure.Factory.cs b/src/core-taggeds-failure/Failure/Failure/Failure.Factory.cs index 80b3ff49..9168b2e0 100644 --- a/src/core-taggeds-failure/Failure/Failure/Failure.Factory.cs +++ b/src/core-taggeds-failure/Failure/Failure/Failure.Factory.cs @@ -10,7 +10,23 @@ public static Failure Create( => new(failureCode, failureMessage); + public static Failure Create( + TFailureCode failureCode, [AllowNull] string failureMessage, Exception? sourceException) + where TFailureCode : struct + => + new(failureCode, failureMessage) + { + SourceException = sourceException + }; + public static Failure Create([AllowNull] string failureMessage) => new(default, failureMessage); + + public static Failure Create([AllowNull] string failureMessage, Exception? sourceException) + => + new(default, failureMessage) + { + SourceException = sourceException + }; } From 9c0486fbf2d7d2345475d72c10f7600002a31096 Mon Sep 17 00:00:00 2001 From: pmosk Date: Wed, 9 Aug 2023 21:24:32 +0400 Subject: [PATCH 2/3] Add FailureExceptionExtensions --- .../FailureExceptionExtensionsTest.cs | 5 +++ .../Test.ToFailure.Unit.cs | 42 +++++++++++++++++++ .../Test.ToFailure.cs | 39 +++++++++++++++++ .../Failure/Failure.T/Failure.T.ToString.cs | 28 ++++--------- .../Extensions.ToFailure.cs | 23 ++++++++++ .../FailureExceptionExtensions.cs | 5 +++ src/core-taggeds/Taggeds/Taggeds.csproj | 4 +- 7 files changed, 124 insertions(+), 22 deletions(-) create mode 100644 src/core-taggeds-failure/Failure.Tests/Test.FailureExceptionExtensions/FailureExceptionExtensionsTest.cs create mode 100644 src/core-taggeds-failure/Failure.Tests/Test.FailureExceptionExtensions/Test.ToFailure.Unit.cs create mode 100644 src/core-taggeds-failure/Failure.Tests/Test.FailureExceptionExtensions/Test.ToFailure.cs create mode 100644 src/core-taggeds-failure/Failure/FailureExceptionExtensions/Extensions.ToFailure.cs create mode 100644 src/core-taggeds-failure/Failure/FailureExceptionExtensions/FailureExceptionExtensions.cs diff --git a/src/core-taggeds-failure/Failure.Tests/Test.FailureExceptionExtensions/FailureExceptionExtensionsTest.cs b/src/core-taggeds-failure/Failure.Tests/Test.FailureExceptionExtensions/FailureExceptionExtensionsTest.cs new file mode 100644 index 00000000..f9e9efb2 --- /dev/null +++ b/src/core-taggeds-failure/Failure.Tests/Test.FailureExceptionExtensions/FailureExceptionExtensionsTest.cs @@ -0,0 +1,5 @@ +namespace PrimeFuncPack.Core.Tests; + +public static partial class FailureExceptionExtensionsTest +{ +} \ No newline at end of file diff --git a/src/core-taggeds-failure/Failure.Tests/Test.FailureExceptionExtensions/Test.ToFailure.Unit.cs b/src/core-taggeds-failure/Failure.Tests/Test.FailureExceptionExtensions/Test.ToFailure.Unit.cs new file mode 100644 index 00000000..fd3d865e --- /dev/null +++ b/src/core-taggeds-failure/Failure.Tests/Test.FailureExceptionExtensions/Test.ToFailure.Unit.cs @@ -0,0 +1,42 @@ +using System; +using Xunit; +using static PrimeFuncPack.Core.Tests.AssertHelper; +using static PrimeFuncPack.UnitTest.TestData; + +namespace PrimeFuncPack.Core.Tests; + +partial class FailureExceptionExtensionsTest +{ + [Theory] + [InlineData(null, EmptyString)] + [InlineData(EmptyString, EmptyString)] + [InlineData(SomeString, SomeString)] + public static void ToFailureUnitFailureCode_SourceExceptionIsNull_ExpectActualExceptionIsNull( + string? sourceFailureMessage, string expectedFailureMessage) + { + Exception? sourceException = null; + var actual = sourceException.ToFailure(sourceFailureMessage); + + AssertEqualFailures( + (Unit.Value, expectedFailureMessage, null), + (actual.FailureCode, actual.FailureMessage, actual.SourceException)); + } + + [Theory] + [InlineData(null, EmptyString)] + [InlineData(EmptyString, EmptyString)] + [InlineData(SomeString, SomeString)] + public static void ToFailureUnitFailureCode_SourceExceptionIsNotNull_ExpectActualExceptionIsSame( + string? sourceFailureMessage, string expectedFailureMessage) + { + var sourceException = new InvalidOperationException( + message: "Some error message", + innerException: new Exception("Some inner error text")); + + var actual = sourceException.ToFailure(sourceFailureMessage); + + AssertEqualFailures( + (Unit.Value, expectedFailureMessage, sourceException), + (actual.FailureCode, actual.FailureMessage, actual.SourceException)); + } +} \ No newline at end of file diff --git a/src/core-taggeds-failure/Failure.Tests/Test.FailureExceptionExtensions/Test.ToFailure.cs b/src/core-taggeds-failure/Failure.Tests/Test.FailureExceptionExtensions/Test.ToFailure.cs new file mode 100644 index 00000000..c11d1329 --- /dev/null +++ b/src/core-taggeds-failure/Failure.Tests/Test.FailureExceptionExtensions/Test.ToFailure.cs @@ -0,0 +1,39 @@ +using System; +using Xunit; +using static PrimeFuncPack.Core.Tests.AssertHelper; +using static PrimeFuncPack.UnitTest.TestData; + +namespace PrimeFuncPack.Core.Tests; + +partial class FailureExceptionExtensionsTest +{ + [Theory] + [InlineData(SomeFailureCode.First, null, EmptyString)] + [InlineData(SomeFailureCode.Second, EmptyString, EmptyString)] + [InlineData(SomeFailureCode.Unknown, SomeString, SomeString)] + public static void ToFailure_SourceExceptionIsNull_ExpectActualExceptionIsNull( + SomeFailureCode sourceFailureCode, string? sourceFailureMessage, string expectedFailureMessage) + { + Exception? sourceException = null; + var actual = sourceException.ToFailure(sourceFailureCode, sourceFailureMessage); + + AssertEqualFailures( + (sourceFailureCode, expectedFailureMessage, null), + (actual.FailureCode, actual.FailureMessage, actual.SourceException)); + } + + [Theory] + [InlineData(SomeFailureCode.First, null, EmptyString)] + [InlineData(SomeFailureCode.Second, EmptyString, EmptyString)] + [InlineData(SomeFailureCode.Unknown, SomeString, SomeString)] + public static void ToFailure_SourceExceptionIsNull_ExpectActualExceptionIsSame( + SomeFailureCode sourceFailureCode, string? sourceFailureMessage, string expectedFailureMessage) + { + var sourceException = new Exception("Some Exception Message"); + var actual = sourceException.ToFailure(sourceFailureCode, sourceFailureMessage); + + AssertEqualFailures( + (sourceFailureCode, expectedFailureMessage, sourceException), + (actual.FailureCode, actual.FailureMessage, actual.SourceException)); + } +} \ No newline at end of file diff --git a/src/core-taggeds-failure/Failure/Failure.T/Failure.T.ToString.cs b/src/core-taggeds-failure/Failure/Failure.T/Failure.T.ToString.cs index 76fffbd7..a3388c2d 100644 --- a/src/core-taggeds-failure/Failure/Failure.T/Failure.T.ToString.cs +++ b/src/core-taggeds-failure/Failure/Failure.T/Failure.T.ToString.cs @@ -1,4 +1,4 @@ -using System.Text; +using System.Runtime.CompilerServices; using static System.FormattableString; namespace System; @@ -7,23 +7,11 @@ partial struct Failure { public override string ToString() => - new StringBuilder( - "Failure<") - .Append( - typeof(TFailureCode).Name) - .Append( - ">:{ \"FailureCode\": ") - .Append( - FailureCode.ToString()) - .Append( - ", \"FailureMessage\": \"") - .Append( - FailureMessage) - .Append( - "\", \"SourceException\": ") - .Append( - SourceException is null ? "null" : Invariant($"\"{SourceException}\"")) - .Append( - " }") - .ToString(); + Invariant( + $"Failure<{typeof(TFailureCode).Name}>:{{ \"FailureCode\": {FailureCode}, \"FailureMessage\": \"{FailureMessage}\", \"SourceException\": {InnerSourceExceptionString()} }}"); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private string InnerSourceExceptionString() + => + SourceException is null ? "null" : Invariant($"\"{SourceException}\""); } diff --git a/src/core-taggeds-failure/Failure/FailureExceptionExtensions/Extensions.ToFailure.cs b/src/core-taggeds-failure/Failure/FailureExceptionExtensions/Extensions.ToFailure.cs new file mode 100644 index 00000000..b2fb2e31 --- /dev/null +++ b/src/core-taggeds-failure/Failure/FailureExceptionExtensions/Extensions.ToFailure.cs @@ -0,0 +1,23 @@ +using System.Diagnostics.CodeAnalysis; + +namespace System; + +partial class FailureExceptionExtensions +{ + public static Failure ToFailure( + this Exception? sourceException, TFailureCode failureCode, [AllowNull] string failureMessage) + where TFailureCode : struct + => + new(failureCode, failureMessage) + { + SourceException = sourceException + }; + + public static Failure ToFailure( + this Exception? sourceException, [AllowNull] string failureMessage) + => + new(default, failureMessage) + { + SourceException = sourceException + }; +} \ No newline at end of file diff --git a/src/core-taggeds-failure/Failure/FailureExceptionExtensions/FailureExceptionExtensions.cs b/src/core-taggeds-failure/Failure/FailureExceptionExtensions/FailureExceptionExtensions.cs new file mode 100644 index 00000000..bca95270 --- /dev/null +++ b/src/core-taggeds-failure/Failure/FailureExceptionExtensions/FailureExceptionExtensions.cs @@ -0,0 +1,5 @@ +namespace System; + +public static partial class FailureExceptionExtensions +{ +} \ No newline at end of file diff --git a/src/core-taggeds/Taggeds/Taggeds.csproj b/src/core-taggeds/Taggeds/Taggeds.csproj index 78fc4f02..432a930e 100644 --- a/src/core-taggeds/Taggeds/Taggeds.csproj +++ b/src/core-taggeds/Taggeds/Taggeds.csproj @@ -17,7 +17,7 @@ PrimeFuncPack Core.Taggeds is a core pack for .NET consisting of fundamental tagged types targeted for use in functional programming: Optional and Result monads, Tagged Union, as well as the convert extensions. System PrimeFuncPack.Core.Taggeds - 2.2.0-preview.3 + 2.2.0-preview.4 @@ -32,7 +32,7 @@ - + From 76f037ca2c7893422962071c7607c5ec2baf02c3 Mon Sep 17 00:00:00 2001 From: pmosk Date: Wed, 9 Aug 2023 21:27:59 +0400 Subject: [PATCH 3/3] Minimal improvements --- .../Failure.T/Failure.T.Equality.GetHashCode.cs | 17 ++++++++--------- .../Failure/Failure.T/Failure.T.cs | 4 +--- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/core-taggeds-failure/Failure/Failure.T/Failure.T.Equality.GetHashCode.cs b/src/core-taggeds-failure/Failure/Failure.T/Failure.T.Equality.GetHashCode.cs index a7052e37..bd3bd896 100644 --- a/src/core-taggeds-failure/Failure/Failure.T/Failure.T.Equality.GetHashCode.cs +++ b/src/core-taggeds-failure/Failure/Failure.T/Failure.T.Equality.GetHashCode.cs @@ -7,19 +7,18 @@ partial struct Failure { public override int GetHashCode() { + HashCode builder = new(); + + builder.Add(EqualityContractHashCode()); + builder.Add(FailureCodeComparer.GetHashCode(FailureCode)); + builder.Add(FailureMessageComparer.GetHashCode(FailureMessage)); + if (SourceException is not null) { - return HashCode.Combine( - EqualityContractHashCode(), - FailureCodeComparer.GetHashCode(FailureCode), - FailureMessageComparer.GetHashCode(FailureMessage), - SourceExceptionComparer.GetHashCode(SourceException)); + builder.Add(SourceExceptionComparer.GetHashCode(SourceException)); } - return HashCode.Combine( - EqualityContractHashCode(), - FailureCodeComparer.GetHashCode(FailureCode), - FailureMessageComparer.GetHashCode(FailureMessage)); + return builder.ToHashCode(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/core-taggeds-failure/Failure/Failure.T/Failure.T.cs b/src/core-taggeds-failure/Failure/Failure.T/Failure.T.cs index 518289c4..e2f763f0 100644 --- a/src/core-taggeds-failure/Failure/Failure.T/Failure.T.cs +++ b/src/core-taggeds-failure/Failure/Failure.T/Failure.T.cs @@ -16,14 +16,12 @@ private Failure(TFailureCode failureCode, string? failureMessage, int _) this.failureCode = failureCode; this.failureMessage = failureMessage; - SourceException = null; } public Failure(TFailureCode failureCode, [AllowNull] string failureMessage) { this.failureCode = failureCode; this.failureMessage = string.IsNullOrEmpty(failureMessage) ? null : failureMessage; - SourceException = null; Debug.Assert(this.failureMessage is null || this.failureMessage.Length != default); } @@ -36,5 +34,5 @@ public string FailureMessage => failureMessage ?? string.Empty; - public Exception? SourceException { get; init; } + public Exception? SourceException { get; init; } = null; } \ No newline at end of file