Example #1
0
        public void Returns_False_If_ExceptionIsThrown()
        {
            int result;
            var success = TryUtility.Try(FakeBuilder.FuncThrows <int>(), out result);

            success.Should().Be(false);
        }
        public void TryFailureWithNotOutputSupportsChaining()
        {
            const string errorMessage = "Something went wrong";
            double       value        = 10;
            var          tryFailure   = TryUtility.CreateTryFailure(new Exception(errorMessage));

            var try1 = TryUtility.Chain(tryFailure, () =>
            {
                value = value / 4;
                return(TryUtility.CreateTrySuccessful());
            });
            var try2 = TryUtility.Chain(try1, () =>
            {
                value = value * 2;
                return(TryUtility.CreateTrySuccessful());
            });
            var try3 = TryUtility.Chain(try2, () =>
            {
                value = value + 15;
                return(TryUtility.CreateTrySuccessful());
            });

            Func <string>            successHandler = () => value.ToString();
            Func <Exception, string> failureHandler = ex => ex.Message;

            var output = try3.Check(successHandler, failureHandler);

            Assert.AreEqual(errorMessage, output);
        }
Example #3
0
        public void Returns_Default_If_ExceptionIsThrown()
        {
            int result;

            TryUtility.Try(FakeBuilder.FuncThrows <int>(), out result);
            result.Should().Be(default(int));
        }
Example #4
0
        public void Returns_True_If_NoExceptionIsThrown()
        {
            int result;
            var success = TryUtility.Try(FakeBuilder.FuncDoesNotThrow <int>(), out result);

            success.Should().Be(true);
        }
        public void TrySuccessfulWithNoOutputSupportsChaining()
        {
            double value         = 10;
            var    trySuccessful = TryUtility.CreateTrySuccessful();

            var try1 = TryUtility.Chain(trySuccessful, () =>
            {
                value = value / 4;
                return(TryUtility.CreateTrySuccessful());
            });
            var try2 = TryUtility.Chain(try1, () =>
            {
                value = value * 2;
                return(TryUtility.CreateTrySuccessful());
            });
            var try3 = TryUtility.Chain(try2, () =>
            {
                value = value + 15;
                return(TryUtility.CreateTrySuccessful());
            });

            Func <string>            successHandler = () => value.ToString();
            Func <Exception, string> failureHandler = ex => ex.Message;

            var output = try3.Check(successHandler, failureHandler);

            Assert.AreEqual("20", output);
        }
        public void TrySuccessfulInitialises()
        {
            int successfulValue = 1;

            var trySuccessful = TryUtility.CreateTrySuccessful <int>(successfulValue);

            Assert.IsNotNull(trySuccessful);
        }
Example #7
0
        public void Returns_Value_If_NoExceptionIsThrown(
            [Frozen] IFixture fixture)
        {
            int result;

            TryUtility.Try(FakeBuilder.FuncDoesNotThrow <int>(fixture), out result);
            result.Should().Be(fixture.Create <int>());
        }
        public void TryFailureInitialises()
        {
            Exception error = new Exception();

            var tryFailure = TryUtility.CreateTryFailure <int>(error);

            Assert.IsNotNull(tryFailure);
        }
Example #9
0
        public Try(Func <TResult> function) : base()
        {
            Results = new TryResult <object> [1];

            TResult resultObject;

            Success    = TryUtility.Try(function, out resultObject);
            Results[0] = new TryResult <object>(resultObject, Success);
        }
Example #10
0
 public void DoesNotThrow()
 {
     try
     {
         TryUtility.Try(FakeBuilder.ActionThrows());
     }
     catch
     {
         Assert.True(false, "Expected no exception from TryUtility, but got one");
     }
 }
Example #11
0
 public void DoesNotThrow()
 {
     try
     {
         int result;
         TryUtility.Try(FakeBuilder.FuncThrows <int>(), out result);
     }
     catch
     {
         Assert.True(false, "Expected no exception from TryUtility, but got one");
     }
 }
        public void TrySuccessfulWithNotOutputInvokesSuccessFunc()
        {
            const string expectedOutput = "Success";

            var trySuccessful = TryUtility.CreateTrySuccessful();

            Func <string>            successHandler = () => expectedOutput;
            Func <Exception, string> failureHandler = e => e.Message;

            var output = trySuccessful.Check(successHandler, failureHandler);

            Assert.AreEqual(expectedOutput, output);
        }
        public void TrySuccessfulInvokesSuccessAction()
        {
            int count           = 10;
            int successfulValue = 1;

            var trySuccessful = TryUtility.CreateTrySuccessful <int>(successfulValue);

            Action <int>       successHandler = i => count = count + i;
            Action <Exception> failureHandler = i => count = count - 1;

            trySuccessful.Check(successHandler, failureHandler);
            Assert.AreEqual(11, count);
        }
        public void TrySuccessInvokesSuccessFunc()
        {
            int successfulValue = 1;

            var trySuccessful = TryUtility.CreateTrySuccessful <int>(successfulValue);

            Func <int, string>       successHandler = i => i.ToString();
            Func <Exception, string> failureHandler = e => e.Message;

            var output = trySuccessful.Check(successHandler, failureHandler);

            Assert.AreEqual("1", output);
        }
        public void TryFailureWithNoOutputInvokesFailedAction()
        {
            int count = 10;

            var tryFailure = TryUtility.CreateTryFailure(new Exception());

            Action             successHandler = () => count = count + 1;
            Action <Exception> failureHandler = e => count = count - 1;

            tryFailure.Check(successHandler, failureHandler);

            Assert.AreEqual(9, count);
        }
        public void TrySuccessfulWithNotOutputInvokesSuccessAction()
        {
            int count = 10;

            var trySuccessful = TryUtility.CreateTrySuccessful();

            Action             successHandler = () => count = count + 1;
            Action <Exception> failureHandler = e => count = count - 1;

            trySuccessful.Check(successHandler, failureHandler);

            Assert.AreEqual(11, count);
        }
        public void TryFailureInvokesFailedAction()
        {
            int       count = 10;
            Exception error = new Exception();

            var tryFailure = TryUtility.CreateTryFailure <int>(error);

            Action <int>       successHandler = i => count = count + i;
            Action <Exception> failureHandler = i => count = count - 1;

            tryFailure.Check(successHandler, failureHandler);
            Assert.AreEqual(9, count);
        }
        public void TryFailureWithNoOutputInvokesFailedFunc()
        {
            const string expectedOutput = "foo";
            Exception    error          = new Exception(expectedOutput);

            var tryFailure = TryUtility.CreateTryFailure(error);

            Func <string>            successHandler = () => expectedOutput;
            Func <Exception, string> failureHandler = e => e.Message;

            var output = tryFailure.Check(successHandler, failureHandler);

            Assert.AreEqual(expectedOutput, output);
        }
        public void TryFailureInvokesFailedFunc()
        {
            const string errorMessage = "foo";
            Exception    error        = new Exception(errorMessage);

            var tryFailure = TryUtility.CreateTryFailure <int>(error);

            Func <int, string>       successHandler = i => i.ToString();
            Func <Exception, string> failureHandler = e => e.Message;

            var output = tryFailure.Check(successHandler, failureHandler);

            Assert.AreEqual("foo", output);
        }
        public void TrySuccessfulMonadSupportsChaining()
        {
            int originalValue = 10;
            var trySuccessful = TryUtility.CreateTrySuccessful(originalValue);

            var try1 = TryUtility.Chain <int, double>(trySuccessful, i => (double)i / 4); // 2.5
            var try2 = TryUtility.Chain <double, int>(try1, d => (int)(d * 2));           // 5
            var try3 = TryUtility.Chain <int, string>(try2, i => i.ToString());           //"5"

            Func <string, string>    successHandler = s => s;
            Func <Exception, string> errorHandler   = e => e.Message;

            var output = try3.Check(successHandler, errorHandler);

            Assert.AreEqual("5", output);
        }
        public void TryFailureMonadSupportChaining()
        {
            const string errorMessage = "Something went wrong";
            Exception    error        = new Exception(errorMessage);

            var tryFailure = TryUtility.CreateTryFailure <int>(error);

            var try1 = TryUtility.Chain <int, double>(tryFailure, i => (double)i / 4);
            var try2 = TryUtility.Chain <double, int>(try1, d => (int)(d * 2));
            var try3 = TryUtility.Chain <int, string>(try2, i => i.ToString());

            Func <string, string>    successHandler = s => s;
            Func <Exception, string> errorHandler   = e => e.Message;

            var output = try3.Check(successHandler, errorHandler);

            Assert.AreEqual(errorMessage, output);
        }
Example #22
0
        public void Returns_False_If_ExceptionIsThrown()
        {
            var success = TryUtility.Try(FakeBuilder.ActionThrows());

            success.Should().Be(false);
        }
Example #23
0
        public void Returns_True_If_NoExceptionIsThrown()
        {
            var success = TryUtility.Try(FakeBuilder.ActionDoesNotThrow());

            success.Should().Be(true);
        }
        public void TrySuccessfulWithNoOutputInitialises()
        {
            var trySuccessful = TryUtility.CreateTrySuccessful();

            Assert.IsNotNull(trySuccessful);
        }
        public void TryFailureWithNotOutputInitialises()
        {
            var tryFailure = TryUtility.CreateTryFailure(new Exception());

            Assert.IsNotNull(tryFailure);
        }
Example #26
0
 public static bool Tries <TResult>(this object obj, Func <TResult> function, out TResult result)
 {
     return(TryUtility.Try(function, out result));
 }
Example #27
0
 public Try(Action action)
 {
     Results    = new TryResult[1];
     Results[0] = new TryResult(TryUtility.Try(action));
 }
Example #28
0
 public static bool Tries(this object obj, Action action)
 {
     return(TryUtility.Try(action));
 }