diff --git a/src/Chessie/ErrorHandling.fs b/src/Chessie/ErrorHandling.fs index 2efb832..04a5d4b 100644 --- a/src/Chessie/ErrorHandling.fs +++ b/src/Chessie/ErrorHandling.fs @@ -361,7 +361,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 24ffc29..a81c236 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 @@ -58,5 +59,29 @@ from c in r3 ifSuccess: (s, _) => Assert.Fail("should fail"), ifFailure: list => Assert.That(list, Is.EquivalentTo(new[] { "fail1" }))); } + + [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