Skip to content

Commit

Permalink
Merge pull request #133 from pfpack/feature/impl-failure-exception
Browse files Browse the repository at this point in the history
Add first implementation of FailureException
  • Loading branch information
andreise authored Dec 4, 2023
2 parents 2149e4e + 4fe8ab1 commit c90c924
Show file tree
Hide file tree
Showing 39 changed files with 278 additions and 142 deletions.
3 changes: 2 additions & 1 deletion src/core-taggeds-failure/Failure.Tests/Failure.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="PrimeFuncPack.UnitTest.Data" Version="3.0.0" />
<PackageReference Include="DeepEqual" Version="4.2.1" />
<PackageReference Include="PrimeFuncPack.UnitTest.Data" Version="3.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.4">
Expand Down
42 changes: 21 additions & 21 deletions src/core-taggeds-failure/Failure.Tests/Source/Source.Equal.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using PrimeFuncPack.UnitTest;

Expand All @@ -12,65 +12,65 @@ public static IEnumerable<object[]> EqualPairTestData
{
new object[]
{
new Failure<SomeFailureCode>(),
default(Failure<SomeFailureCode>)
new Failure<EnumType>(),
default(Failure<EnumType>)
},
new object[]
{
new Failure<SomeFailureCode>(),
new Failure<SomeFailureCode>(default, null)
new Failure<EnumType>(),
new Failure<EnumType>(default, null)
},
new object[]
{
default(Failure<SomeFailureCode>),
new Failure<SomeFailureCode>(default, string.Empty)
default(Failure<EnumType>),
new Failure<EnumType>(default, string.Empty)
{
SourceException = null
}
},
new object[]
{
new Failure<SomeFailureCode>(default, null),
new Failure<SomeFailureCode>()
new Failure<EnumType>(default, null),
new Failure<EnumType>()
},
new object[]
{
new Failure<SomeFailureCode>(default, string.Empty),
default(Failure<SomeFailureCode>)
new Failure<EnumType>(default, string.Empty),
default(Failure<EnumType>)
},
new object[]
{
new Failure<SomeFailureCode>(SomeFailureCode.First, null)
new Failure<EnumType>(EnumType.One, null)
{
SourceException = SomeException.SomeInstance
},
new Failure<SomeFailureCode>(SomeFailureCode.First, null)
new Failure<EnumType>(EnumType.One, null)
{
SourceException = SomeException.SomeInstance
}
},
new object[]
{
new Failure<SomeFailureCode>(SomeFailureCode.Second, string.Empty),
new Failure<SomeFailureCode>(SomeFailureCode.Second, null)
new Failure<EnumType>(EnumType.Two, string.Empty),
new Failure<EnumType>(EnumType.Two, null)
},
new object[]
{
new Failure<SomeFailureCode>(SomeFailureCode.Third, null),
new Failure<SomeFailureCode>(SomeFailureCode.Third, string.Empty)
new Failure<EnumType>(EnumType.Three, null),
new Failure<EnumType>(EnumType.Three, string.Empty)
},
new object[]
{
new Failure<SomeFailureCode>(SomeFailureCode.Unknown, string.Empty),
new Failure<SomeFailureCode>(SomeFailureCode.Unknown, string.Empty)
new Failure<EnumType>(EnumType.Zero, string.Empty),
new Failure<EnumType>(EnumType.Zero, string.Empty)
},
new object[]
{
new Failure<SomeFailureCode>(SomeFailureCode.First, TestData.SomeString)
new Failure<EnumType>(EnumType.One, TestData.SomeString)
{
SourceException = SomeException.SomeInstance
},
new Failure<SomeFailureCode>(SomeFailureCode.First, TestData.SomeString)
new Failure<EnumType>(EnumType.One, TestData.SomeString)
{
SourceException = SomeException.SomeInstance
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using PrimeFuncPack.UnitTest;

namespace PrimeFuncPack.Core.Tests;

partial class FailureTestSource
{
public static IEnumerable<object[]> ToExceptionTestData
=>
new[]
{
new object[]
{
default(Failure<EnumType>),
InnerCreate(default(EnumType), null, null)
},
new object[]
{
new Failure<EnumType>(EnumType.One, null),
InnerCreate(EnumType.One, null, null)
},
new object[]
{
new Failure<EnumType>(EnumType.Two, string.Empty),
InnerCreate(EnumType.Two, null, null)
},
new object[]
{
new Failure<EnumType>(EnumType.Zero, TestData.WhiteSpaceString),
InnerCreate(EnumType.Zero, TestData.WhiteSpaceString, null)
},
new object[]
{
new Failure<EnumType>(EnumType.Two, TestData.SomeString),
InnerCreate(EnumType.Two, TestData.SomeString, null)
},
new object[]
{
new Failure<EnumType>(EnumType.Three, null)
{
SourceException = new InvalidOperationException("Some error message")
},
InnerCreate(EnumType.Three, null, new InvalidOperationException("Some error message"))
},
new object[]
{
new Failure<EnumType>(EnumType.One, string.Empty)
{
SourceException = new("Some Exception")
},
InnerCreate(EnumType.One, null, new("Some Exception"))
},
new object[]
{
new Failure<EnumType>(EnumType.One, TestData.MixedWhiteSpacesString)
{
SourceException = new("Some Exception")
},
InnerCreate(EnumType.One, TestData.MixedWhiteSpacesString, new("Some Exception"))
},
new object[]
{
new Failure<EnumType>(EnumType.Two, TestData.AnotherString)
{
SourceException = new InvalidCastException("Some error text", new SomeException())
},
InnerCreate(EnumType.Two, TestData.AnotherString, new InvalidCastException("Some error text", new SomeException()))
}
};

private static Failure<TFailureCode>.Exception InnerCreate<TFailureCode>(
TFailureCode failureCode, string? message, Exception? innerException)
where TFailureCode : struct
{
var type = typeof(Failure<TFailureCode>.Exception);

var constructor = typeof(Failure<TFailureCode>.Exception).GetConstructor(
bindingAttr: BindingFlags.NonPublic | BindingFlags.Instance,
types: new[] { typeof(TFailureCode), typeof(string), typeof(Exception) })
?? throw new InvalidOperationException($"Required constructor in type {type} was not found");

return (Failure<TFailureCode>.Exception)constructor.Invoke(new object?[] { failureCode, message, innerException });
}
}
30 changes: 15 additions & 15 deletions src/core-taggeds-failure/Failure.Tests/Source/Source.Unequal.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using PrimeFuncPack.UnitTest;

Expand All @@ -12,47 +12,47 @@ public static IEnumerable<object[]> UnequalPairTestData
{
new object[]
{
new Failure<SomeFailureCode>(),
new Failure<SomeFailureCode>(SomeFailureCode.First, null)
new Failure<EnumType>(),
new Failure<EnumType>(EnumType.One, null)
},
new object[]
{
default(Failure<SomeFailureCode>),
new Failure<SomeFailureCode>(SomeFailureCode.Second, TestData.WhiteSpaceString)
default(Failure<EnumType>),
new Failure<EnumType>(EnumType.Two, TestData.WhiteSpaceString)
},
new object[]
{
new Failure<SomeFailureCode>(SomeFailureCode.Third, null),
default(Failure<SomeFailureCode>)
new Failure<EnumType>(EnumType.Three, null),
default(Failure<EnumType>)
},
new object[]
{
new Failure<SomeFailureCode>(SomeFailureCode.First, TestData.TabString),
new Failure<SomeFailureCode>()
new Failure<EnumType>(EnumType.One, TestData.TabString),
new Failure<EnumType>()
},
new object[]
{
new Failure<SomeFailureCode>(SomeFailureCode.First, TestData.SomeString),
new Failure<SomeFailureCode>(SomeFailureCode.Second, TestData.SomeString)
new Failure<EnumType>(EnumType.One, TestData.SomeString),
new Failure<EnumType>(EnumType.Two, TestData.SomeString)
},
new object[]
{
new Failure<SomeFailureCode>(SomeFailureCode.First, TestData.SomeString)
new Failure<EnumType>(EnumType.One, TestData.SomeString)
{
SourceException = SomeException.SomeInstance
},
new Failure<SomeFailureCode>(SomeFailureCode.First, TestData.UpperSomeString)
new Failure<EnumType>(EnumType.One, TestData.UpperSomeString)
{
SourceException = SomeException.SomeInstance
}
},
new object[]
{
new Failure<SomeFailureCode>(SomeFailureCode.First, TestData.SomeString)
new Failure<EnumType>(EnumType.One, TestData.SomeString)
{
SourceException = new SomeException()
},
new Failure<SomeFailureCode>(SomeFailureCode.First, TestData.SomeString)
new Failure<EnumType>(EnumType.One, TestData.SomeString)
{
SourceException = new SomeException()
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using PrimeFuncPack.UnitTest;
using Xunit;
using static PrimeFuncPack.Core.Tests.AssertHelper;
using static PrimeFuncPack.UnitTest.TestData;
Expand All @@ -24,15 +25,15 @@ public static void DeconstructWithSourceException_SourceExceptionIsNull_ExpectCo
}

[Theory]
[InlineData(SomeFailureCode.First, null, EmptyString)]
[InlineData(SomeFailureCode.Unknown, EmptyString, EmptyString)]
[InlineData(SomeFailureCode.Second, SomeString, SomeString)]
[InlineData(EnumType.One, null, EmptyString)]
[InlineData(EnumType.Zero, EmptyString, EmptyString)]
[InlineData(EnumType.Two, SomeString, SomeString)]
public static void DeconstructWithSourceException_SourceExceptionIsNotNull_ExpectCorrectValue(
SomeFailureCode sourceFailureCode, string? sourceFailureMessage, string expectedFailureMessage)
EnumType sourceFailureCode, string? sourceFailureMessage, string expectedFailureMessage)
{
var sourceException = new Exception();

var source = new Failure<SomeFailureCode>(sourceFailureCode, sourceFailureMessage)
var source = new Failure<EnumType>(sourceFailureCode, sourceFailureMessage)
{
SourceException = sourceException
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using PrimeFuncPack.UnitTest;
using Xunit;
using static PrimeFuncPack.Core.Tests.AssertHelper;
using static PrimeFuncPack.UnitTest.TestData;
Expand Down Expand Up @@ -26,20 +27,20 @@ public static void Deconstruct_SourceFailureMessageIsNull_ExpectFailureCodeIsEqu
}

[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)]
[InlineData(EnumType.Zero, EmptyString)]
[InlineData(EnumType.Zero, WhiteSpaceString)]
[InlineData(EnumType.Zero, TabString)]
[InlineData(EnumType.Zero, SomeString)]
[InlineData(EnumType.One, EmptyString)]
[InlineData(EnumType.Two, WhiteSpaceString)]
[InlineData(EnumType.Three, TabString)]
[InlineData(EnumType.Two, LowerSomeString)]
[InlineData(EnumType.One, SomeString)]
[InlineData(EnumType.Three, UpperSomeString)]
public static void Deconstruct_SourceFailureMessageIsNotNull_ExpectFailureCodeAndMessageAreEqualToSource(
SomeFailureCode sourceFailureCode, string sourceFailureMessage)
EnumType sourceFailureCode, string sourceFailureMessage)
{
var source = new Failure<SomeFailureCode>(sourceFailureCode, sourceFailureMessage);
var source = new Failure<EnumType>(sourceFailureCode, sourceFailureMessage);

var (actualFailureCode, actualFailureMessage) = source;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using PrimeFuncPack.UnitTest;
using Xunit;

namespace PrimeFuncPack.Core.Tests;
Expand All @@ -8,7 +9,7 @@ partial class FailureTest
[Theory]
[MemberData(nameof(FailureTestSource.EqualPairTestData), MemberType = typeof(FailureTestSource))]
public static void Equality_LeftIsEqualToRight_ExpectTrue(
Failure<SomeFailureCode> left, Failure<SomeFailureCode> right)
Failure<EnumType> left, Failure<EnumType> right)
{
var actual = left == right;
Assert.True(actual);
Expand All @@ -17,7 +18,7 @@ public static void Equality_LeftIsEqualToRight_ExpectTrue(
[Theory]
[MemberData(nameof(FailureTestSource.UnequalPairTestData), MemberType = typeof(FailureTestSource))]
public static void Equality_LeftIsNotEqualToRight_ExpectTrue(
Failure<SomeFailureCode> left, Failure<SomeFailureCode> right)
Failure<EnumType> left, Failure<EnumType> right)
{
var actual = left == right;
Assert.False(actual);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using PrimeFuncPack.UnitTest;
using Xunit;

namespace PrimeFuncPack.Core.Tests;
Expand All @@ -8,7 +9,7 @@ partial class FailureTest
[Theory]
[MemberData(nameof(FailureTestSource.EqualPairTestData), MemberType = typeof(FailureTestSource))]
public static void Inequality_LeftIsEqualToRight_ExpectFalse(
Failure<SomeFailureCode> left, Failure<SomeFailureCode> right)
Failure<EnumType> left, Failure<EnumType> right)
{
var actual = left != right;
Assert.False(actual);
Expand All @@ -17,7 +18,7 @@ public static void Inequality_LeftIsEqualToRight_ExpectFalse(
[Theory]
[MemberData(nameof(FailureTestSource.UnequalPairTestData), MemberType = typeof(FailureTestSource))]
public static void Inequality_LeftIsNotEqualToRight_ExpectTrue(
Failure<SomeFailureCode> left, Failure<SomeFailureCode> right)
Failure<EnumType> left, Failure<EnumType> right)
{
var actual = left != right;
Assert.True(actual);
Expand Down
Loading

0 comments on commit c90c924

Please sign in to comment.