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")); }
public void OrThrowWithText_DoesNotThrowIfSuccess() { var testValue = 0; Assert.DoesNotThrow(() => testValue = Failable <int, int> .Success(1).OrThrow("Test")); Assert.That(testValue, Is.EqualTo(1)); }
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)); }
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); }
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)); }
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")); }
public void TryGet_ErrorResult_Success() { var failable = Failable <string, Exception> .Success("blah"); failable.TryGetFailure(out var s); Assert.IsNull(s); }
public void TryGet_Result_Success() { var failable = Failable <string, Exception> .Success("blub"); failable.TryGetValue(out var s); Assert.That(s, Is.EqualTo("blub")); }
public void ConvertToOption_Some() { var failable = Failable <string, int> .Success("hubba"); var option = failable.ToOption(); Assert.That(option.IsSome, Is.True); }
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")); }
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); }
public void ConvertToEFailable_Success() { var failable = Failable <string, bool> .Success("hallo"); var EFailable = failable.ToEFailable(new ArgumentException()); Assert.That(EFailable.IsSuccess, Is.True); Assert.That(EFailable.OrThrow, Is.EqualTo("hallo")); }
public void Flatten_DifferentFailType_Success() { var a = Failable <string, int> .Success("hallo"); var b = Failable <Failable <string, string>, int> .Success(Failable <string, string> .Success("hallo")); Assert.That(b, Is.Not.EqualTo(a)); Assert.That(b.Flatten((s) => 42), Is.EqualTo(a)); }
public void Flatten_Success() { var a = Failable <string, int> .Success("hallo"); var b = Failable <Failable <string, int>, int> .Success(Failable <string, int> .Success("hallo")); Assert.That(b, Is.Not.EqualTo(a)); Assert.That(b.Flatten(), Is.EqualTo(a)); }
public void ConvertToOption_SwitchValueType_Some() { var failable = Failable <string, int> .Success("hubba"); var option = failable.ToOptionWith((s) => true); Assert.That(option.IsSome, Is.True); Assert.IsTrue(option.OrThrow()); }
public void ConvertToEFailable_WithConverter_Success() { var failable = Failable <int, string> .Success(42); var eFailable = failable.ToEFailableWith(i => $"1{i}", new Exception("whatever")); Assert.That(eFailable.IsSuccess, Is.True); Assert.That(eFailable.OrThrow(), Is.EqualTo("142")); }
public void ConvertToMaybe_Success() { var failable = Failable <string, Exception> .Success("hallo"); var maybe = failable.ToMaybe(); Assert.That(maybe.IsSome, Is.True); Assert.That(maybe.OrThrow, Is.EqualTo("hallo")); }
public void MapFailure_Success_SideEffect() { var myInt = 0; var original = Failable <bool, string> .Success(false); var result = original.MapFailure(s => ++ myInt); Assert.That(myInt, Is.EqualTo(0)); }
public void ValuePropagationInFlatMap() { var hallo = Failable <string, int> .Success("hallo"); var sp = Failable <string, int> .Success(" "); var result = hallo.FlatMap(h => sp.Map(space => h + space + "welt")); Assert.That(result.Or("nix da"), Is.EqualTo("hallo welt")); }
public void Map_NestingInFlatMap() { var flag1 = Failable <int, string> .Success(1); var flag2 = Failable <int, string> .Success(2); var result = flag1.FlatMap(outerInt => flag2.Map(innerInt => outerInt + innerInt)); Assert.That(result.Or(-999), Is.EqualTo(3)); }
public void OrWithParallelType_Success() { var mySubclass = new MySubclass(); var myOtherSubclass = new MyOtherSubclass(); var underTest = Failable <MyClass, Exception> .Success(mySubclass); var actualValue = underTest.Or(myOtherSubclass); Assert.That(actualValue, Is.SameAs(mySubclass)); }
public void ConstructSuccess_ValueIsAnException() { //WICHTIG: Es gibt Argumente, das hier zu verbieten und analog zum Failable 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 = Failable <Exception, string> .Success(new Exception()); Assert.That(x.IsSuccess, Is.True); Assert.That(x.IsFailure, Is.False); }
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); }
public void Equals_DifferentTypeInequal() { var t1 = new Tuple <string, int>("abc", 123); var t2 = new Tuple <string, string>("abc", "123"); var x = Failable <Tuple <string, int>, string> .Success(t1); var y = Failable <Tuple <string, string>, string> .Success(t2); Assert.That(t1.Equals(t2), Is.False); Assert.That(x.Equals(y), Is.False); }
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)); }
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)); }
public void Equals_SuccessEqual() { var t1 = new Tuple <string, int>("abc", 123); var t2 = new Tuple <string, int>("abc", 123); var x = Failable <Tuple <string, int>, string> .Success(t1); var y = Failable <Tuple <string, int>, string> .Success(t2); Assert.That(t1.Equals(t2), Is.True); Assert.That(x.Equals(y), Is.True); }
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)); }
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); }
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 }