diff --git a/TUnit.Assertions.Tests/AssertConditions/BecauseTests.cs b/TUnit.Assertions.Tests/AssertConditions/BecauseTests.cs index 3d4baba302..d75995cdb0 100644 --- a/TUnit.Assertions.Tests/AssertConditions/BecauseTests.cs +++ b/TUnit.Assertions.Tests/AssertConditions/BecauseTests.cs @@ -13,7 +13,7 @@ public async Task Include_Because_Reason_In_Message() await Assert.That(variable).IsFalse().Because(because); }; - var exception = await Assert.ThrowsAsync(action); + var exception = await Assert.That(action).Throws(); await Assert.That(exception.Message).Contains(because); } @@ -30,7 +30,7 @@ public async Task Prefix_Because_Message(string because, string expectedWithPref await Assert.That(variable).IsFalse().Because(because); }; - var exception = await Assert.ThrowsAsync(action); + var exception = await Assert.That(action).Throws(); await Assert.That(exception.Message).Contains(expectedWithPrefix); } @@ -45,7 +45,7 @@ public async Task Honor_Already_Present_Because_Prefix() await Assert.That(variable).IsFalse().Because(because); }; - var exception = await Assert.ThrowsAsync(action); + var exception = await Assert.That(action).Throws(); await Assert.That(exception.Message).Contains(because) .And.DoesNotContain("because because"); } @@ -65,7 +65,7 @@ At Assert.That(variable).IsFalse() await Assert.That(variable).IsFalse(); }; - var exception = await Assert.ThrowsAsync(action); + var exception = await Assert.That(action).Throws(); await Assert.That(exception.Message).IsEqualTo(expectedMessage); } @@ -87,7 +87,7 @@ await Assert.That(variable).IsTrue().Because(because) .And.IsFalse(); }; - var exception = await Assert.ThrowsAsync(action); + var exception = await Assert.That(action).Throws(); await Assert.That(exception.Message).IsEqualTo(expectedMessage); } @@ -104,7 +104,7 @@ await Assert.That(variable).IsTrue().Because(because1) .And.IsFalse().Because(because2); }; - var exception = await Assert.ThrowsAsync(action); + var exception = await Assert.That(action).Throws(); await Assert.That(exception.Message).Contains(because1); } @@ -121,7 +121,7 @@ await Assert.That(variable).IsTrue().Because(because1) .And.IsFalse().Because(because2); }; - var exception = await Assert.ThrowsAsync(action); + var exception = await Assert.That(action).Throws(); await Assert.That(exception.Message).Contains(because2); } @@ -138,7 +138,7 @@ await Assert.That(variable).IsFalse().Because(because1) .Or.IsFalse().Because(because2); }; - var exception = await Assert.ThrowsAsync(action); + var exception = await Assert.That(action).Throws(); await Assert.That(exception.Message).Contains(because1).And.Contains(because2); } } \ No newline at end of file diff --git a/TUnit.Assertions.UnitTests/AsyncTaskTests.cs b/TUnit.Assertions.UnitTests/AsyncTaskTests.cs index bb76621112..8b5bb6199f 100644 --- a/TUnit.Assertions.UnitTests/AsyncTaskTests.cs +++ b/TUnit.Assertions.UnitTests/AsyncTaskTests.cs @@ -40,30 +40,30 @@ public async Task Task_Is_Callable() [Test] public async Task Func_Throws_Task_Is_Callable() { - await TUnitAssert.ThrowsAsync(() => Task.FromException(new DivideByZeroException())); + await TUnitAssert.That(() => Task.FromException(new DivideByZeroException())).ThrowsException(); } [Test] public async Task Func_Throws_Awaited_Task_Is_Callable() { - await TUnitAssert.ThrowsAsync(async () => await Task.FromException(new DivideByZeroException())); + await TUnitAssert.That(async () => await Task.FromException(new DivideByZeroException())).ThrowsException(); } [Test] public async Task Func_Throws_Awaited_ValueTask_Is_Callable() { - await TUnitAssert.ThrowsAsync(async () => await ValueTask.FromException(new DivideByZeroException())); + await TUnitAssert.That(async () => await ValueTask.FromException(new DivideByZeroException())).ThrowsException(); } [Test] public async Task Throws_Task_Is_Callable() { - await TUnitAssert.ThrowsAsync(Task.FromException(new DivideByZeroException())); + await TUnitAssert.That(Task.FromException(new DivideByZeroException())).ThrowsException(); } [Test] public async Task Throws_ValueTask_Is_Callable() { - await TUnitAssert.ThrowsAsync(ValueTask.FromException(new DivideByZeroException())); + await TUnitAssert.That(ValueTask.FromException(new DivideByZeroException())).ThrowsException(); } } \ No newline at end of file diff --git a/TUnit.Assertions/Assert.cs b/TUnit.Assertions/Assert.cs index 5ab34054ac..e50cfbb461 100644 --- a/TUnit.Assertions/Assert.cs +++ b/TUnit.Assertions/Assert.cs @@ -2,7 +2,6 @@ using System.Runtime.CompilerServices; using TUnit.Assertions.AssertionBuilders; using TUnit.Assertions.Exceptions; -using TUnit.Assertions.Extensions; namespace TUnit.Assertions; @@ -63,73 +62,4 @@ public static void Fail(string reason) { throw new AssertionException(reason); } - - public static Task ThrowsAsync(Func @delegate, - [CallerArgumentExpression("delegate")] string? doNotPopulateThisValue = null) - => ThrowsAsync(@delegate, doNotPopulateThisValue); - - public static Task ThrowsAsync(Task @delegate, - [CallerArgumentExpression("delegate")] string? doNotPopulateThisValue = null) - => ThrowsAsync(async () => await @delegate, doNotPopulateThisValue); - - public static Task ThrowsAsync(ValueTask @delegate, - [CallerArgumentExpression("delegate")] string? doNotPopulateThisValue = null) - => ThrowsAsync(async () => await @delegate, doNotPopulateThisValue); - - public static async Task ThrowsAsync(Func @delegate, [CallerArgumentExpression("delegate")] string? doNotPopulateThisValue = null) where TException : Exception - { - try - { - await @delegate(); - Fail($"No exception was thrown by {doNotPopulateThisValue.GetStringOr("the delegate")}"); - } - catch (Exception e) when(e is not AssertionException) - { - if (e is TException exception) - { - return exception; - } - - Fail($"Exception is of type {e.GetType().Name} instead of {typeof(TException).Name} for {doNotPopulateThisValue.GetStringOr("the delegate")}"); - } - catch (TException e) - { - // In case we want to assert to catch an AssertionException - return e; - } - - return null!; - } - - public static Task ThrowsAsync(Task @delegate, - [CallerArgumentExpression("delegate")] string? doNotPopulateThisValue = null) where TException : Exception - => ThrowsAsync(async () => await @delegate, doNotPopulateThisValue); - - public static Task ThrowsAsync(ValueTask @delegate, - [CallerArgumentExpression("delegate")] string? doNotPopulateThisValue = null) where TException : Exception - => ThrowsAsync(async () => await @delegate, doNotPopulateThisValue); - - public static Exception Throws(Action @delegate, - [CallerArgumentExpression("delegate")] string? doNotPopulateThisValue = null) - => Throws(@delegate, doNotPopulateThisValue); - - public static TException Throws(Action @delegate, [CallerArgumentExpression("delegate")] string? doNotPopulateThisValue = null) where TException : Exception - { - try - { - @delegate(); - Fail($"No exception was thrown by {doNotPopulateThisValue.GetStringOr("the delegate")}"); - } - catch (Exception e) when(e is not AssertionException) - { - if (e is TException exception) - { - return exception; - } - - Fail($"Exception is of type {e.GetType().Name} instead of {typeof(TException).Name} for {doNotPopulateThisValue.GetStringOr("the delegate")}"); - } - - return null!; - } } \ No newline at end of file diff --git a/TUnit.Assertions/AssertConditions/Throws/ThrowsException.cs b/TUnit.Assertions/AssertConditions/Throws/ThrowsException.cs index 7fd8c39bff..3e6e878b83 100644 --- a/TUnit.Assertions/AssertConditions/Throws/ThrowsException.cs +++ b/TUnit.Assertions/AssertConditions/Throws/ThrowsException.cs @@ -8,39 +8,35 @@ namespace TUnit.Assertions.AssertConditions.Throws; public class ThrowsException( InvokableDelegateAssertionBuilder delegateAssertionBuilder, IDelegateSource delegateSource, - Func exceptionSelector, - [CallerMemberName] string callerMemberName = "") + Func exceptionSelector) where TException : Exception { - private readonly IDelegateSource _delegateSource = delegateSource; - private readonly Func _exceptionSelector = exceptionSelector; - private IDelegateSource delegateSource; - private Func exceptionSelector; - public ThrowsException WithMessageMatching(StringMatcher match, [CallerArgumentExpression("match")] string doNotPopulateThisValue = "") { - _delegateSource.RegisterAssertion(new ThrowsWithMessageMatchingAssertCondition(match, _exceptionSelector) - , [doNotPopulateThisValue]); + delegateSource.RegisterAssertion( + new ThrowsWithMessageMatchingAssertCondition(match, exceptionSelector), + [doNotPopulateThisValue]); return this; } public ThrowsException WithMessage(string expected, [CallerArgumentExpression("expected")] string doNotPopulateThisValue = "") { - _delegateSource.RegisterAssertion(new ThrowsWithMessageAssertCondition(expected, StringComparison.Ordinal, _exceptionSelector) - , [doNotPopulateThisValue]); + delegateSource.RegisterAssertion( + new ThrowsWithMessageAssertCondition(expected, StringComparison.Ordinal, exceptionSelector), + [doNotPopulateThisValue]); return this; } public ThrowsException WithInnerException() { - _delegateSource.AssertionBuilder.AppendExpression($"{nameof(WithInnerException)}()"); - return new(delegateAssertionBuilder, _delegateSource, e => _exceptionSelector(e)?.InnerException); + delegateSource.AssertionBuilder.AppendExpression($"{nameof(WithInnerException)}()"); + return new(delegateAssertionBuilder, delegateSource, e => exceptionSelector(e)?.InnerException); } - public TaskAwaiter GetAwaiter() + public TaskAwaiter GetAwaiter() { var task = delegateAssertionBuilder.ProcessAssertionsAsync( d => d.Exception as TException); - return task.GetAwaiter(); + return task.GetAwaiter()!; } } \ No newline at end of file