public void Extensions_Safe_Catch2() { var ex0 = new Ex("ex"); var ex1 = new Ex1("ex"); var ex2 = new Ex2("ex"); var ex3 = new Ex3("ex"); var baseEx = new BaseEx("ex"); var subEx = new SubEx("ex"); var err1 = Safe.Try <bool, Ex1, Ex2>(() => { throw ex1; }); var err2 = Safe.Try <bool, Ex1, Ex2>(() => { throw ex2; }); Assert.IsFalse(err1.HasValue); Assert.IsFalse(err2.HasValue); Assert.IsInstanceOfType(err1.Match(x => null, ex => ex), typeof(Ex1)); Assert.IsInstanceOfType(err2.Match(x => null, ex => ex), typeof(Ex2)); Assert.AreEqual(err1.Match(x => null, ex => ex), ex1); Assert.AreEqual(err2.Match(x => null, ex => ex), ex2); CustomAssert.Throws <Ex>(() => Safe.Try <bool, Ex2, Ex3>(() => { throw ex0; })); CustomAssert.Throws <Ex1>(() => Safe.Try <bool, Ex2, Ex3>(() => { throw ex1; })); Safe.Try <bool, BaseEx, Ex2>(() => { throw subEx; }); Safe.Try <bool, Ex, Ex2>(() => { throw ex0; }); Safe.Try <bool, Ex, Ex2>(() => { throw ex1; }); var success = Safe.Try <bool, Ex1, Ex2>(() => true); Assert.IsTrue(success.ValueOr(false)); }
public void Extensions_Safe_Catch5() { var ex0 = new Ex("ex"); var ex1 = new Ex1("ex"); var ex2 = new Ex2("ex"); var ex3 = new Ex3("ex"); var ex4 = new Ex4("ex"); var ex5 = new Ex5("ex"); var ex6 = new Ex6("ex"); var baseEx = new BaseEx("ex"); var subEx = new SubEx("ex"); var err1 = Safe.Try <bool, Ex1, Ex2, Ex3, Ex4, Ex5>(() => { throw ex1; }); var err2 = Safe.Try <bool, Ex1, Ex2, Ex3, Ex4, Ex5>(() => { throw ex2; }); var err3 = Safe.Try <bool, Ex1, Ex2, Ex3, Ex4, Ex5>(() => { throw ex3; }); var err4 = Safe.Try <bool, Ex1, Ex2, Ex3, Ex4, Ex5>(() => { throw ex4; }); var err5 = Safe.Try <bool, Ex1, Ex2, Ex3, Ex4, Ex5>(() => { throw ex5; }); Assert.IsFalse(err1.HasValue); Assert.IsFalse(err2.HasValue); Assert.IsFalse(err3.HasValue); Assert.IsFalse(err4.HasValue); Assert.IsFalse(err5.HasValue); Assert.IsInstanceOf(typeof(Ex1), err1.Match(x => null, ex => ex)); Assert.IsInstanceOf(typeof(Ex2), err2.Match(x => null, ex => ex)); Assert.IsInstanceOf(typeof(Ex3), err3.Match(x => null, ex => ex)); Assert.IsInstanceOf(typeof(Ex4), err4.Match(x => null, ex => ex)); Assert.IsInstanceOf(typeof(Ex5), err5.Match(x => null, ex => ex)); Assert.AreEqual(err1.Match(x => null, ex => ex), ex1); Assert.AreEqual(err2.Match(x => null, ex => ex), ex2); Assert.AreEqual(err3.Match(x => null, ex => ex), ex3); Assert.AreEqual(err4.Match(x => null, ex => ex), ex4); Assert.AreEqual(err5.Match(x => null, ex => ex), ex5); CustomAssert.Throws <Ex>(() => Safe.Try <bool, Ex2, Ex3, Ex4, Ex5, Ex6>(() => { throw ex0; })); CustomAssert.Throws <Ex1>(() => Safe.Try <bool, Ex2, Ex3, Ex4, Ex5, Ex6>(() => { throw ex1; })); Safe.Try <bool, BaseEx, Ex2, Ex3, Ex4, Ex5>(() => { throw subEx; }); Safe.Try <bool, Ex, Ex2, Ex3, Ex4, Ex5>(() => { throw ex0; }); Safe.Try <bool, Ex, Ex2, Ex3, Ex4, Ex5>(() => { throw ex1; }); var success = Safe.Try <bool, Ex1, Ex2, Ex3, Ex4, Ex5>(() => true); Assert.IsTrue(success.ValueOr(false)); }