Created
December 17, 2014 10:54
-
-
Save vbfox/aa4dd3b9ae0d1e92a612 to your computer and use it in GitHub Desktop.
F# Option type support for NFluent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.FSharp.Core; | |
using NFluent; | |
using NFluent.Extensibility; | |
namespace UnitTests | |
{ | |
public static class FSharOptionCheckExtensions | |
{ | |
public static void IsNone<T>(this ICheck<FSharpOption<T>> check) | |
{ | |
var checker = ExtensibilityHelper.ExtractChecker(check); | |
var entity = string.Format("{0} option", typeof(T).Name); | |
var notMessage = FluentMessage.BuildMessage("The {0} is None but should be Some.").For(entity); | |
checker.ExecuteCheck( | |
() => | |
{ | |
if (!FSharpOption<T>.get_IsNone(checker.Value)) | |
{ | |
var message = FluentMessage.BuildMessage("The {0} is Some but should be None.") | |
.For(entity).On(checker.Value); | |
throw new FluentCheckException(message.ToString()); | |
} | |
}, | |
notMessage.ToString()); | |
} | |
public static IFSharpOptionCheckLink<T> IsSome<T>(this ICheck<FSharpOption<T>> check) | |
{ | |
var checker = ExtensibilityHelper.ExtractChecker(check); | |
var entity = string.Format("{0} option", typeof(T).Name); | |
var notMessage = FluentMessage.BuildMessage("The {0} is Some but should be None.") | |
.For(entity).On(checker.Value); | |
checker.ExecuteCheck( | |
() => | |
{ | |
if (!FSharpOption<T>.get_IsSome(checker.Value)) | |
{ | |
var message = FluentMessage.BuildMessage("The {0} is None but should be Some.") | |
.For(entity); | |
throw new FluentCheckException(message.ToString()); | |
} | |
}, | |
notMessage.ToString()); | |
return new FSharpOptionCheckLink<T>(check); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using JetBrains.Annotations; | |
using Microsoft.FSharp.Core; | |
using NFluent; | |
using NFluent.Extensibility; | |
namespace UnitTests | |
{ | |
internal class FSharpOptionCheckLink<TOptionValue> : IFSharpOptionCheckLink<TOptionValue> | |
{ | |
private readonly ICheck<FSharpOption<TOptionValue>> previousCheck; | |
public FSharpOptionCheckLink([NotNull] ICheck<FSharpOption<TOptionValue>> previousCheck) | |
{ | |
if (previousCheck == null) | |
{ | |
throw new ArgumentNullException("previousCheck"); | |
} | |
this.previousCheck = previousCheck; | |
} | |
public ICheck<FSharpOption<TOptionValue>> And | |
{ | |
get | |
{ | |
var forkableCheck = (IForkableCheck)previousCheck; | |
return forkableCheck.ForkInstance() as ICheck<FSharpOption<TOptionValue>>; | |
} | |
} | |
public ICheck<TOptionValue> Which | |
{ | |
get | |
{ | |
var checkForExtensibility = ExtensibilityHelper.ExtractChecker(previousCheck); | |
if (!FSharpOption<TOptionValue>.get_IsSome(checkForExtensibility.Value)) | |
{ | |
throw new FluentCheckException(FluentMessage.BuildMessage("The checked option has no value to be checked.").ToString()); | |
} | |
return Check.That(checkForExtensibility.Value.Value); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.FSharp.Core; | |
using NFluent; | |
namespace UnitTests | |
{ | |
public interface IFSharpOptionCheckLink<TOptionValue> | |
{ | |
ICheck<FSharpOption<TOptionValue>> And { get; } | |
ICheck<TOptionValue> Which { get; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment