public void Create_Exceptional_Recover_Continue_Empty_Custom_Type() { UnitTest( _ => new InformationSource(new FirstSource("Third")), s => { return(Exc.Create <IImmutableList <string>, ArgumentException>(_ => s.UnsafeGetFirst().GetInformation()) .OnFailure(e => { var information = new InformationSource(new SecondSource("First")); return information.UnsafeGetSecond().GetInformation(); }) .OnFailure(e => default(IImmutableList <string>)) .OnEmpty(_ => { var information = new InformationSource(new FirstSource("First")); return information.UnsafeGetFirst().GetInformation(); })); }, s => { Assert.Equal(new List <string> { "1", "2", "3" }.Map(), s.UnsafeGetSuccess()); } ); }
public void Associativity(Exc <int, Exception> exc) { UnitTest( _ => rec(func((int o) => Exc.Create <int, Exception>(__ => o / 3)), func((int o) => success <int, Exception>(o * 4))), r => exc.FlatMap(r.Item1).FlatMap(r.Item2).SafeEquals(exc.FlatMap(v => r.Item1(v).FlatMap(r.Item2))), Assert.True ); }
public void Create_Exceptional_Throws() { UnitTest( _ => "Funk12", s => act(() => Exc.Create <string, EmptyValueException>(_ => GetNameById(s))), a => Assert.Throws <ArgumentException>(a) ); }
public void Create_Exceptional_Throws_Recover_Chain_Throws() { UnitTest( _ => "Funk12", s => { return(act(() => Exc.Create <string, ArgumentException>(_ => GetNameById(s)) .OnFailure(e => GetNameById(null)))); }, a => Assert.Throws <InvalidOperationException>(a) ); }
public void Create_Exceptional_Continue() { UnitTest( _ => "Funk123", s => { return(Exc.Create <string, ArgumentException>(_ => GetNameById(s)) .Map(ss => ss.Concat(GetNameById(s)))); }, s => Assert.Equal("HarunHarun", s.UnsafeGetSuccess()) ); }
public void Deconstruct_Exc() { UnitTest( _ => "Funk123", s => { var(success, failure) = Exc.Create <string, ArgumentException>(_ => GetNameById(s)) .Map(ss => ss.Concat(GetNameById(s))); return(success, failure); }, s => Assert.Equal("HarunHarun", s.success.UnsafeGet()) ); }
public void Create_Exceptional() { UnitTest( _ => Exc.Create <string, ArgumentException>(__ => GetNameById("Funk12")), e => e.Failure, f => { Assert.True(f.NotEmpty); Assert.NotEmpty(f.UnsafeGet()); var action = act(() => throw f.UnsafeGet()); Assert.Throws <EnumerableException <ArgumentException> >(action); } ); }
public void Create_Exceptional_Throws_Recover() { UnitTest( _ => "Funk12", s => { return(Exc.Create <string, ArgumentException>(_ => GetNameById(s)) .OnFailure(e => GetNameById("Funk123"))); }, e => { Assert.True(e.IsSuccess); Assert.Equal("Harun", e.Success.UnsafeGet()); } ); }
public static Exc <T, JsonException> SafeDeserialize <T>(this string content) => Exc.Create <T, JsonException>(_ => JsonConvert.DeserializeObject <T>(content));