public static E AssertThrows <E, T>(Span <T> span, AssertThrowsAction <T> action) where E : Exception { Exception exception; try { action(span); exception = null; } catch (Exception ex) { exception = ex; } switch (exception) { case null: throw new ThrowsException(typeof(E)); case E ex when(ex.GetType() == typeof(E)): return(ex); default: throw new ThrowsException(typeof(E), exception); } }
public static E Throws <E, T>(string expectedParamName, Span <T> span, AssertThrowsAction <T> action) where E : ArgumentException { E exception = AssertThrows <E, T>(span, action); Assert.Equal(expectedParamName, exception.ParamName); return(exception); }
// Cannot use standard Assert.Throws() when testing Span - Span and closures don't get along. public static void AssertThrows <E, T>(Span <T> span, AssertThrowsAction <T> action) where E : Exception { try { action(span); Assert.False(true, $"Expected exception: {typeof(E)}"); } catch (Exception ex) { Assert.True(ex is E, $"Wrong exception thrown. Expected: {typeof(E)} Actual: {ex.GetType()}"); } }
// Cannot use standard Assert.Throws() when testing Span - Span and closures don't get along. private static void AssertThrows <E, T>(Span <T> span, AssertThrowsAction <T> action) where E : Exception { try { action(span); Assert.False(true, "Expected exception: " + typeof(E).GetType()); } catch (E) { } catch (Exception wrongException) { Assert.False(true, "Wrong exception thrown: Expected " + typeof(E).GetType() + ": Actual: " + wrongException.GetType()); } }
public static void AssertThrows <E, T>(Span <T> span, AssertThrowsAction <T> action) where E : Exception { Exception exception; try { action(span); exception = null; } catch (Exception ex) { exception = ex; } if (exception == null) { throw new ThrowsException(typeof(E)); } if (exception.GetType() != typeof(E)) { throw new ThrowsException(typeof(E), exception); } }