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
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ public abstract partial class ThatDelegate
{
private static readonly string DoesNotThrowExpectation = "does not throw any exception";

private static ConstraintResult DoesNotThrowResult(Exception? exception)
private static ConstraintResult DoesNotThrowResult<TException>(Exception? exception)
where TException : Exception?
{
if (exception is not null)
{
return new ConstraintResult.Failure<Exception?>(exception, DoesNotThrowExpectation,
$"it did throw {exception.FormatForMessage()}", true);
}

return new ConstraintResult.Success<Exception?>(null, DoesNotThrowExpectation, true);
return new ConstraintResult.Success<TException?>(default, DoesNotThrowExpectation, true);
}

private readonly struct DoesNotThrowConstraint<TValue> : IDelegateConstraint<TValue>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public ConstraintResult IsMetBy(DelegateSource.NoValue actual, Exception? except
{
if (!throwOptions.DoCheckThrow)
{
return DoesNotThrowResult(exception);
return DoesNotThrowResult<TException>(exception);
}

if (exception is TException typedException)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public ConstraintResult IsMetBy(DelegateSource.NoValue actual, Exception? except
{
if (!throwOptions.DoCheckThrow)
{
return DoesNotThrowResult(exception);
return DoesNotThrowResult<Exception>(exception);
}

if (exception is TException typedException && exception.GetType() == typeof(TException))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// ReSharper disable once CheckNamespace
namespace Testably.Expectations;

public abstract partial class ThatDelegate
{
internal class ThrowsOption
{
public bool DoCheckThrow { get; private set; } = true;

public void CheckThrow(bool doCheckThrow)
{
DoCheckThrow = doCheckThrow;
}
}
}
10 changes: 0 additions & 10 deletions Source/Testably.Expectations/Delegates/ThatDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,4 @@ public DelegateExpectationResult<Exception> ThrowsException()
b => b.AppendMethod(nameof(ThrowsException))),
throwOptions);
}

internal class ThrowsOption
{
public bool DoCheckThrow { get; private set; } = true;

public void CheckThrow(bool doCheckThrow)
{
DoCheckThrow = doCheckThrow;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,31 @@ await Expect.That(action).ThrowsException()
}

[Fact]
public async Task WhenTrue_ShouldSucceedWhenAnExceptionWasThrow()
public async Task ShouldSupportChainedConstraintsForTypedException()
{
Action action = () => { };

await Expect.That(action).Throws<ArgumentException>()
.OnlyIf(false)
.Which.HasMessage("foo");
}

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

async Task Act()
=> await Expect.That(action).ThrowsException().OnlyIf(true);
=> await Expect.That(action).ThrowsException().OnlyIf(false);

await Expect.That(Act).DoesNotThrow();
await Expect.That(Act).ThrowsException()
.Which.HasMessage("""
Expected that action
does not throw any exception,
but it did throw an Exception
at Expect.That(action).ThrowsException().OnlyIf(false)
""");
}

[Fact]
Expand All @@ -36,6 +52,7 @@ async Task Act()

await Expect.That(Act).DoesNotThrow();
}

[Fact]
public async Task WhenTrue_ShouldFailWhenNoExceptionWasThrow()
{
Expand All @@ -54,21 +71,15 @@ at Expect.That(action).ThrowsException().OnlyIf(true)
}

[Fact]
public async Task WhenFalse_ShouldFailWhenAnExceptionWasThrown()
public async Task WhenTrue_ShouldSucceedWhenAnExceptionWasThrow()
{
Exception exception = new("");
Action action = () => throw exception;

async Task Act()
=> await Expect.That(action).ThrowsException().OnlyIf(false);
=> await Expect.That(action).ThrowsException().OnlyIf(true);

await Expect.That(Act).ThrowsException()
.Which.HasMessage("""
Expected that action
does not throw any exception,
but it did throw an Exception
at Expect.That(action).ThrowsException().OnlyIf(false)
""");
await Expect.That(Act).DoesNotThrow();
}
}
}
Loading