/// <summary> /// Used when a specific type of exception is not expected and a return value is expected when it does not occur. /// </summary> /// <typeparam name="T">Exception type</typeparam> /// <typeparam name="R">Return type</typeparam> /// <param name="Delegate">Delegate that returns a value</param> /// <param name="UserFailedMessage">Message passed to the output in the case of a failed test</param> /// <returns>Returns the value returned by the delegate or thw appropriate exception</returns> public static R DoesNotThrow <T, R>(ReturnObjectDelegate <R> Delegate, string UserFailedMessage = "Assert.DoesNotThrow<T,R>() Failed") { try { return(Delegate()); } catch (Exception e) { if (e is T) { throw new DoesNotThrow(typeof(T), e, UserFailedMessage); } } return(default(R)); }
/// <summary> /// Used when a specific type of exception is expected (or a return value is expected when it does not occur) /// </summary> /// <typeparam name="T">Exception type</typeparam> /// <typeparam name="R">Return type</typeparam> /// <param name="Delegate">Delegate that returns a value</param> /// <param name="UserFailedMessage">Message passed to the output in the case of a failed test</param> /// <returns>Returns the value returned by the delegate or thw appropriate exception</returns> public static R Throws <T, R>(ReturnObjectDelegate <R> Delegate, string UserFailedMessage = "Assert.Throws<T,R>() Failed") { bool Exception = false; try { return(Delegate()); } catch (Exception e) { if (!(e is T)) { throw new Throws(typeof(T), e, UserFailedMessage); } Exception = true; } if (!Exception) { throw new Throws(typeof(T), null, UserFailedMessage); } return(default(R)); }
/// <summary> /// Calls the specified delegate and throws any errors that it receives /// and returns a value /// </summary> /// <param name="Delegate">Delegate to call</param> public static T Do <T>(ReturnObjectDelegate <T> Delegate) { return(Delegate()); }