Пример #1
0
        public void JoinAppliedToRightOfRight()
        {
            var actual = Either.Join(EitherString.Right(EitherString.Right(42)));

            Assert.That(actual.IsRight, Is.True);
            Assert.That(actual.Right, Is.EqualTo(42));
        }
Пример #2
0
        public void JoinAppliedToRightOfLeft()
        {
            var actual = Either.Join(EitherString.Right(EitherString.Left <int>("error")));

            Assert.That(actual.IsLeft, Is.True);
            Assert.That(actual.Left, Is.EqualTo("error"));
        }
Пример #3
0
        public void MapEitherAppliedToRight()
        {
            var either = EitherString.Right(42);
            var actual = either.MapEither(null, r => r + r);

            Assert.That(actual, Is.EqualTo(84));
        }
Пример #4
0
        public void ReplicateM_AppliedToRight()
        {
            var actual = Either.ReplicateM_(5, EitherString.Right(42));

            Assert.That(actual.IsRight, Is.True);
            Assert.That(actual.Right, Is.EqualTo(new Unit()));
        }
Пример #5
0
        public void MatchOfTAppliedToRight()
        {
            var either = EitherString.Right(42);
            var actual = either.Match(_ => 1.0, _ => 2.0);

            Assert.That(actual, Is.EqualTo(2.0));
        }
Пример #6
0
        public void BindLeftIdentity()
        {
            // (return x) >>= f == f x
            const int x = 5;
            Func <int, Either <string, int> > f = n => EitherString.Right(n * n);

            Assert.That(EitherString.Return(x).Bind(f), Is.EqualTo(f(x)));
        }
Пример #7
0
        public void MapM_WithFuncReturningMixtureOfLeftsAndRights()
        {
            var ints   = new[] { 1, 2, 3, 4, 5 };
            var actual = Either.MapM_(n => n < 4 ? EitherString.Right(Convert.ToString(n)) : EitherString.Left <string>("error"), ints);

            Assert.That(actual.IsLeft, Is.True);
            Assert.That(actual.Left, Is.EqualTo("error"));
        }
Пример #8
0
        public void MapM_WithFuncReturningRights()
        {
            var ints   = new[] { 1, 2, 3, 4, 5 };
            var actual = Either.MapM_(n => EitherString.Right(Convert.ToString(n)), ints);

            Assert.That(actual.IsRight, Is.True);
            Assert.That(actual.Right, Is.EqualTo(new Unit()));
        }
Пример #9
0
        public void LeftAppliedToRightThrowsException()
        {
            var either = EitherString.Right(42);

            #pragma warning disable 168
            Assert.Throws <InvalidOperationException>(() => { var dummy = either.Left; });
            #pragma warning restore 168
        }
Пример #10
0
        public void Right()
        {
            var either = EitherString.Right(42);

            Assert.That(either.IsLeft, Is.False);
            Assert.That(either.IsRight, Is.True);
            Assert.That(either.Right, Is.EqualTo(42));
        }
Пример #11
0
        public void BindAssociativity()
        {
            // (m >>= f) >>= g == m >>= (\x -> f x >>= g)
            Func <int, Either <string, int> >    f = n => EitherString.Right(n * n);
            Func <int, Either <string, string> > g = n => EitherString.Right(Convert.ToString(n));
            var m = EitherString.Return(5);

            Assert.That(m.Bind(f).Bind(g), Is.EqualTo(m.Bind(x => f(x).Bind(g))));
        }
Пример #12
0
 private static IEnumerable <Either <string, int> > MixtureOfLeftsAndRights()
 {
     return(new[]
     {
         EitherString.Right(1),
         EitherString.Right(2),
         EitherString.Left <int>("Error 1"),
         EitherString.Right(4),
         EitherString.Left <int>("Error 2"),
         EitherString.Right(6)
     });
 }
Пример #13
0
        public void MatchAppliedToRight()
        {
            var either            = EitherString.Right(42);
            var leftActionCalled  = false;
            var rightActionCalled = false;
            var rightActionParam  = default(int);

            either.Match(
                _ =>
            {
                leftActionCalled = true;
            },
                a =>
            {
                rightActionCalled = true;
                rightActionParam  = a;
            });
            Assert.That(leftActionCalled, Is.False);
            Assert.That(rightActionCalled, Is.True);
            Assert.That(rightActionParam, Is.EqualTo(42));
        }
Пример #14
0
 public Either <string, int> ToEither()
 {
     return(_error != null?EitherString.Left <int>(_error) : EitherString.Right(_number));
 }
Пример #15
0
 private static TestCaseData MakeEqualsTestCaseData(string e, int n, bool flag)
 {
     return(new TestCaseData(EitherString.Left <int>(e), EitherString.Right(n), flag));
 }
Пример #16
0
 private static TestCaseData MakeEqualsTestCaseData(int n1, int n2, bool flag)
 {
     return(new TestCaseData(EitherString.Right(n1), EitherString.Right(n2), flag));
 }