From 0ace247e1d914c9c6675139f67c46cb49ac1c9e9 Mon Sep 17 00:00:00 2001 From: Ben Collins Date: Tue, 8 Jan 2019 11:40:40 -0600 Subject: [PATCH] Implement void Match; closes #23 --- src/TrueMyth/Maybe.cs | 15 +++++++++++++++ src/TrueMyth/Result.cs | 15 +++++++++++++++ test/TrueMyth.Test/MaybeTests.cs | 14 ++++++++++++++ test/TrueMyth.Test/ResultTests.MemberData.cs | 6 ++++++ test/TrueMyth.Test/ResultTests.cs | 10 ++++++++++ 5 files changed, 60 insertions(+) diff --git a/src/TrueMyth/Maybe.cs b/src/TrueMyth/Maybe.cs index 5e61120..0fdd39c 100644 --- a/src/TrueMyth/Maybe.cs +++ b/src/TrueMyth/Maybe.cs @@ -302,6 +302,21 @@ private Maybe(TValue value) /// public UValue Match(Func just, Func nothing) => this._isJust ? just(this._value) : nothing(); + /// + /// Provides a similar functionality as , but without return types. + /// + public void Match(Action just, Action nothing) + { + if (this._isJust) + { + just(this._value); + } + else + { + nothing(); + } + } + /// /// Static factory method for creation of new Just Maybe<TValue>s. If value is a /// reference type and is null, this returns a Nothing. diff --git a/src/TrueMyth/Result.cs b/src/TrueMyth/Result.cs index 10d1dae..a76da5e 100644 --- a/src/TrueMyth/Result.cs +++ b/src/TrueMyth/Result.cs @@ -392,6 +392,21 @@ public Result MapErr(Func mapFn) => !this /// public T Match(Func ok, Func err) => this._isOk ? ok(this._value) : err(this._error); + /// + /// Provides similar functionality as , but with no return type. + /// + public void Match(Action ok, Action err) + { + if (this.IsOk) + { + ok(this._value); + } + else + { + err(this._error); + } + } + /// /// Provide a fallback for a given Result. Behaves like a logical or: if the result value is an Ok, returns that result; otherwise, /// returns the defaultResult value. diff --git a/test/TrueMyth.Test/MaybeTests.cs b/test/TrueMyth.Test/MaybeTests.cs index 0dd09b7..7eb4355 100644 --- a/test/TrueMyth.Test/MaybeTests.cs +++ b/test/TrueMyth.Test/MaybeTests.cs @@ -184,6 +184,20 @@ public void Match_Theory(Maybe m1) if (m1.IsNothing) Assert.Equal(2, result); } + [Theory] + [MemberData(nameof(GetEqualityTheoryData1Param))] + public void Match_Void_Theory(Maybe m1) + { + m1.Match( + just: _ => { + Assert.True(m1.IsJust); + }, + nothing: () => { + Assert.True(m1.IsNothing); + } + ); + } + [Theory] [MemberData(nameof(GetOrTheoryData))] public void Or_Theory(Maybe m1, Maybe m2, Maybe expectation) diff --git a/test/TrueMyth.Test/ResultTests.MemberData.cs b/test/TrueMyth.Test/ResultTests.MemberData.cs index d5fe39c..7ce8917 100644 --- a/test/TrueMyth.Test/ResultTests.MemberData.cs +++ b/test/TrueMyth.Test/ResultTests.MemberData.cs @@ -28,5 +28,11 @@ public static IEnumerable GetToStringTests() yield return new object[] { SimpleResult.Ok(7), $"Ok<{7.GetType().Name}>[7]"}; yield return new object[] { SimpleResult.Err("error"), $"Err<{"error".GetType().Name}>[error]"}; } + + public static IEnumerable GetResultTheoryData1() + { + yield return new object[] { SimpleResult.Ok(7) }; + yield return new object[] { SimpleResult.Err("test") }; + } } } \ No newline at end of file diff --git a/test/TrueMyth.Test/ResultTests.cs b/test/TrueMyth.Test/ResultTests.cs index 1b8a20f..260ad73 100644 --- a/test/TrueMyth.Test/ResultTests.cs +++ b/test/TrueMyth.Test/ResultTests.cs @@ -164,6 +164,16 @@ public void Match_OkCallsOk_Ok() Assert.Equal("ok", functionCalled); } + [Theory] + [MemberData(nameof(GetResultTheoryData1))] + public void Match_Void_Theory(SimpleResult r) + { + r.Match( + ok: _ => Assert.True(r.IsOk), + err: _ => Assert.True(r.IsErr) + ); + } + // Or [Fact] public void Or_OkReturnsThis_Ok()