public void CreateRight_EitherHasRightValue() { var result = Either <string, bool> .CreateRight(true); Assert.IsTrue(result.HasRight); Assert.AreEqual(true, result.RightValue); }
public void SwitchTest() { var left = Either.CreateLeft <int, string>(1); var right = Either.CreateRight <int, string>("s"); var assertCount = 0; left.Switch(i => { Assert.AreEqual(1, i); assertCount++; }, Fail); Assert.AreEqual(1, assertCount); right.Switch(Fail, i => { Assert.AreEqual("s", i); assertCount++; }); Assert.AreEqual(2, assertCount); var result1 = left.Switch(i => i * 2, Fail <string, int>); Assert.AreEqual(2, result1); var result2 = right.Switch(Fail <int, int>, s => 5); Assert.AreEqual(5, result2); }
public void CreateRight_EitherDoesNotHaveLeftValue() { var result = Either <string, bool> .CreateRight(true); Assert.IsTrue(result.HasRight); var temp = result.LeftValue; }
public static void Main(string[] args) { var worldOrError = Parser.Default.ParseArguments <CommandOptions>(args).MapResult( opts => Either.CreateLeft <Maybe <GameVariables>, string>(MapArgs(opts)), err => Either.CreateRight <Maybe <GameVariables>, string>(string.Join(", ", err))); worldOrError.Switch(StartGame, Console.Write); }
public void Apply_WithReturn_RightFuncRun() { var result = Either <int, int> .CreateRight(1); int branchOutput = -1; branchOutput = result.Apply(l => 1, r => 2); Assert.AreEqual(2, branchOutput); }
public void Apply_NoReturn_RightActionRun() { var result = Either <int, int> .CreateRight(1); bool leftBranchTaken = false; bool rightBranchTaken = false; result.Apply(l => { leftBranchTaken = false; }, r => { rightBranchTaken = true; }); Assert.IsFalse(leftBranchTaken); Assert.IsTrue(rightBranchTaken); }
protected override IDisposable SubscribeCore(IObserver <TResult> observer) { var leftSubscription = new SingleAssignmentDisposable(); var rightSubscription = new SingleAssignmentDisposable(); var combiner = combinerSelector(observer, leftSubscription, rightSubscription); var gate = new object(); leftSubscription.Disposable = leftSource.Materialize().Select(x => Either <Notification <TLeft>, Notification <TRight> > .CreateLeft(x)).Synchronize(gate).Subscribe(combiner); rightSubscription.Disposable = rightSource.Materialize().Select(x => Either <Notification <TLeft>, Notification <TRight> > .CreateRight(x)).Synchronize(gate).Subscribe(combiner); return(StableCompositeDisposable.Create(leftSubscription, rightSubscription)); }
public void Rights_RightValuesCollated() { var result = Either.Rights <string, int>(new List <Either <string, int> >() { Either <string, int> .CreateRight(1), Either <string, int> .CreateLeft("2"), Either <string, int> .CreateRight(3), }); Assert.IsNotNull(result); Assert.AreEqual(2, result.Count()); Assert.IsTrue(result.Any(i => i == 1)); Assert.IsTrue(result.Any(i => i == 3)); }
public void Lefts_LeftValuesCollated() { var result = Either.Lefts <string, int>(new List <Either <string, int> >() { Either <string, int> .CreateLeft("1"), Either <string, int> .CreateRight(2), Either <string, int> .CreateLeft("3"), }); Assert.IsNotNull(result); Assert.AreEqual(2, result.Count()); Assert.IsTrue(result.Any(i => i == "1")); Assert.IsTrue(result.Any(i => i == "3")); }
private static IObservable <TResult> Combine <TLeft, TRight, TResult>(IObservable <TLeft> leftSource, IObservable <TRight> rightSource, Func <IObserver <TResult>, IDisposable, IDisposable, IObserver <Either <Notification <TLeft>, Notification <TRight> > > > combinerSelector) { return(new AnonymousObservable <TResult>(observer => { var leftSubscription = new SingleAssignmentDisposable(); var rightSubscription = new SingleAssignmentDisposable(); var combiner = combinerSelector(observer, leftSubscription, rightSubscription); var gate = new object(); leftSubscription.Disposable = leftSource.Materialize().Select(x => Either <Notification <TLeft>, Notification <TRight> > .CreateLeft(x)).Synchronize(gate).Subscribe(combiner); rightSubscription.Disposable = rightSource.Materialize().Select(x => Either <Notification <TLeft>, Notification <TRight> > .CreateRight(x)).Synchronize(gate).Subscribe(combiner); return StableCompositeDisposable.Create(leftSubscription, rightSubscription); })); }
public void Partition_ValuesCollated() { var result = Either.Partition <string, int>(new List <Either <string, int> >() { Either <string, int> .CreateRight(1), Either <string, int> .CreateLeft("2"), Either <string, int> .CreateRight(3), }); Assert.IsNotNull(result); Assert.AreEqual(1, result.Item1.Count()); Assert.IsTrue(result.Item1.Any(i => i == "2")); Assert.AreEqual(2, result.Item2.Count()); Assert.IsTrue(result.Item2.Any(i => i == 1)); Assert.IsTrue(result.Item2.Any(i => i == 3)); }
public void CreateRight_NullValueThrows() { var result = Either <string, object> .CreateRight(null); }
public void LeftValue_ThrowsWhenCreatedAsRight() { var result = Either <int, int> .CreateRight(1); var temp = result.LeftValue; }
public void HasRight_TrueWhenCreatedWithRight() { var result = Either <int, int> .CreateRight(1); Assert.IsTrue(result.HasRight); }
/// <summary> /// Right injection /// </summary> public static Either <t, right> Right <right>(right value) => Either <t, right> .CreateRight(value);
public void Apply_NoReturn_NullRightActionThrows() { var result = Either <string, int> .CreateRight(1); result.Apply(l => { }, null); }
public void RightValue_DoesNotThrowWhenCreatedAsRight() { var result = Either <int, int> .CreateRight(1); Assert.AreEqual(1, result.RightValue); }
public void CreatesByStaticMethod() => ValidateRightEitherInterface(Either <int, string> .CreateRight(value));