Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Source/aweXpect.Core/Delegates/ThatDelegateThrows.Within.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ public partial class ThatDelegateThrows<TException>
/// </summary>
public ThatDelegateThrows<TException> 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;
Expand Down
34 changes: 30 additions & 4 deletions Tests/aweXpect.Tests/Delegates/ThatDelegate.Throws.Within.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CustomException>().Within(-1.Milliseconds());

await That(Act).Throws<ArgumentOutOfRangeException>()
.WithParamName("duration").And
.WithMessage("The duration must not be negative").AsPrefix();
}

[Fact]
public async Task WhenExactExceptionTypeIsThrownInTime_ShouldSucceed()
{
Expand Down Expand Up @@ -146,12 +159,12 @@ public async Task WhenSubjectIsNull_ShouldFail()
Action? subject = null;

async Task Act()
=> await That(subject!).Throws<CustomException>().Within(5.Seconds());
=> await That(subject!).Throws<CustomException>().Within(0.Seconds());

await That(Act).Throws<XunitException>()
.WithMessage("""
Expected that subject
throws a ThatDelegate.CustomException within 0:05,
throws a ThatDelegate.CustomException within 0:00,
but it was <null>
""");
}
Expand Down Expand Up @@ -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<ArgumentOutOfRangeException>()
.WithParamName("duration").And
.WithMessage("The duration must not be negative").AsPrefix();
}

[Fact]
public async Task WhenExactExceptionTypeIsThrownInTime_ShouldSucceed()
{
Expand Down Expand Up @@ -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<XunitException>()
.WithMessage("""
Expected that subject
throws a ThatDelegate.CustomException within 0:05,
throws a ThatDelegate.CustomException within 0:00,
but it was <null>
""");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CustomException>().Within(-1.Milliseconds());

await That(Act).Throws<ArgumentOutOfRangeException>()
.WithParamName("duration").And
.WithMessage("The duration must not be negative").AsPrefix();
}

[Fact]
public async Task WhenExactExceptionTypeIsThrownInTime_ShouldSucceed()
{
Expand Down Expand Up @@ -152,12 +165,12 @@ public async Task WhenSubjectIsNull_ShouldFail()
Action? subject = null;

async Task Act()
=> await That(subject!).ThrowsExactly<CustomException>().Within(5.Seconds());
=> await That(subject!).ThrowsExactly<CustomException>().Within(0.Seconds());

await That(Act).ThrowsExactly<XunitException>()
.WithMessage("""
Expected that subject
throws exactly a ThatDelegate.CustomException within 0:05,
throws exactly a ThatDelegate.CustomException within 0:00,
but it was <null>
""");
}
Expand Down Expand Up @@ -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<ArgumentOutOfRangeException>()
.WithParamName("duration").And
.WithMessage("The duration must not be negative").AsPrefix();
}

[Fact]
public async Task WhenExactExceptionTypeIsThrownInTime_ShouldSucceed()
{
Expand Down Expand Up @@ -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<XunitException>()
.WithMessage("""
Expected that subject
throws exactly a ThatDelegate.CustomException within 0:05,
throws exactly a ThatDelegate.CustomException within 0:00,
but it was <null>
""");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Exception> 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<ArgumentOutOfRangeException>()
.WithParamName("duration").And
.WithMessage("The duration must not be negative").AsPrefix();
}

[Fact]
Expand All @@ -73,6 +74,18 @@ but it took *
""").AsWildcard();
}

[Fact]
public async Task WhenExceptionTypeIsThrownInTime_ShouldSucceed()
{
Exception exception = new CustomException();
Action action = () => throw exception;

async Task<Exception> Act()
=> await That(action).ThrowsException().Within(5.Seconds());

await That(Act).DoesNotThrow();
}

[Fact]
public async Task WhenNoExceptionIsThrownAndExecutionTimeIsTooLarge_ShouldFail()
{
Expand Down Expand Up @@ -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<XunitException>()
.WithMessage("""
Expected that subject
throws an exception within 0:05,
throws an exception within 0:00,
but it was <null>
""");
}
Expand Down