Skip to content

Commit

Permalink
Merge pull request #114 from pfpack/feature/improve-or-throw-methods
Browse files Browse the repository at this point in the history
Feature/improve or throw methods
  • Loading branch information
andreise authored Nov 21, 2022
2 parents e45ac5f + c17c730 commit 1134968
Show file tree
Hide file tree
Showing 16 changed files with 272 additions and 32 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x
include-prerelease: false
Expand Down Expand Up @@ -121,4 +121,8 @@ jobs:

- name: Push Packages
if: ${{ github.event_name == 'release' }}
run: dotnet nuget push "../../../nuget/*.nupkg" -s https://api.nuget.org/v3/index.json -k ${{ secrets.NuGetSourcePassword }} --skip-duplicate
run: >
dotnet nuget push "../../../nuget/*.nupkg"
-s https://api.nuget.org/v3/index.json
-k ${{ secrets.NuGetSourcePassword }}
--skip-duplicate
4 changes: 2 additions & 2 deletions src/core-taggeds-result/Result.Tests/Result.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

<ItemGroup>
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.3.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="PrimeFuncPack.UnitTest.Data" Version="3.0.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,64 @@ namespace PrimeFuncPack.Core.Tests;

partial class ResultTest
{
[Test]
[Test]
[TestCaseSource(typeof(TestDataSource), nameof(TestDataSource.FailureDefaultTestSource))]
public void FailureOrThrowWithExceptionFactory_ExceptionFactoryIsNull_ExpectArgumentNullException(
Result<RefType, StructType> source)
{
var exceptionFactory = (Func<Exception>)null!;
var actualException = Assert.Throws<ArgumentNullException>(Test);

Assert.AreEqual("exceptionFactory", actualException?.ParamName);

void Test()
=>
_ = source.FailureOrThrow(exceptionFactory);
}

[Test]
[TestCaseSource(typeof(TestDataSource), nameof(TestDataSource.FailureDefaultTestSource))]
public void FailureOrThrowWithExceptionFactory_SourceIsDefault_ExpectDefaultFailureValue(
Result<RefType, StructType> source)
{
var actual = source.FailureOrThrow(() => new SomeException());
var actual = source.FailureOrThrow(CreateException);
var expected = default(StructType);

Assert.AreEqual(expected, actual);

static Exception CreateException()
=>
new SomeException();
}

[Test]
[TestCaseSource(typeof(TestDataSource), nameof(TestDataSource.FailureSomeTextStructTypeTestSource))]
public void FailureOrThrowWithExceptionFactory_SourceIsFailure_ExpectFailureValue(
Result<RefType, StructType> source)
{
var actual = source.FailureOrThrow(() => new SomeException());
var actual = source.FailureOrThrow(CreateException);
var expected = SomeTextStructType;

Assert.AreEqual(expected, actual);

static Exception CreateException()
=>
new SomeException();
}

[Test]
[Test]
[TestCaseSource(typeof(TestDataSource), nameof(TestDataSource.SuccessNullTestSource))]
[TestCaseSource(typeof(TestDataSource), nameof(TestDataSource.SuccessPlusFifteenIdRefTypeTestSource))]
public void FailureOrThrowWithExceptionFactory_SourceIsSuccess_ExpectExceptionFromFactory(
Result<RefType, StructType> source)
{
var exceptionFromFactory = new SomeException();
var actualException = Assert.Throws<SomeException>(Test);

var actualException = Assert.Throws<SomeException>(
() => _ = source.FailureOrThrow(() => exceptionFromFactory));

Assert.AreEqual(exceptionFromFactory, actualException);

void Test()
=>
_ = source.FailureOrThrow(() => exceptionFromFactory);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using NUnit.Framework;
using PrimeFuncPack.UnitTest;
using System;
using static PrimeFuncPack.UnitTest.TestData;

namespace PrimeFuncPack.Core.Tests;

partial class ResultTest
{
[Test]
[TestCaseSource(typeof(TestDataSource), nameof(TestDataSource.FailureDefaultTestSource))]
public void FailureOrThrowWithExceptionFactoryFromSuccess_ExceptionFactoryIsNull_ExpectDefaultFailureValue(
Result<RefType, StructType> source)
{
var exceptionFactory = (Func<RefType, Exception>)null!;
var actualException = Assert.Throws<ArgumentNullException>(Test);

Assert.AreEqual("exceptionFactory", actualException?.ParamName);

void Test()
=>
_ = source.FailureOrThrow(exceptionFactory);
}

[Test]
[TestCaseSource(typeof(TestDataSource), nameof(TestDataSource.FailureDefaultTestSource))]
public void FailureOrThrowWithExceptionFactoryFromSuccess_SourceIsDefault_ExpectDefaultFailureValue(
Result<RefType, StructType> source)
{
var actual = source.FailureOrThrow(CreateException);
var expected = default(StructType);

Assert.AreEqual(expected, actual);

static Exception CreateException(RefType success)
=>
new SomeException<RefType>(success);
}

[Test]
[TestCaseSource(typeof(TestDataSource), nameof(TestDataSource.FailureSomeTextStructTypeTestSource))]
public void FailureOrThrowWithExceptionFactoryFromSuccess_SourceIsFailure_ExpectFailureValue(
Result<RefType, StructType> source)
{
var actual = source.FailureOrThrow(CreateException);
var expected = SomeTextStructType;

Assert.AreEqual(expected, actual);

static Exception CreateException(RefType success)
=>
new SomeException<RefType>(success);
}

[Test]
[TestCaseSource(typeof(TestDataSource), nameof(TestDataSource.SuccessPlusFifteenIdRefTypeTestSource))]
public void FailureOrThrowWithExceptionFactoryFromSuccess_SourceIsSuccess_ExpectExceptionFromFactory(
Result<RefType, StructType> source)
{
var actualException = Assert.Throws<SomeException<RefType>>(Test);

Assert.AreEqual(PlusFifteenIdRefType, actualException?.Value);

void Test()
=>
_ = source.FailureOrThrow(CreateException);

static Exception CreateException(RefType success)
=>
new SomeException<RefType>(success);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace PrimeFuncPack.Core.Tests;

partial class ResultTest
{
[Test]
[Test]
[TestCaseSource(typeof(TestDataSource), nameof(TestDataSource.FailureDefaultTestSource))]
public void FailureOrThrow_SourceIsDefault_ExpectDefaultFailureValue(
Result<RefType, StructType> source)
Expand All @@ -29,15 +29,18 @@ public void FailureOrThrow_SourceIsFailure_ExpectFailureValue(
Assert.AreEqual(expected, actual);
}

[Test]
[Test]
[TestCaseSource(typeof(TestDataSource), nameof(TestDataSource.SuccessNullTestSource))]
[TestCaseSource(typeof(TestDataSource), nameof(TestDataSource.SuccessPlusFifteenIdRefTypeTestSource))]
public void FailureOrThrow_SourceIsSuccess_ExpectInvalidOperationException(
Result<RefType, StructType> source)
{
var actualEx = Assert.Throws<InvalidOperationException>(
() => _ = source.FailureOrThrow());
var actualEx = Assert.Throws<InvalidOperationException>(Test);

Assert.True(actualEx!.Message.Contains("Failure", StringComparison.InvariantCultureIgnoreCase));

void Test()
=>
_ = source.FailureOrThrow();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,47 @@ namespace PrimeFuncPack.Core.Tests;

partial class ResultTest
{
[Test]
[Test]
[TestCaseSource(typeof(TestDataSource), nameof(TestDataSource.SuccessMinusFifteenIdRefTypeTestSource))]
public void SuccessOrThrowWithExceptionFactory_ExceptionFactoryIsNull_ExpectArgumentNullException(
Result<RefType, StructType> source)
{
var exceptionFactory = (Func<Exception>)null!;
var actualException = Assert.Throws<ArgumentNullException>(Test);

Assert.AreEqual("exceptionFactory", actualException?.ParamName);

void Test()
=>
_ = source.SuccessOrThrow(exceptionFactory);
}

[Test]
[TestCaseSource(typeof(TestDataSource), nameof(TestDataSource.SuccessNullTestSource))]
public void SuccessOrThrowWithExceptionFactory_SourceIsSuccessAndValueIsNull_ExpectNull(
Result<RefType, StructType> source)
{
var actual = source.SuccessOrThrow(() => new SomeException());
var actual = source.SuccessOrThrow(CreateException);
Assert.IsNull(actual);

static Exception CreateException()
=>
new SomeException();
}

[Test]
[Test]
[TestCaseSource(typeof(TestDataSource), nameof(TestDataSource.SuccessMinusFifteenIdRefTypeTestSource))]
public void SuccessOrThrowWithExceptionFactory_SourceIsSuccessAndValueIsNotNull_ExpectSuccessValue(
Result<RefType, StructType> source)
{
var actual = source.SuccessOrThrow(() => new SomeException());
var actual = source.SuccessOrThrow(CreateException);
var expected = MinusFifteenIdRefType;

Assert.AreEqual(expected, actual);

static Exception CreateException()
=>
new SomeException();
}

[Test]
Expand All @@ -34,10 +57,16 @@ public void SuccessOrThrowWithExceptionFactory_SourceIsDefaultOrFailure_ExpectEx
Result<RefType, StructType> source)
{
var exceptionFromFactory = new SomeException();
var actualException = Assert.Throws<SomeException>(Test);

var actualException = Assert.Throws<SomeException>(
() => _ = source.SuccessOrThrow(() => exceptionFromFactory));

Assert.AreEqual(exceptionFromFactory, actualException);

void Test()
=>
_ = source.SuccessOrThrow(CreateException);

Exception CreateException()
=>
exceptionFromFactory;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using NUnit.Framework;
using PrimeFuncPack.UnitTest;
using System;
using static PrimeFuncPack.UnitTest.TestData;

namespace PrimeFuncPack.Core.Tests;

partial class ResultTest
{
[Test]
[TestCaseSource(typeof(TestDataSource), nameof(TestDataSource.SuccessMinusFifteenIdRefTypeTestSource))]
public void SuccessOrThrowWithExceptionFactoryFromFailure_ExceptionFactoryIsNull_ExpectArgumentNullException(
Result<RefType, StructType> source)
{
var exceptionFactory = (Func<StructType, Exception>)null!;
var actualException = Assert.Throws<ArgumentNullException>(Test);

Assert.AreEqual("exceptionFactory", actualException?.ParamName);

void Test()
=>
_ = source.SuccessOrThrow(exceptionFactory);
}

[Test]
[TestCaseSource(typeof(TestDataSource), nameof(TestDataSource.SuccessNullTestSource))]
public void SuccessOrThrowWithExceptionFactoryFromFailure_SourceIsSuccessAndValueIsNull_ExpectNull(
Result<RefType, StructType> source)
{
var actual = source.SuccessOrThrow(CreateException);
Assert.IsNull(actual);

static Exception CreateException(StructType failure)
=>
new SomeException<StructType>(failure);
}

[Test]
[TestCaseSource(typeof(TestDataSource), nameof(TestDataSource.SuccessPlusFifteenIdRefTypeTestSource))]
public void SuccessOrThrowWithExceptionFactoryFromFailure_SourceIsSuccessAndValueIsNotNull_ExpectSuccessValue(
Result<RefType, StructType> source)
{
var actual = source.SuccessOrThrow(CreateException);
var expected = PlusFifteenIdRefType;

Assert.AreEqual(expected, actual);

static Exception CreateException(StructType failure)
=>
new SomeException<StructType>(failure);
}

[Test]
[TestCaseSource(typeof(TestDataSource), nameof(TestDataSource.FailureSomeTextStructTypeTestSource))]
public void SuccessOrThrowWithExceptionFactoryFromFailure_SourceIsDefaultOrFailure_ExpectExceptionFromFactory(
Result<RefType, StructType> source)
{
var actualException = Assert.Throws< SomeException<StructType>>(Test);

Assert.AreEqual(SomeTextStructType, actualException?.Value);

void Test()
=>
_ = source.SuccessOrThrow(CreateException);

static SomeException<StructType> CreateException(StructType failure)
=>
new(failure);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace PrimeFuncPack.Core.Tests;

partial class ResultTest
{
[Test]
[Test]
[TestCaseSource(typeof(TestDataSource), nameof(TestDataSource.SuccessNullTestSource))]
public void SuccessOrThrow_SourceIsSuccessAndValueIsNull_ExpectNull(
Result<RefType?, StructType> source)
Expand All @@ -33,9 +33,12 @@ public void SuccessOrThrow_SourceIsSuccessAndValueIsNotNull_ExpectSuccessValue(
public void SuccessOrThrow_SourceIsDefaultOrFailure_ExpectInvalidOperationException(
Result<RefType, StructType> source)
{
var actualEx = Assert.Throws<InvalidOperationException>(
() => _ = source.SuccessOrThrow());

var actualEx = Assert.Throws<InvalidOperationException>(Test);

Assert.True(actualEx!.Message.Contains("Success", StringComparison.InvariantCultureIgnoreCase));

void Test()
=>
_ = source.SuccessOrThrow();
}
}
12 changes: 12 additions & 0 deletions src/core-taggeds-result/Result.Tests/TestData/SomeException.T.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace PrimeFuncPack.Core.Tests;

internal sealed class SomeException<T> : Exception
{
public SomeException(T value)
=>
Value = value;

public T Value { get; }
}
Loading

0 comments on commit 1134968

Please sign in to comment.