예제 #1
0
        public async Task Result_With_Error_Maps__Expects_Selector_Never_Be_Executed()
        {
            var selectorExectued = false;
            await AssertionUtilities
            .Division(2, 0)
            .MapAsync(async x => {
                await AssertionUtilities.Delay;
                selectorExectued = true;
                return(x * 8);
            })
            .AssertError("Can not divide '2' with '0'.");

            Assert.False(selectorExectued,
                         "The selector function should never get executed if there's no value in the Result<T, TError>.");
        }
예제 #2
0
        Result_With_Value__Expect_Selector()
        {
            var selectorExectued      = false;
            var errorSelectorExectued = false;
            var result = AssertionUtilities.Division(10, 2).Match(d => {
                selectorExectued = true;
                return(d);
            }, s => {
                errorSelectorExectued = true;
                return(-1);
            });

            Assert.True(selectorExectued, "Selector should get executed.");
            Assert.False(errorSelectorExectued, "Error selector not should get exectued.");
            Assert.Equal(5, result);
        }
 public async Task Result_With_Value_Flatmaps_Result_with_Value__Expects_Result_With_Value() {
     var flatSelectorExecuted = false;
     var errorSelectorExecuted = false;
     await AssertionUtilities
         .Division(2, 2)
         .FlatMapAsync(x => {
             flatSelectorExecuted = true;
             return AssertionUtilities.DivisionAsync(x, 2);
         }, s => {
             errorSelectorExecuted = true;
             return s;
         })
         .AssertValue(0.5d);
     Assert.True(flatSelectorExecuted, "flatmapselector should get executed.");
     Assert.False(errorSelectorExecuted, "Errorselector should not get exeuted.");
 }
예제 #4
0
        public async Task Result_With_Error_Flatmaps_Result_with_Error__Expects_Result_With_Error()
        {
            var flatSelectorExecuted  = false;
            var errorSelectorExecuted = false;
            await AssertionUtilities.Division(2, 0)
            .FlattenAsync(x => {
                flatSelectorExecuted = true;
                return(AssertionUtilities.DivisionAsync(x, 0));
            }, s => {
                errorSelectorExecuted = true;
                return(s);
            }).AssertError("Can not divide '2' with '0'.");

            Assert.False(errorSelectorExecuted,
                         "Errorselector should not get exeuted since there is an error in the source.");
            Assert.False(flatSelectorExecuted);
        }
예제 #5
0
        Result_With_Error__Expects_Predicate_Never_To_Be_Executed_And_ErrorSelector_Never_To_Be_Invoked_With_Parameter_ErrorSelector()
        {
            var predicateExectued     = false;
            var errorSelectorExectued = false;
            await AssertionUtilities.Division(10, 0).FilterAsync(async d => {
                predicateExectued = true;
                await Task.Delay(50);
                return(d == 2);
            }, x => {
                errorSelectorExectued = true;
                return("This should never happen!");
            }).AssertError("Can not divide '10' with '0'.");

            Assert.False(predicateExectued,
                         "Should not get exectued since there's an error before the predicate was applied.");
            Assert.False(errorSelectorExectued,
                         "Should not get exectued since there's an error before the predicate was applied.");
        }
예제 #6
0
        Async_ErrorSelector_Result_With_Value_Expects__Selector_To_Be_Executed_And_ErrorSelector_To_Never_Be_Invoked()
        {
            var selectorExectued      = false;
            var errorSelectorExectued = false;
            await AssertionUtilities
            .Division(10, 2)
            .FullMapAsync(d => {
                selectorExectued = true;
                return(d * 10);
            }, async s => {
                await AssertionUtilities.Delay;
                errorSelectorExectued = true;
                return(s);
            }).AssertValue(50);

            Assert.True(selectorExectued, "Should get exectued since there's an value from the result.");
            Assert.False(errorSelectorExectued, "Should not get exectued since there's an value from the result.");
        }
예제 #7
0
        Result_With_Value_With_Truthy_Predicate__Expects_Predicate_To_Be_Executed_And_ErrorSelector_To_Never_Be_Invoked_With_Parameter_ErrorSelector()
        {
            var predicateExectued     = false;
            var errorSelectorExectued = false;

            AssertionUtilities
            .Division(10, 2)
            .Filter(d => {
                predicateExectued = true;
                return(true);
            }, x => {
                errorSelectorExectued = true;
                return("This should never happen.");
            })
            .AssertValue(5);

            Assert.True(predicateExectued, "Should get exectued since there's a value from the result.");
            Assert.False(errorSelectorExectued, "Should not get exectued since the predicate was falsy.");
        }
        public async Task Result_With_Error_Flatmaps_Result_with_Value__Expects_Result_With_Error() {
            var flatSelectorExecuted = false;
            var errorSelectorExecuted = false;
            await AssertionUtilities
                .Division(2, 0)
                .FlatMapAsync(x => {
                    flatSelectorExecuted = true;
                    return AssertionUtilities.DivisionAsync(x, 2);
                }, s => {
                    errorSelectorExecuted = true;
                    return s;
                })
                .AssertError("Can not divide '2' with '0'.");

            Assert.False(errorSelectorExecuted,
                "Errorselector should not get exeuted since there is an error in the source.");
            Assert.False(flatSelectorExecuted,
                "The flatmap selector should not get exectued if the source Result<T, TError> contains error.");
        }
예제 #9
0
        public void Result_With_Value_Flattens_Result_with_Error__Expects_Result_With_Error()
        {
            var flatSelectorExecuted  = false;
            var errorSelectorExecuted = false;

            AssertionUtilities
            .Division(2, 2)
            .Flatten(x => {
                flatSelectorExecuted = true;
                return(AssertionUtilities.Division(x, 0));
            }, s => {
                errorSelectorExecuted = true;
                return(s);
            })
            .AssertError("Can not divide '1' with '0'.");

            Assert.True(errorSelectorExecuted,
                        "Errorselector should get exeuted since the errror came from the result given to the flatselector.");
            Assert.True(flatSelectorExecuted,
                        "The flatselector should not get executed since flatselector result failed.");
        }
예제 #10
0
        Result_With_Value_With_Falsy_Predicate__Expects_Predicate_To_Be_Executed_And_ErrorSelector_To_Never_Be_Invoked()
        {
            var predicateExectued     = false;
            var errorSelectorExectued = false;
            await AssertionUtilities
            .Division(10, 2)
            .IsErrorWhenAsync(async d => {
                await AssertionUtilities.Delay;
                predicateExectued = true;
                return(false);
            }, x => {
                errorSelectorExectued = true;
                return("Bad");
            })
            .AssertValue(5);

            Assert.True(predicateExectued,
                        "Should get exectued since there's a value from the result.");
            Assert.False(errorSelectorExectued,
                         "Should not get exectued since the predicate was falsy.");
        }
예제 #11
0
        public void Result_With_Value_FlatmapsRS_Result_with_Value__Expects_Result_With_Value()
        {
            var flatSelectorExecuted   = false;
            var resultSelectorExectued = false;
            var errorSelectorExecuted  = false;

            AssertionUtilities
            .Division(2, 2)
            .FullFlatMap(x => {
                flatSelectorExecuted = true;
                return(AssertionUtilities.Division(x, 2));
            }, (y, x) => {
                resultSelectorExectued = true;
                return(y + x);
            }, s => {
                errorSelectorExecuted = true;
                return(s);
            }).AssertValue(1.5d);
            Assert.True(flatSelectorExecuted, "Flatmapselecotr should get executed.");
            Assert.True(resultSelectorExectued,
                        "ResultSelector should get executed since both source and the result from flatmapselector contains values.");
            Assert.False(errorSelectorExecuted,
                         "Erroselector should not get executed since both source and the result from flatmapselector contains values.");
        }