Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions src/Nito.Try/Try(T).cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading.Tasks;

namespace Nito
Expand Down Expand Up @@ -105,10 +103,29 @@ public async Task<Try<TResult>> Bind<TResult>(Func<T, Task<Try<TResult>>> bind)
}

/// <summary>
/// Executes an action for the wrapped exception or value.
/// Executes a method (Action) for the wrapped exception or value.
/// </summary>
/// <param name="whenException">The action to execute if this instance is an exception.</param>
/// <param name="whenValue">The action to execute if this instance is a value.</param>
/// <param name="whenException">The method to execute if this instance is an exception.</param>
/// <param name="whenValue">The method to execute if this instance is a value.</param>
public void Match(Action<Exception> whenException, Action<T> whenValue)
{
_ = whenException ?? throw new ArgumentNullException(nameof(whenException));
_ = whenValue ?? throw new ArgumentNullException(nameof(whenValue));
if (IsException)
{
whenException(_exception!);
}
else
{
whenValue(_value);
}
}

/// <summary>
/// Executes a method (Func) for the wrapped exception or value.
/// </summary>
/// <param name="whenException">The method to execute if this instance is an exception.</param>
/// <param name="whenValue">The method to execute if this instance is a value.</param>
public TResult Match<TResult>(Func<Exception, TResult> whenException, Func<T, TResult> whenValue)
{
_ = whenException ?? throw new ArgumentNullException(nameof(whenException));
Expand Down
51 changes: 48 additions & 3 deletions test/UnitTests/TryUnitTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Nito;
using Xunit;
Expand Down Expand Up @@ -135,6 +132,31 @@ public void Match_ForValue_OnlyExecutesValueFunc()
Assert.Equal(5, result);
}

[Fact]
public void Match_ForValue_OnlyExecutesValueAction()
{
var exceptionInvoked = false;
Exception exception = null;
var valueInvoked = false;
int? value = 0;
var t = Try.FromValue(13);
t.Match(
ex =>
{
exceptionInvoked = true;
exception = ex;
},
v =>
{
valueInvoked = true;
value = v;
});
Assert.False(exceptionInvoked);
Assert.True(valueInvoked);
Assert.Equal(13, value);
Assert.Null(exception);
}

[Fact]
public void Match_ForException_OnlyExecutesExceptionFunc()
{
Expand Down Expand Up @@ -162,6 +184,29 @@ public void Match_ForException_OnlyExecutesExceptionFunc()
Assert.Equal("test", exception.Message);
}

[Fact]
public void Match_ForException_OnlyExecutesExceptionAction()
{
var exceptionInvoked = false;
Exception exception = null;
var valueInvoked = false;
var t = Try.FromException<int>(new InvalidOperationException("test"));
t.Match(
ex =>
{
exceptionInvoked = true;
exception = ex;
},
v =>
{
valueInvoked = true;
});
Assert.True(exceptionInvoked);
Assert.False(valueInvoked);
Assert.IsType<InvalidOperationException>(exception);
Assert.Equal("test", exception.Message);
}

[Fact]
public void Match_FuncThrows_PropagatesException()
{
Expand Down