Esempio n. 1
0
        public void BindShouldNotInvokeFuncWhenException()
        {
            // Arrange
            var exception = new SomeException("Some exception");
            Exceptional <int> exceptional = exception;

            Exceptional <bool> exceptionalWithSuccess = true;

            var anotherException = new SomeException("Some another exception");
            Exceptional <bool> exceptionalWithException = anotherException;


            Func <int, Exceptional <bool> > toAnotherExceptional = (i) =>
            {
                if (i.Equals(1))
                {
                    return(exceptionalWithSuccess);
                }
                else
                {
                    return(exceptionalWithException);
                }
            };

            // Act
            var result = exceptional.Bind(toAnotherExceptional);


            // Assert
            result.ToString().Should().Be(exceptional.ToString());
        }
Esempio n. 2
0
        public void BindShouldSucceedWhenSuccess()
        {
            // Arrange
            var value = 1;
            Exceptional <int> exceptional = value;

            Exceptional <bool> exceptionalWithSuccess = true;

            var exception = new SomeException("Some exception");
            Exceptional <bool> exceptionalWithException = exception;


            Func <int, Exceptional <bool> > toAnotherExceptional = (i) =>
            {
                if (i.Equals(1))
                {
                    return(exceptionalWithSuccess);
                }
                else
                {
                    return(exceptionalWithException);
                }
            };

            // Act
            var result = exceptional.Bind(toAnotherExceptional);


            // Assert
            result.Should().Be(exceptionalWithSuccess);
        }
Esempio n. 3
0
        public void Bind_Transform_ET_To_ER(int?num)
        {
            Exceptional <int> valid = num.HasValue ? (Exceptional <int>)num : (ErrorException)("num is null");
            bool isValid            = num.HasValue;
            var  res = valid.Bind(i => (Exceptional <string>) $"val is {i}");

            res.Match(err => Assert.Equal("num is null", err.Message),
                      msg => Assert.Equal($"val is {num}", msg)
                      );
        }
 static Exceptional <R> Apply <T, R>(this Exceptional <Func <T, R> > exceptionalF, Exceptional <T> exceptionalT)
 => exceptionalT.Bind(t => exceptionalF.Bind <Func <T, R>, R>(f => f(t)));
Esempio n. 5
0
 public static Exceptional <R> SelectMany <T, R>(this Exceptional <T> exceptional, Func <T, Exceptional <R> > func)
 => exceptional.Bind(func);
Esempio n. 6
0
 /// <summary>
 /// Binds an <see cref="Exceptional"/> inner value with an async func and awaits for it to finish
 /// </summary>
 public static Exceptional <RR> BindTask <R, RR>(this Exceptional <R> @this, Func <R, Task <Exceptional <RR> > > func)
 => @this.Bind(e => func(e).GetAwaiter().GetResult());