diff --git a/Source/aweXpect.Core/Delegates/ThatDelegateThrows.Within.cs b/Source/aweXpect.Core/Delegates/ThatDelegateThrows.Within.cs index d5da78224..8202dea9c 100644 --- a/Source/aweXpect.Core/Delegates/ThatDelegateThrows.Within.cs +++ b/Source/aweXpect.Core/Delegates/ThatDelegateThrows.Within.cs @@ -10,6 +10,11 @@ public partial class ThatDelegateThrows /// public ThatDelegateThrows Within(TimeSpan duration) { + if (duration < TimeSpan.Zero) + { + throw new ArgumentOutOfRangeException(nameof(duration), "The duration must not be negative."); + } + TimeSpanEqualityOptions options = new(); options.Within(duration); _throwOptions.ExecutionTimeOptions = options; diff --git a/Tests/aweXpect.Tests/Delegates/ThatDelegate.Throws.Within.Tests.cs b/Tests/aweXpect.Tests/Delegates/ThatDelegate.Throws.Within.Tests.cs index 90a7dc899..5700f6655 100644 --- a/Tests/aweXpect.Tests/Delegates/ThatDelegate.Throws.Within.Tests.cs +++ b/Tests/aweXpect.Tests/Delegates/ThatDelegate.Throws.Within.Tests.cs @@ -41,6 +41,19 @@ public async Task WhenAwaited_ShouldReturnThrownException(string value) await That(result).IsSameAs(exception); } + [Fact] + public async Task WhenDurationIsNegative_ShouldThrowArgumentOutOfRangeException() + { + Action? subject = null; + + async Task Act() + => await That(subject!).Throws().Within(-1.Milliseconds()); + + await That(Act).Throws() + .WithParamName("duration").And + .WithMessage("The duration must not be negative").AsPrefix(); + } + [Fact] public async Task WhenExactExceptionTypeIsThrownInTime_ShouldSucceed() { @@ -146,12 +159,12 @@ public async Task WhenSubjectIsNull_ShouldFail() Action? subject = null; async Task Act() - => await That(subject!).Throws().Within(5.Seconds()); + => await That(subject!).Throws().Within(0.Seconds()); await That(Act).Throws() .WithMessage(""" Expected that subject - throws a ThatDelegate.CustomException within 0:05, + throws a ThatDelegate.CustomException within 0:00, but it was """); } @@ -210,6 +223,19 @@ public async Task WhenAwaited_ShouldReturnThrownException(string value) await That(result).IsSameAs(exception); } + [Fact] + public async Task WhenDurationIsNegative_ShouldThrowArgumentOutOfRangeException() + { + Action? subject = null; + + async Task Act() + => await That(subject!).Throws(typeof(CustomException)).Within(-1.Milliseconds()); + + await That(Act).Throws() + .WithParamName("duration").And + .WithMessage("The duration must not be negative").AsPrefix(); + } + [Fact] public async Task WhenExactExceptionTypeIsThrownInTime_ShouldSucceed() { @@ -315,12 +341,12 @@ public async Task WhenSubjectIsNull_ShouldFail() Action? subject = null; async Task Act() - => await That(subject!).Throws(typeof(CustomException)).Within(5.Seconds()); + => await That(subject!).Throws(typeof(CustomException)).Within(0.Seconds()); await That(Act).Throws() .WithMessage(""" Expected that subject - throws a ThatDelegate.CustomException within 0:05, + throws a ThatDelegate.CustomException within 0:00, but it was """); } diff --git a/Tests/aweXpect.Tests/Delegates/ThatDelegate.ThrowsExactly.Within.Tests.cs b/Tests/aweXpect.Tests/Delegates/ThatDelegate.ThrowsExactly.Within.Tests.cs index 78edbfce9..73cc6e56e 100644 --- a/Tests/aweXpect.Tests/Delegates/ThatDelegate.ThrowsExactly.Within.Tests.cs +++ b/Tests/aweXpect.Tests/Delegates/ThatDelegate.ThrowsExactly.Within.Tests.cs @@ -41,6 +41,19 @@ public async Task WhenAwaited_ShouldReturnThrownException(string value) await That(result).IsSameAs(exception); } + [Fact] + public async Task WhenDurationIsNegative_ShouldThrowArgumentOutOfRangeException() + { + Action? subject = null; + + async Task Act() + => await That(subject!).ThrowsExactly().Within(-1.Milliseconds()); + + await That(Act).Throws() + .WithParamName("duration").And + .WithMessage("The duration must not be negative").AsPrefix(); + } + [Fact] public async Task WhenExactExceptionTypeIsThrownInTime_ShouldSucceed() { @@ -152,12 +165,12 @@ public async Task WhenSubjectIsNull_ShouldFail() Action? subject = null; async Task Act() - => await That(subject!).ThrowsExactly().Within(5.Seconds()); + => await That(subject!).ThrowsExactly().Within(0.Seconds()); await That(Act).ThrowsExactly() .WithMessage(""" Expected that subject - throws exactly a ThatDelegate.CustomException within 0:05, + throws exactly a ThatDelegate.CustomException within 0:00, but it was """); } @@ -217,6 +230,19 @@ public async Task WhenAwaited_ShouldReturnThrownException(string value) await That(result).IsSameAs(exception); } + [Fact] + public async Task WhenDurationIsNegative_ShouldThrowArgumentOutOfRangeException() + { + Action? subject = null; + + async Task Act() + => await That(subject!).ThrowsExactly(typeof(CustomException)).Within(-1.Milliseconds()); + + await That(Act).Throws() + .WithParamName("duration").And + .WithMessage("The duration must not be negative").AsPrefix(); + } + [Fact] public async Task WhenExactExceptionTypeIsThrownInTime_ShouldSucceed() { @@ -328,12 +354,12 @@ public async Task WhenSubjectIsNull_ShouldFail() Action? subject = null; async Task Act() - => await That(subject!).ThrowsExactly(typeof(CustomException)).Within(5.Seconds()); + => await That(subject!).ThrowsExactly(typeof(CustomException)).Within(0.Seconds()); await That(Act).ThrowsExactly() .WithMessage(""" Expected that subject - throws exactly a ThatDelegate.CustomException within 0:05, + throws exactly a ThatDelegate.CustomException within 0:00, but it was """); } diff --git a/Tests/aweXpect.Tests/Delegates/ThatDelegate.ThrowsException.Within.Tests.cs b/Tests/aweXpect.Tests/Delegates/ThatDelegate.ThrowsException.Within.Tests.cs index 5ddf8887c..5e602e73d 100644 --- a/Tests/aweXpect.Tests/Delegates/ThatDelegate.ThrowsException.Within.Tests.cs +++ b/Tests/aweXpect.Tests/Delegates/ThatDelegate.ThrowsException.Within.Tests.cs @@ -41,15 +41,16 @@ public async Task WhenAwaited_ShouldReturnThrownException(string value) } [Fact] - public async Task WhenExceptionTypeIsThrownInTime_ShouldSucceed() + public async Task WhenDurationIsNegative_ShouldThrowArgumentOutOfRangeException() { - Exception exception = new CustomException(); - Action action = () => throw exception; + Action? subject = null; - async Task Act() - => await That(action).ThrowsException().Within(5.Seconds()); + async Task Act() + => await That(subject!).ThrowsException().Within(-1.Milliseconds()); - await That(Act).DoesNotThrow(); + await That(Act).Throws() + .WithParamName("duration").And + .WithMessage("The duration must not be negative").AsPrefix(); } [Fact] @@ -73,6 +74,18 @@ but it took * """).AsWildcard(); } + [Fact] + public async Task WhenExceptionTypeIsThrownInTime_ShouldSucceed() + { + Exception exception = new CustomException(); + Action action = () => throw exception; + + async Task Act() + => await That(action).ThrowsException().Within(5.Seconds()); + + await That(Act).DoesNotThrow(); + } + [Fact] public async Task WhenNoExceptionIsThrownAndExecutionTimeIsTooLarge_ShouldFail() { @@ -114,12 +127,12 @@ public async Task WhenSubjectIsNull_ShouldFail() Action? subject = null; async Task Act() - => await That(subject!).ThrowsException().Within(5.Seconds()); + => await That(subject!).ThrowsException().Within(0.Seconds()); await That(Act).Throws() .WithMessage(""" Expected that subject - throws an exception within 0:05, + throws an exception within 0:00, but it was """); }