Пример #1
0
        public void OrThrowWithText_DoesNotThrowIfSome()
        {
            var testValue = 0;

            Assert.DoesNotThrow(() => testValue = EOption <int> .Some(1).OrThrow("Test"));
            Assert.That(testValue, Is.EqualTo(1));
        }
Пример #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));
        }
Пример #3
0
        public void OrThrow_DoesNotThrowIfSome()
        {
            var testValue = ""; //to be overwritten by "happy"

            Assert.DoesNotThrow(() => testValue = EOption <string> .Some("HAPPY").OrThrow());
            Assert.That(testValue, Is.EqualTo("HAPPY"));
        }
Пример #4
0
        public void TryGet_Result_Some()
        {
            var EOption = EOption <string> .Some("blub");

            EOption.TryGetValue(out var s);

            Assert.That(s, Is.EqualTo("blub"));
        }
Пример #5
0
        public void ConstructSome_ViaActualValue()
        {
            var testValue = EOption <object> .Some(new object());

            Assert.That(testValue.IsSome, Is.True);
            Assert.That(testValue.IsNone, Is.False);
            Assert.That(testValue.IsFailure, Is.False);
        }
Пример #6
0
        public void FlatMapToDifferentType()
        {
            var one = EOption <int> .Some(1);

            EOption <string> onePlusOne = one.FlatMap(i => EOption <string> .Some($"{i}+1=2"));

            Assert.That(onePlusOne.OrThrow(), Is.EqualTo("1+1=2"));
        }
Пример #7
0
        public void Map_NestingInMap()
        {
            var flag1 = EOption <bool> .Some(true);

            var result = flag1.Map(_ => EOption <bool> .Some(true));

            Assert.That(result.Or(EOption <bool> .Failure(new ArgumentException("disgusting"))).Or(false), Is.True);
        }
Пример #8
0
        public void Equals_SomeInequal()
        {
            var x = EOption <int> .Some(4);

            var y = EOption <int> .Some(5);

            Assert.That(x.Equals(y), Is.False);
        }
Пример #9
0
        public void Map_Success()
        {
            var original = EOption <string> .Some("hallo");

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

            Assert.That(result.OrThrow, Is.EqualTo("hallo welt"));
        }
Пример #10
0
        public void ConvertToEFailable_Some()
        {
            var EOption = EOption <string> .Some("hallo");

            var failable = EOption.ToEFailable(new ArgumentException());

            Assert.That(failable.IsSuccess, Is.True);
            Assert.That(failable.OrThrow, Is.EqualTo("hallo"));
        }
Пример #11
0
        public void OrConvertToFailable_Some()
        {
            var eoption = EOption <int> .Some(123);

            var failable = eoption.OrToFailable(456);

            Assert.That(failable.IsSuccess, Is.True);
            Assert.That(failable.OrThrow, Is.EqualTo(123));
        }
Пример #12
0
        public void ConvertToOption_Some()
        {
            var EOption = EOption <string> .Some("hallo");

            var option = EOption.ToOption();

            Assert.That(option.IsSome, Is.True);
            Assert.That(option.OrThrow, Is.EqualTo("hallo"));
        }
Пример #13
0
        public void ConvertToMaybe_Some()
        {
            var EOption = EOption <string> .Some("hallo");

            var maybe = EOption.ToMaybe();

            Assert.That(maybe.IsSome, Is.True);
            Assert.That(maybe.OrThrow, Is.EqualTo("hallo"));
        }
Пример #14
0
        public void Equals_None()
        {
            var some = EOption <string> .Some("something");

            var fail = EOption <string> .Failure(new ArgumentException( "inexcusable" ));

            Assert.That(EOption <string> .None.Equals(some), Is.False);
            Assert.That(EOption <string> .None.Equals(fail), Is.False);
            Assert.That(EOption <string> .None.Equals(EOption <string> .None), Is.True);
        }
Пример #15
0
        public void ValuePropagationInFlatMap()
        {
            var hallo = EOption <string> .Some("hallo");

            var sp = EOption <string> .Some(" ");

            var result = hallo.FlatMap(h => sp.Map(space => h + space + "welt"));

            Assert.That(result.Or("nix da"), Is.EqualTo("hallo welt"));
        }
Пример #16
0
        public void Map_NestingInFlatMap()
        {
            var flag1 = EOption <int> .Some(1);

            var flag2 = EOption <int> .Some(2);

            var result = flag1.FlatMap(outerInt => flag2.Map(innerInt => outerInt + innerInt));

            Assert.That(result.Or(-999), Is.EqualTo(3));
        }
Пример #17
0
        public void ConstructSome_ViaActualNullableValue()
        {
            int?testObject = 1;
            var testValue  = EOption <int> .Some(testObject);

            Assert.That(testValue.IsSome, Is.True);
            Assert.That(testValue.IsNone, Is.False);
            Assert.That(testValue.IsFailure, Is.False);
            Assert.That(testValue.Or(2), Is.EqualTo(1));
        }
Пример #18
0
        public void OrWithParallelSubtype_Some()
        {
            var mySubclass      = new MySubclass();
            var myOtherSubclass = new MyOtherSubclass();

            var underTest = EOption <MyClass> .Some(mySubclass);

            var actualValue = underTest.Or(myOtherSubclass);

            Assert.That(actualValue, Is.SameAs(mySubclass));
        }
Пример #19
0
        public void Equals_FailedInequalToSome()
        {
            var x = EOption <int> .Failure(new ArgumentException( "how appaling!" ));

            var y = EOption <int> .Some(0);

            var z = EOption <EOption <int> > .Some(y);

            Assert.That(x.Equals(y), Is.False);
            Assert.That(x.Equals(z), Is.False);
        }
Пример #20
0
        public void Flatten_None()
        {
            var a = EOption <string> .None;
            var b = EOption <EOption <string> > .Some(EOption <string> .None);

            var c = EOption <EOption <string> > .Some(EOption <string> .Failure(new Exception("")));

            Assert.That(b, Is.Not.EqualTo(a));
            Assert.That(b.Flatten(), Is.EqualTo(a));
            Assert.That(b.Flatten(), Is.Not.EqualTo(c));
        }
Пример #21
0
        public void Map_NestingInFlatMap_FlattenToEOption()
        {
            var flag1 = EFailable <int> .Success(1);

            var flag2 = EOption <int> .Some(2);

            var result = flag1.FlatMap(outerInt => flag2.Map(innerInt => outerInt + innerInt));

            Assert.True(result.IsSome);
            Assert.That(result.Or(-999), Is.EqualTo(3));
        }
Пример #22
0
        public void Equals_DifferentTypeInequal()
        {
            var t1 = new Tuple <string, int>("abc", 123);
            var t2 = new Tuple <string, string>("abc", "123");

            var x = EOption <Tuple <string, int> > .Some(t1);

            var y = EOption <Tuple <string, string> > .Some(t2);

            Assert.That(t1.Equals(t2), Is.False);
            Assert.That(x.Equals(y), Is.False);
        }
Пример #23
0
        public void Flatten_Some()
        {
            var a = EOption <string> .Some("hallo");

            var b = EOption <EOption <string> > .Some(EOption <string> .Some("hallo"));

            var c = EOption <EOption <string> > .Some(EOption <string> .None);

            Assert.That(b, Is.Not.EqualTo(a));
            Assert.That(b.Flatten(), Is.EqualTo(a));
            Assert.That(b.Flatten(), Is.Not.EqualTo(c));
        }
Пример #24
0
        public void ConstructSome_ValueIsAnException()
        {
            //WICHTIG: Es gibt Argumente, das hier zu verbieten und analog zum EOption auf ein Failure zu mappen
            //         Es gibt aber auch Argumente, es so zu lassen wie es ist. Aktuell wurde sich für die
            //         weniger invasive Variante entschieden, vor allem da es weniger implizite Sachen macht.
            //         Dieser Test dient zur Dokumentation dieses Verhaltens.
            var x = EOption <Exception> .Some(new Exception());

            Assert.That(x.IsSome, Is.True);
            Assert.That(x.IsNone, Is.False);
            Assert.That(x.IsFailure, Is.False);
        }
Пример #25
0
        public void Flatten_Failure()
        {
            var a = EOption <string> .Failure(new Exception( "" ));

            var b = EOption <EOption <string> > .Some(a); //use same instance so that Exception.Equals() returns true

            var c = EOption <EOption <string> > .Some(EOption <string> .None);

            Assert.That(b, Is.Not.EqualTo(a));
            Assert.That(b.Flatten(), Is.EqualTo(a));
            Assert.That(b.Flatten(), Is.Not.EqualTo(c));
        }
Пример #26
0
        public void Equals_SomeEqual()
        {
            var t1 = new Tuple <string, int>("abc", 123);
            var t2 = new Tuple <string, int>("abc", 123);

            var x = EOption <Tuple <string, int> > .Some(t1);

            var y = EOption <Tuple <string, int> > .Some(t2);

            Assert.That(t1.Equals(t2), Is.True);
            Assert.That(x.Equals(y), Is.True);
        }
Пример #27
0
        public void Nesting()
        {
            var someInnerSome = EOption <EOption <int> > .Some(EOption <int> .Some(123));

            var someInnerNone = EOption <EOption <int> > .Some(EOption <int> .None);

            var someInnerFail = EOption <EOption <int> > .Some(EOption <int> .Failure(new ArgumentException()));

            var failure = EOption <EOption <int> > .Failure(new ArgumentException());

            var none = EOption <EOption <int> > .None;

            Assert.That(someInnerSome.IsSome, Is.True);
            Assert.That(someInnerNone.IsSome, Is.True);
            Assert.That(someInnerFail.IsSome, Is.True);
            Assert.That(someInnerSome.OrThrow().IsSome, Is.True);
            Assert.That(someInnerNone.OrThrow().IsNone, Is.True);
            Assert.That(someInnerFail.OrThrow().IsFailure, Is.True);
            Assert.That(failure.IsFailure, Is.True);
            Assert.That(none.IsNone, Is.True);
        }
Пример #28
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));
        }
Пример #29
0
        public void TryGet_Value_Some()
        {
            var EOption = EOption <string> .Some("blub");

            Assert.IsTrue(EOption.TryGetValue(out var s));
        }
Пример #30
0
        public void Is_PredicateNonMatchingDefined()
        {
            var EOption = EOption <string> .Some("hubba");

            Assert.That(EOption.Holds(s => s.Equals("hubba-hub")), Is.EqualTo(false));
        }