From 6b9a4d882ba2b711acb7b9b6755393cc892b4178 Mon Sep 17 00:00:00 2001 From: Leif Battermann Date: Sun, 17 Jan 2016 10:19:39 +0100 Subject: [PATCH 1/2] Added an extension method for C# to convert an FSharpOption into a Result --- src/Chessie/ErrorHandling.fs | 9 +++++-- tests/Chessie.CSharp.Test/ExtensionsTests.cs | 25 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/Chessie/ErrorHandling.fs b/src/Chessie/ErrorHandling.fs index d816252..a45ad7e 100644 --- a/src/Chessie/ErrorHandling.fs +++ b/src/Chessie/ErrorHandling.fs @@ -362,7 +362,12 @@ type ResultExtensions () = /// Otherwise the exisiting error messages are propagated. [] static member inline Join (this: Result<'TOuter, 'TMessage>, inner: Result<'TInner, 'TMessage>, outerKeySelector: Func<'TOuter,'TKey>, innerKeySelector: Func<'TInner, 'TKey>, resultSelector: Func<'TOuter, 'TInner, 'TResult>) = - let curry func = fun a -> fun b -> func (a, b) + let curry func = fun a b -> func (a, b) curry resultSelector.Invoke this - <*> inner \ No newline at end of file + <*> inner + + /// Converts an option into a Result. + [] + static member ToResult(this, msg) = + this |> failIfNone msg \ No newline at end of file diff --git a/tests/Chessie.CSharp.Test/ExtensionsTests.cs b/tests/Chessie.CSharp.Test/ExtensionsTests.cs index d5c4118..89a28c9 100644 --- a/tests/Chessie.CSharp.Test/ExtensionsTests.cs +++ b/tests/Chessie.CSharp.Test/ExtensionsTests.cs @@ -1,6 +1,7 @@ using System; using Chessie.ErrorHandling; using Chessie.ErrorHandling.CSharp; +using Microsoft.FSharp.Core; using NUnit.Framework; namespace Chessie.CSharp.Test @@ -24,5 +25,29 @@ public void JoinToResultsOfSuccessWorks() }, ifFailure: errs => Assert.Fail()); } + + [Test] + public void ToResultOnSomeShouldSucceed() + { + var opt = FSharpOption.Some(42); + var result = opt.ToResult("error"); + result.Match( + ifSuccess: (x, msgs) => + { + Assert.AreEqual(42, x); + Assert.That(msgs, Is.Empty); + }, + ifFailure: errs => Assert.Fail()); + } + + [Test] + public void ToResultOnNoneShoulFail() + { + var opt = FSharpOption.None; + var result = opt.ToResult("error"); + result.Match( + ifSuccess: (x, _) => Assert.Fail(), + ifFailure: errs => Assert.That(errs, Is.EquivalentTo(new[] {"error"}))); + } } } \ No newline at end of file From 8b26347a24781dcab2e0986b450ca04d55cda474 Mon Sep 17 00:00:00 2001 From: Leif Battermann Date: Sun, 17 Jan 2016 12:05:05 +0100 Subject: [PATCH 2/2] merge error fixed --- tests/Chessie.CSharp.Test/ExtensionsTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Chessie.CSharp.Test/ExtensionsTests.cs b/tests/Chessie.CSharp.Test/ExtensionsTests.cs index cc62493..a81c236 100644 --- a/tests/Chessie.CSharp.Test/ExtensionsTests.cs +++ b/tests/Chessie.CSharp.Test/ExtensionsTests.cs @@ -82,6 +82,6 @@ public void ToResultOnNoneShoulFail() result.Match( ifSuccess: (x, _) => Assert.Fail(), ifFailure: errs => Assert.That(errs, Is.EquivalentTo(new[] {"error"}))); - + } } } \ No newline at end of file