示例#1
0
        public void ConstructFailure_ErrorNotNull()
        {
            var testValue = Failable <object, object> .Failure(new object());

            Assert.That(testValue.IsSuccess, Is.False);
            Assert.That(testValue.IsFailure, Is.True);
        }
示例#2
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)
                );
        }
示例#3
0
        public void Equals_Option()
        {
            var aEOption = EOption <int> .Some(4);

            var aOption = Option <int, Exception> .Some(4);

            var bEOption = EOption <int> .None;
            var bOption  = Option <int, Exception> .None;

            var exception = new ArgumentException();
            var cEOption  = EFailable <int> .Failure(exception);

            var cOption = Failable <int, Exception> .Failure(exception);

            Assert.That(aEOption.GetHashCode(), Is.EqualTo(aOption.GetHashCode()), "HashCode not correct (Success-Case)");
            Assert.That(bEOption.GetHashCode(), Is.EqualTo(bOption.GetHashCode()), "HashCode not correct (None-Case)");
            Assert.That(cEOption.GetHashCode(), Is.EqualTo(cOption.GetHashCode()), "HashCode not correct (Failure-Case)");

            Assert.That(aEOption, Is.EqualTo(aOption), "EOption-Equals is buggy! (Some-Case)");
            Assert.That(bEOption, Is.EqualTo(bOption), "EOption-Equals is buggy! (None-Case)");
            Assert.That(cEOption, Is.EqualTo(cOption), "EOption-Equals is buggy! (Failure-Case)");
            Assert.That(aOption, Is.EqualTo(aEOption), "Implementation of Option is not accepting EOption! (Some-Case)");
            Assert.That(bOption, Is.EqualTo(bEOption), "Implementation of Option is not accepting EOption! (None-Case)");
            Assert.That(cOption, Is.EqualTo(cEOption), "Implementation of Option is not accepting EOption! (Failure-Case)");

            Assert.That(aEOption, Is.Not.EqualTo(bOption));  //sanity-checks
            Assert.That(cOption, Is.Not.EqualTo(bEOption));
        }
示例#4
0
        private void onRegisterRejected(object sender, Failable e)
        {
            switch (e.ErrorCode)
            {
            case 3:
                DisplayAlert("Fehler", Strings.Error_MailAlreadyInUse, "OK");
                break;

            case 4:
                DisplayAlert("Fehler", Strings.Error_UsernameAlreadyInUse, "OK");
                break;

            case 5:
                DisplayAlert("Fehler", Strings.Error_InvalidMail, "OK");
                break;

            case 12:
                DisplayAlert("Fehler", Strings.Error_InvalidUsername, "OK");
                break;

            case 13:
                DisplayAlert("Fehler", Strings.Error_InvalidPassword, "OK");
                break;

            default:
                OnError(sender, e);
                break;
            }
        }
示例#5
0
        public void OrThrow_DoesNotThrowIfSuccess()
        {
            var testValue = ""; //to be overwritten by "happy"

            Assert.DoesNotThrow(() => testValue = Failable <string, int> .Success("HAPPY").OrThrow());
            Assert.That(testValue, Is.EqualTo("HAPPY"));
        }
示例#6
0
        public void OrThrowWithText_DoesNotThrowIfSuccess()
        {
            var testValue = 0;

            Assert.DoesNotThrow(() => testValue = Failable <int, int> .Success(1).OrThrow("Test"));
            Assert.That(testValue, Is.EqualTo(1));
        }
示例#7
0
        public void MapFailure_Success()
        {
            var original = Failable <bool, string> .Success(false);

            var result = original.MapFailure(s => s += " welt");

            Assert.That(result.OrThrow(), Is.EqualTo(false));
        }
示例#8
0
        public void MapFailure_Failure()
        {
            var original = Failable <bool, string> .Failure("hallo");

            var result = original.MapFailure(s => s += " welt");

            Assert.That(result.FailureOrThrow(), Is.EqualTo("hallo welt"));
        }
示例#9
0
        public void Map_NestingInMap()
        {
            var flag = Failable <bool, string> .Success(true);

            var result = flag.Map(_ => Failable <bool, string> .Success(true));

            Assert.That(result.Or(Failable <bool, string> .Failure("disgusting")).Or(false), Is.True);
        }
示例#10
0
        public void Equals_SuccessInequal()
        {
            var x = Failable <int, string> .Success(4);

            var y = Failable <int, string> .Success(5);

            Assert.That(x.Equals(y), Is.False);
        }
示例#11
0
        public void TryGet_Result_Success()
        {
            var failable = Failable <string, Exception> .Success("blub");

            failable.TryGetValue(out var s);

            Assert.That(s, Is.EqualTo("blub"));
        }
示例#12
0
        public void OrThrowWithText_DoesThrowIfFailure()
        {
            var f = Failable <string, int> .Failure(-1);

            var excp = Assert.Throws <FailableFailureException>(() => f.OrThrow("test"));

            Assert.That(excp.Message, Is.EqualTo("test"));
        }
示例#13
0
        public void ConstructSuccess_NotNull()
        {
            var testValue = Failable <object, object> .Success(new object());

            Assert.That(testValue.IsSuccess, Is.True);
            Assert.That(testValue.IsFailure, Is.False);
            Assert.That(testValue.Enum, Is.EqualTo(FlbType.Success));
        }
示例#14
0
        public void Equals_DifferentTypeInequalForFailure()
        {
            var x = Failable <int, string> .Failure("horrible");

            var y = Failable <object, string> .Failure("horrible");

            Assert.That(x.Equals(y), Is.False);
        }
示例#15
0
        public void Equals_FailedInequal()
        {
            var x = Failable <object, string> .Failure("what a disappointment");

            var y = Failable <object, string> .Failure("yet another disappointment");

            Assert.That(x.Equals(y), Is.False);
        }
示例#16
0
        public void Equals_FailedEqual()
        {
            var x = Failable <object, int> .Failure(123);

            var y = Failable <object, int> .Failure(123);

            Assert.That(x.Equals(y), Is.True);
        }
示例#17
0
        public void MapFailureToDifferentType()
        {
            var one = Failable <bool, int> .Failure(1);

            Failable <bool, string> onePlusOne = one.MapFailure(i => $"{i}+1=2");

            Assert.That(onePlusOne.FailureOrThrow(), Is.EqualTo("1+1=2"));
        }
示例#18
0
        public void ConvertToMaybe_Failure()
        {
            var failable = Failable <string, Exception> .Failure(new ArgumentException());

            var maybe = failable.ToMaybe();

            Assert.That(maybe.IsNone, Is.True);
        }
示例#19
0
        public void TryGet_Value_Failure()
        {
            var failable = Failable <string, Exception> .Failure(new ArgumentException());

            failable.TryGetValue(out var s);

            Assert.IsNull(s);
        }
示例#20
0
        public void ConvertToOption_Some()
        {
            var failable = Failable <string, int> .Success("hubba");

            var option = failable.ToOption();

            Assert.That(option.IsSome, Is.True);
        }
示例#21
0
        public void TryGet_ErrorResult_Success()
        {
            var failable = Failable <string, Exception> .Success("blah");

            failable.TryGetFailure(out var s);

            Assert.IsNull(s);
        }
示例#22
0
        public void ConvertToFailable_None()
        {
            var maybe = Maybe <string> .None;
            Failable <string, ArgumentException> failable = maybe.ToFailable(new ArgumentException("msg"));

            Assert.That(failable.IsFailure, Is.True);
            Assert.That(failable.FailureOrThrow().Message, Is.EqualTo("msg"));
        }
示例#23
0
        public void ConvertToOption_None()
        {
            var failable = Failable <string, int> .Failure(42);

            var option = failable.ToOption();

            Assert.That(option.IsFailure, Is.True);
        }
示例#24
0
        public void Map_Success()
        {
            var original = Failable <string, bool> .Success("hallo");

            var result = original.Map(s => s += " welt");

            Assert.That(result.OrThrow, Is.EqualTo("hallo welt"));
        }
示例#25
0
        public void ConvertToOption_SwitchValueType_None()
        {
            var failable = Failable <string, int> .Failure(42);

            var option = failable.ToOptionWith((s) => true);

            Assert.That(option.IsFailure, Is.True);
        }
示例#26
0
        public void Map_Failure()
        {
            var original = Failable <string, bool> .Failure(false);

            var result = original.Map(s => s += " welt");

            Assert.That(result.FailureOrThrow, Is.EqualTo(false));
        }
示例#27
0
        public void ConvertToFailable_Success()
        {
            var EFailable = EFailable <string> .Success("hubba");

            Failable <string, Exception> failable = EFailable.ToFailable();

            Assert.That(failable.IsSuccess, Is.True);
        }
示例#28
0
        public void ConvertFailable_Failure()
        {
            var EFailable = EFailable <string> .Failure(new ArgumentException( "abc" ));

            Failable <string, Exception> failable = EFailable.ToFailable();

            Assert.That(failable.IsFailure, Is.True);
        }
示例#29
0
        public void FlatMapToDifferentType()
        {
            var one = Failable <int, bool> .Success(1);

            Failable <string, bool> onePlusOne = one.FlatMap(i => Failable <string, bool> .Success($"{i}+1=2"));

            Assert.That(onePlusOne.OrThrow(), Is.EqualTo("1+1=2"));
        }
示例#30
0
        public void ConvertToEFailable_WithConverter_Failure()
        {
            var failable = Failable <int, string> .Failure("will-be-overwritten");

            var eFailable = failable.ToEFailableWith(i => $"1{i}", new Exception("new"));

            Assert.That(eFailable.IsFailure, Is.True);
            Assert.That(eFailable.FailureOrThrow().Message, Is.EqualTo("new"));
        }