From f6f33299072f9e126cb38b0d128f0b3a2da8a991 Mon Sep 17 00:00:00 2001 From: Mohammad Sepahvand Date: Sun, 10 Jan 2021 02:19:43 +1100 Subject: [PATCH] Add a version of Match that doesn't return anything and accepts an Action --- src/Nito.Try/Try(T).cs | 27 ++++++++++++++---- test/UnitTests/TryUnitTests.cs | 51 ++++++++++++++++++++++++++++++++-- 2 files changed, 70 insertions(+), 8 deletions(-) diff --git a/src/Nito.Try/Try(T).cs b/src/Nito.Try/Try(T).cs index b3c88ea..4177f93 100644 --- a/src/Nito.Try/Try(T).cs +++ b/src/Nito.Try/Try(T).cs @@ -1,7 +1,5 @@ using System; -using System.Collections.Generic; using System.Runtime.ExceptionServices; -using System.Text; using System.Threading.Tasks; namespace Nito @@ -105,10 +103,29 @@ public async Task> Bind(Func>> bind) } /// - /// Executes an action for the wrapped exception or value. + /// Executes a method (Action) for the wrapped exception or value. /// - /// The action to execute if this instance is an exception. - /// The action to execute if this instance is a value. + /// The method to execute if this instance is an exception. + /// The method to execute if this instance is a value. + public void Match(Action whenException, Action whenValue) + { + _ = whenException ?? throw new ArgumentNullException(nameof(whenException)); + _ = whenValue ?? throw new ArgumentNullException(nameof(whenValue)); + if (IsException) + { + whenException(_exception!); + } + else + { + whenValue(_value); + } + } + + /// + /// Executes a method (Func) for the wrapped exception or value. + /// + /// The method to execute if this instance is an exception. + /// The method to execute if this instance is a value. public TResult Match(Func whenException, Func whenValue) { _ = whenException ?? throw new ArgumentNullException(nameof(whenException)); diff --git a/test/UnitTests/TryUnitTests.cs b/test/UnitTests/TryUnitTests.cs index d03dd8f..3c07a85 100644 --- a/test/UnitTests/TryUnitTests.cs +++ b/test/UnitTests/TryUnitTests.cs @@ -1,7 +1,4 @@ using System; -using System.Collections; -using System.Collections.Generic; -using System.Text; using System.Threading.Tasks; using Nito; using Xunit; @@ -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() { @@ -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(new InvalidOperationException("test")); + t.Match( + ex => + { + exceptionInvoked = true; + exception = ex; + }, + v => + { + valueInvoked = true; + }); + Assert.True(exceptionInvoked); + Assert.False(valueInvoked); + Assert.IsType(exception); + Assert.Equal("test", exception.Message); + } + [Fact] public void Match_FuncThrows_PropagatesException() {