Пример #1
0
        public void Should_Not_Select_Square_Of_Ten_Given_Failed_State()
        {
            Failable <int> input = default;

            // From value
            input
            .Pipe(
                Where <int>(x => x % 2 == 1)
                )
            .Select(_ => _ * _)
            .Match(
                success: (_) => throw new Exception("Test has failed"),
                failure: (_) => Assert.Equal(WHERE_EXCEPTION_MESSAGE, _.Message)
                );

            // From exception
            Failable
            .From <object>(new Exception("It could not create the value"))
            .Pipe(
                Where <object>(x => x is not Exception)
                )
            .Match(
                success: (_) => throw new Exception("Test has failed"),
                failure: (_) => Assert.Equal("It could not create the value", _.Message)
                );
        }
Пример #2
0
        public void Should_Not_Do()
        {
            Failable <int> input = 21;

            _ =
                input
                .Pipe(
                    Where <int>(x => x < 20, new Exception("Value must be less than 20")),
                    Do <int>(() => throw new Exception("Test has failed. It should not run this function"))
                    );

            _ =
                input
                .Pipe(
                    Where <int>(x => x < 20, new Exception("Value must be less than 20")),
                    Do <int>(() => throw new Exception("Test has failed. It should not run this function"))
                    );
        }
Пример #3
0
        public void Should_Do()
        {
            Failable <int> input = 10;

            _ =
                input
                .Pipe(
                    Where <int>(x => x < 20, new Exception("Value must be less than 20")),
                    Do <int>(() => Assert.True(true))
                    );

            _ =
                input
                .Pipe(
                    Where <int>(x => x < 20, new Exception("Value must be less than 20")),
                    Do <int>((x) => Assert.True(x == 10))
                    );
        }
Пример #4
0
        public void Should_Do_All_Actions()
        {
            Failable <bool> input = false;

            Failable <bool> value =
                input
                .Pipe(
                    Do <bool>((_) => {})
                    )
                .Select();

            value.Match(
                success: (_) => Assert.True(true),
                failure: (_) => throw new Exception("Test has failed")
                );
        }
Пример #5
0
        public void Should_Not_Select_Given_Invalid_Input()
        {
            Failable <bool> input = false;

            Failable <string> value =
                input
                .Pipe(
                    Where <bool>(x => x, new Exception("Value must be true"))
                    )
                .Select((value) => "Value is true");

            value.Match(
                success: (value) => throw new Exception("Test has failed. It should not run the select function"),
                failure: (error) => Assert.Equal("Value must be true", error.Message)
                );
        }
Пример #6
0
        public void Should_Not_Select_Square_Of_Ten_Given_Unsuccessful_Predicate_With_Custom_Exception()
        {
            Failable <int> input = 10;

            Failable <int> value =
                input
                .Pipe(
                    Where <int>(x => x % 2 == 1, new Exception(WHERE_CUSTOM_EXCEPTION_MESSAGE))
                    )
                .Select(_ => _ * _);

            value.Match(
                success: (_) => throw new Exception("Test has failed"),
                failure: (_) => Assert.Equal(WHERE_CUSTOM_EXCEPTION_MESSAGE, _.Message)
                );
        }
Пример #7
0
        public void Should_Select_Square_Of_Ten_Given_Successful_Predicate()
        {
            Failable <int> input = 10;

            Failable <int> value =
                input
                .Pipe(
                    Where <int>(x => x % 2 == 0)
                    )
                .Select((_) => _ * _);

            value.Match(
                success: (_) => Assert.Equal(100, _),
                failure: (_) => throw new Exception("Test has failed")
                );
        }
Пример #8
0
        public void Should_Not_Do_Last_Action()
        {
            Failable <bool> input = false;

            Failable <bool> value =
                input
                .Pipe(
                    Do <bool>((_) => {}),
                    Do <bool>((_) => Assert.False(_))
                    )
                .Select();

            value.Match(
                success: (_) => Assert.False(_),
                failure: (error) => throw new Exception("It should not run this block")
                );
        }