Skip to content

Commit

Permalink
Add SourceException
Browse files Browse the repository at this point in the history
  • Loading branch information
pmosk committed Aug 9, 2023
1 parent db51c16 commit dce101c
Show file tree
Hide file tree
Showing 32 changed files with 506 additions and 868 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using Xunit;
using System;
using Xunit;

namespace PrimeFuncPack.Core.Tests;

internal static class AssertHelper
{
public static void AssertEqualFailures<TFailureCode>(
(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);
}
}
16 changes: 8 additions & 8 deletions src/core-taggeds-failure/Failure.Tests/Failure.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@
<AssemblyName>PrimeFuncPack.Core.Failure.Tests</AssemblyName>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Failure\Failure.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="PrimeFuncPack.UnitTest.Data" Version="3.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.0" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Failure\Failure.csproj" />
</ItemGroup>

</Project>
15 changes: 15 additions & 0 deletions src/core-taggeds-failure/Failure.Tests/Source/FailureTestSource.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
79 changes: 79 additions & 0 deletions src/core-taggeds-failure/Failure.Tests/Source/Source.Equal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using PrimeFuncPack.UnitTest;

namespace PrimeFuncPack.Core.Tests;

partial class FailureTestSource
{
public static IEnumerable<object[]> EqualPairTestData
=>
new[]
{
new object[]
{
new Failure<SomeFailureCode>(),
default(Failure<SomeFailureCode>)
},
new object[]
{
new Failure<SomeFailureCode>(),
new Failure<SomeFailureCode>(default, null)
},
new object[]
{
default(Failure<SomeFailureCode>),
new Failure<SomeFailureCode>(default, string.Empty)
{
SourceException = null
}
},
new object[]
{
new Failure<SomeFailureCode>(default, null),
new Failure<SomeFailureCode>()
},
new object[]
{
new Failure<SomeFailureCode>(default, string.Empty),
default(Failure<SomeFailureCode>)
},
new object[]
{
new Failure<SomeFailureCode>(SomeFailureCode.First, null)
{
SourceException = SomeException.SomeInstance
},
new Failure<SomeFailureCode>(SomeFailureCode.First, null)
{
SourceException = SomeException.SomeInstance
}
},
new object[]
{
new Failure<SomeFailureCode>(SomeFailureCode.Second, string.Empty),
new Failure<SomeFailureCode>(SomeFailureCode.Second, null)
},
new object[]
{
new Failure<SomeFailureCode>(SomeFailureCode.Third, null),
new Failure<SomeFailureCode>(SomeFailureCode.Third, string.Empty)
},
new object[]
{
new Failure<SomeFailureCode>(SomeFailureCode.Unknown, string.Empty),
new Failure<SomeFailureCode>(SomeFailureCode.Unknown, string.Empty)
},
new object[]
{
new Failure<SomeFailureCode>(SomeFailureCode.First, TestData.SomeString)
{
SourceException = SomeException.SomeInstance
},
new Failure<SomeFailureCode>(SomeFailureCode.First, TestData.SomeString)
{
SourceException = SomeException.SomeInstance
}
}
};
}
61 changes: 61 additions & 0 deletions src/core-taggeds-failure/Failure.Tests/Source/Source.Unequal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using PrimeFuncPack.UnitTest;

namespace PrimeFuncPack.Core.Tests;

partial class FailureTestSource
{
public static IEnumerable<object[]> UnequalPairTestData
=>
new[]
{
new object[]
{
new Failure<SomeFailureCode>(),
new Failure<SomeFailureCode>(SomeFailureCode.First, null)
},
new object[]
{
default(Failure<SomeFailureCode>),
new Failure<SomeFailureCode>(SomeFailureCode.Second, TestData.WhiteSpaceString)
},
new object[]
{
new Failure<SomeFailureCode>(SomeFailureCode.Third, null),
default(Failure<SomeFailureCode>)
},
new object[]
{
new Failure<SomeFailureCode>(SomeFailureCode.First, TestData.TabString),
new Failure<SomeFailureCode>()
},
new object[]
{
new Failure<SomeFailureCode>(SomeFailureCode.First, TestData.SomeString),
new Failure<SomeFailureCode>(SomeFailureCode.Second, TestData.SomeString)
},
new object[]
{
new Failure<SomeFailureCode>(SomeFailureCode.First, TestData.SomeString)
{
SourceException = SomeException.SomeInstance
},
new Failure<SomeFailureCode>(SomeFailureCode.First, TestData.UpperSomeString)
{
SourceException = SomeException.SomeInstance
}
},
new object[]
{
new Failure<SomeFailureCode>(SomeFailureCode.First, TestData.SomeString)
{
SourceException = new SomeException()
},
new Failure<SomeFailureCode>(SomeFailureCode.First, TestData.SomeString)
{
SourceException = new SomeException()
}
}
};
}
Original file line number Diff line number Diff line change
@@ -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<int>(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<SomeFailureCode>(sourceFailureCode, sourceFailureMessage)
{
SourceException = sourceException
};

var (actualFailureCode, actualFailureMessage, actualException) = source;

AssertEqualFailures(
(sourceFailureCode, expectedFailureMessage, sourceException),
(actualFailureCode, actualFailureMessage, actualException));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ 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<int>(sourceFailureCode, null);

var (actualFailureCode, actualFailureMessage) = source;

AssertEqualFailures(
(source.FailureCode, source.FailureMessage),
(actualFailureCode, actualFailureMessage));
(source.FailureCode, source.FailureMessage, null),
(actualFailureCode, actualFailureMessage, null));
}

[Theory]
Expand All @@ -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<SomeFailureCode>(sourceFailureCode, sourceFailureMessage);

var (actualFailureCode, actualFailureMessage) = source;

AssertEqualFailures(
(source.FailureCode, source.FailureMessage),
(actualFailureCode, actualFailureMessage));
(source.FailureCode, source.FailureMessage, null),
(actualFailureCode, actualFailureMessage, null));
}
}
Loading

0 comments on commit dce101c

Please sign in to comment.