Ejemplo n.º 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);
        }
Ejemplo n.º 2
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));
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
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"));
        }
Ejemplo n.º 7
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"));
        }
Ejemplo n.º 8
0
        public void TryGet_Value_Failure()
        {
            var failable = Failable <string, Exception> .Failure(new ArgumentException());

            failable.TryGetValue(out var s);

            Assert.IsNull(s);
        }
Ejemplo n.º 9
0
        public void ConvertToMaybe_Failure()
        {
            var failable = Failable <string, Exception> .Failure(new ArgumentException());

            var maybe = failable.ToMaybe();

            Assert.That(maybe.IsNone, Is.True);
        }
Ejemplo n.º 10
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"));
        }
Ejemplo n.º 11
0
        public void ConvertToOption_None()
        {
            var failable = Failable <string, int> .Failure(42);

            var option = failable.ToOption();

            Assert.That(option.IsFailure, Is.True);
        }
Ejemplo n.º 12
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);
        }
Ejemplo n.º 13
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));
        }
Ejemplo n.º 14
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);
        }
Ejemplo n.º 15
0
        public void ConvertToEFailable_Failure()
        {
            var failable = Failable <string, bool> .Failure(true);

            EFailable <string> EFailable = failable.ToEFailable(new ArgumentException("msg"));

            Assert.That(EFailable.IsFailure, Is.True);
            Assert.That(EFailable.FailureOrThrow().Message, Is.EqualTo("msg"));
        }
Ejemplo n.º 16
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"));
        }
Ejemplo n.º 17
0
        public void Map_Failure_SideEffect()
        {
            var myInt    = 0;
            var original = Failable <string, bool> .Failure(false);

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

            Assert.That(myInt, Is.EqualTo(0));
        }
Ejemplo n.º 18
0
        public void TryGet_ErrorResult_Failure()
        {
            var failable = Failable <string, Exception> .Failure(new ArgumentException("msg"));

            failable.TryGetFailure(out var e);

            Assert.That(e, Is.TypeOf <ArgumentException>());
            Assert.That(e.Message, Is.EqualTo("msg"));
        }
Ejemplo n.º 19
0
        public void OrWithParallelType_Failure()
        {
            var myOtherSubclass = new MyOtherSubclass();

            var underTest = Failable <MyClass, Exception> .Failure(new ArgumentException( "irrelevant" ));

            var actualValue = underTest.Or(myOtherSubclass);

            Assert.That(actualValue, Is.SameAs(myOtherSubclass));
        }
Ejemplo n.º 20
0
        public void Equals_FailedInequalToSuccess()
        {
            var x = Failable <int, string> .Failure("how appaling!");

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

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

            Assert.That(x.Equals(y), Is.False);
            Assert.That(x.Equals(z), Is.False);
        }
Ejemplo n.º 21
0
        public void Flatten_Failure()
        {
            var a = Failable <string, int> .Failure(42);

            var b = Failable <Failable <string, int>, int> .Success(Failable <string, int> .Failure(42));

            var c = Failable <Failable <string, int>, int> .Success(Failable <string, int> .Success("whatever"));

            Assert.That(b, Is.Not.EqualTo(a));
            Assert.That(b.Flatten(), Is.EqualTo(a));
            Assert.That(b.Flatten(), Is.Not.EqualTo(c));
        }
Ejemplo n.º 22
0
        public void Flatten_DifferentFailType_Failure()
        {
            var a = Failable <string, int> .Failure(42);

            var b = Failable <Failable <string, string>, int> .Success(Failable <string, string> .Failure("error"));

            var c = Failable <Failable <string, string>, int> .Success(Failable <string, string> .Success("whatever"));

            Assert.That(b, Is.Not.EqualTo(a));
            Assert.That(b.Flatten((s) => 42), Is.EqualTo(a));
            Assert.That(b.Flatten((s) => 42), Is.Not.EqualTo(c));
        }
Ejemplo n.º 23
0
        public void Equals_Failable_DifferentType()
        {
            var aEFail = EFailable <int> .Success(4);

            var aFail = Failable <int, string> .Success(4);

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

            var bFail = Failable <int, string> .Failure("whatever");

            Assert.That(aEFail, Is.Not.EqualTo(aFail));
            Assert.That(bEFail, Is.Not.EqualTo(bFail));
            Assert.That(aFail, Is.Not.EqualTo(aEFail));
            Assert.That(bFail, Is.Not.EqualTo(bEFail));
        }
Ejemplo n.º 24
0
        public void Nesting()
        {
            var successInnerSuccess = Failable <Failable <int, string>, string> .Success(Failable <int, string> .Success(123));

            var successInnerFail = Failable <Failable <int, string>, string> .Success(Failable <int, string> .Failure("hallo"));

            var failureInnerSuccess = Failable <int, Failable <int, string> > .Failure(Failable <int, string> .Success(123));

            var failureInnerFail = Failable <int, Failable <int, string> > .Failure(Failable <int, string> .Failure("hallo"));

            Assert.That(successInnerSuccess.IsSuccess, Is.True);
            Assert.That(successInnerFail.IsSuccess, Is.True);
            Assert.That(successInnerSuccess.OrThrow().IsSuccess, Is.True);
            Assert.That(successInnerFail.OrThrow().IsSuccess, Is.False);
            Assert.That(failureInnerSuccess.IsSuccess, Is.False);
            Assert.That(failureInnerFail.IsSuccess, Is.False);
            Assert.That(failureInnerSuccess.FailureOrThrow().IsSuccess, Is.True);
            Assert.That(failureInnerFail.FailureOrThrow().IsSuccess, Is.False);
        }
Ejemplo n.º 25
0
        public void Equals_Failable()
        {
            var aEFail = EFailable <int> .Success(4);

            var aFail = Failable <int, Exception> .Success(4);

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

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

            Assert.That(aEFail.GetHashCode(), Is.EqualTo(aFail.GetHashCode()), "HashCode not correct (Success-Case)");
            Assert.That(bEFail.GetHashCode(), Is.EqualTo(bFail.GetHashCode()), "HashCode not correct (Failure-Case)");

            Assert.That(aEFail, Is.EqualTo(aFail), "EFailable-Equals is buggy! (Success-Case)");
            Assert.That(bEFail, Is.EqualTo(bFail), "EFailable-Equals is buggy! (Failure-Case)");
            Assert.That(aFail, Is.EqualTo(aEFail), "Implementation of Failable is not accepting EFailable! (Success-Case)");
            Assert.That(bFail, Is.EqualTo(bEFail), "Implementation of Failable is not accepting EFailable! (Failure-Case)");

            Assert.That(aEFail, Is.Not.EqualTo(bFail));  //sanity-check
        }
Ejemplo n.º 26
0
        public void Equals_Option_DifferentType()
        {
            var aEOption = EOption <int> .Some(4);

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

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

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

            var cOption = Failable <int, string> .Failure("whatever");

            Assert.That(aEOption, Is.Not.EqualTo(aOption));
            Assert.That(bEOption, Is.Not.EqualTo(bOption));
            Assert.That(cEOption, Is.Not.EqualTo(cOption));
            Assert.That(aOption, Is.Not.EqualTo(aEOption));
            Assert.That(bOption, Is.Not.EqualTo(bEOption));
            Assert.That(cOption, Is.Not.EqualTo(cEOption));
        }
Ejemplo n.º 27
0
        public void TryGet_Result_Failure()
        {
            var failable = Failable <string, Exception> .Failure(new ArgumentException());

            Assert.IsFalse(failable.TryGetValue(out var s));
        }
Ejemplo n.º 28
0
        public void ContainsFalseWhenNone()
        {
            var f = Failable <string, Exception> .Failure(new ArgumentException());

            Assert.That(f.Is("hubba"), Is.EqualTo(false));
        }
Ejemplo n.º 29
0
        public void Is_PredicateUndefined()
        {
            var failable = Failable <string, Exception> .Failure(new ArgumentException());

            Assert.That(failable.Holds(s => s.Equals("hubba-hub")), Is.EqualTo(false));
        }
Ejemplo n.º 30
0
        public void TryGet_Error_Failure()
        {
            var failable = Failable <string, Exception> .Failure(new ArgumentException());

            Assert.IsTrue(failable.TryGetFailure(out var s));
        }